blob: b887c7adaf4ddc32ff7711bc6076586cce74fda1 [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//
David Neto9fc86582016-09-01 15:33:59 -04003// 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)83e5a292015-05-22 18:26:19 +01006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01008//
David Neto9fc86582016-09-01 15:33:59 -04009// 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)83e5a292015-05-22 18:26:19 +010014
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010015#include "diagnostic.h"
16
Lei Zhang620f05e2016-09-16 16:12:04 -040017#include <cassert>
18#include <cstring>
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010019#include <iostream>
20
Lei Zhang755f97f2016-09-02 18:06:18 -040021#include "table.h"
Lei Zhang923f6c12015-11-11 12:45:23 -050022
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010023// Diagnostic API
24
25spv_diagnostic spvDiagnosticCreate(const spv_position position,
Lei Zhang1a0334e2015-11-02 09:41:20 -050026 const char* message) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010027 spv_diagnostic diagnostic = new spv_diagnostic_t;
Lei Zhang40056702015-09-11 14:31:27 -040028 if (!diagnostic) return nullptr;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010029 size_t length = strlen(message) + 1;
30 diagnostic->error = new char[length];
Lei Zhang40056702015-09-11 14:31:27 -040031 if (!diagnostic->error) {
32 delete diagnostic;
33 return nullptr;
34 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010035 diagnostic->position = *position;
David Netoc9786432015-09-01 18:05:14 -040036 diagnostic->isTextSource = false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010037 memset(diagnostic->error, 0, length);
38 strncpy(diagnostic->error, message, length);
39 return diagnostic;
40}
41
42void spvDiagnosticDestroy(spv_diagnostic diagnostic) {
Lei Zhang40056702015-09-11 14:31:27 -040043 if (!diagnostic) return;
Eric Engestromeb6ae972016-02-18 23:41:16 +000044 delete[] diagnostic->error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010045 delete diagnostic;
46}
47
48spv_result_t spvDiagnosticPrint(const spv_diagnostic diagnostic) {
Lei Zhang40056702015-09-11 14:31:27 -040049 if (!diagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010050
David Netoc9786432015-09-01 18:05:14 -040051 if (diagnostic->isTextSource) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010052 // NOTE: This is a text position
53 // NOTE: add 1 to the line as editors start at line 1, we are counting new
54 // line characters to start at line 0
55 std::cerr << "error: " << diagnostic->position.line + 1 << ": "
56 << diagnostic->position.column + 1 << ": " << diagnostic->error
57 << "\n";
58 return SPV_SUCCESS;
David Netoc9786432015-09-01 18:05:14 -040059 } else {
60 // NOTE: Assume this is a binary position
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010061 std::cerr << "error: " << diagnostic->position.index << ": "
62 << diagnostic->error << "\n";
63 return SPV_SUCCESS;
64 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010065}
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040066
David Neto01656362015-11-20 10:44:41 -050067namespace libspirv {
68
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040069DiagnosticStream::~DiagnosticStream() {
Lei Zhang755f97f2016-09-02 18:06:18 -040070 if (error_ != SPV_FAILED_MATCH && consumer_ != nullptr) {
Lei Zhang620f05e2016-09-16 16:12:04 -040071 auto level = SPV_MSG_ERROR;
Lei Zhang755f97f2016-09-02 18:06:18 -040072 switch (error_) {
73 case SPV_SUCCESS:
74 case SPV_REQUESTED_TERMINATION: // Essentially success.
Lei Zhang620f05e2016-09-16 16:12:04 -040075 level = SPV_MSG_INFO;
Lei Zhang755f97f2016-09-02 18:06:18 -040076 break;
77 case SPV_WARNING:
David Netoc3caa542017-03-16 15:19:51 -040078 level = SPV_MSG_WARNING;
Lei Zhang755f97f2016-09-02 18:06:18 -040079 break;
80 case SPV_UNSUPPORTED:
81 case SPV_ERROR_INTERNAL:
82 case SPV_ERROR_INVALID_TABLE:
Lei Zhang620f05e2016-09-16 16:12:04 -040083 level = SPV_MSG_INTERNAL_ERROR;
Lei Zhang755f97f2016-09-02 18:06:18 -040084 break;
85 case SPV_ERROR_OUT_OF_MEMORY:
Lei Zhang620f05e2016-09-16 16:12:04 -040086 level = SPV_MSG_FATAL;
Lei Zhang755f97f2016-09-02 18:06:18 -040087 break;
88 default:
89 break;
90 }
Lei Zhang35902792016-09-16 15:43:41 -040091 consumer_(level, "input", position_, stream_.str().c_str());
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040092 }
93}
Lei Zhang755f97f2016-09-02 18:06:18 -040094
95void UseDiagnosticAsMessageConsumer(spv_context context,
96 spv_diagnostic* diagnostic) {
97 assert(diagnostic && *diagnostic == nullptr);
98
Lei Zhang620f05e2016-09-16 16:12:04 -040099 auto create_diagnostic = [diagnostic](spv_message_level_t, const char*,
100 const spv_position_t& position,
101 const char* message) {
Lei Zhang755f97f2016-09-02 18:06:18 -0400102 auto p = position;
103 spvDiagnosticDestroy(*diagnostic); // Avoid memory leak.
104 *diagnostic = spvDiagnosticCreate(&p, message);
105 };
106 SetContextMessageConsumer(context, std::move(create_diagnostic));
107}
108
109std::string spvResultToString(spv_result_t res) {
Umar Arshadc7413852015-12-15 21:44:21 -0500110 std::string out;
111 switch (res) {
112 case SPV_SUCCESS:
113 out = "SPV_SUCCESS";
114 break;
115 case SPV_UNSUPPORTED:
116 out = "SPV_UNSUPPORTED";
117 break;
118 case SPV_END_OF_STREAM:
119 out = "SPV_END_OF_STREAM";
120 break;
121 case SPV_WARNING:
122 out = "SPV_WARNING";
123 break;
124 case SPV_FAILED_MATCH:
125 out = "SPV_FAILED_MATCH";
126 break;
127 case SPV_REQUESTED_TERMINATION:
128 out = "SPV_REQUESTED_TERMINATION";
129 break;
130 case SPV_ERROR_INTERNAL:
131 out = "SPV_ERROR_INTERNAL";
132 break;
133 case SPV_ERROR_OUT_OF_MEMORY:
134 out = "SPV_ERROR_OUT_OF_MEMORY";
135 break;
136 case SPV_ERROR_INVALID_POINTER:
137 out = "SPV_ERROR_INVALID_POINTER";
138 break;
139 case SPV_ERROR_INVALID_BINARY:
140 out = "SPV_ERROR_INVALID_BINARY";
141 break;
142 case SPV_ERROR_INVALID_TEXT:
143 out = "SPV_ERROR_INVALID_TEXT";
144 break;
145 case SPV_ERROR_INVALID_TABLE:
146 out = "SPV_ERROR_INVALID_TABLE";
147 break;
148 case SPV_ERROR_INVALID_VALUE:
149 out = "SPV_ERROR_INVALID_VALUE";
150 break;
151 case SPV_ERROR_INVALID_DIAGNOSTIC:
152 out = "SPV_ERROR_INVALID_DIAGNOSTIC";
153 break;
154 case SPV_ERROR_INVALID_LOOKUP:
155 out = "SPV_ERROR_INVALID_LOOKUP";
156 break;
157 case SPV_ERROR_INVALID_ID:
158 out = "SPV_ERROR_INVALID_ID";
159 break;
160 case SPV_ERROR_INVALID_CFG:
161 out = "SPV_ERROR_INVALID_CFG";
162 break;
163 case SPV_ERROR_INVALID_LAYOUT:
164 out = "SPV_ERROR_INVALID_LAYOUT";
165 break;
166 default:
167 out = "Unknown Error";
168 }
169 return out;
170}
David Neto01656362015-11-20 10:44:41 -0500171
172} // namespace libspirv