17#ifndef LIB_ERROR_REPORTER_H_
18#define LIB_ERROR_REPORTER_H_
23#include <unordered_map>
25#include <boost/format.hpp>
27#include "error_catalog.h"
28#include "error_helper.h"
29#include "exceptions.h"
32enum class DiagnosticAction {
46 unsigned int infoCount;
47 unsigned int warningCount;
48 unsigned int errorCount;
49 unsigned int maxErrorCount;
72 if (!source.isValid())
return false;
88 defaultInfoDiagnosticAction(DiagnosticAction::Info),
89 defaultWarningDiagnosticAction(DiagnosticAction::Warn) {
94 template <
typename... T>
95 std::string bug_message(
const char *format, T... args) {
96 boost::format fmt(format);
97 std::string message = ::bug_helper(fmt,
"",
"",
"", args...);
101 template <
typename... T>
102 std::string format_message(
const char *format, T... args) {
103 boost::format fmt(format);
104 std::string message = ::error_helper(fmt, args...).toString();
110 typename =
typename std::enable_if<std::is_base_of<Util::IHasSourceInfo, T>::value>::type,
112 void diagnose(DiagnosticAction action,
const int errorCode,
const char *format,
113 const char *suffix,
const T *node, Args... args) {
118 diagnose(da, name, format, suffix, node, args...);
120 diagnose(action,
nullptr, format, suffix, node, std::forward<Args>(args)...);
126 typename =
typename std::enable_if<std::is_base_of<Util::IHasSourceInfo, T>::value>::type,
128 void diagnose(DiagnosticAction action,
const int errorCode,
const char *format,
129 const char *suffix,
const T &node, Args... args) {
130 diagnose(action, errorCode, format, suffix, &node, std::forward<Args>(args)...);
133 template <
typename... Args>
134 void diagnose(DiagnosticAction action,
const int errorCode,
const char *format,
135 const char *suffix, Args... args) {
139 diagnose(da, name, format, suffix, args...);
141 diagnose(action,
nullptr, format, suffix, std::forward<Args>(args)...);
146 template <
typename... T>
147 void diagnose(DiagnosticAction action,
const char *diagnosticName,
const char *format,
148 const char *suffix, T... args) {
149 if (action == DiagnosticAction::Ignore)
return;
151 ErrorMessage::MessageType msgType = ErrorMessage::MessageType::None;
152 if (action == DiagnosticAction::Info) {
155 if (errorCount > 0)
return;
158 msgType = ErrorMessage::MessageType::Info;
159 }
else if (action == DiagnosticAction::Warn) {
162 if (errorCount > 0)
return;
165 msgType = ErrorMessage::MessageType::Warning;
166 }
else if (action == DiagnosticAction::Error) {
168 msgType = ErrorMessage::MessageType::Error;
171 boost::format fmt(format);
172 ErrorMessage msg(msgType, diagnosticName ? diagnosticName :
"", suffix);
173 msg = ::error_helper(fmt, msg, args...);
176 if (errorCount > maxErrorCount)
177 FATAL_ERROR(
"Number of errors exceeded set maximum of %1%", maxErrorCount);
180 unsigned getErrorCount()
const {
return errorCount; }
182 unsigned getMaxErrorCount()
const {
return maxErrorCount; }
185 auto r = maxErrorCount;
186 maxErrorCount = newMaxCount;
190 unsigned getWarningCount()
const {
return warningCount; }
192 unsigned getInfoCount()
const {
return infoCount; }
198 void setOutputStream(std::ostream *stream) {
outputstream = stream; }
200 std::ostream *getOutputStream()
const {
return outputstream; }
204 template <
typename T>
207 std::stringstream ss;
225 cstring message = Util::vprintf_format(fmt, args);
234 parser_error(sources, fmt, args);
242 if (defaultAction == DiagnosticAction::Error)
return defaultAction;
243 auto it = diagnosticActions.find(diagnostic);
244 if (it != diagnosticActions.end())
return it->second;
247 if (defaultAction == DiagnosticAction::Warn &&
248 defaultWarningDiagnosticAction != DiagnosticAction::Warn)
249 return defaultWarningDiagnosticAction;
250 return defaultAction;
255 diagnosticActions[diagnostic] = action;
263 defaultWarningDiagnosticAction = action;
271 defaultInfoDiagnosticAction = action;
276 DiagnosticAction defaultInfoDiagnosticAction;
279 DiagnosticAction defaultWarningDiagnosticAction;
282 std::unordered_map<cstring, DiagnosticAction> diagnosticActions;
cstring getName(int errorCode)
retrieve the name for errorCode
Definition error_catalog.h:125
static ErrorCatalog & getCatalog()
Return the singleton object.
Definition error_catalog.h:99
Definition error_reporter.h:44
std::ostream * outputstream
the maximum number of errors that we print before fail
Definition error_reporter.h:51
void parser_error(const Util::InputSources *sources, const char *fmt, va_list args)
Definition error_reporter.h:220
void setDefaultWarningDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to warning().
Definition error_reporter.h:262
void setDefaultInfoDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to ::info().
Definition error_reporter.h:270
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:54
const char * get_error_name(int errorCode)
retrieve the format from the error catalog
Definition error_reporter.h:78
DiagnosticAction getDiagnosticAction(cstring diagnostic, DiagnosticAction defaultAction)
Definition error_reporter.h:240
DiagnosticAction getDefaultInfoDiagnosticAction()
Definition error_reporter.h:267
bool error_reported(int err, const Util::SourceInfo source)
Definition error_reporter.h:71
virtual void emit_message(const ErrorMessage &msg)
Output the message and flush the stream.
Definition error_reporter.h:57
void parser_error(const Util::SourceInfo &location, const T &message)
Definition error_reporter.h:205
void setDiagnosticAction(cstring diagnostic, DiagnosticAction action)
Set the action to take for the given diagnostic.
Definition error_reporter.h:254
void diagnose(DiagnosticAction action, const char *diagnosticName, const char *format, const char *suffix, T... args)
Definition error_reporter.h:147
unsigned setMaxErrorCount(unsigned newMaxCount)
set maxErrorCount to a the @newMaxCount threshold and return the previous value
Definition error_reporter.h:184
DiagnosticAction getDefaultWarningDiagnosticAction()
Definition error_reporter.h:259
unsigned getDiagnosticCount() const
Definition error_reporter.h:196
Definition source_file.h:126
Definition source_file.h:56
Definition error_message.h:36
Definition error_message.h:64