blob: 5787120696cadd97936e4444312d27a61f23b247 [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>
David Neto169266e2017-10-01 09:27:00 -040020#include <sstream>
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010021
Lei Zhang755f97f2016-09-02 18:06:18 -040022#include "table.h"
Lei Zhang923f6c12015-11-11 12:45:23 -050023
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010024// Diagnostic API
25
26spv_diagnostic spvDiagnosticCreate(const spv_position position,
Lei Zhang1a0334e2015-11-02 09:41:20 -050027 const char* message) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010028 spv_diagnostic diagnostic = new spv_diagnostic_t;
Lei Zhang40056702015-09-11 14:31:27 -040029 if (!diagnostic) return nullptr;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010030 size_t length = strlen(message) + 1;
31 diagnostic->error = new char[length];
Lei Zhang40056702015-09-11 14:31:27 -040032 if (!diagnostic->error) {
33 delete diagnostic;
34 return nullptr;
35 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010036 diagnostic->position = *position;
David Netoc9786432015-09-01 18:05:14 -040037 diagnostic->isTextSource = false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010038 memset(diagnostic->error, 0, length);
39 strncpy(diagnostic->error, message, length);
40 return diagnostic;
41}
42
43void spvDiagnosticDestroy(spv_diagnostic diagnostic) {
Lei Zhang40056702015-09-11 14:31:27 -040044 if (!diagnostic) return;
Eric Engestromeb6ae972016-02-18 23:41:16 +000045 delete[] diagnostic->error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010046 delete diagnostic;
47}
48
49spv_result_t spvDiagnosticPrint(const spv_diagnostic diagnostic) {
Lei Zhang40056702015-09-11 14:31:27 -040050 if (!diagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010051
David Netoc9786432015-09-01 18:05:14 -040052 if (diagnostic->isTextSource) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010053 // NOTE: This is a text position
54 // NOTE: add 1 to the line as editors start at line 1, we are counting new
55 // line characters to start at line 0
56 std::cerr << "error: " << diagnostic->position.line + 1 << ": "
57 << diagnostic->position.column + 1 << ": " << diagnostic->error
58 << "\n";
59 return SPV_SUCCESS;
David Netoc9786432015-09-01 18:05:14 -040060 } else {
61 // NOTE: Assume this is a binary position
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010062 std::cerr << "error: " << diagnostic->position.index << ": "
63 << diagnostic->error << "\n";
64 return SPV_SUCCESS;
65 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010066}
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040067
David Neto01656362015-11-20 10:44:41 -050068namespace libspirv {
69
David Neto169266e2017-10-01 09:27:00 -040070DiagnosticStream::DiagnosticStream(DiagnosticStream&& other)
71 : stream_(),
72 position_(other.position_),
73 consumer_(other.consumer_),
74 error_(other.error_) {
75 // Prevent the other object from emitting output during destruction.
76 other.error_ = SPV_FAILED_MATCH;
77 // Some platforms are missing support for std::ostringstream functionality,
78 // including: move constructor, swap method. Either would have been a
79 // better choice than copying the string.
80 stream_ << other.stream_.str();
81}
82
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040083DiagnosticStream::~DiagnosticStream() {
Lei Zhang755f97f2016-09-02 18:06:18 -040084 if (error_ != SPV_FAILED_MATCH && consumer_ != nullptr) {
Lei Zhang620f05e2016-09-16 16:12:04 -040085 auto level = SPV_MSG_ERROR;
Lei Zhang755f97f2016-09-02 18:06:18 -040086 switch (error_) {
87 case SPV_SUCCESS:
88 case SPV_REQUESTED_TERMINATION: // Essentially success.
Lei Zhang620f05e2016-09-16 16:12:04 -040089 level = SPV_MSG_INFO;
Lei Zhang755f97f2016-09-02 18:06:18 -040090 break;
91 case SPV_WARNING:
David Netoc3caa542017-03-16 15:19:51 -040092 level = SPV_MSG_WARNING;
Lei Zhang755f97f2016-09-02 18:06:18 -040093 break;
94 case SPV_UNSUPPORTED:
95 case SPV_ERROR_INTERNAL:
96 case SPV_ERROR_INVALID_TABLE:
Lei Zhang620f05e2016-09-16 16:12:04 -040097 level = SPV_MSG_INTERNAL_ERROR;
Lei Zhang755f97f2016-09-02 18:06:18 -040098 break;
99 case SPV_ERROR_OUT_OF_MEMORY:
Lei Zhang620f05e2016-09-16 16:12:04 -0400100 level = SPV_MSG_FATAL;
Lei Zhang755f97f2016-09-02 18:06:18 -0400101 break;
102 default:
103 break;
104 }
Lei Zhang35902792016-09-16 15:43:41 -0400105 consumer_(level, "input", position_, stream_.str().c_str());
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400106 }
107}
Lei Zhang755f97f2016-09-02 18:06:18 -0400108
109void UseDiagnosticAsMessageConsumer(spv_context context,
110 spv_diagnostic* diagnostic) {
111 assert(diagnostic && *diagnostic == nullptr);
112
Lei Zhang620f05e2016-09-16 16:12:04 -0400113 auto create_diagnostic = [diagnostic](spv_message_level_t, const char*,
114 const spv_position_t& position,
115 const char* message) {
Lei Zhang755f97f2016-09-02 18:06:18 -0400116 auto p = position;
117 spvDiagnosticDestroy(*diagnostic); // Avoid memory leak.
118 *diagnostic = spvDiagnosticCreate(&p, message);
119 };
David Netoc2999272017-11-22 15:18:37 -0500120 libspirv::SetContextMessageConsumer(context, std::move(create_diagnostic));
Lei Zhang755f97f2016-09-02 18:06:18 -0400121}
122
123std::string spvResultToString(spv_result_t res) {
Umar Arshadc7413852015-12-15 21:44:21 -0500124 std::string out;
125 switch (res) {
126 case SPV_SUCCESS:
127 out = "SPV_SUCCESS";
128 break;
129 case SPV_UNSUPPORTED:
130 out = "SPV_UNSUPPORTED";
131 break;
132 case SPV_END_OF_STREAM:
133 out = "SPV_END_OF_STREAM";
134 break;
135 case SPV_WARNING:
136 out = "SPV_WARNING";
137 break;
138 case SPV_FAILED_MATCH:
139 out = "SPV_FAILED_MATCH";
140 break;
141 case SPV_REQUESTED_TERMINATION:
142 out = "SPV_REQUESTED_TERMINATION";
143 break;
144 case SPV_ERROR_INTERNAL:
145 out = "SPV_ERROR_INTERNAL";
146 break;
147 case SPV_ERROR_OUT_OF_MEMORY:
148 out = "SPV_ERROR_OUT_OF_MEMORY";
149 break;
150 case SPV_ERROR_INVALID_POINTER:
151 out = "SPV_ERROR_INVALID_POINTER";
152 break;
153 case SPV_ERROR_INVALID_BINARY:
154 out = "SPV_ERROR_INVALID_BINARY";
155 break;
156 case SPV_ERROR_INVALID_TEXT:
157 out = "SPV_ERROR_INVALID_TEXT";
158 break;
159 case SPV_ERROR_INVALID_TABLE:
160 out = "SPV_ERROR_INVALID_TABLE";
161 break;
162 case SPV_ERROR_INVALID_VALUE:
163 out = "SPV_ERROR_INVALID_VALUE";
164 break;
165 case SPV_ERROR_INVALID_DIAGNOSTIC:
166 out = "SPV_ERROR_INVALID_DIAGNOSTIC";
167 break;
168 case SPV_ERROR_INVALID_LOOKUP:
169 out = "SPV_ERROR_INVALID_LOOKUP";
170 break;
171 case SPV_ERROR_INVALID_ID:
172 out = "SPV_ERROR_INVALID_ID";
173 break;
174 case SPV_ERROR_INVALID_CFG:
175 out = "SPV_ERROR_INVALID_CFG";
176 break;
177 case SPV_ERROR_INVALID_LAYOUT:
178 out = "SPV_ERROR_INVALID_LAYOUT";
179 break;
180 default:
181 out = "Unknown Error";
182 }
183 return out;
184}
David Neto01656362015-11-20 10:44:41 -0500185
186} // namespace libspirv