8#ifndef LIB_ERROR_REPORTER_H_
9#define LIB_ERROR_REPORTER_H_
15#include <unordered_map>
17#include <boost/format.hpp>
19#include "absl/strings/str_format.h"
20#include "bug_helper.h"
21#include "error_catalog.h"
22#include "error_helper.h"
23#include "exceptions.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 boost::format fmt(format);
95 return ::P4::bug_helper(fmt,
"",
"", std::forward<Args>(args)...);
98 template <
typename... Args>
99 std::string format_message(
const char *format, Args &&...args) {
100 boost::format fmt(format);
101 return ::P4::error_helper(fmt, std::forward<Args>(args)...).toString();
104 template <class T, typename = decltype(std::declval<T>()->getSourceInfo()),
typename... Args>
105 void diagnose(
DiagnosticAction action,
const int errorCode,
const char *format,
106 const char *suffix, T node, Args &&...args) {
107 if (!node ||
error_reported(errorCode, node->getSourceInfo()))
return;
111 node, std::forward<Args>(args)...);
113 diagnose(action,
nullptr, format, suffix, node, std::forward<Args>(args)...);
116 template <
typename... Args>
117 void diagnose(
DiagnosticAction action,
const int errorCode,
const char *format,
118 const char *suffix, Args &&...args) {
121 std::forward<Args>(args)...);
123 diagnose(action,
nullptr, format, suffix, std::forward<Args>(args)...);
128 template <
typename... Args>
130 const char *suffix, Args &&...args) {
131 if (action == DiagnosticAction::Ignore)
return;
133 ErrorMessage::MessageType msgType = ErrorMessage::MessageType::None;
137 if (errorCount > 0)
return;
140 msgType = ErrorMessage::MessageType::Info;
144 if (errorCount > 0)
return;
147 msgType = ErrorMessage::MessageType::Warning;
150 msgType = ErrorMessage::MessageType::Error;
153 boost::format fmt(format);
154 ErrorMessage msg(msgType, diagnosticName ? diagnosticName :
"", suffix);
155 msg = ::P4::error_helper(fmt, msg, std::forward<Args>(args)...);
158 if (errorCount > maxErrorCount)
159 FATAL_ERROR(
"Number of errors exceeded set maximum of %1%", maxErrorCount);
162 unsigned getErrorCount()
const {
return errorCount; }
164 unsigned getMaxErrorCount()
const {
return maxErrorCount; }
167 auto r = maxErrorCount;
168 maxErrorCount = newMaxCount;
172 unsigned getWarningCount()
const {
return warningCount; }
174 unsigned getInfoCount()
const {
return infoCount; }
180 void setOutputStream(std::ostream *stream) {
outputstream = stream; }
182 std::ostream *getOutputStream()
const {
return outputstream; }
186 template <
typename T>
189 std::stringstream ss;
201 template <
typename... Args>
211 if (!absl::FormatUntyped(&message, absl::UntypedFormatSpec(fmt),
212 {absl::FormatArg(args)...})) {
213 BUG(
"Failed to format string %s", fmt);
225 auto it = diagnosticActions.find(diagnostic);
226 if (it != diagnosticActions.end())
return it->second;
231 return defaultWarningDiagnosticAction;
232 return defaultAction;
237 diagnosticActions[
cstring(diagnostic)] = action;
245 defaultWarningDiagnosticAction = action;
253 defaultInfoDiagnosticAction = action;
264 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:244
void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action)
Set the action to take for the given diagnostic.
Definition error_reporter.h:236
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:178
DiagnosticAction getDefaultWarningDiagnosticAction()
Definition error_reporter.h:241
void diagnose(DiagnosticAction action, const char *diagnosticName, const char *format, const char *suffix, Args &&...args)
Definition error_reporter.h:129
DiagnosticAction getDefaultInfoDiagnosticAction()
Definition error_reporter.h:249
unsigned setMaxErrorCount(unsigned newMaxCount)
set maxErrorCount to a the @newMaxCount threshold and return the previous value
Definition error_reporter.h:166
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:187
void setDefaultInfoDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::info().
Definition error_reporter.h:252
DiagnosticAction getDiagnosticAction(int errorCode, cstring diagnostic, DiagnosticAction defaultAction)
Definition error_reporter.h:221
void parser_error(const Util::InputSources *sources, const char *fmt, Args &&...args)
Definition error_reporter.h:202
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:13
unsigned infoCount()
Definition lib/error.h:42
DiagnosticAction
An action to take when a diagnostic message is triggered.
Definition error_reporter.h:28
@ Info
Take no action and continue compilation.
Definition error_reporter.h:30
@ Warn
Print an info message and continue compilation.
Definition error_reporter.h:31
@ Error
Print a warning and continue compilation.
Definition error_reporter.h:32
unsigned warningCount()
Definition lib/error.h:37
unsigned errorCount()
Definition lib/error.h:34
Definition error_message.h:29
Definition error_message.h:61