blob: 74ce76baea5f0fc9b2a9895823b46eb6f42ed1e4 [file] [log] [blame]
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01001// Copyright (c) 2015 The Khronos Group Inc.
2//
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
27#include <libspirv/libspirv.h>
28#include "diagnostic.h"
29
30#include <assert.h>
31#include <string.h>
32
33#include <iostream>
34
35// Diagnostic API
36
37spv_diagnostic spvDiagnosticCreate(const spv_position position,
38 const char *message) {
39 spv_diagnostic diagnostic = new spv_diagnostic_t;
Lei Zhang40056702015-09-11 14:31:27 -040040 if (!diagnostic) return nullptr;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010041 size_t length = strlen(message) + 1;
42 diagnostic->error = new char[length];
Lei Zhang40056702015-09-11 14:31:27 -040043 if (!diagnostic->error) {
44 delete diagnostic;
45 return nullptr;
46 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010047 diagnostic->position = *position;
David Netoc9786432015-09-01 18:05:14 -040048 diagnostic->isTextSource = false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010049 memset(diagnostic->error, 0, length);
50 strncpy(diagnostic->error, message, length);
51 return diagnostic;
52}
53
54void spvDiagnosticDestroy(spv_diagnostic diagnostic) {
Lei Zhang40056702015-09-11 14:31:27 -040055 if (!diagnostic) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010056 if (diagnostic->error) {
57 delete[] diagnostic->error;
58 }
59 delete diagnostic;
60}
61
62spv_result_t spvDiagnosticPrint(const spv_diagnostic diagnostic) {
Lei Zhang40056702015-09-11 14:31:27 -040063 if (!diagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010064
David Netoc9786432015-09-01 18:05:14 -040065 if (diagnostic->isTextSource) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010066 // NOTE: This is a text position
67 // NOTE: add 1 to the line as editors start at line 1, we are counting new
68 // line characters to start at line 0
69 std::cerr << "error: " << diagnostic->position.line + 1 << ": "
70 << diagnostic->position.column + 1 << ": " << diagnostic->error
71 << "\n";
72 return SPV_SUCCESS;
David Netoc9786432015-09-01 18:05:14 -040073 } else {
74 // NOTE: Assume this is a binary position
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010075 std::cerr << "error: " << diagnostic->position.index << ": "
76 << diagnostic->error << "\n";
77 return SPV_SUCCESS;
78 }
79
80 return SPV_ERROR_INVALID_VALUE;
81}
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040082
83
84DiagnosticStream::~DiagnosticStream() {
85 if (pDiagnostic_) {
86 *pDiagnostic_ = spvDiagnosticCreate(position_, stream_.str().c_str());
87 }
88}
89