2016-01-07 18:44:22 +00:00
|
|
|
// Copyright (c) 2015-2016 The Khronos Group Inc.
|
2015-05-22 17:26:19 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-05-22 17:26:19 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2015-05-22 17:26:19 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#ifndef SOURCE_DIAGNOSTIC_H_
|
|
|
|
#define SOURCE_DIAGNOSTIC_H_
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2015-09-24 14:26:51 +00:00
|
|
|
#include <sstream>
|
2016-09-21 21:16:31 +00:00
|
|
|
#include <string>
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2016-09-16 20:12:04 +00:00
|
|
|
#include "spirv-tools/libspirv.hpp"
|
2015-11-11 17:45:23 +00:00
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
namespace spvtools {
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2015-10-09 19:48:09 +00:00
|
|
|
// A DiagnosticStream remembers the current position of the input and an error
|
|
|
|
// code, and captures diagnostic messages via the left-shift operator.
|
2015-10-14 15:31:51 +00:00
|
|
|
// If the error code is not SPV_FAILED_MATCH, then captured messages are
|
|
|
|
// emitted during the destructor.
|
2015-09-24 14:26:51 +00:00
|
|
|
class DiagnosticStream {
|
|
|
|
public:
|
2018-07-07 13:38:00 +00:00
|
|
|
DiagnosticStream(spv_position_t position, const MessageConsumer& consumer,
|
2018-06-19 20:02:44 +00:00
|
|
|
const std::string& disassembled_instruction,
|
2015-10-09 19:48:09 +00:00
|
|
|
spv_result_t error)
|
2018-06-19 20:02:44 +00:00
|
|
|
: position_(position),
|
|
|
|
consumer_(consumer),
|
|
|
|
disassembled_instruction_(disassembled_instruction),
|
|
|
|
error_(error) {}
|
2015-09-24 14:26:51 +00:00
|
|
|
|
2017-10-01 13:27:00 +00:00
|
|
|
// Creates a DiagnosticStream from an expiring DiagnosticStream.
|
|
|
|
// The new object takes the contents of the other, and prevents the
|
|
|
|
// other from emitting anything during destruction.
|
|
|
|
DiagnosticStream(DiagnosticStream&& other);
|
2015-09-24 14:26:51 +00:00
|
|
|
|
2017-10-01 13:27:00 +00:00
|
|
|
// Destroys a DiagnosticStream.
|
|
|
|
// If its status code is something other than SPV_FAILED_MATCH
|
|
|
|
// then emit the accumulated message to the consumer.
|
2015-09-24 14:26:51 +00:00
|
|
|
~DiagnosticStream();
|
|
|
|
|
|
|
|
// Adds the given value to the diagnostic message to be written.
|
|
|
|
template <typename T>
|
2015-11-02 14:41:20 +00:00
|
|
|
DiagnosticStream& operator<<(const T& val) {
|
2015-09-24 14:26:51 +00:00
|
|
|
stream_ << val;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-10-09 19:48:09 +00:00
|
|
|
// Conversion operator to spv_result, returning the error code.
|
|
|
|
operator spv_result_t() { return error_; }
|
|
|
|
|
2015-09-24 14:26:51 +00:00
|
|
|
private:
|
2017-10-01 13:27:00 +00:00
|
|
|
std::ostringstream stream_;
|
2015-10-28 17:16:56 +00:00
|
|
|
spv_position_t position_;
|
2018-07-07 13:38:00 +00:00
|
|
|
MessageConsumer consumer_; // Message consumer callback.
|
2018-06-19 20:02:44 +00:00
|
|
|
std::string disassembled_instruction_;
|
2015-10-09 19:48:09 +00:00
|
|
|
spv_result_t error_;
|
2015-09-24 14:26:51 +00:00
|
|
|
};
|
|
|
|
|
2016-09-02 22:06:18 +00:00
|
|
|
// Changes the MessageConsumer in |context| to one that updates |diagnostic|
|
|
|
|
// with the last message received.
|
|
|
|
//
|
|
|
|
// This function expects that |diagnostic| is not nullptr and its content is a
|
|
|
|
// nullptr.
|
|
|
|
void UseDiagnosticAsMessageConsumer(spv_context context,
|
|
|
|
spv_diagnostic* diagnostic);
|
|
|
|
|
2015-12-16 02:44:21 +00:00
|
|
|
std::string spvResultToString(spv_result_t res);
|
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
} // namespace spvtools
|
2015-11-20 15:44:41 +00:00
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#endif // SOURCE_DIAGNOSTIC_H_
|