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"
35enum class DiagnosticAction {
49 unsigned int infoCount;
50 unsigned int warningCount;
51 unsigned int errorCount;
52 unsigned int maxErrorCount;
75 if (!source.isValid())
return false;
89 defaultInfoDiagnosticAction(DiagnosticAction::Info),
90 defaultWarningDiagnosticAction(DiagnosticAction::Warn) {
95 template <
typename... Args>
96 std::string bug_message(
const char *format, Args &&...args) {
97 boost::format fmt(format);
100 return ::bug_helper(fmt,
"",
"", std::forward<Args>(args)...);
103 template <
typename... Args>
104 std::string format_message(
const char *format, Args &&...args) {
105 boost::format fmt(format);
106 return ::error_helper(fmt, std::forward<Args>(args)...).toString();
109 template <
class T,
typename = std::enable_if_t<Util::has_SourceInfo_v<T>>,
typename... Args>
110 void diagnose(DiagnosticAction action,
const int errorCode,
const char *format,
111 const char *suffix,
const T *node, Args &&...args) {
116 diagnose(da, name, format, suffix, node, std::forward<Args>(args)...);
118 diagnose(action,
nullptr, format, suffix, node, std::forward<Args>(args)...);
122 template <
class T,
typename = std::enable_if_t<Util::has_SourceInfo_v<T>>,
typename... Args>
123 void diagnose(DiagnosticAction action,
const int errorCode,
const char *format,
124 const char *suffix,
const T &node, Args &&...args) {
125 diagnose(action, errorCode, format, suffix, &node, std::forward<Args>(args)...);
128 template <
typename... Args>
129 void diagnose(DiagnosticAction action,
const int errorCode,
const char *format,
130 const char *suffix, Args &&...args) {
134 diagnose(da, name, format, suffix, std::forward<Args>(args)...);
136 diagnose(action,
nullptr, format, suffix, std::forward<Args>(args)...);
141 template <
typename... Args>
142 void diagnose(DiagnosticAction action,
const char *diagnosticName,
const char *format,
143 const char *suffix, Args &&...args) {
144 if (action == DiagnosticAction::Ignore)
return;
146 ErrorMessage::MessageType msgType = ErrorMessage::MessageType::None;
147 if (action == DiagnosticAction::Info) {
150 if (errorCount > 0)
return;
153 msgType = ErrorMessage::MessageType::Info;
154 }
else if (action == DiagnosticAction::Warn) {
157 if (errorCount > 0)
return;
160 msgType = ErrorMessage::MessageType::Warning;
161 }
else if (action == DiagnosticAction::Error) {
163 msgType = ErrorMessage::MessageType::Error;
166 boost::format fmt(format);
167 ErrorMessage msg(msgType, diagnosticName ? diagnosticName :
"", suffix);
168 msg = ::error_helper(fmt, msg, std::forward<Args>(args)...);
171 if (errorCount > maxErrorCount)
172 FATAL_ERROR(
"Number of errors exceeded set maximum of %1%", maxErrorCount);
175 unsigned getErrorCount()
const {
return errorCount; }
177 unsigned getMaxErrorCount()
const {
return maxErrorCount; }
180 auto r = maxErrorCount;
181 maxErrorCount = newMaxCount;
185 unsigned getWarningCount()
const {
return warningCount; }
187 unsigned getInfoCount()
const {
return infoCount; }
193 void setOutputStream(std::ostream *stream) {
outputstream = stream; }
195 std::ostream *getOutputStream()
const {
return outputstream; }
199 template <
typename T>
202 std::stringstream ss;
214 template <
typename... Args>
224 if (!absl::FormatUntyped(&message, absl::UntypedFormatSpec(fmt),
225 {absl::FormatArg(args)...})) {
226 BUG(
"Failed to format string");
236 if (defaultAction == DiagnosticAction::Error)
return defaultAction;
237 auto it = diagnosticActions.find(diagnostic);
238 if (it != diagnosticActions.end())
return it->second;
241 if (defaultAction == DiagnosticAction::Warn &&
242 defaultWarningDiagnosticAction != DiagnosticAction::Warn)
243 return defaultWarningDiagnosticAction;
244 return defaultAction;
249 diagnosticActions[
cstring(diagnostic)] = action;
257 defaultWarningDiagnosticAction = action;
265 defaultInfoDiagnosticAction = action;
270 DiagnosticAction defaultInfoDiagnosticAction;
273 DiagnosticAction defaultWarningDiagnosticAction;
276 std::unordered_map<cstring, DiagnosticAction> diagnosticActions;
cstring getName(int errorCode)
retrieve the name for errorCode
Definition error_catalog.h:127
static ErrorCatalog & getCatalog()
Return the singleton object.
Definition error_catalog.h:101
Definition error_reporter.h:47
std::ostream * outputstream
the maximum number of errors that we print before fail
Definition error_reporter.h:54
void setDefaultWarningDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to warning().
Definition error_reporter.h:256
void setDefaultInfoDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to info().
Definition error_reporter.h:264
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:57
void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action)
Set the action to take for the given diagnostic.
Definition error_reporter.h:248
void diagnose(DiagnosticAction action, const char *diagnosticName, const char *format, const char *suffix, Args &&...args)
Definition error_reporter.h:142
DiagnosticAction getDiagnosticAction(cstring diagnostic, DiagnosticAction defaultAction)
Definition error_reporter.h:234
void parser_error(const Util::InputSources *sources, const char *fmt, Args &&...args)
Definition error_reporter.h:215
DiagnosticAction getDefaultInfoDiagnosticAction()
Definition error_reporter.h:261
cstring get_error_name(int errorCode)
retrieve the format from the error catalog
Definition error_reporter.h:81
bool error_reported(int err, const Util::SourceInfo source)
Definition error_reporter.h:74
virtual void emit_message(const ErrorMessage &msg)
Output the message and flush the stream.
Definition error_reporter.h:60
void parser_error(const Util::SourceInfo &location, const T &message)
Definition error_reporter.h:200
unsigned setMaxErrorCount(unsigned newMaxCount)
set maxErrorCount to a the @newMaxCount threshold and return the previous value
Definition error_reporter.h:179
DiagnosticAction getDefaultWarningDiagnosticAction()
Definition error_reporter.h:253
unsigned getDiagnosticCount() const
Definition error_reporter.h:191
Definition source_file.h:123
Definition source_file.h:53
Definition error_message.h:36
Definition error_message.h:68