P4C
The P4 Compiler
Loading...
Searching...
No Matches
lib/error.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8/* -*-C++-*- */
9
10#ifndef LIB_ERROR_H_
11#define LIB_ERROR_H_
12
13#include <type_traits>
14
15#include "lib/compile_context.h"
16#include "lib/cstring.h"
17#include "lib/error_reporter.h"
18
19// This should eventually be turned to 0 when all the code is converted
20#define LEGACY 1
21
22namespace P4 {
23
25inline unsigned errorCount() { return BaseCompileContext::get().errorReporter().getErrorCount(); }
26
28inline unsigned warningCount() {
29 return BaseCompileContext::get().errorReporter().getWarningCount();
30}
31
33inline unsigned infoCount() { return BaseCompileContext::get().errorReporter().getInfoCount(); }
34
40
41// Errors, warnings, and infos support boost-style placeholders (%1%, %2%, ...)
42// and printf/absl::StrFormat-style placeholders (%s, %d, ...).
43
45// LEGACY: once we transition to error types, this should be deprecated
46#if LEGACY
47template <typename... Args>
48inline void error(const char *format, Args &&...args) {
49 auto &context = BaseCompileContext::get();
50 auto action = context.getDefaultErrorDiagnosticAction();
51 context.errorReporter().diagnose(action, nullptr, format, "", std::forward<Args>(args)...);
52}
53#endif
54
57template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T>>, class... Args>
58void error(const int kind, const char *format, const T *node, Args &&...args) {
59 auto &context = BaseCompileContext::get();
60 auto action = context.getDefaultErrorDiagnosticAction();
61 context.errorReporter().diagnose(action, kind, format, "", node, std::forward<Args>(args)...);
62}
63
65template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T>>, class... Args>
66void errorWithSuffix(const int kind, const char *format, const char *suffix, const T *node,
67 Args &&...args) {
68 auto &context = BaseCompileContext::get();
69 auto action = context.getDefaultErrorDiagnosticAction();
70 context.errorReporter().diagnose(action, kind, format, suffix, node,
71 std::forward<Args>(args)...);
72}
73
75template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T> && !std::is_pointer_v<T>>,
76 class... Args>
77void error(const int kind, const char *format, const T &node, Args &&...args) {
78 error(kind, format, &node, std::forward<Args>(args)...);
79}
80
81#if LEGACY
85// LEGACY: once we transition to error types, this should be deprecated
86template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T>>, class... Args>
87void error(const char *format, const T *node, Args &&...args) {
88 error(ErrorType::LEGACY_ERROR, format, node, std::forward<Args>(args)...);
89}
90
92// LEGACY: once we transition to error types, this should be deprecated
93template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T> && !std::is_pointer_v<T>>,
94 class... Args>
95void error(const char *format, const T &node, Args &&...args) {
96 error(ErrorType::LEGACY_ERROR, format, node, std::forward<Args>(args)...);
97}
98#endif
99
102template <typename... Args>
103void error(const int kind, const char *format, Args &&...args) {
104 auto &context = BaseCompileContext::get();
105 auto action = context.getDefaultErrorDiagnosticAction();
106 context.errorReporter().diagnose(action, kind, format, "", std::forward<Args>(args)...);
107}
108
110#define ERROR_CHECK(e, ...) \
111 do { \
112 if (!(e)) ::P4::error(__VA_ARGS__); \
113 } while (0)
114
115#if LEGACY
117template <typename... Args>
118inline void warning(const char *format, Args &&...args) {
119 auto &context = BaseCompileContext::get();
120 auto action = context.getDefaultWarningDiagnosticAction();
121 context.errorReporter().diagnose(action, nullptr, format, "", std::forward<Args>(args)...);
122}
123#endif
124
126template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T>>, class... Args>
127void warning(const int kind, const char *format, const T *node, Args &&...args) {
128 auto &context = BaseCompileContext::get();
129 auto action = context.getDefaultWarningDiagnosticAction();
130 context.errorReporter().diagnose(action, kind, format, "", node, std::forward<Args>(args)...);
131}
132
134template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T> && !std::is_pointer_v<T>>,
135 class... Args>
136void warning(const int kind, const char *format, const T &node, Args &&...args) {
137 ::P4::warning(kind, format, &node, std::forward<Args>(args)...);
138}
139
142template <typename... Args>
143void warning(const int kind, const char *format, Args &&...args) {
144 auto &context = BaseCompileContext::get();
145 auto action = context.getDefaultWarningDiagnosticAction();
146 context.errorReporter().diagnose(action, kind, format, "", std::forward<Args>(args)...);
147}
148
150#define WARN_CHECK(e, ...) \
151 do { \
152 if (!(e)) ::P4::warning(__VA_ARGS__); \
153 } while (0)
154
156template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T>>, class... Args>
157void info(const int kind, const char *format, const T *node, Args &&...args) {
158 auto &context = BaseCompileContext::get();
159 auto action = context.getDefaultInfoDiagnosticAction();
160 context.errorReporter().diagnose(action, kind, format, "", node, std::forward<Args>(args)...);
161}
162
164template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T> && !std::is_pointer_v<T>>,
165 class... Args>
166void info(const int kind, const char *format, const T &node, Args &&...args) {
167 ::P4::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
179} // namespace P4
180
181#endif /* LIB_ERROR_H_ */
virtual ErrorReporter & errorReporter()
Definition compile_context.cpp:59
static BaseCompileContext & get()
Definition compile_context.cpp:55
unsigned getDiagnosticCount() const
Definition error_reporter.h:174
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
void warning(const char *format, Args &&...args)
Report a warning with the given message.
Definition lib/error.h:118
unsigned infoCount()
Definition lib/error.h:33
unsigned diagnosticCount()
Definition lib/error.h:37
unsigned warningCount()
Definition lib/error.h:28
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:66
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:48
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:157
unsigned errorCount()
Definition lib/error.h:25