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 | |
| 30 | class SourceFileToDelete { |
| 31 | public: |
| 32 | static SourceFileToDelete& Get() { |
| 33 | static SourceFileToDelete* instance = new SourceFileToDelete(); |
| 34 | return *instance; |
| 35 | } |
| 36 | |
| 37 | /// Adds file to the list that will be deleted on call to Free() |
| 38 | /// Note - this function is _not_ thread safe. If we have multiple internal |
| 39 | /// compiler errors occurring at the same time on different threads, then |
| 40 | /// we're in serious trouble. |
| 41 | void Add(Source::File* file) { files.emplace_back(file); } |
| 42 | |
| 43 | /// Free deletes all the source files added by calls to Add() and then this |
| 44 | /// SourceFileToDelete object. The SourceFileToDelete must not be used after |
| 45 | /// calling. |
| 46 | void Free() { |
| 47 | for (auto* file : files) { |
| 48 | delete file; |
| 49 | } |
| 50 | delete this; |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | std::vector<Source::File*> files; |
| 55 | }; |
| 56 | |
| 57 | } // namespace |
| 58 | |
| 59 | void FreeInternalCompilerErrors() { |
| 60 | SourceFileToDelete::Get().Free(); |
| 61 | } |
| 62 | |
| 63 | void SetInternalCompilerErrorReporter(InternalCompilerErrorReporter* reporter) { |
| 64 | ice_reporter = reporter; |
| 65 | } |
| 66 | |
| 67 | void InternalCompilerError(const char* filepath, |
| 68 | size_t line, |
| 69 | const std::string& msg, |
| 70 | diag::List& diagnostics) { |
| 71 | auto* file = new Source::File(filepath, ""); |
| 72 | |
| 73 | SourceFileToDelete::Get().Add(file); |
| 74 | |
| 75 | Source source{Source::Range{Source::Location{line}}, file}; |
| 76 | diagnostics.add_ice(msg, source); |
| 77 | |
| 78 | if (ice_reporter) { |
| 79 | ice_reporter(diagnostics); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | } // namespace tint |