blob: 16bc33a9f35ed5b1a1c52d221d10eee4ebb0eb08 [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
17#include <assert.h>
18#include <string.h>
19
20#include <iostream>
21
David Neto5a703352016-02-17 14:44:00 -050022#include "spirv-tools/libspirv.h"
Lei Zhang755f97f2016-09-02 18:06:18 -040023#include "table.h"
Lei Zhang923f6c12015-11-11 12:45:23 -050024
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010025// Diagnostic API
26
27spv_diagnostic spvDiagnosticCreate(const spv_position position,
Lei Zhang1a0334e2015-11-02 09:41:20 -050028 const char* message) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010029 spv_diagnostic diagnostic = new spv_diagnostic_t;
Lei Zhang40056702015-09-11 14:31:27 -040030 if (!diagnostic) return nullptr;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010031 size_t length = strlen(message) + 1;
32 diagnostic->error = new char[length];
Lei Zhang40056702015-09-11 14:31:27 -040033 if (!diagnostic->error) {
34 delete diagnostic;
35 return nullptr;
36 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010037 diagnostic->position = *position;
David Netoc9786432015-09-01 18:05:14 -040038 diagnostic->isTextSource = false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010039 memset(diagnostic->error, 0, length);
40 strncpy(diagnostic->error, message, length);
41 return diagnostic;
42}
43
44void spvDiagnosticDestroy(spv_diagnostic diagnostic) {
Lei Zhang40056702015-09-11 14:31:27 -040045 if (!diagnostic) return;
Eric Engestromeb6ae972016-02-18 23:41:16 +000046 delete[] diagnostic->error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010047 delete diagnostic;
48}
49
50spv_result_t spvDiagnosticPrint(const spv_diagnostic diagnostic) {
Lei Zhang40056702015-09-11 14:31:27 -040051 if (!diagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010052
David Netoc9786432015-09-01 18:05:14 -040053 if (diagnostic->isTextSource) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010054 // 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 Netoc9786432015-09-01 18:05:14 -040061 } else {
62 // NOTE: Assume this is a binary position
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010063 std::cerr << "error: " << diagnostic->position.index << ": "
64 << diagnostic->error << "\n";
65 return SPV_SUCCESS;
66 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010067}
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040068
David Neto01656362015-11-20 10:44:41 -050069namespace libspirv {
70
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040071DiagnosticStream::~DiagnosticStream() {
Lei Zhang755f97f2016-09-02 18:06:18 -040072 using spvtools::MessageLevel;
73 if (error_ != SPV_FAILED_MATCH && consumer_ != nullptr) {
74 auto level = MessageLevel::Error;
75 switch (error_) {
76 case SPV_SUCCESS:
77 case SPV_REQUESTED_TERMINATION: // Essentially success.
78 level = MessageLevel::Info;
79 break;
80 case SPV_WARNING:
81 level = MessageLevel::Warning;
82 break;
83 case SPV_UNSUPPORTED:
84 case SPV_ERROR_INTERNAL:
85 case SPV_ERROR_INVALID_TABLE:
86 level = MessageLevel::InternalError;
87 break;
88 case SPV_ERROR_OUT_OF_MEMORY:
89 level = MessageLevel::Fatal;
90 break;
91 default:
92 break;
93 }
Lei Zhang35902792016-09-16 15:43:41 -040094 consumer_(level, "input", position_, stream_.str().c_str());
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040095 }
96}
Lei Zhang755f97f2016-09-02 18:06:18 -040097
98void UseDiagnosticAsMessageConsumer(spv_context context,
99 spv_diagnostic* diagnostic) {
100 assert(diagnostic && *diagnostic == nullptr);
101
102 auto create_diagnostic = [diagnostic](spvtools::MessageLevel, const char*,
103 const spv_position_t& position,
104 const char* message) {
105 auto p = position;
106 spvDiagnosticDestroy(*diagnostic); // Avoid memory leak.
107 *diagnostic = spvDiagnosticCreate(&p, message);
108 };
109 SetContextMessageConsumer(context, std::move(create_diagnostic));
110}
111
112std::string spvResultToString(spv_result_t res) {
Umar Arshadc7413852015-12-15 21:44:21 -0500113 std::string out;
114 switch (res) {
115 case SPV_SUCCESS:
116 out = "SPV_SUCCESS";
117 break;
118 case SPV_UNSUPPORTED:
119 out = "SPV_UNSUPPORTED";
120 break;
121 case SPV_END_OF_STREAM:
122 out = "SPV_END_OF_STREAM";
123 break;
124 case SPV_WARNING:
125 out = "SPV_WARNING";
126 break;
127 case SPV_FAILED_MATCH:
128 out = "SPV_FAILED_MATCH";
129 break;
130 case SPV_REQUESTED_TERMINATION:
131 out = "SPV_REQUESTED_TERMINATION";
132 break;
133 case SPV_ERROR_INTERNAL:
134 out = "SPV_ERROR_INTERNAL";
135 break;
136 case SPV_ERROR_OUT_OF_MEMORY:
137 out = "SPV_ERROR_OUT_OF_MEMORY";
138 break;
139 case SPV_ERROR_INVALID_POINTER:
140 out = "SPV_ERROR_INVALID_POINTER";
141 break;
142 case SPV_ERROR_INVALID_BINARY:
143 out = "SPV_ERROR_INVALID_BINARY";
144 break;
145 case SPV_ERROR_INVALID_TEXT:
146 out = "SPV_ERROR_INVALID_TEXT";
147 break;
148 case SPV_ERROR_INVALID_TABLE:
149 out = "SPV_ERROR_INVALID_TABLE";
150 break;
151 case SPV_ERROR_INVALID_VALUE:
152 out = "SPV_ERROR_INVALID_VALUE";
153 break;
154 case SPV_ERROR_INVALID_DIAGNOSTIC:
155 out = "SPV_ERROR_INVALID_DIAGNOSTIC";
156 break;
157 case SPV_ERROR_INVALID_LOOKUP:
158 out = "SPV_ERROR_INVALID_LOOKUP";
159 break;
160 case SPV_ERROR_INVALID_ID:
161 out = "SPV_ERROR_INVALID_ID";
162 break;
163 case SPV_ERROR_INVALID_CFG:
164 out = "SPV_ERROR_INVALID_CFG";
165 break;
166 case SPV_ERROR_INVALID_LAYOUT:
167 out = "SPV_ERROR_INVALID_LAYOUT";
168 break;
169 default:
170 out = "Unknown Error";
171 }
172 return out;
173}
David Neto01656362015-11-20 10:44:41 -0500174
175} // namespace libspirv