diff --git a/Changelog.md b/Changelog.md index 69c3573fc..2ab1bb037 100644 --- a/Changelog.md +++ b/Changelog.md @@ -7,6 +7,7 @@ Language Features: Compiler Features: * Commandline Interface: Accept nested brackets in step sequences passed to ``--yul-optimizations``. * Commandline Interface: Add ``--debug-info`` option for selecting how much extra debug information should be included in the produced EVM assembly and Yul code. + * Commandline Interface: Use different colors when printing errors, warnings and infos. * SMTChecker: Output values for ``block.*``, ``msg.*`` and ``tx.*`` variables that are present in the called functions. * Standard JSON: Accept nested brackets in step sequences passed to ``settings.optimizer.details.yulDetails.optimizerSteps``. * Standard JSON: Add ``settings.debug.debugInfo`` option for selecting how much extra debug information should be included in the produced EVM assembly and Yul code. diff --git a/liblangutil/Exceptions.cpp b/liblangutil/Exceptions.cpp index 34aa69803..a410a9863 100644 --- a/liblangutil/Exceptions.cpp +++ b/liblangutil/Exceptions.cpp @@ -23,6 +23,9 @@ #include +#include +#include + using namespace std; using namespace solidity; using namespace solidity::langutil; @@ -71,3 +74,15 @@ Error::Error( if (!_description.empty()) *this << util::errinfo_comment(_description); } + +optional Error::severityFromString(string _input) +{ + boost::algorithm::to_lower(_input); + boost::algorithm::trim(_input); + + for (Severity severity: {Severity::Error, Severity::Warning, Severity::Info}) + if (_input == formatErrorSeverityLowercase(severity)) + return severity; + + return nullopt; +} diff --git a/liblangutil/Exceptions.h b/liblangutil/Exceptions.h index 6946ff463..0c8f03cb9 100644 --- a/liblangutil/Exceptions.h +++ b/liblangutil/Exceptions.h @@ -258,6 +258,8 @@ public: solAssert(false, ""); } + static std::optional severityFromString(std::string _input); + private: ErrorId m_errorId; Type m_type; diff --git a/liblangutil/SourceReferenceFormatter.cpp b/liblangutil/SourceReferenceFormatter.cpp index 623e13dd1..0edd551b8 100644 --- a/liblangutil/SourceReferenceFormatter.cpp +++ b/liblangutil/SourceReferenceFormatter.cpp @@ -65,9 +65,21 @@ AnsiColorized SourceReferenceFormatter::frameColored() const return AnsiColorized(m_stream, m_colored, {BOLD, BLUE}); } -AnsiColorized SourceReferenceFormatter::errorColored() const +AnsiColorized SourceReferenceFormatter::errorColored(optional _severity) const { - return AnsiColorized(m_stream, m_colored, {BOLD, RED}); + // We used to color messages of any severity as errors so this seems like a good default + // for cases where severity cannot be determined. + char const* textColor = RED; + + if (_severity.has_value()) + switch (_severity.value()) + { + case Error::Severity::Error: textColor = RED; break; + case Error::Severity::Warning: textColor = YELLOW; break; + case Error::Severity::Info: textColor = WHITE; break; + } + + return AnsiColorized(m_stream, m_colored, {BOLD, textColor}); } AnsiColorized SourceReferenceFormatter::messageColored() const @@ -164,9 +176,10 @@ void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref) void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg) { // exception header line - errorColored() << _msg.severity; + optional severity = Error::severityFromString(_msg.severity); + errorColored(severity) << _msg.severity; if (m_withErrorIds && _msg.errorId.has_value()) - errorColored() << " (" << _msg.errorId.value().error << ")"; + errorColored(severity) << " (" << _msg.errorId.value().error << ")"; messageColored() << ": " << _msg.primary.message << '\n'; printSourceLocation(_msg.primary); diff --git a/liblangutil/SourceReferenceFormatter.h b/liblangutil/SourceReferenceFormatter.h index 5bac03a9a..e18400b60 100644 --- a/liblangutil/SourceReferenceFormatter.h +++ b/liblangutil/SourceReferenceFormatter.h @@ -87,7 +87,7 @@ public: private: util::AnsiColorized normalColored() const; util::AnsiColorized frameColored() const; - util::AnsiColorized errorColored() const; + util::AnsiColorized errorColored(std::optional _severity) const; util::AnsiColorized messageColored() const; util::AnsiColorized secondaryColored() const; util::AnsiColorized highlightColored() const;