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 SOURCE_DIAGNOSTIC_H_
16 #define SOURCE_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
, const MessageConsumer
& consumer
,
32 const std::string
& disassembled_instruction
,
34 : position_(position
),
36 disassembled_instruction_(disassembled_instruction
),
39 // Creates a DiagnosticStream from an expiring DiagnosticStream.
40 // The new object takes the contents of the other, and prevents the
41 // other from emitting anything during destruction.
42 DiagnosticStream(DiagnosticStream
&& other
);
44 // Destroys a DiagnosticStream.
45 // If its status code is something other than SPV_FAILED_MATCH
46 // then emit the accumulated message to the consumer.
49 // Adds the given value to the diagnostic message to be written.
51 DiagnosticStream
& operator<<(const T
& val
) {
56 // Conversion operator to spv_result, returning the error code.
57 operator spv_result_t() { return error_
; }
60 std::ostringstream stream_
;
61 spv_position_t position_
;
62 MessageConsumer consumer_
; // Message consumer callback.
63 std::string disassembled_instruction_
;
67 // Changes the MessageConsumer in |context| to one that updates |diagnostic|
68 // with the last message received.
70 // This function expects that |diagnostic| is not nullptr and its content is a
72 void UseDiagnosticAsMessageConsumer(spv_context context
,
73 spv_diagnostic
* diagnostic
);
75 std::string
spvResultToString(spv_result_t res
);
77 } // namespace spvtools
79 #endif // SOURCE_DIAGNOSTIC_H_