17#ifndef LIB_ERROR_REPORTER_H_
18#define LIB_ERROR_REPORTER_H_
24#include <unordered_map>
26#include <boost/format.hpp>
28#include "absl/strings/str_format.h"
29#include "bug_helper.h"
30#include "error_catalog.h"
31#include "error_helper.h"
32#include "exceptions.h"
51 unsigned int infoCount;
52 unsigned int warningCount;
53 unsigned int errorCount;
54 unsigned int maxErrorCount;
77 if (!source.isValid())
return false;
96 virtual ~ErrorReporter() =
default;
99 template <
typename... Args>
100 std::string bug_message(
const char *format, Args &&...args) {
101 boost::format fmt(format);
104 return ::P4::bug_helper(fmt,
"",
"", std::forward<Args>(args)...);
107 template <
typename... Args>
108 std::string format_message(
const char *format, Args &&...args) {
109 boost::format fmt(format);
110 return ::P4::error_helper(fmt, std::forward<Args>(args)...).toString();
113 template <class T, typename = decltype(std::declval<T>()->getSourceInfo()),
typename... Args>
114 void diagnose(
DiagnosticAction action,
const int errorCode,
const char *format,
115 const char *suffix, T node, Args &&...args) {
116 if (!node ||
error_reported(errorCode, node->getSourceInfo()))
return;
120 node, std::forward<Args>(args)...);
122 diagnose(action,
nullptr, format, suffix, node, std::forward<Args>(args)...);
125 template <
typename... Args>
126 void diagnose(
DiagnosticAction action,
const int errorCode,
const char *format,
127 const char *suffix, Args &&...args) {
130 std::forward<Args>(args)...);
132 diagnose(action,
nullptr, format, suffix, std::forward<Args>(args)...);
137 template <
typename... Args>
139 const char *suffix, Args &&...args) {
140 if (action == DiagnosticAction::Ignore)
return;
142 ErrorMessage::MessageType msgType = ErrorMessage::MessageType::None;
146 if (errorCount > 0)
return;
149 msgType = ErrorMessage::MessageType::Info;
153 if (errorCount > 0)
return;
156 msgType = ErrorMessage::MessageType::Warning;
159 msgType = ErrorMessage::MessageType::Error;
162 boost::format fmt(format);
163 ErrorMessage msg(msgType, diagnosticName ? diagnosticName :
"", suffix);
164 msg = ::P4::error_helper(fmt, msg, std::forward<Args>(args)...);
167 if (errorCount > maxErrorCount)
168 FATAL_ERROR(
"Number of errors exceeded set maximum of %1%", maxErrorCount);
171 unsigned getErrorCount()
const {
return errorCount; }
173 unsigned getMaxErrorCount()
const {
return maxErrorCount; }
176 auto r = maxErrorCount;
177 maxErrorCount = newMaxCount;
181 unsigned getWarningCount()
const {
return warningCount; }
183 unsigned getInfoCount()
const {
return infoCount; }
189 void setOutputStream(std::ostream *stream) {
outputstream = stream; }
191 std::ostream *getOutputStream()
const {
return outputstream; }
195 template <
typename T>
198 std::stringstream ss;
210 template <
typename... Args>
220 if (!absl::FormatUntyped(&message, absl::UntypedFormatSpec(fmt),
221 {absl::FormatArg(args)...})) {
222 BUG(
"Failed to format string %s", fmt);
234 auto it = diagnosticActions.find(diagnostic);
235 if (it != diagnosticActions.end())
return it->second;
240 return defaultWarningDiagnosticAction;
241 return defaultAction;
246 diagnosticActions[
cstring(diagnostic)] = action;
254 defaultWarningDiagnosticAction = action;
262 defaultInfoDiagnosticAction = action;
273 std::unordered_map<cstring, DiagnosticAction> diagnosticActions;
static ErrorCatalog & getCatalog()
Return the singleton object.
Definition error_catalog.h:111
cstring getName(int errorCode)
retrieve the name for errorCode
Definition error_catalog.h:137
Definition error_reporter.h:49
void setDefaultWarningDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::warning().
Definition error_reporter.h:253
void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action)
Set the action to take for the given diagnostic.
Definition error_reporter.h:245
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:59
bool error_reported(int err, const Util::SourceInfo source)
Definition error_reporter.h:76
unsigned getDiagnosticCount() const
Definition error_reporter.h:187
DiagnosticAction getDefaultWarningDiagnosticAction()
Definition error_reporter.h:250
void diagnose(DiagnosticAction action, const char *diagnosticName, const char *format, const char *suffix, Args &&...args)
Definition error_reporter.h:138
DiagnosticAction getDefaultInfoDiagnosticAction()
Definition error_reporter.h:258
unsigned setMaxErrorCount(unsigned newMaxCount)
set maxErrorCount to a the @newMaxCount threshold and return the previous value
Definition error_reporter.h:175
cstring get_error_name(int errorCode)
retrieve the format from the error catalog
Definition error_reporter.h:83
virtual void emit_message(const ErrorMessage &msg)
Output the message and flush the stream.
Definition error_reporter.h:62
std::ostream * outputstream
the maximum number of errors that we print before fail
Definition error_reporter.h:56
void parser_error(const Util::SourceInfo &location, const T &message)
Definition error_reporter.h:196
void setDefaultInfoDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::info().
Definition error_reporter.h:261
DiagnosticAction getDiagnosticAction(int errorCode, cstring diagnostic, DiagnosticAction defaultAction)
Definition error_reporter.h:230
void parser_error(const Util::InputSources *sources, const char *fmt, Args &&...args)
Definition error_reporter.h:211
Definition source_file.h:132
Definition source_file.h:56
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
unsigned infoCount()
Definition lib/error.h:42
DiagnosticAction
An action to take when a diagnostic message is triggered.
Definition error_reporter.h:37
@ Info
Take no action and continue compilation.
Definition error_reporter.h:39
@ Warn
Print an info message and continue compilation.
Definition error_reporter.h:40
@ Error
Print a warning and continue compilation.
Definition error_reporter.h:41
unsigned warningCount()
Definition lib/error.h:37
unsigned errorCount()
Definition lib/error.h:34
Definition error_message.h:38
Definition error_message.h:70