Ben Clayton | 6b4924f | 2021-02-17 20:13:34 +0000 | [diff] [blame] | 1 | // Copyright 2021 The Tint Authors. |
| 2 | // |
| 3 | // 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 |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // 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. |
| 14 | |
| 15 | #include "src/debug.h" |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | |
| 19 | #include <sstream> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "src/diagnostic/diagnostic.h" |
| 24 | |
| 25 | namespace tint { |
| 26 | namespace { |
| 27 | |
| 28 | InternalCompilerErrorReporter* ice_reporter = nullptr; |
| 29 | |
Ben Clayton | 8903685 | 2021-02-18 20:23:09 +0000 | [diff] [blame] | 30 | /// Note - this class is _not_ thread safe. If we have multiple internal |
| 31 | /// compiler errors occurring at the same time on different threads, then |
| 32 | /// we're in serious trouble. |
Ben Clayton | 6b4924f | 2021-02-17 20:13:34 +0000 | [diff] [blame] | 33 | class SourceFileToDelete { |
Ben Clayton | 8903685 | 2021-02-18 20:23:09 +0000 | [diff] [blame] | 34 | static SourceFileToDelete* instance; |
| 35 | |
Ben Clayton | 6b4924f | 2021-02-17 20:13:34 +0000 | [diff] [blame] | 36 | public: |
Ben Clayton | 8903685 | 2021-02-18 20:23:09 +0000 | [diff] [blame] | 37 | /// Adds file to the list that will be deleted on call to Free() |
| 38 | static void Add(Source::File* file) { |
| 39 | if (!instance) { |
| 40 | instance = new SourceFileToDelete(); |
| 41 | } |
| 42 | instance->files.emplace_back(file); |
Ben Clayton | 6b4924f | 2021-02-17 20:13:34 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Ben Clayton | 6b4924f | 2021-02-17 20:13:34 +0000 | [diff] [blame] | 45 | /// Free deletes all the source files added by calls to Add() and then this |
Ben Clayton | 8903685 | 2021-02-18 20:23:09 +0000 | [diff] [blame] | 46 | /// SourceFileToDelete object. |
| 47 | static void Free() { |
| 48 | if (instance) { |
| 49 | for (auto* file : instance->files) { |
| 50 | delete file; |
| 51 | } |
| 52 | delete instance; |
| 53 | instance = nullptr; |
Ben Clayton | 6b4924f | 2021-02-17 20:13:34 +0000 | [diff] [blame] | 54 | } |
Ben Clayton | 6b4924f | 2021-02-17 20:13:34 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | private: |
| 58 | std::vector<Source::File*> files; |
| 59 | }; |
| 60 | |
Ben Clayton | 8903685 | 2021-02-18 20:23:09 +0000 | [diff] [blame] | 61 | SourceFileToDelete* SourceFileToDelete::instance = nullptr; |
| 62 | |
Ben Clayton | 6b4924f | 2021-02-17 20:13:34 +0000 | [diff] [blame] | 63 | } // namespace |
| 64 | |
| 65 | void FreeInternalCompilerErrors() { |
Ben Clayton | 8903685 | 2021-02-18 20:23:09 +0000 | [diff] [blame] | 66 | SourceFileToDelete::Free(); |
Ben Clayton | 6b4924f | 2021-02-17 20:13:34 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void SetInternalCompilerErrorReporter(InternalCompilerErrorReporter* reporter) { |
| 70 | ice_reporter = reporter; |
| 71 | } |
| 72 | |
Ben Clayton | 3a9a55e | 2021-02-18 16:33:38 +0000 | [diff] [blame] | 73 | InternalCompilerError::InternalCompilerError(const char* file, |
| 74 | size_t line, |
| 75 | diag::List& diagnostics) |
| 76 | : file_(file), line_(line), diagnostics_(diagnostics) {} |
| 77 | |
| 78 | InternalCompilerError::~InternalCompilerError() { |
| 79 | auto* file = new Source::File(file_, ""); |
Ben Clayton | 6b4924f | 2021-02-17 20:13:34 +0000 | [diff] [blame] | 80 | |
Ben Clayton | 8903685 | 2021-02-18 20:23:09 +0000 | [diff] [blame] | 81 | SourceFileToDelete::Add(file); |
Ben Clayton | 6b4924f | 2021-02-17 20:13:34 +0000 | [diff] [blame] | 82 | |
Ben Clayton | 3a9a55e | 2021-02-18 16:33:38 +0000 | [diff] [blame] | 83 | Source source{Source::Range{Source::Location{line_}}, file}; |
| 84 | diagnostics_.add_ice(msg_.str(), source); |
Ben Clayton | 6b4924f | 2021-02-17 20:13:34 +0000 | [diff] [blame] | 85 | |
| 86 | if (ice_reporter) { |
Ben Clayton | 3a9a55e | 2021-02-18 16:33:38 +0000 | [diff] [blame] | 87 | ice_reporter(diagnostics_); |
Ben Clayton | 6b4924f | 2021-02-17 20:13:34 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | } // namespace tint |