diagnostic.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #include "diagnostic.h"
  15. #include <cassert>
  16. #include <cstring>
  17. #include <iostream>
  18. #include <sstream>
  19. #include "table.h"
  20. // Diagnostic API
  21. spv_diagnostic spvDiagnosticCreate(const spv_position position,
  22. const char* message) {
  23. spv_diagnostic diagnostic = new spv_diagnostic_t;
  24. if (!diagnostic) return nullptr;
  25. size_t length = strlen(message) + 1;
  26. diagnostic->error = new char[length];
  27. if (!diagnostic->error) {
  28. delete diagnostic;
  29. return nullptr;
  30. }
  31. diagnostic->position = *position;
  32. diagnostic->isTextSource = false;
  33. memset(diagnostic->error, 0, length);
  34. strncpy(diagnostic->error, message, length);
  35. return diagnostic;
  36. }
  37. void spvDiagnosticDestroy(spv_diagnostic diagnostic) {
  38. if (!diagnostic) return;
  39. delete[] diagnostic->error;
  40. delete diagnostic;
  41. }
  42. spv_result_t spvDiagnosticPrint(const spv_diagnostic diagnostic) {
  43. if (!diagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
  44. if (diagnostic->isTextSource) {
  45. // NOTE: This is a text position
  46. // NOTE: add 1 to the line as editors start at line 1, we are counting new
  47. // line characters to start at line 0
  48. std::cerr << "error: " << diagnostic->position.line + 1 << ": "
  49. << diagnostic->position.column + 1 << ": " << diagnostic->error
  50. << "\n";
  51. return SPV_SUCCESS;
  52. } else {
  53. // NOTE: Assume this is a binary position
  54. std::cerr << "error: " << diagnostic->position.index << ": "
  55. << diagnostic->error << "\n";
  56. return SPV_SUCCESS;
  57. }
  58. }
  59. namespace libspirv {
  60. DiagnosticStream::DiagnosticStream(DiagnosticStream&& other)
  61. : stream_(),
  62. position_(other.position_),
  63. consumer_(other.consumer_),
  64. error_(other.error_) {
  65. // Prevent the other object from emitting output during destruction.
  66. other.error_ = SPV_FAILED_MATCH;
  67. // Some platforms are missing support for std::ostringstream functionality,
  68. // including: move constructor, swap method. Either would have been a
  69. // better choice than copying the string.
  70. stream_ << other.stream_.str();
  71. }
  72. DiagnosticStream::~DiagnosticStream() {
  73. if (error_ != SPV_FAILED_MATCH && consumer_ != nullptr) {
  74. auto level = SPV_MSG_ERROR;
  75. switch (error_) {
  76. case SPV_SUCCESS:
  77. case SPV_REQUESTED_TERMINATION: // Essentially success.
  78. level = SPV_MSG_INFO;
  79. break;
  80. case SPV_WARNING:
  81. level = SPV_MSG_WARNING;
  82. break;
  83. case SPV_UNSUPPORTED:
  84. case SPV_ERROR_INTERNAL:
  85. case SPV_ERROR_INVALID_TABLE:
  86. level = SPV_MSG_INTERNAL_ERROR;
  87. break;
  88. case SPV_ERROR_OUT_OF_MEMORY:
  89. level = SPV_MSG_FATAL;
  90. break;
  91. default:
  92. break;
  93. }
  94. consumer_(level, "input", position_, stream_.str().c_str());
  95. }
  96. }
  97. void UseDiagnosticAsMessageConsumer(spv_context context,
  98. spv_diagnostic* diagnostic) {
  99. assert(diagnostic && *diagnostic == nullptr);
  100. auto create_diagnostic = [diagnostic](spv_message_level_t, const char*,
  101. const spv_position_t& position,
  102. const char* message) {
  103. auto p = position;
  104. spvDiagnosticDestroy(*diagnostic); // Avoid memory leak.
  105. *diagnostic = spvDiagnosticCreate(&p, message);
  106. };
  107. libspirv::SetContextMessageConsumer(context, std::move(create_diagnostic));
  108. }
  109. std::string spvResultToString(spv_result_t res) {
  110. std::string out;
  111. switch (res) {
  112. case SPV_SUCCESS:
  113. out = "SPV_SUCCESS";
  114. break;
  115. case SPV_UNSUPPORTED:
  116. out = "SPV_UNSUPPORTED";
  117. break;
  118. case SPV_END_OF_STREAM:
  119. out = "SPV_END_OF_STREAM";
  120. break;
  121. case SPV_WARNING:
  122. out = "SPV_WARNING";
  123. break;
  124. case SPV_FAILED_MATCH:
  125. out = "SPV_FAILED_MATCH";
  126. break;
  127. case SPV_REQUESTED_TERMINATION:
  128. out = "SPV_REQUESTED_TERMINATION";
  129. break;
  130. case SPV_ERROR_INTERNAL:
  131. out = "SPV_ERROR_INTERNAL";
  132. break;
  133. case SPV_ERROR_OUT_OF_MEMORY:
  134. out = "SPV_ERROR_OUT_OF_MEMORY";
  135. break;
  136. case SPV_ERROR_INVALID_POINTER:
  137. out = "SPV_ERROR_INVALID_POINTER";
  138. break;
  139. case SPV_ERROR_INVALID_BINARY:
  140. out = "SPV_ERROR_INVALID_BINARY";
  141. break;
  142. case SPV_ERROR_INVALID_TEXT:
  143. out = "SPV_ERROR_INVALID_TEXT";
  144. break;
  145. case SPV_ERROR_INVALID_TABLE:
  146. out = "SPV_ERROR_INVALID_TABLE";
  147. break;
  148. case SPV_ERROR_INVALID_VALUE:
  149. out = "SPV_ERROR_INVALID_VALUE";
  150. break;
  151. case SPV_ERROR_INVALID_DIAGNOSTIC:
  152. out = "SPV_ERROR_INVALID_DIAGNOSTIC";
  153. break;
  154. case SPV_ERROR_INVALID_LOOKUP:
  155. out = "SPV_ERROR_INVALID_LOOKUP";
  156. break;
  157. case SPV_ERROR_INVALID_ID:
  158. out = "SPV_ERROR_INVALID_ID";
  159. break;
  160. case SPV_ERROR_INVALID_CFG:
  161. out = "SPV_ERROR_INVALID_CFG";
  162. break;
  163. case SPV_ERROR_INVALID_LAYOUT:
  164. out = "SPV_ERROR_INVALID_LAYOUT";
  165. break;
  166. default:
  167. out = "Unknown Error";
  168. }
  169. return out;
  170. }
  171. } // namespace libspirv