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 are specified using boost::format format strings, i.e.,
42// %1%, %2%, etc (starting at 1, not at 0).
43// Some compatibility for printf-style arguments is also supported.
44
46// LEGACY: once we transition to error types, this should be deprecated
47#if LEGACY
48template <typename... Args>
49inline void error(const char *format, Args &&...args) {
50 auto &context = BaseCompileContext::get();
51 auto action = context.getDefaultErrorDiagnosticAction();
52 context.errorReporter().diagnose(action, nullptr, format, "", std::forward<Args>(args)...);
53}
54#endif
55
58template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T>>, 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, std::forward<Args>(args)...);
63}
64
66template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T>>, class... Args>
67void errorWithSuffix(const int kind, const char *format, const char *suffix, const T *node,
68 Args &&...args) {
69 auto &context = BaseCompileContext::get();
70 auto action = context.getDefaultErrorDiagnosticAction();
71 context.errorReporter().diagnose(action, kind, format, suffix, node,
72 std::forward<Args>(args)...);
73}
74
76template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T> && !std::is_pointer_v<T>>,
77 class... Args>
78void error(const int kind, const char *format, const T &node, Args &&...args) {
79 error(kind, format, &node, std::forward<Args>(args)...);
80}
81
82#if LEGACY
86// LEGACY: once we transition to error types, this should be deprecated
87template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T>>, class... Args>
88void error(const char *format, const T *node, Args &&...args) {
89 error(ErrorType::LEGACY_ERROR, format, node, std::forward<Args>(args)...);
90}
91
93// LEGACY: once we transition to error types, this should be deprecated
94template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T> && !std::is_pointer_v<T>>,
95 class... Args>
96void error(const char *format, const T &node, Args &&...args) {
97 error(ErrorType::LEGACY_ERROR, format, node, std::forward<Args>(args)...);
98}
99#endif
100
103template <typename... Args>
104void error(const int kind, const char *format, Args &&...args) {
105 auto &context = BaseCompileContext::get();
106 auto action = context.getDefaultErrorDiagnosticAction();
107 context.errorReporter().diagnose(action, kind, format, "", std::forward<Args>(args)...);
108}
109
111#define ERROR_CHECK(e, ...) \
112 do { \
113 if (!(e)) ::P4::error(__VA_ARGS__); \
114 } while (0)
115
116#if LEGACY
118template <typename... Args>
119inline void warning(const char *format, Args &&...args) {
120 auto &context = BaseCompileContext::get();
121 auto action = context.getDefaultWarningDiagnosticAction();
122 context.errorReporter().diagnose(action, nullptr, format, "", std::forward<Args>(args)...);
123}
124#endif
125
127template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T>>, class... Args>
128void warning(const int kind, const char *format, const T *node, Args &&...args) {
129 auto &context = BaseCompileContext::get();
130 auto action = context.getDefaultWarningDiagnosticAction();
131 context.errorReporter().diagnose(action, kind, format, "", node, std::forward<Args>(args)...);
132}
133
135template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T> && !std::is_pointer_v<T>>,
136 class... Args>
137void warning(const int kind, const char *format, const T &node, Args &&...args) {
138 ::P4::warning(kind, format, &node, std::forward<Args>(args)...);
139}
140
143template <typename... Args>
144void warning(const int kind, const char *format, Args &&...args) {
145 auto &context = BaseCompileContext::get();
146 auto action = context.getDefaultWarningDiagnosticAction();
147 context.errorReporter().diagnose(action, kind, format, "", std::forward<Args>(args)...);
148}
149
151#define WARN_CHECK(e, ...) \
152 do { \
153 if (!(e)) ::P4::warning(__VA_ARGS__); \
154 } while (0)
155
157template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T>>, class... Args>
158void info(const int kind, const char *format, const T *node, Args &&...args) {
159 auto &context = BaseCompileContext::get();
160 auto action = context.getDefaultInfoDiagnosticAction();
161 context.errorReporter().diagnose(action, kind, format, "", node, std::forward<Args>(args)...);
162}
163
165template <class T, typename = std::enable_if_t<Util::has_SourceInfo_v<T> && !std::is_pointer_v<T>>,
166 class... Args>
167void info(const int kind, const char *format, const T &node, Args &&...args) {
168 ::P4::info(kind, format, &node, std::forward<Args>(args)...);
169}
170
173template <typename... Args>
174void info(const int kind, const char *format, Args &&...args) {
175 auto &context = BaseCompileContext::get();
176 auto action = context.getDefaultInfoDiagnosticAction();
177 context.errorReporter().diagnose(action, kind, format, "", std::forward<Args>(args)...);
178}
179
180} // namespace P4
181
182#endif /* LIB_ERROR_H_ */
virtual ErrorReporter & errorReporter()
Definition compile_context.cpp:54
static BaseCompileContext & get()
Definition compile_context.cpp:50
unsigned getDiagnosticCount() const
Definition error_reporter.h:178
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:119
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:67
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:49
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:158
unsigned errorCount()
Definition lib/error.h:25