blob: fb6af498c618f127b65cc94154de3cbad04f2727 [file] [log] [blame]
Dejan Mircevskib6fe02f2016-01-07 13:44:22 -05001// Copyright (c) 2015-2016 The Khronos Group Inc.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01002//
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)83e5a292015-05-22 18:26:19 +010027#include "diagnostic.h"
28
29#include <assert.h>
30#include <string.h>
31
32#include <iostream>
33
David Neto5a703352016-02-17 14:44:00 -050034#include "spirv-tools/libspirv.h"
Lei Zhang923f6c12015-11-11 12:45:23 -050035
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010036// Diagnostic API
37
38spv_diagnostic spvDiagnosticCreate(const spv_position position,
Lei Zhang1a0334e2015-11-02 09:41:20 -050039 const char* message) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010040 spv_diagnostic diagnostic = new spv_diagnostic_t;
Lei Zhang40056702015-09-11 14:31:27 -040041 if (!diagnostic) return nullptr;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010042 size_t length = strlen(message) + 1;
43 diagnostic->error = new char[length];
Lei Zhang40056702015-09-11 14:31:27 -040044 if (!diagnostic->error) {
45 delete diagnostic;
46 return nullptr;
47 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010048 diagnostic->position = *position;
David Netoc9786432015-09-01 18:05:14 -040049 diagnostic->isTextSource = false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010050 memset(diagnostic->error, 0, length);
51 strncpy(diagnostic->error, message, length);
52 return diagnostic;
53}
54
55void spvDiagnosticDestroy(spv_diagnostic diagnostic) {
Lei Zhang40056702015-09-11 14:31:27 -040056 if (!diagnostic) return;
Eric Engestromeb6ae972016-02-18 23:41:16 +000057 delete[] diagnostic->error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010058 delete diagnostic;
59}
60
61spv_result_t spvDiagnosticPrint(const spv_diagnostic diagnostic) {
Lei Zhang40056702015-09-11 14:31:27 -040062 if (!diagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010063
David Netoc9786432015-09-01 18:05:14 -040064 if (diagnostic->isTextSource) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010065 // 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 Netoc9786432015-09-01 18:05:14 -040072 } else {
73 // NOTE: Assume this is a binary position
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010074 std::cerr << "error: " << diagnostic->position.index << ": "
75 << diagnostic->error << "\n";
76 return SPV_SUCCESS;
77 }
78
79 return SPV_ERROR_INVALID_VALUE;
80}
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040081
David Neto01656362015-11-20 10:44:41 -050082namespace libspirv {
83
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040084DiagnosticStream::~DiagnosticStream() {
David Neto51013d12015-10-14 11:31:51 -040085 if (pDiagnostic_ && error_ != SPV_FAILED_MATCH) {
David Netobae88512015-10-28 13:16:56 -040086 *pDiagnostic_ = spvDiagnosticCreate(&position_, stream_.str().c_str());
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040087 }
88}
Umar Arshadc7413852015-12-15 21:44:21 -050089std::string
90spvResultToString(spv_result_t res) {
91 std::string out;
92 switch (res) {
93 case SPV_SUCCESS:
94 out = "SPV_SUCCESS";
95 break;
96 case SPV_UNSUPPORTED:
97 out = "SPV_UNSUPPORTED";
98 break;
99 case SPV_END_OF_STREAM:
100 out = "SPV_END_OF_STREAM";
101 break;
102 case SPV_WARNING:
103 out = "SPV_WARNING";
104 break;
105 case SPV_FAILED_MATCH:
106 out = "SPV_FAILED_MATCH";
107 break;
108 case SPV_REQUESTED_TERMINATION:
109 out = "SPV_REQUESTED_TERMINATION";
110 break;
111 case SPV_ERROR_INTERNAL:
112 out = "SPV_ERROR_INTERNAL";
113 break;
114 case SPV_ERROR_OUT_OF_MEMORY:
115 out = "SPV_ERROR_OUT_OF_MEMORY";
116 break;
117 case SPV_ERROR_INVALID_POINTER:
118 out = "SPV_ERROR_INVALID_POINTER";
119 break;
120 case SPV_ERROR_INVALID_BINARY:
121 out = "SPV_ERROR_INVALID_BINARY";
122 break;
123 case SPV_ERROR_INVALID_TEXT:
124 out = "SPV_ERROR_INVALID_TEXT";
125 break;
126 case SPV_ERROR_INVALID_TABLE:
127 out = "SPV_ERROR_INVALID_TABLE";
128 break;
129 case SPV_ERROR_INVALID_VALUE:
130 out = "SPV_ERROR_INVALID_VALUE";
131 break;
132 case SPV_ERROR_INVALID_DIAGNOSTIC:
133 out = "SPV_ERROR_INVALID_DIAGNOSTIC";
134 break;
135 case SPV_ERROR_INVALID_LOOKUP:
136 out = "SPV_ERROR_INVALID_LOOKUP";
137 break;
138 case SPV_ERROR_INVALID_ID:
139 out = "SPV_ERROR_INVALID_ID";
140 break;
141 case SPV_ERROR_INVALID_CFG:
142 out = "SPV_ERROR_INVALID_CFG";
143 break;
144 case SPV_ERROR_INVALID_LAYOUT:
145 out = "SPV_ERROR_INVALID_LAYOUT";
146 break;
147 default:
148 out = "Unknown Error";
149 }
150 return out;
151}
David Neto01656362015-11-20 10:44:41 -0500152
153} // namespace libspirv