1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
#include "errorhandler.hpp"
namespace Compiler
{
ErrorHandler::ErrorHandler()
: mWarnings(0)
, mErrors(0)
, mWarningsMode(1)
, mDowngradeErrors(false)
{
}
ErrorHandler::~ErrorHandler() = default;
// Was compiling successful?
bool ErrorHandler::isGood() const
{
return mErrors == 0;
}
// Return number of errors
int ErrorHandler::countErrors() const
{
return mErrors;
}
// Return number of warnings
int ErrorHandler::countWarnings() const
{
return mWarnings;
}
// Generate a warning message.
void ErrorHandler::warning(const std::string& message, const TokenLoc& loc)
{
if (mWarningsMode == 1 ||
// temporarily change from mode 2 to mode 1 if error downgrading is enabled to
// avoid infinite recursion
(mWarningsMode == 2 && mDowngradeErrors))
{
++mWarnings;
report(message, loc, WarningMessage);
}
else if (mWarningsMode == 2)
error(message, loc);
}
// Generate an error message.
void ErrorHandler::error(const std::string& message, const TokenLoc& loc)
{
if (mDowngradeErrors)
{
warning(message, loc);
return;
}
++mErrors;
report(message, loc, ErrorMessage);
}
// Generate an error message for an unexpected EOF.
void ErrorHandler::endOfFile()
{
++mErrors;
report("unexpected end of file", ErrorMessage);
}
// Remove all previous error/warning events
void ErrorHandler::reset()
{
mErrors = mWarnings = 0;
}
void ErrorHandler::setWarningsMode(int mode)
{
mWarningsMode = mode;
}
void ErrorHandler::downgradeErrors(bool downgrade)
{
mDowngradeErrors = downgrade;
}
ErrorDowngrade::ErrorDowngrade(ErrorHandler& handler)
: mHandler(handler)
{
mHandler.downgradeErrors(true);
}
ErrorDowngrade::~ErrorDowngrade()
{
mHandler.downgradeErrors(false);
}
}
|