Dejan Mircevski | b6fe02f | 2016-01-07 13:44:22 -0500 | [diff] [blame] | 1 | // Copyright (c) 2015-2016 The Khronos Group Inc. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 2 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 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 |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 6 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 8 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 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. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 14 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 15 | #include "diagnostic.h" |
| 16 | |
Lei Zhang | 620f05e | 2016-09-16 16:12:04 -0400 | [diff] [blame] | 17 | #include <cassert> |
| 18 | #include <cstring> |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 19 | #include <iostream> |
David Neto | 169266e | 2017-10-01 09:27:00 -0400 | [diff] [blame] | 20 | #include <sstream> |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 21 | |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 22 | #include "table.h" |
Lei Zhang | 923f6c1 | 2015-11-11 12:45:23 -0500 | [diff] [blame] | 23 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 24 | // Diagnostic API |
| 25 | |
| 26 | spv_diagnostic spvDiagnosticCreate(const spv_position position, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 27 | const char* message) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 28 | spv_diagnostic diagnostic = new spv_diagnostic_t; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 29 | if (!diagnostic) return nullptr; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 30 | size_t length = strlen(message) + 1; |
| 31 | diagnostic->error = new char[length]; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 32 | if (!diagnostic->error) { |
| 33 | delete diagnostic; |
| 34 | return nullptr; |
| 35 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 36 | diagnostic->position = *position; |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 37 | diagnostic->isTextSource = false; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 38 | memset(diagnostic->error, 0, length); |
| 39 | strncpy(diagnostic->error, message, length); |
| 40 | return diagnostic; |
| 41 | } |
| 42 | |
| 43 | void spvDiagnosticDestroy(spv_diagnostic diagnostic) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 44 | if (!diagnostic) return; |
Eric Engestrom | eb6ae97 | 2016-02-18 23:41:16 +0000 | [diff] [blame] | 45 | delete[] diagnostic->error; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 46 | delete diagnostic; |
| 47 | } |
| 48 | |
| 49 | spv_result_t spvDiagnosticPrint(const spv_diagnostic diagnostic) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 50 | if (!diagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 51 | |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 52 | if (diagnostic->isTextSource) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 53 | // NOTE: This is a text position |
| 54 | // NOTE: add 1 to the line as editors start at line 1, we are counting new |
| 55 | // line characters to start at line 0 |
| 56 | std::cerr << "error: " << diagnostic->position.line + 1 << ": " |
| 57 | << diagnostic->position.column + 1 << ": " << diagnostic->error |
| 58 | << "\n"; |
| 59 | return SPV_SUCCESS; |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 60 | } else { |
| 61 | // NOTE: Assume this is a binary position |
dan sinclair | 5109104 | 2018-07-03 15:06:54 -0400 | [diff] [blame] | 62 | std::cerr << "error: "; |
| 63 | if (diagnostic->position.index > 0) |
| 64 | std::cerr << diagnostic->position.index << ": "; |
| 65 | std::cerr << diagnostic->error << "\n"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 66 | return SPV_SUCCESS; |
| 67 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 68 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 69 | |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 70 | namespace spvtools { |
David Neto | 0165636 | 2015-11-20 10:44:41 -0500 | [diff] [blame] | 71 | |
David Neto | 169266e | 2017-10-01 09:27:00 -0400 | [diff] [blame] | 72 | DiagnosticStream::DiagnosticStream(DiagnosticStream&& other) |
| 73 | : stream_(), |
| 74 | position_(other.position_), |
| 75 | consumer_(other.consumer_), |
dan sinclair | f80696e | 2018-06-19 16:02:44 -0400 | [diff] [blame] | 76 | disassembled_instruction_(std::move(other.disassembled_instruction_)), |
David Neto | 169266e | 2017-10-01 09:27:00 -0400 | [diff] [blame] | 77 | error_(other.error_) { |
| 78 | // Prevent the other object from emitting output during destruction. |
| 79 | other.error_ = SPV_FAILED_MATCH; |
| 80 | // Some platforms are missing support for std::ostringstream functionality, |
| 81 | // including: move constructor, swap method. Either would have been a |
| 82 | // better choice than copying the string. |
| 83 | stream_ << other.stream_.str(); |
| 84 | } |
| 85 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 86 | DiagnosticStream::~DiagnosticStream() { |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 87 | if (error_ != SPV_FAILED_MATCH && consumer_ != nullptr) { |
Lei Zhang | 620f05e | 2016-09-16 16:12:04 -0400 | [diff] [blame] | 88 | auto level = SPV_MSG_ERROR; |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 89 | switch (error_) { |
| 90 | case SPV_SUCCESS: |
| 91 | case SPV_REQUESTED_TERMINATION: // Essentially success. |
Lei Zhang | 620f05e | 2016-09-16 16:12:04 -0400 | [diff] [blame] | 92 | level = SPV_MSG_INFO; |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 93 | break; |
| 94 | case SPV_WARNING: |
David Neto | c3caa54 | 2017-03-16 15:19:51 -0400 | [diff] [blame] | 95 | level = SPV_MSG_WARNING; |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 96 | break; |
| 97 | case SPV_UNSUPPORTED: |
| 98 | case SPV_ERROR_INTERNAL: |
| 99 | case SPV_ERROR_INVALID_TABLE: |
Lei Zhang | 620f05e | 2016-09-16 16:12:04 -0400 | [diff] [blame] | 100 | level = SPV_MSG_INTERNAL_ERROR; |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 101 | break; |
| 102 | case SPV_ERROR_OUT_OF_MEMORY: |
Lei Zhang | 620f05e | 2016-09-16 16:12:04 -0400 | [diff] [blame] | 103 | level = SPV_MSG_FATAL; |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 104 | break; |
| 105 | default: |
| 106 | break; |
| 107 | } |
dan sinclair | f80696e | 2018-06-19 16:02:44 -0400 | [diff] [blame] | 108 | if (disassembled_instruction_.size() > 0) |
| 109 | stream_ << std::endl << " " << disassembled_instruction_ << std::endl; |
| 110 | |
Lei Zhang | 3590279 | 2016-09-16 15:43:41 -0400 | [diff] [blame] | 111 | consumer_(level, "input", position_, stream_.str().c_str()); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 112 | } |
| 113 | } |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 114 | |
| 115 | void UseDiagnosticAsMessageConsumer(spv_context context, |
| 116 | spv_diagnostic* diagnostic) { |
| 117 | assert(diagnostic && *diagnostic == nullptr); |
| 118 | |
Lei Zhang | 620f05e | 2016-09-16 16:12:04 -0400 | [diff] [blame] | 119 | auto create_diagnostic = [diagnostic](spv_message_level_t, const char*, |
| 120 | const spv_position_t& position, |
| 121 | const char* message) { |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 122 | auto p = position; |
| 123 | spvDiagnosticDestroy(*diagnostic); // Avoid memory leak. |
| 124 | *diagnostic = spvDiagnosticCreate(&p, message); |
| 125 | }; |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 126 | SetContextMessageConsumer(context, std::move(create_diagnostic)); |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | std::string spvResultToString(spv_result_t res) { |
Umar Arshad | c741385 | 2015-12-15 21:44:21 -0500 | [diff] [blame] | 130 | std::string out; |
| 131 | switch (res) { |
| 132 | case SPV_SUCCESS: |
| 133 | out = "SPV_SUCCESS"; |
| 134 | break; |
| 135 | case SPV_UNSUPPORTED: |
| 136 | out = "SPV_UNSUPPORTED"; |
| 137 | break; |
| 138 | case SPV_END_OF_STREAM: |
| 139 | out = "SPV_END_OF_STREAM"; |
| 140 | break; |
| 141 | case SPV_WARNING: |
| 142 | out = "SPV_WARNING"; |
| 143 | break; |
| 144 | case SPV_FAILED_MATCH: |
| 145 | out = "SPV_FAILED_MATCH"; |
| 146 | break; |
| 147 | case SPV_REQUESTED_TERMINATION: |
| 148 | out = "SPV_REQUESTED_TERMINATION"; |
| 149 | break; |
| 150 | case SPV_ERROR_INTERNAL: |
| 151 | out = "SPV_ERROR_INTERNAL"; |
| 152 | break; |
| 153 | case SPV_ERROR_OUT_OF_MEMORY: |
| 154 | out = "SPV_ERROR_OUT_OF_MEMORY"; |
| 155 | break; |
| 156 | case SPV_ERROR_INVALID_POINTER: |
| 157 | out = "SPV_ERROR_INVALID_POINTER"; |
| 158 | break; |
| 159 | case SPV_ERROR_INVALID_BINARY: |
| 160 | out = "SPV_ERROR_INVALID_BINARY"; |
| 161 | break; |
| 162 | case SPV_ERROR_INVALID_TEXT: |
| 163 | out = "SPV_ERROR_INVALID_TEXT"; |
| 164 | break; |
| 165 | case SPV_ERROR_INVALID_TABLE: |
| 166 | out = "SPV_ERROR_INVALID_TABLE"; |
| 167 | break; |
| 168 | case SPV_ERROR_INVALID_VALUE: |
| 169 | out = "SPV_ERROR_INVALID_VALUE"; |
| 170 | break; |
| 171 | case SPV_ERROR_INVALID_DIAGNOSTIC: |
| 172 | out = "SPV_ERROR_INVALID_DIAGNOSTIC"; |
| 173 | break; |
| 174 | case SPV_ERROR_INVALID_LOOKUP: |
| 175 | out = "SPV_ERROR_INVALID_LOOKUP"; |
| 176 | break; |
| 177 | case SPV_ERROR_INVALID_ID: |
| 178 | out = "SPV_ERROR_INVALID_ID"; |
| 179 | break; |
| 180 | case SPV_ERROR_INVALID_CFG: |
| 181 | out = "SPV_ERROR_INVALID_CFG"; |
| 182 | break; |
| 183 | case SPV_ERROR_INVALID_LAYOUT: |
| 184 | out = "SPV_ERROR_INVALID_LAYOUT"; |
| 185 | break; |
| 186 | default: |
| 187 | out = "Unknown Error"; |
| 188 | } |
| 189 | return out; |
| 190 | } |
David Neto | 0165636 | 2015-11-20 10:44:41 -0500 | [diff] [blame] | 191 | |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 192 | } // namespace spvtools |