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