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;
97 template <
typename... Args>
98 std::string bug_message(
const char *format, Args &&...args) {
99 boost::format fmt(format);
102 return ::P4::bug_helper(fmt,
"",
"", std::forward<Args>(args)...);
105 template <
typename... Args>
106 std::string format_message(
const char *format, Args &&...args) {
107 boost::format fmt(format);
108 return ::P4::error_helper(fmt, std::forward<Args>(args)...).toString();
111 template <class T, typename = decltype(std::declval<T>()->getSourceInfo()),
typename... Args>
112 void diagnose(
DiagnosticAction action,
const int errorCode,
const char *format,
113 const char *suffix, T node, Args &&...args) {
114 if (!node ||
error_reported(errorCode, node->getSourceInfo()))
return;
118 node, std::forward<Args>(args)...);
120 diagnose(action,
nullptr, format, suffix, node, std::forward<Args>(args)...);
123 template <
typename... Args>
124 void diagnose(
DiagnosticAction action,
const int errorCode,
const char *format,
125 const char *suffix, Args &&...args) {
128 std::forward<Args>(args)...);
130 diagnose(action,
nullptr, format, suffix, std::forward<Args>(args)...);
135 template <
typename... Args>
137 const char *suffix, Args &&...args) {
138 if (action == DiagnosticAction::Ignore)
return;
140 ErrorMessage::MessageType msgType = ErrorMessage::MessageType::None;
147 msgType = ErrorMessage::MessageType::Info;
154 msgType = ErrorMessage::MessageType::Warning;
157 msgType = ErrorMessage::MessageType::Error;
160 boost::format fmt(format);
161 ErrorMessage msg(msgType, diagnosticName ? diagnosticName :
"", suffix);
162 msg = ::P4::error_helper(fmt, msg, std::forward<Args>(args)...);
166 FATAL_ERROR(
"Number of errors exceeded set maximum of %1%", maxErrorCount);
169 unsigned getErrorCount()
const {
return errorCount; }
171 unsigned getMaxErrorCount()
const {
return maxErrorCount; }
174 auto r = maxErrorCount;
175 maxErrorCount = newMaxCount;
179 unsigned getWarningCount()
const {
return warningCount; }
181 unsigned getInfoCount()
const {
return infoCount; }
187 void setOutputStream(std::ostream *stream) {
outputstream = stream; }
189 std::ostream *getOutputStream()
const {
return outputstream; }
193 template <
typename T>
196 std::stringstream ss;
208 template <
typename... Args>
218 if (!absl::FormatUntyped(&message, absl::UntypedFormatSpec(fmt),
219 {absl::FormatArg(args)...})) {
220 BUG(
"Failed to format string");
232 auto it = diagnosticActions.find(diagnostic);
233 if (it != diagnosticActions.end())
return it->second;
238 return defaultWarningDiagnosticAction;
239 return defaultAction;
244 diagnosticActions[
cstring(diagnostic)] = action;
252 defaultWarningDiagnosticAction = action;
260 defaultInfoDiagnosticAction = action;
271 std::unordered_map<cstring, DiagnosticAction> diagnosticActions;
static ErrorCatalog & getCatalog()
Return the singleton object.
Definition error_catalog.h:105
cstring getName(int errorCode)
retrieve the name for errorCode
Definition error_catalog.h:131
Definition error_reporter.h:49
void setDefaultWarningDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::warning().
Definition error_reporter.h:251
void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action)
Set the action to take for the given diagnostic.
Definition error_reporter.h:243
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:185
DiagnosticAction getDefaultWarningDiagnosticAction()
Definition error_reporter.h:248
void diagnose(DiagnosticAction action, const char *diagnosticName, const char *format, const char *suffix, Args &&...args)
Definition error_reporter.h:136
DiagnosticAction getDefaultInfoDiagnosticAction()
Definition error_reporter.h:256
unsigned setMaxErrorCount(unsigned newMaxCount)
set maxErrorCount to a the @newMaxCount threshold and return the previous value
Definition error_reporter.h:173
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:194
void setDefaultInfoDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::info().
Definition error_reporter.h:259
DiagnosticAction getDiagnosticAction(int errorCode, cstring diagnostic, DiagnosticAction defaultAction)
Definition error_reporter.h:228
void parser_error(const Util::InputSources *sources, const char *fmt, Args &&...args)
Definition error_reporter.h:209
Definition source_file.h:131
Definition source_file.h:55
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
DiagnosticAction
An action to take when a diagnostic message is triggered.
Definition error_reporter.h:37
@ Info
Take no action and continue compilation.
@ Warn
Print an info message and continue compilation.
@ Error
Print a warning and continue compilation.
unsigned errorCount()
Definition lib/error.h:35
Definition error_message.h:38
Definition error_message.h:70