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 | |
| 17 | #include <assert.h> |
| 18 | #include <string.h> |
| 19 | |
| 20 | #include <iostream> |
| 21 | |
David Neto | 5a70335 | 2016-02-17 14:44:00 -0500 | [diff] [blame] | 22 | #include "spirv-tools/libspirv.h" |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 23 | #include "table.h" |
Lei Zhang | 923f6c1 | 2015-11-11 12:45:23 -0500 | [diff] [blame] | 24 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 25 | // Diagnostic API |
| 26 | |
| 27 | spv_diagnostic spvDiagnosticCreate(const spv_position position, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 28 | const char* message) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 29 | spv_diagnostic diagnostic = new spv_diagnostic_t; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 30 | if (!diagnostic) return nullptr; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 31 | size_t length = strlen(message) + 1; |
| 32 | diagnostic->error = new char[length]; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 33 | if (!diagnostic->error) { |
| 34 | delete diagnostic; |
| 35 | return nullptr; |
| 36 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 37 | diagnostic->position = *position; |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 38 | diagnostic->isTextSource = false; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 39 | memset(diagnostic->error, 0, length); |
| 40 | strncpy(diagnostic->error, message, length); |
| 41 | return diagnostic; |
| 42 | } |
| 43 | |
| 44 | void spvDiagnosticDestroy(spv_diagnostic diagnostic) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 45 | if (!diagnostic) return; |
Eric Engestrom | eb6ae97 | 2016-02-18 23:41:16 +0000 | [diff] [blame] | 46 | delete[] diagnostic->error; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 47 | delete diagnostic; |
| 48 | } |
| 49 | |
| 50 | spv_result_t spvDiagnosticPrint(const spv_diagnostic diagnostic) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 51 | if (!diagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 52 | |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 53 | if (diagnostic->isTextSource) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 54 | // NOTE: This is a text position |
| 55 | // NOTE: add 1 to the line as editors start at line 1, we are counting new |
| 56 | // line characters to start at line 0 |
| 57 | std::cerr << "error: " << diagnostic->position.line + 1 << ": " |
| 58 | << diagnostic->position.column + 1 << ": " << diagnostic->error |
| 59 | << "\n"; |
| 60 | return SPV_SUCCESS; |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 61 | } else { |
| 62 | // NOTE: Assume this is a binary position |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 63 | std::cerr << "error: " << diagnostic->position.index << ": " |
| 64 | << diagnostic->error << "\n"; |
| 65 | return SPV_SUCCESS; |
| 66 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 67 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 68 | |
David Neto | 0165636 | 2015-11-20 10:44:41 -0500 | [diff] [blame] | 69 | namespace libspirv { |
| 70 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 71 | DiagnosticStream::~DiagnosticStream() { |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 72 | if (error_ != SPV_FAILED_MATCH && consumer_ != nullptr) { |
Lei Zhang | 6849a3c | 2016-09-21 12:44:37 -0400 | [diff] [blame] | 73 | auto level = spvtools::SPV_MSG_ERROR; |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 74 | switch (error_) { |
| 75 | case SPV_SUCCESS: |
| 76 | case SPV_REQUESTED_TERMINATION: // Essentially success. |
Lei Zhang | 6849a3c | 2016-09-21 12:44:37 -0400 | [diff] [blame] | 77 | level = spvtools::SPV_MSG_INFO; |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 78 | break; |
| 79 | case SPV_WARNING: |
Lei Zhang | 6849a3c | 2016-09-21 12:44:37 -0400 | [diff] [blame] | 80 | level = spvtools::SPV_MSG_WARNINING; |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 81 | break; |
| 82 | case SPV_UNSUPPORTED: |
| 83 | case SPV_ERROR_INTERNAL: |
| 84 | case SPV_ERROR_INVALID_TABLE: |
Lei Zhang | 6849a3c | 2016-09-21 12:44:37 -0400 | [diff] [blame] | 85 | level = spvtools::SPV_MSG_INTERNAL_ERROR; |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 86 | break; |
| 87 | case SPV_ERROR_OUT_OF_MEMORY: |
Lei Zhang | 6849a3c | 2016-09-21 12:44:37 -0400 | [diff] [blame] | 88 | level = spvtools::SPV_MSG_FATAL; |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 89 | break; |
| 90 | default: |
| 91 | break; |
| 92 | } |
Lei Zhang | 3590279 | 2016-09-16 15:43:41 -0400 | [diff] [blame] | 93 | consumer_(level, "input", position_, stream_.str().c_str()); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 94 | } |
| 95 | } |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 96 | |
| 97 | void UseDiagnosticAsMessageConsumer(spv_context context, |
| 98 | spv_diagnostic* diagnostic) { |
| 99 | assert(diagnostic && *diagnostic == nullptr); |
| 100 | |
Lei Zhang | 6849a3c | 2016-09-21 12:44:37 -0400 | [diff] [blame] | 101 | auto create_diagnostic = [diagnostic]( |
| 102 | spvtools::spv_message_level_t, const char*, |
| 103 | const spv_position_t& position, const char* message) { |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 104 | auto p = position; |
| 105 | spvDiagnosticDestroy(*diagnostic); // Avoid memory leak. |
| 106 | *diagnostic = spvDiagnosticCreate(&p, message); |
| 107 | }; |
| 108 | SetContextMessageConsumer(context, std::move(create_diagnostic)); |
| 109 | } |
| 110 | |
| 111 | std::string spvResultToString(spv_result_t res) { |
Umar Arshad | c741385 | 2015-12-15 21:44:21 -0500 | [diff] [blame] | 112 | std::string out; |
| 113 | switch (res) { |
| 114 | case SPV_SUCCESS: |
| 115 | out = "SPV_SUCCESS"; |
| 116 | break; |
| 117 | case SPV_UNSUPPORTED: |
| 118 | out = "SPV_UNSUPPORTED"; |
| 119 | break; |
| 120 | case SPV_END_OF_STREAM: |
| 121 | out = "SPV_END_OF_STREAM"; |
| 122 | break; |
| 123 | case SPV_WARNING: |
| 124 | out = "SPV_WARNING"; |
| 125 | break; |
| 126 | case SPV_FAILED_MATCH: |
| 127 | out = "SPV_FAILED_MATCH"; |
| 128 | break; |
| 129 | case SPV_REQUESTED_TERMINATION: |
| 130 | out = "SPV_REQUESTED_TERMINATION"; |
| 131 | break; |
| 132 | case SPV_ERROR_INTERNAL: |
| 133 | out = "SPV_ERROR_INTERNAL"; |
| 134 | break; |
| 135 | case SPV_ERROR_OUT_OF_MEMORY: |
| 136 | out = "SPV_ERROR_OUT_OF_MEMORY"; |
| 137 | break; |
| 138 | case SPV_ERROR_INVALID_POINTER: |
| 139 | out = "SPV_ERROR_INVALID_POINTER"; |
| 140 | break; |
| 141 | case SPV_ERROR_INVALID_BINARY: |
| 142 | out = "SPV_ERROR_INVALID_BINARY"; |
| 143 | break; |
| 144 | case SPV_ERROR_INVALID_TEXT: |
| 145 | out = "SPV_ERROR_INVALID_TEXT"; |
| 146 | break; |
| 147 | case SPV_ERROR_INVALID_TABLE: |
| 148 | out = "SPV_ERROR_INVALID_TABLE"; |
| 149 | break; |
| 150 | case SPV_ERROR_INVALID_VALUE: |
| 151 | out = "SPV_ERROR_INVALID_VALUE"; |
| 152 | break; |
| 153 | case SPV_ERROR_INVALID_DIAGNOSTIC: |
| 154 | out = "SPV_ERROR_INVALID_DIAGNOSTIC"; |
| 155 | break; |
| 156 | case SPV_ERROR_INVALID_LOOKUP: |
| 157 | out = "SPV_ERROR_INVALID_LOOKUP"; |
| 158 | break; |
| 159 | case SPV_ERROR_INVALID_ID: |
| 160 | out = "SPV_ERROR_INVALID_ID"; |
| 161 | break; |
| 162 | case SPV_ERROR_INVALID_CFG: |
| 163 | out = "SPV_ERROR_INVALID_CFG"; |
| 164 | break; |
| 165 | case SPV_ERROR_INVALID_LAYOUT: |
| 166 | out = "SPV_ERROR_INVALID_LAYOUT"; |
| 167 | break; |
| 168 | default: |
| 169 | out = "Unknown Error"; |
| 170 | } |
| 171 | return out; |
| 172 | } |
David Neto | 0165636 | 2015-11-20 10:44:41 -0500 | [diff] [blame] | 173 | |
| 174 | } // namespace libspirv |