8#ifndef LIB_ERROR_REPORTER_H_
9#define LIB_ERROR_REPORTER_H_
15#include <unordered_map>
18#include "absl/strings/str_cat.h"
19#include "absl/strings/str_format.h"
20#include "error_catalog.h"
21#include "error_helper.h"
22#include "exceptions.h"
23#include "lib/format.h"
24#include "lib/source_file.h"
42 unsigned int infoCount;
43 unsigned int warningCount;
44 unsigned int errorCount;
45 unsigned int maxErrorCount;
68 if (!source.isValid())
return false;
87 virtual ~ErrorReporter() =
default;
90 template <
typename... Args>
91 std::string bug_message(
const char *format, Args &&...args) {
92 return createBugMessage(format, args...);
96 template <
typename... Args>
97 std::string format_message(
const char *format, Args &&...args) {
98 return ::P4::error_helper(format, std::forward<Args>(args)...).toString();
101 template <class T, typename = decltype(std::declval<T>()->getSourceInfo()),
typename... Args>
102 void diagnose(
DiagnosticAction action,
const int errorCode,
const char *format,
103 const char *suffix, T node, Args &&...args) {
104 if (!node ||
error_reported(errorCode, node->getSourceInfo()))
return;
108 node, std::forward<Args>(args)...);
110 diagnose(action,
nullptr, format, suffix, node, std::forward<Args>(args)...);
113 template <
typename... Args>
114 void diagnose(
DiagnosticAction action,
const int errorCode,
const char *format,
115 const char *suffix, Args &&...args) {
118 std::forward<Args>(args)...);
120 diagnose(action,
nullptr, format, suffix, std::forward<Args>(args)...);
125 template <
typename... Args>
127 const char *suffix, Args &&...args) {
128 if (action == DiagnosticAction::Ignore)
return;
130 ErrorMessage::MessageType msgType = ErrorMessage::MessageType::None;
134 if (errorCount > 0)
return;
137 msgType = ErrorMessage::MessageType::Info;
141 if (errorCount > 0)
return;
144 msgType = ErrorMessage::MessageType::Warning;
147 msgType = ErrorMessage::MessageType::Error;
150 ErrorMessage msg(msgType, diagnosticName ? diagnosticName :
"", suffix);
151 msg = ::P4::error_helper(format, std::move(msg), std::forward<Args>(args)...);
154 if (errorCount >= maxErrorCount)
155 FATAL_ERROR(
"Number of errors exceeded set maximum of %1%", maxErrorCount);
158 unsigned getErrorCount()
const {
return errorCount; }
160 unsigned getMaxErrorCount()
const {
return maxErrorCount; }
163 auto r = maxErrorCount;
164 maxErrorCount = newMaxCount;
168 unsigned getWarningCount()
const {
return warningCount; }
170 unsigned getInfoCount()
const {
return infoCount; }
176 void setOutputStream(std::ostream *stream) {
outputstream = stream; }
178 std::ostream *getOutputStream()
const {
return outputstream; }
182 template <
typename T>
185 std::stringstream ss;
197 template <
typename... Args>
207 if (!absl::FormatUntyped(&message, absl::UntypedFormatSpec(fmt),
208 {absl::FormatArg(args)...})) {
209 BUG(
"Failed to format string %s", fmt);
221 auto it = diagnosticActions.find(diagnostic);
222 if (it != diagnosticActions.end())
return it->second;
227 return defaultWarningDiagnosticAction;
228 return defaultAction;
233 diagnosticActions[
cstring(diagnostic)] = action;
241 defaultWarningDiagnosticAction = action;
249 defaultInfoDiagnosticAction = action;
260 std::unordered_map<cstring, DiagnosticAction> diagnosticActions;
static ErrorCatalog & getCatalog()
Return the singleton object.
Definition error_catalog.h:103
cstring getName(int errorCode)
retrieve the name for errorCode
Definition error_catalog.h:129
Definition error_reporter.h:40
void setDefaultWarningDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::warning().
Definition error_reporter.h:240
void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action)
Set the action to take for the given diagnostic.
Definition error_reporter.h:232
std::set< std::pair< int, const Util::SourceInfo > > errorTracker
Track errors or warnings that have already been issued for a particular source location.
Definition error_reporter.h:50
bool error_reported(int err, const Util::SourceInfo source)
Definition error_reporter.h:67
unsigned getDiagnosticCount() const
Definition error_reporter.h:174
DiagnosticAction getDefaultWarningDiagnosticAction()
Definition error_reporter.h:237
void diagnose(DiagnosticAction action, const char *diagnosticName, const char *format, const char *suffix, Args &&...args)
Definition error_reporter.h:126
DiagnosticAction getDefaultInfoDiagnosticAction()
Definition error_reporter.h:245
unsigned setMaxErrorCount(unsigned newMaxCount)
set maxErrorCount to a the @newMaxCount threshold and return the previous value
Definition error_reporter.h:162
cstring get_error_name(int errorCode)
retrieve the format from the error catalog
Definition error_reporter.h:74
virtual void emit_message(const ErrorMessage &msg)
Output the message and flush the stream.
Definition error_reporter.h:53
std::ostream * outputstream
the maximum number of errors that we print before fail
Definition error_reporter.h:47
void parser_error(const Util::SourceInfo &location, const T &message)
Definition error_reporter.h:183
void setDefaultInfoDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::info().
Definition error_reporter.h:248
DiagnosticAction getDiagnosticAction(int errorCode, cstring diagnostic, DiagnosticAction defaultAction)
Definition error_reporter.h:217
void parser_error(const Util::InputSources *sources, const char *fmt, Args &&...args)
Definition error_reporter.h:198
Definition source_file.h:123
Definition source_file.h:47
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
unsigned infoCount()
Definition lib/error.h:33
DiagnosticAction
An action to take when a diagnostic message is triggered.
Definition error_reporter.h:29
@ Info
Take no action and continue compilation.
Definition error_reporter.h:31
@ Warn
Print an info message and continue compilation.
Definition error_reporter.h:32
@ Error
Print a warning and continue compilation.
Definition error_reporter.h:33
unsigned warningCount()
Definition lib/error.h:28
unsigned errorCount()
Definition lib/error.h:25
Definition error_message.h:29
Definition error_message.h:61