blob: 9da7e38c4f2ce8ba3fe2f5f2d6c7d393edfb89eb [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 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010078}
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040079
David Neto01656362015-11-20 10:44:41 -050080namespace libspirv {
81
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040082DiagnosticStream::~DiagnosticStream() {
David Neto51013d12015-10-14 11:31:51 -040083 if (pDiagnostic_ && error_ != SPV_FAILED_MATCH) {
David Netobae88512015-10-28 13:16:56 -040084 *pDiagnostic_ = spvDiagnosticCreate(&position_, stream_.str().c_str());
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040085 }
86}
Umar Arshadc7413852015-12-15 21:44:21 -050087std::string
88spvResultToString(spv_result_t res) {
89 std::string out;
90 switch (res) {
91 case SPV_SUCCESS:
92 out = "SPV_SUCCESS";
93 break;
94 case SPV_UNSUPPORTED:
95 out = "SPV_UNSUPPORTED";
96 break;
97 case SPV_END_OF_STREAM:
98 out = "SPV_END_OF_STREAM";
99 break;
100 case SPV_WARNING:
101 out = "SPV_WARNING";
102 break;
103 case SPV_FAILED_MATCH:
104 out = "SPV_FAILED_MATCH";
105 break;
106 case SPV_REQUESTED_TERMINATION:
107 out = "SPV_REQUESTED_TERMINATION";
108 break;
109 case SPV_ERROR_INTERNAL:
110 out = "SPV_ERROR_INTERNAL";
111 break;
112 case SPV_ERROR_OUT_OF_MEMORY:
113 out = "SPV_ERROR_OUT_OF_MEMORY";
114 break;
115 case SPV_ERROR_INVALID_POINTER:
116 out = "SPV_ERROR_INVALID_POINTER";
117 break;
118 case SPV_ERROR_INVALID_BINARY:
119 out = "SPV_ERROR_INVALID_BINARY";
120 break;
121 case SPV_ERROR_INVALID_TEXT:
122 out = "SPV_ERROR_INVALID_TEXT";
123 break;
124 case SPV_ERROR_INVALID_TABLE:
125 out = "SPV_ERROR_INVALID_TABLE";
126 break;
127 case SPV_ERROR_INVALID_VALUE:
128 out = "SPV_ERROR_INVALID_VALUE";
129 break;
130 case SPV_ERROR_INVALID_DIAGNOSTIC:
131 out = "SPV_ERROR_INVALID_DIAGNOSTIC";
132 break;
133 case SPV_ERROR_INVALID_LOOKUP:
134 out = "SPV_ERROR_INVALID_LOOKUP";
135 break;
136 case SPV_ERROR_INVALID_ID:
137 out = "SPV_ERROR_INVALID_ID";
138 break;
139 case SPV_ERROR_INVALID_CFG:
140 out = "SPV_ERROR_INVALID_CFG";
141 break;
142 case SPV_ERROR_INVALID_LAYOUT:
143 out = "SPV_ERROR_INVALID_LAYOUT";
144 break;
145 default:
146 out = "Unknown Error";
147 }
148 return out;
149}
David Neto01656362015-11-20 10:44:41 -0500150
151} // namespace libspirv