P4C
The P4 Compiler
Loading...
Searching...
No Matches
lib/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 <type_traits>
23
24#include "lib/compile_context.h"
25#include "lib/cstring.h"
26#include "lib/error_reporter.h"
27
28// This should eventually be turned to 0 when all the code is converted
29#define LEGACY 1
30
31namespace P4 {
32
35inline unsigned errorCount() { return BaseCompileContext::get().errorReporter().getErrorCount(); }
36
42
43// Errors (and warnings) are specified using boost::format format strings, i.e.,
44// %1%, %2%, etc (starting at 1, not at 0).
45// Some compatibility for printf-style arguments is also supported.
46
48// LEGACY: once we transition to error types, this should be deprecated
49#if LEGACY
50template <typename... Args>
51inline void error(const char *format, Args &&...args) {
52 auto &context = BaseCompileContext::get();
53 auto action = context.getDefaultErrorDiagnosticAction();
54 context.errorReporter().diagnose(action, nullptr, format, "", std::forward<Args>(args)...);
55}
56#endif
57
60template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T>>, class... Args>
61void error(const int kind, const char *format, const T *node, Args &&...args) {
62 auto &context = BaseCompileContext::get();
63 auto action = context.getDefaultErrorDiagnosticAction();
64 context.errorReporter().diagnose(action, kind, format, "", node, std::forward<Args>(args)...);
65}
66
68template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T>>, 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,
74 std::forward<Args>(args)...);
75}
76
78template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T> && !std::is_pointer_v<T>>,
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, typename = std::enable_if_t<Util::has_SourceInfo_v<T>>, class... Args>
90void error(const char *format, const T *node, Args &&...args) {
91 error(ErrorType::LEGACY_ERROR, format, node, std::forward<Args>(args)...);
92}
93
95// LEGACY: once we transition to error types, this should be deprecated
96template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T> && !std::is_pointer_v<T>>,
97 class... Args>
98void error(const char *format, const T &node, Args &&...args) {
99 error(ErrorType::LEGACY_ERROR, format, node, std::forward<Args>(args)...);
100}
101#endif
102
105template <typename... Args>
106void error(const int kind, const char *format, Args &&...args) {
107 auto &context = BaseCompileContext::get();
108 auto action = context.getDefaultErrorDiagnosticAction();
109 context.errorReporter().diagnose(action, kind, format, "", std::forward<Args>(args)...);
110}
111
112#if LEGACY
114template <typename... Args>
115inline void warning(const char *format, Args &&...args) {
116 auto &context = BaseCompileContext::get();
117 auto action = context.getDefaultWarningDiagnosticAction();
118 context.errorReporter().diagnose(action, nullptr, format, "", std::forward<Args>(args)...);
119}
120#endif
121
123template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T>>, class... Args>
124void warning(const int kind, const char *format, const T *node, Args &&...args) {
125 auto &context = BaseCompileContext::get();
126 auto action = context.getDefaultWarningDiagnosticAction();
127 context.errorReporter().diagnose(action, kind, format, "", node, std::forward<Args>(args)...);
128}
129
131template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T> && !std::is_pointer_v<T>>,
132 class... Args>
133void warning(const int kind, const char *format, const T &node, Args &&...args) {
134 ::P4::warning(kind, format, &node, std::forward<Args>(args)...);
135}
136
139template <typename... Args>
140void warning(const int kind, const char *format, Args &&...args) {
141 auto &context = BaseCompileContext::get();
142 auto action = context.getDefaultWarningDiagnosticAction();
143 context.errorReporter().diagnose(action, kind, format, "", std::forward<Args>(args)...);
144}
145
147template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T>>, class... Args>
148void info(const int kind, const char *format, const T *node, Args &&...args) {
149 auto &context = BaseCompileContext::get();
150 auto action = context.getDefaultInfoDiagnosticAction();
151 context.errorReporter().diagnose(action, kind, format, "", node, std::forward<Args>(args)...);
152}
153
155template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T> && !std::is_pointer_v<T>>,
156 class... Args>
157void info(const int kind, const char *format, const T &node, Args &&...args) {
158 ::P4::info(kind, format, &node, std::forward<Args>(args)...);
159}
160
163template <typename... Args>
164void info(const int kind, const char *format, Args &&...args) {
165 auto &context = BaseCompileContext::get();
166 auto action = context.getDefaultInfoDiagnosticAction();
167 context.errorReporter().diagnose(action, kind, format, "", std::forward<Args>(args)...);
168}
169
170} // namespace P4
171
172#endif /* LIB_ERROR_H_ */
virtual ErrorReporter & errorReporter()
Definition compile_context.cpp:65
static BaseCompileContext & get()
Definition compile_context.cpp:61
unsigned getDiagnosticCount() const
Definition error_reporter.h:185
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
void warning(const char *format, Args &&...args)
Report a warning with the given message.
Definition lib/error.h:115
unsigned diagnosticCount()
Definition lib/error.h:39
void errorWithSuffix(const int kind, const char *format, const char *suffix, const T *node, Args &&...args)
This is similar to the above method, but also has a suffix.
Definition lib/error.h:69
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:51
void info(const int kind, const char *format, const T *node, Args &&...args)
Report info messages of type kind. Requires that the node argument have source info.
Definition lib/error.h:148
unsigned errorCount()
Definition lib/error.h:35