blob: b1a9cacf8e52c2d160faea11f525981b9ee78cae [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 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
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040070DiagnosticStream::~DiagnosticStream() {
David Neto51013d12015-10-14 11:31:51 -040071 if (pDiagnostic_ && error_ != SPV_FAILED_MATCH) {
David Netobae88512015-10-28 13:16:56 -040072 *pDiagnostic_ = spvDiagnosticCreate(&position_, stream_.str().c_str());
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040073 }
74}
Umar Arshadc7413852015-12-15 21:44:21 -050075std::string
76spvResultToString(spv_result_t res) {
77 std::string out;
78 switch (res) {
79 case SPV_SUCCESS:
80 out = "SPV_SUCCESS";
81 break;
82 case SPV_UNSUPPORTED:
83 out = "SPV_UNSUPPORTED";
84 break;
85 case SPV_END_OF_STREAM:
86 out = "SPV_END_OF_STREAM";
87 break;
88 case SPV_WARNING:
89 out = "SPV_WARNING";
90 break;
91 case SPV_FAILED_MATCH:
92 out = "SPV_FAILED_MATCH";
93 break;
94 case SPV_REQUESTED_TERMINATION:
95 out = "SPV_REQUESTED_TERMINATION";
96 break;
97 case SPV_ERROR_INTERNAL:
98 out = "SPV_ERROR_INTERNAL";
99 break;
100 case SPV_ERROR_OUT_OF_MEMORY:
101 out = "SPV_ERROR_OUT_OF_MEMORY";
102 break;
103 case SPV_ERROR_INVALID_POINTER:
104 out = "SPV_ERROR_INVALID_POINTER";
105 break;
106 case SPV_ERROR_INVALID_BINARY:
107 out = "SPV_ERROR_INVALID_BINARY";
108 break;
109 case SPV_ERROR_INVALID_TEXT:
110 out = "SPV_ERROR_INVALID_TEXT";
111 break;
112 case SPV_ERROR_INVALID_TABLE:
113 out = "SPV_ERROR_INVALID_TABLE";
114 break;
115 case SPV_ERROR_INVALID_VALUE:
116 out = "SPV_ERROR_INVALID_VALUE";
117 break;
118 case SPV_ERROR_INVALID_DIAGNOSTIC:
119 out = "SPV_ERROR_INVALID_DIAGNOSTIC";
120 break;
121 case SPV_ERROR_INVALID_LOOKUP:
122 out = "SPV_ERROR_INVALID_LOOKUP";
123 break;
124 case SPV_ERROR_INVALID_ID:
125 out = "SPV_ERROR_INVALID_ID";
126 break;
127 case SPV_ERROR_INVALID_CFG:
128 out = "SPV_ERROR_INVALID_CFG";
129 break;
130 case SPV_ERROR_INVALID_LAYOUT:
131 out = "SPV_ERROR_INVALID_LAYOUT";
132 break;
133 default:
134 out = "Unknown Error";
135 }
136 return out;
137}
David Neto01656362015-11-20 10:44:41 -0500138
139} // namespace libspirv