blob: 3e7e7de6f6bce9d1124b34007fbfd993da5a2d72 [file] [log] [blame]
Ben Clayton6b4924f2021-02-17 20:13:34 +00001// 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#ifndef SRC_DEBUG_H_
16#define SRC_DEBUG_H_
17
18#include <string>
19
20#include "src/diagnostic/diagnostic.h"
21#include "src/diagnostic/formatter.h"
22#include "src/diagnostic/printer.h"
23
24namespace tint {
25
26/// Function type used for registering an internal compiler error reporter
27using InternalCompilerErrorReporter = void(const diag::List&);
28
29/// Frees any memory allocated for reporting internal compiler errors.
30/// Must only be called once on application termination.
31/// If an internal compiler error is raised and this function is not called,
32/// then memory will leak.
33void FreeInternalCompilerErrors();
34
35/// Sets the global error reporter to be called in case of internal compiler
36/// errors.
37/// @param reporter the error reporter
38void SetInternalCompilerErrorReporter(InternalCompilerErrorReporter* reporter);
39
40/// InternalCompilerError adds the internal compiler error message to the
41/// diagnostics list, and then calls the InternalCompilerErrorReporter if one is
42/// set.
43/// @param file the file containing the ICE
44/// @param line the line containing the ICE
45/// @param msg the ICE message
46/// @param diagnostics the list of diagnostics to append the ICE message to
47void InternalCompilerError(const char* file,
48 size_t line,
49 const std::string& msg,
50 diag::List& diagnostics);
51
52} // namespace tint
53
54/// TINT_ICE() is a macro for appending an internal compiler error message
55/// to the diagnostics list `diagnostics`, and calling the
56/// InternalCompilerErrorReporter with the full diagnostic list if a reporter is
57/// set.
58/// The ICE message contains the callsite's file and line.
59#define TINT_ICE(diagnostics, msg) \
60 tint::InternalCompilerError(__FILE__, __LINE__, msg, diagnostics)
61
62/// TINT_UNREACHABLE() is a macro for appending a "TINT_UNREACHABLE"
63/// internal compiler error message to the diagnostics list `diagnostics`, and
64/// calling the InternalCompilerErrorReporter with the full diagnostic list if a
65/// reporter is set.
66/// The ICE message contains the callsite's file and line.
67#define TINT_UNREACHABLE(diagnostics) TINT_ICE(diagnostics, "TINT_UNREACHABLE")
68
69#endif // SRC_DEBUG_H_