diff --git a/liblangutil/Exceptions.h b/liblangutil/Exceptions.h index 6946ff463..a3e3adca3 100644 --- a/liblangutil/Exceptions.h +++ b/liblangutil/Exceptions.h @@ -23,31 +23,44 @@ #pragma once -#include +#include #include #include -#include +#include #include #include #include +#include +#include #include #include #include -#include namespace solidity::langutil { class Error; using ErrorList = std::vector>; -struct CompilerError: virtual util::Exception {}; -struct StackTooDeepError: virtual CompilerError {}; -struct InternalCompilerError: virtual util::Exception {}; -struct FatalError: virtual util::Exception {}; -struct UnimplementedFeatureError: virtual util::Exception {}; -struct InvalidAstError: virtual util::Exception {}; +struct CompilerError: virtual util::Exception +{ +}; +struct StackTooDeepError: virtual CompilerError +{ +}; +struct InternalCompilerError: virtual util::Exception +{ +}; +struct FatalError: virtual util::Exception +{ +}; +struct UnimplementedFeatureError: virtual util::Exception +{ +}; +struct InvalidAstError: virtual util::Exception +{ +}; /// Assertion that throws an InternalCompilerError containing the given description if it is not met. @@ -111,7 +124,6 @@ struct InvalidAstError: virtual util::Exception {}; "AST assertion failed" \ ) - using errorSourceLocationInfo = std::pair; class SecondarySourceLocation @@ -161,7 +173,7 @@ struct ErrorId bool operator!=(ErrorId const& _rhs) const { return !(*this == _rhs); } bool operator<(ErrorId const& _rhs) const { return error < _rhs.error; } }; -constexpr ErrorId operator"" _error(unsigned long long _error) { return ErrorId{ _error }; } +constexpr ErrorId operator"" _error(unsigned long long _error) { return ErrorId{_error}; } class Error: virtual public util::Exception { @@ -190,8 +202,7 @@ public: Type _type, std::string const& _description, SourceLocation const& _location = SourceLocation(), - SecondarySourceLocation const& _secondaryLocation = SecondarySourceLocation() - ); + SecondarySourceLocation const& _secondaryLocation = SecondarySourceLocation()); ErrorId errorId() const { return m_errorId; } Type type() const { return m_type; } @@ -215,15 +226,9 @@ public: return Severity::Error; } - static bool isError(Severity _severity) - { - return _severity == Severity::Error; - } + static bool isError(Severity _severity) { return _severity == Severity::Error; } - static bool isError(Type _type) - { - return isError(errorSeverity(_type)); - } + static bool isError(Type _type) { return isError(errorSeverity(_type)); } static bool containsErrors(ErrorList const& _list) { @@ -243,6 +248,11 @@ public: return "Error"; } + static std::optional stringToSeverity(Severity _severity) + { + return _severity ? std::optional{"Error, Warning, Info"} : std::nullopt; + } + static std::string formatErrorSeverityLowercase(Severity _severity) { switch (_severity) @@ -263,5 +273,4 @@ private: Type m_type; std::string m_typeName; }; - } diff --git a/liblangutil/SourceReferenceFormatter.cpp b/liblangutil/SourceReferenceFormatter.cpp index 623e13dd1..992a7c772 100644 --- a/liblangutil/SourceReferenceFormatter.cpp +++ b/liblangutil/SourceReferenceFormatter.cpp @@ -19,12 +19,12 @@ * Formatting functions for errors referencing positions and locations in the source. */ -#include -#include +#include #include #include +#include +#include #include -#include #include using namespace std; @@ -35,7 +35,6 @@ using namespace solidity::util::formatting; namespace { - std::string replaceNonTabs(std::string_view _utf8Input, char _filler) { std::string output; @@ -49,25 +48,31 @@ std::string replaceNonTabs(std::string_view _utf8Input, char _filler) std::string SourceReferenceFormatter::formatErrorInformation(Error const& _error, CharStream const& _charStream) { - return formatErrorInformation( - _error, - SingletonCharStreamProvider(_charStream) - ); + return formatErrorInformation(_error, SingletonCharStreamProvider(_charStream)); } -AnsiColorized SourceReferenceFormatter::normalColored() const -{ - return AnsiColorized(m_stream, m_colored, {WHITE}); -} +AnsiColorized SourceReferenceFormatter::normalColored() const { return AnsiColorized(m_stream, m_colored, {WHITE}); } AnsiColorized SourceReferenceFormatter::frameColored() const { return AnsiColorized(m_stream, m_colored, {BOLD, BLUE}); } -AnsiColorized SourceReferenceFormatter::errorColored() const +AnsiColorized SourceReferenceFormatter::errorColored() const { return AnsiColorized(m_stream, m_colored, {BOLD, RED}); } + +AnsiColorized SourceReferenceFormatter::infoColored() const { - return AnsiColorized(m_stream, m_colored, {BOLD, RED}); + return AnsiColorized(m_stream, m_colored, {BOLD, WHITE}); +} + +AnsiColorized SourceReferenceFormatter::warningColored() const +{ + return AnsiColorized(m_stream, m_colored, {BOLD, YELLOW}); +} + +AnsiColorized SourceReferenceFormatter::unknownColored() const +{ + return AnsiColorized(m_stream, m_colored, {BOLD, WHITE}); } AnsiColorized SourceReferenceFormatter::messageColored() const @@ -133,11 +138,10 @@ void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref) frameColored() << '|'; m_stream << ' ' << replaceNonTabs(text.substr(0, static_cast(_ref.startColumn)), ' '); - diagColored() << ( - locationLength == 0 ? - "^" : - replaceNonTabs(text.substr(static_cast(_ref.startColumn), locationLength), '^') - ); + diagColored() + << (locationLength == 0 + ? "^" + : replaceNonTabs(text.substr(static_cast(_ref.startColumn), locationLength), '^')); m_stream << '\n'; } else @@ -164,10 +168,20 @@ void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref) void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg) { // exception header line - errorColored() << _msg.severity; - if (m_withErrorIds && _msg.errorId.has_value()) - errorColored() << " (" << _msg.errorId.value().error << ")"; - messageColored() << ": " << _msg.primary.message << '\n'; + if (_msg.errorId.has_value()) + { + if (_msg.severity == "Error") + errorColored() << " (" << _msg.errorId.value().error << ")"; + + else if (_msg.severity == "Warning") + warningColored() << " (" << _msg.errorId.value().error << ")"; + + else if (_msg.severity == "Info") + infoColored() << " (" << _msg.errorId.value().error << ")"; + + else + unknownColored() << " (" << _msg.errorId.value().error << ")"; + } printSourceLocation(_msg.primary); @@ -181,7 +195,8 @@ void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtracto m_stream << '\n'; } -void SourceReferenceFormatter::printExceptionInformation(util::Exception const& _exception, std::string const& _severity) +void SourceReferenceFormatter::printExceptionInformation( + util::Exception const& _exception, std::string const& _severity) { printExceptionInformation(SourceReferenceExtractor::extract(m_charStreamProvider, _exception, _severity)); }