P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
error.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17/* -*-C++-*- */
18
19#ifndef LIB_ERROR_H_
20#define LIB_ERROR_H_
21
22#include "lib/compile_context.h"
23#include "lib/cstring.h"
24#include "lib/error_reporter.h"
25
26// This should eventually be turned to 0 when all the code is converted
27#define LEGACY 1
28
31inline unsigned errorCount() { return BaseCompileContext::get().errorReporter().getErrorCount(); }
32
35inline unsigned diagnosticCount() {
37}
38
39// Errors (and warnings) are specified using boost::format format strings, i.e.,
40// %1%, %2%, etc (starting at 1, not at 0).
41// Some compatibility for printf-style arguments is also supported.
42
44// LEGACY: once we transition to error types, this should be deprecated
45#if LEGACY
46template <typename... T>
47inline void error(const char *format, T... args) {
48 auto &context = BaseCompileContext::get();
49 auto action = context.getDefaultErrorDiagnosticAction();
50 context.errorReporter().diagnose(action, nullptr, format, "", args...);
51}
52#endif
53
56template <class T,
57 typename = typename std::enable_if<std::is_base_of<Util::IHasSourceInfo, T>::value>::type,
58 class... Args>
59void error(const int kind, const char *format, const T *node, Args... args) {
60 auto &context = BaseCompileContext::get();
61 auto action = context.getDefaultErrorDiagnosticAction();
62 context.errorReporter().diagnose(action, kind, format, "", node, args...);
63}
64
66template <class T,
67 typename = typename std::enable_if<std::is_base_of<Util::IHasSourceInfo, T>::value>::type,
68 class... Args>
69void errorWithSuffix(const int kind, const char *format, const char *suffix, const T *node,
70 Args... args) {
71 auto &context = BaseCompileContext::get();
72 auto action = context.getDefaultErrorDiagnosticAction();
73 context.errorReporter().diagnose(action, kind, format, suffix, node, args...);
74}
75
77template <class T,
78 typename = typename std::enable_if<std::is_base_of<Util::IHasSourceInfo, T>::value>::type,
79 class... Args>
80void error(const int kind, const char *format, const T &node, Args... args) {
81 error(kind, format, &node, std::forward<Args>(args)...);
82}
83
84#if LEGACY
88// LEGACY: once we transition to error types, this should be deprecated
89template <class T,
90 typename = typename std::enable_if<std::is_base_of<Util::IHasSourceInfo, T>::value>::type,
91 class... Args>
92void error(const char *format, const T *node, Args... args) {
93 error(ErrorType::LEGACY_ERROR, format, node, std::forward<Args>(args)...);
94}
95
97// LEGACY: once we transition to error types, this should be deprecated
98template <class T,
99 typename = typename std::enable_if<std::is_base_of<Util::IHasSourceInfo, T>::value>::type,
100 class... Args>
101void error(const char *format, const T &node, Args... args) {
102 error(ErrorType::LEGACY_ERROR, format, node, std::forward<Args>(args)...);
103}
104#endif
105
108template <typename... Args>
109void error(const int kind, const char *format, Args... args) {
110 auto &context = BaseCompileContext::get();
111 auto action = context.getDefaultErrorDiagnosticAction();
112 context.errorReporter().diagnose(action, kind, format, "", std::forward<Args>(args)...);
113}
114
115#if LEGACY
117template <typename... T>
118inline void warning(const char *format, T... args) {
119 auto &context = BaseCompileContext::get();
120 auto action = context.getDefaultWarningDiagnosticAction();
121 context.errorReporter().diagnose(action, nullptr, format, "", args...);
122}
123#endif
124
126template <class T,
127 typename = typename std::enable_if<std::is_base_of<Util::IHasSourceInfo, T>::value>::type,
128 class... Args>
129void warning(const int kind, const char *format, const T *node, Args... args) {
130 auto &context = BaseCompileContext::get();
131 auto action = context.getDefaultWarningDiagnosticAction();
132 context.errorReporter().diagnose(action, kind, format, "", node, args...);
133}
134
136template <class T,
137 typename = typename std::enable_if<std::is_base_of<Util::IHasSourceInfo, T>::value>::type,
138 class... Args>
139void warning(const int kind, const char *format, const T &node, Args... args) {
140 ::warning(kind, format, &node, std::forward<Args>(args)...);
141}
142
145template <typename... Args>
146void warning(const int kind, const char *format, Args... args) {
147 auto &context = BaseCompileContext::get();
148 auto action = context.getDefaultWarningDiagnosticAction();
149 context.errorReporter().diagnose(action, kind, format, "", std::forward<Args>(args)...);
150}
151
153template <class T,
154 typename = typename std::enable_if<std::is_base_of<Util::IHasSourceInfo, T>::value>::type,
155 class... Args>
156void info(const int kind, const char *format, const T *node, Args... args) {
157 auto &context = BaseCompileContext::get();
158 auto action = context.getDefaultInfoDiagnosticAction();
159 context.errorReporter().diagnose(action, kind, format, "", node, args...);
160}
161
163template <class T,
164 typename = typename std::enable_if<std::is_base_of<Util::IHasSourceInfo, T>::value>::type,
165 class... Args>
166void info(const int kind, const char *format, const T &node, Args... args) {
167 ::info(kind, format, &node, std::forward<Args>(args)...);
168}
169
172template <typename... Args>
173void info(const int kind, const char *format, Args... args) {
174 auto &context = BaseCompileContext::get();
175 auto action = context.getDefaultInfoDiagnosticAction();
176 context.errorReporter().diagnose(action, kind, format, "", std::forward<Args>(args)...);
177}
178
192template <typename... T>
193inline void diagnose(DiagnosticAction defaultAction, const char *diagnosticName, const char *format,
194 const char *suffix, T... args) {
195 auto &context = BaseCompileContext::get();
196 auto action = context.getDiagnosticAction(diagnosticName, defaultAction);
197 context.errorReporter().diagnose(action, diagnosticName, format, suffix, args...);
198}
199
200#endif /* LIB_ERROR_H_ */
static BaseCompileContext & get()
Definition compile_context.cpp:59
virtual ErrorReporter & errorReporter()
Definition compile_context.cpp:63
unsigned getDiagnosticCount() const
Definition error_reporter.h:196