1// Copyright (c) 2015-2016 The Khronos Group Inc. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and/or associated documentation files (the 5// "Materials"), to deal in the Materials without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Materials, and to 8// permit persons to whom the Materials are furnished to do so, subject to 9// the following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Materials. 13// 14// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS 15// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS 16// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT 17// https://www.khronos.org/registry/ 18// 19// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 22// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 23// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 24// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 25// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 26 27#ifndef LIBSPIRV_DIAGNOSTIC_H_ 28#define LIBSPIRV_DIAGNOSTIC_H_ 29 30#include <iostream> 31#include <sstream> 32#include <utility> 33 34#include "spirv-tools/libspirv.h" 35 36/// For debugging purposes only 37/// Prints the string to stdout 38#define MSG(msg) \ 39 do { \ 40 libspirv::message(__FILE__, size_t(__LINE__), msg); \ 41 } while (0) 42 43/// For debugging purposes only 44/// prints the variable value/location/and name to stdout 45#define SHOW(exp) \ 46 do { \ 47 libspirv::message(__FILE__, size_t(__LINE__), #exp, (exp)); \ 48 } while (0) 49 50namespace libspirv { 51class diagnostic_helper { 52 public: 53 diagnostic_helper(spv_position_t& position, spv_diagnostic* diagnostic) 54 : position_(&position), diagnostic_(diagnostic) {} 55 56 diagnostic_helper(spv_position position, spv_diagnostic* diagnostic) 57 : position_(position), diagnostic_(diagnostic) {} 58 59 ~diagnostic_helper() { 60 *diagnostic_ = spvDiagnosticCreate(position_, stream().str().c_str()); 61 } 62 63 std::stringstream& stream() { return stream_; } 64 65 private: 66 std::stringstream stream_; 67 spv_position position_; 68 spv_diagnostic* diagnostic_; 69}; 70 71// A DiagnosticStream remembers the current position of the input and an error 72// code, and captures diagnostic messages via the left-shift operator. 73// If the error code is not SPV_FAILED_MATCH, then captured messages are 74// emitted during the destructor. 75// TODO(awoloszyn): This is very similar to diagnostic_helper, and hides 76// the data more easily. Replace diagnostic_helper elsewhere 77// eventually. 78class DiagnosticStream { 79 public: 80 DiagnosticStream(spv_position_t position, spv_diagnostic* pDiagnostic, 81 spv_result_t error) 82 : position_(position), pDiagnostic_(pDiagnostic), error_(error) {} 83 84 DiagnosticStream(DiagnosticStream&& other) 85 : stream_(other.stream_.str()), 86 position_(other.position_), 87 pDiagnostic_(other.pDiagnostic_), 88 error_(other.error_) { 89 // The other object's destructor will emit the text in its stream_ 90 // member if its pDiagnostic_ member is non-null. Prevent that, 91 // since emitting that text is now the responsibility of *this. 92 other.pDiagnostic_ = nullptr; 93 } 94 95 ~DiagnosticStream(); 96 97 // Adds the given value to the diagnostic message to be written. 98 template <typename T> 99 DiagnosticStream& operator<<(const T& val) { 100 stream_ << val; 101 return *this; 102 } 103 104 // Conversion operator to spv_result, returning the error code. 105 operator spv_result_t() { return error_; } 106 107 private: 108 std::stringstream stream_; 109 spv_position_t position_; 110 spv_diagnostic* pDiagnostic_; 111 spv_result_t error_; 112}; 113 114#define DIAGNOSTIC \ 115 libspirv::diagnostic_helper helper(position, pDiagnostic); \ 116 helper.stream() 117 118std::string spvResultToString(spv_result_t res); 119 120/// Helper functions for printing debugging information 121void message(std::string file, size_t line, std::string name); 122 123template <typename T> 124void message(std::string file, size_t line, std::string name, T val) { 125 std::cout << file << ":" << line << ": " << name << " " << val << std::endl; 126} 127 128} // namespace libspirv 129 130#endif // LIBSPIRV_DIAGNOSTIC_H_ 131