1 // Copyright (c) 2015-2016 The Khronos Group Inc.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #ifndef LIBSPIRV_DIAGNOSTIC_H_
16 #define LIBSPIRV_DIAGNOSTIC_H_
21 #include "spirv-tools/libspirv.hpp"
25 // A DiagnosticStream remembers the current position of the input and an error
26 // code, and captures diagnostic messages via the left-shift operator.
27 // If the error code is not SPV_FAILED_MATCH, then captured messages are
28 // emitted during the destructor.
29 class DiagnosticStream {
31 DiagnosticStream(spv_position_t position,
32 const spvtools::MessageConsumer& consumer,
34 : position_(position), consumer_(consumer), error_(error) {}
36 DiagnosticStream(DiagnosticStream&& other)
37 : stream_(other.stream_.str()),
38 position_(other.position_),
39 consumer_(other.consumer_),
40 error_(other.error_) {}
44 // Adds the given value to the diagnostic message to be written.
46 DiagnosticStream& operator<<(const T& val) {
51 // Conversion operator to spv_result, returning the error code.
52 operator spv_result_t() { return error_; }
55 std::stringstream stream_;
56 spv_position_t position_;
57 const spvtools::MessageConsumer& consumer_; // Message consumer callback.
61 // Changes the MessageConsumer in |context| to one that updates |diagnostic|
62 // with the last message received.
64 // This function expects that |diagnostic| is not nullptr and its content is a
66 void UseDiagnosticAsMessageConsumer(spv_context context,
67 spv_diagnostic* diagnostic);
69 std::string spvResultToString(spv_result_t res);
71 } // namespace libspirv
73 #endif // LIBSPIRV_DIAGNOSTIC_H_