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;
95 virtual ~ErrorReporter() =
default;
98 template <
typename... Args>
99 std::string bug_message(
const char *format, Args &&...args) {
100 boost::format fmt(format);
103 return ::P4::bug_helper(fmt,
"",
"", std::forward<Args>(args)...);
106 template <
typename... Args>
107 std::string format_message(
const char *format, Args &&...args) {
108 boost::format fmt(format);
109 return ::P4::error_helper(fmt, std::forward<Args>(args)...).toString();
112 template <class T, typename = decltype(std::declval<T>()->getSourceInfo()),
typename... Args>
113 void diagnose(
DiagnosticAction action,
const int errorCode,
const char *format,
114 const char *suffix, T node, Args &&...args) {
115 if (!node ||
error_reported(errorCode, node->getSourceInfo()))
return;
119 node, std::forward<Args>(args)...);
121 diagnose(action,
nullptr, format, suffix, node, std::forward<Args>(args)...);
124 template <
typename... Args>
125 void diagnose(
DiagnosticAction action,
const int errorCode,
const char *format,
126 const char *suffix, Args &&...args) {
129 std::forward<Args>(args)...);
131 diagnose(action,
nullptr, format, suffix, std::forward<Args>(args)...);
136 template <
typename... Args>
138 const char *suffix, Args &&...args) {
139 if (action == DiagnosticAction::Ignore)
return;
141 ErrorMessage::MessageType msgType = ErrorMessage::MessageType::None;
145 if (errorCount > 0)
return;
148 msgType = ErrorMessage::MessageType::Info;
152 if (errorCount > 0)
return;
155 msgType = ErrorMessage::MessageType::Warning;
158 msgType = ErrorMessage::MessageType::Error;
161 boost::format fmt(format);
162 ErrorMessage msg(msgType, diagnosticName ? diagnosticName :
"", suffix);
163 msg = ::P4::error_helper(fmt, msg, std::forward<Args>(args)...);
166 if (errorCount > maxErrorCount)
167 FATAL_ERROR(
"Number of errors exceeded set maximum of %1%", maxErrorCount);
170 unsigned getErrorCount()
const {
return errorCount; }
172 unsigned getMaxErrorCount()
const {
return maxErrorCount; }
175 auto r = maxErrorCount;
176 maxErrorCount = newMaxCount;
180 unsigned getWarningCount()
const {
return warningCount; }
182 unsigned getInfoCount()
const {
return infoCount; }
188 void setOutputStream(std::ostream *stream) {
outputstream = stream; }
190 std::ostream *getOutputStream()
const {
return outputstream; }
194 template <
typename T>
197 std::stringstream ss;
209 template <
typename... Args>
219 if (!absl::FormatUntyped(&message, absl::UntypedFormatSpec(fmt),
220 {absl::FormatArg(args)...})) {
221 BUG(
"Failed to format string %s", fmt);
233 auto it = diagnosticActions.find(diagnostic);
234 if (it != diagnosticActions.end())
return it->second;
239 return defaultWarningDiagnosticAction;
240 return defaultAction;
245 diagnosticActions[
cstring(diagnostic)] = action;
253 defaultWarningDiagnosticAction = action;
261 defaultInfoDiagnosticAction = action;
272 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:252
void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action)
Set the action to take for the given diagnostic.
Definition error_reporter.h:244
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:186
DiagnosticAction getDefaultWarningDiagnosticAction()
Definition error_reporter.h:249
void diagnose(DiagnosticAction action, const char *diagnosticName, const char *format, const char *suffix, Args &&...args)
Definition error_reporter.h:137
DiagnosticAction getDefaultInfoDiagnosticAction()
Definition error_reporter.h:257
unsigned setMaxErrorCount(unsigned newMaxCount)
set maxErrorCount to a the @newMaxCount threshold and return the previous value
Definition error_reporter.h:174
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:195
void setDefaultInfoDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::info().
Definition error_reporter.h:260
DiagnosticAction getDiagnosticAction(int errorCode, cstring diagnostic, DiagnosticAction defaultAction)
Definition error_reporter.h:229
void parser_error(const Util::InputSources *sources, const char *fmt, Args &&...args)
Definition error_reporter.h:210
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