diagnostic.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) 2015-2016 The Khronos Group Inc.
  2. //
  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
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  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.
  14. #ifndef SOURCE_DIAGNOSTIC_H_
  15. #define SOURCE_DIAGNOSTIC_H_
  16. #include <sstream>
  17. #include <string>
  18. #include "spirv-tools/libspirv.hpp"
  19. namespace spvtools {
  20. // A DiagnosticStream remembers the current position of the input and an error
  21. // code, and captures diagnostic messages via the left-shift operator.
  22. // If the error code is not SPV_FAILED_MATCH, then captured messages are
  23. // emitted during the destructor.
  24. class DiagnosticStream {
  25. public:
  26. DiagnosticStream(spv_position_t position, const MessageConsumer& consumer,
  27. const std::string& disassembled_instruction,
  28. spv_result_t error)
  29. : position_(position),
  30. consumer_(consumer),
  31. disassembled_instruction_(disassembled_instruction),
  32. error_(error) {}
  33. // Creates a DiagnosticStream from an expiring DiagnosticStream.
  34. // The new object takes the contents of the other, and prevents the
  35. // other from emitting anything during destruction.
  36. DiagnosticStream(DiagnosticStream&& other);
  37. // Destroys a DiagnosticStream.
  38. // If its status code is something other than SPV_FAILED_MATCH
  39. // then emit the accumulated message to the consumer.
  40. ~DiagnosticStream();
  41. // Adds the given value to the diagnostic message to be written.
  42. template <typename T>
  43. DiagnosticStream& operator<<(const T& val) {
  44. stream_ << val;
  45. return *this;
  46. }
  47. // Conversion operator to spv_result, returning the error code.
  48. operator spv_result_t() { return error_; }
  49. private:
  50. std::ostringstream stream_;
  51. spv_position_t position_;
  52. MessageConsumer consumer_; // Message consumer callback.
  53. std::string disassembled_instruction_;
  54. spv_result_t error_;
  55. };
  56. // Changes the MessageConsumer in |context| to one that updates |diagnostic|
  57. // with the last message received.
  58. //
  59. // This function expects that |diagnostic| is not nullptr and its content is a
  60. // nullptr.
  61. void UseDiagnosticAsMessageConsumer(spv_context context,
  62. spv_diagnostic* diagnostic);
  63. std::string spvResultToString(spv_result_t res);
  64. } // namespace spvtools
  65. #endif // SOURCE_DIAGNOSTIC_H_