Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 1 | // Copyright (c) 2015 The Khronos Group Inc. |
| 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 | |
| 27 | #include <libspirv/libspirv.h> |
| 28 | #include "diagnostic.h" |
| 29 | |
| 30 | #include <assert.h> |
| 31 | #include <string.h> |
| 32 | |
| 33 | #include <iostream> |
| 34 | |
| 35 | // Diagnostic API |
| 36 | |
| 37 | spv_diagnostic spvDiagnosticCreate(const spv_position position, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 38 | const char* message) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 39 | spv_diagnostic diagnostic = new spv_diagnostic_t; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 40 | if (!diagnostic) return nullptr; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 41 | size_t length = strlen(message) + 1; |
| 42 | diagnostic->error = new char[length]; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 43 | if (!diagnostic->error) { |
| 44 | delete diagnostic; |
| 45 | return nullptr; |
| 46 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 47 | diagnostic->position = *position; |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 48 | diagnostic->isTextSource = false; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 49 | memset(diagnostic->error, 0, length); |
| 50 | strncpy(diagnostic->error, message, length); |
| 51 | return diagnostic; |
| 52 | } |
| 53 | |
| 54 | void spvDiagnosticDestroy(spv_diagnostic diagnostic) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 55 | if (!diagnostic) return; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 56 | if (diagnostic->error) { |
| 57 | delete[] diagnostic->error; |
| 58 | } |
| 59 | delete diagnostic; |
| 60 | } |
| 61 | |
| 62 | spv_result_t spvDiagnosticPrint(const spv_diagnostic diagnostic) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 63 | if (!diagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 64 | |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 65 | if (diagnostic->isTextSource) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 66 | // NOTE: This is a text position |
| 67 | // NOTE: add 1 to the line as editors start at line 1, we are counting new |
| 68 | // line characters to start at line 0 |
| 69 | std::cerr << "error: " << diagnostic->position.line + 1 << ": " |
| 70 | << diagnostic->position.column + 1 << ": " << diagnostic->error |
| 71 | << "\n"; |
| 72 | return SPV_SUCCESS; |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 73 | } else { |
| 74 | // NOTE: Assume this is a binary position |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 75 | std::cerr << "error: " << diagnostic->position.index << ": " |
| 76 | << diagnostic->error << "\n"; |
| 77 | return SPV_SUCCESS; |
| 78 | } |
| 79 | |
| 80 | return SPV_ERROR_INVALID_VALUE; |
| 81 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 82 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 83 | DiagnosticStream::~DiagnosticStream() { |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 84 | if (pDiagnostic_ && error_ != SPV_FAILED_MATCH) { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 85 | *pDiagnostic_ = spvDiagnosticCreate(position_, stream_.str().c_str()); |
| 86 | } |
| 87 | } |