Home | History | Annotate | Download | only in source
      1 // Copyright (c) 2015-2016 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 "diagnostic.h"
     28 
     29 #include <assert.h>
     30 #include <string.h>
     31 
     32 #include <iostream>
     33 
     34 #include "spirv-tools/libspirv.h"
     35 
     36 // Diagnostic API
     37 
     38 spv_diagnostic spvDiagnosticCreate(const spv_position position,
     39                                    const char* message) {
     40   spv_diagnostic diagnostic = new spv_diagnostic_t;
     41   if (!diagnostic) return nullptr;
     42   size_t length = strlen(message) + 1;
     43   diagnostic->error = new char[length];
     44   if (!diagnostic->error) {
     45     delete diagnostic;
     46     return nullptr;
     47   }
     48   diagnostic->position = *position;
     49   diagnostic->isTextSource = false;
     50   memset(diagnostic->error, 0, length);
     51   strncpy(diagnostic->error, message, length);
     52   return diagnostic;
     53 }
     54 
     55 void spvDiagnosticDestroy(spv_diagnostic diagnostic) {
     56   if (!diagnostic) return;
     57   delete[] diagnostic->error;
     58   delete diagnostic;
     59 }
     60 
     61 spv_result_t spvDiagnosticPrint(const spv_diagnostic diagnostic) {
     62   if (!diagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
     63 
     64   if (diagnostic->isTextSource) {
     65     // 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;
     72   } else {
     73     // NOTE: Assume this is a binary position
     74     std::cerr << "error: " << diagnostic->position.index << ": "
     75               << diagnostic->error << "\n";
     76     return SPV_SUCCESS;
     77   }
     78 }
     79 
     80 namespace libspirv {
     81 
     82 DiagnosticStream::~DiagnosticStream() {
     83   if (pDiagnostic_ && error_ != SPV_FAILED_MATCH) {
     84     *pDiagnostic_ = spvDiagnosticCreate(&position_, stream_.str().c_str());
     85   }
     86 }
     87 std::string
     88 spvResultToString(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 }
    150 
    151 void message(std::string file, size_t line, std::string name) {
    152   std::cout << file << ":" << line << ": " << name << std::endl;
    153 }
    154 
    155 }  // namespace libspirv
    156