diff --git a/liblangutil/Exceptions.h b/liblangutil/Exceptions.h index d74ee82c5..fc7286ed1 100644 --- a/liblangutil/Exceptions.h +++ b/liblangutil/Exceptions.h @@ -37,6 +37,7 @@ #include #include #include +#include namespace solidity::langutil { @@ -228,6 +229,13 @@ public: } } + static constexpr Severity errorSeverityOrType(std::variant _typeOrSeverity) + { + if (std::holds_alternative(_typeOrSeverity)) + return errorSeverity(std::get(_typeOrSeverity)); + return std::get(_typeOrSeverity); + } + static bool isError(Severity _severity) { return _severity == Severity::Error; @@ -282,6 +290,13 @@ public: util::unreachable(); } + static std::string formatTypeOrSeverity(std::variant _typeOrSeverity) + { + if (std::holds_alternative(_typeOrSeverity)) + return formatErrorType(std::get(_typeOrSeverity)); + return formatErrorSeverity(std::get(_typeOrSeverity)); + } + static std::string formatErrorSeverityLowercase(Severity _severity) { std::string severityValue = formatErrorSeverity(_severity); diff --git a/liblangutil/SourceReferenceExtractor.cpp b/liblangutil/SourceReferenceExtractor.cpp index 14dbc25e6..01f22a1f7 100644 --- a/liblangutil/SourceReferenceExtractor.cpp +++ b/liblangutil/SourceReferenceExtractor.cpp @@ -22,6 +22,7 @@ #include #include +#include using namespace std; using namespace solidity; @@ -30,7 +31,7 @@ using namespace solidity::langutil; SourceReferenceExtractor::Message SourceReferenceExtractor::extract( CharStreamProvider const& _charStreamProvider, util::Exception const& _exception, - Error::Type _type + std::variant _typeOrSeverity ) { SourceLocation const* location = boost::get_error_info(_exception); @@ -44,15 +45,16 @@ SourceReferenceExtractor::Message SourceReferenceExtractor::extract( for (auto const& info: secondaryLocation->infos) secondary.emplace_back(extract(_charStreamProvider, &info.second, info.first)); - return Message{std::move(primary), _type, std::move(secondary), nullopt}; + return Message{std::move(primary), _typeOrSeverity, std::move(secondary), nullopt}; } SourceReferenceExtractor::Message SourceReferenceExtractor::extract( CharStreamProvider const& _charStreamProvider, - Error const& _error + Error const& _error, + std::variant _typeOrSeverity ) { - Message message = extract(_charStreamProvider, _error, _error.type()); + Message message = extract(_charStreamProvider, static_cast(_error), _typeOrSeverity); message.errorId = _error.errorId(); return message; } diff --git a/liblangutil/SourceReferenceExtractor.h b/liblangutil/SourceReferenceExtractor.h index 75e56b4d9..3f7751f0b 100644 --- a/liblangutil/SourceReferenceExtractor.h +++ b/liblangutil/SourceReferenceExtractor.h @@ -24,6 +24,7 @@ #include #include #include +#include namespace solidity::langutil { @@ -55,12 +56,21 @@ namespace SourceReferenceExtractor struct Message { SourceReference primary; - Error::Type type; + std::variant _typeOrSeverity; std::vector secondary; std::optional errorId; }; - Message extract(CharStreamProvider const& _charStreamProvider, util::Exception const& _exception, Error::Type _type); + Message extract( + CharStreamProvider const& _charStreamProvider, + util::Exception const& _exception, + std::variant _typeOrSeverity + ); + Message extract( + CharStreamProvider const& _charStreamProvider, + Error const& _error, + std::variant _typeOrSeverity + ); Message extract(CharStreamProvider const& _charStreamProvider, Error const& _error); SourceReference extract(CharStreamProvider const& _charStreamProvider, SourceLocation const* _location, std::string message = ""); } diff --git a/liblangutil/SourceReferenceFormatter.cpp b/liblangutil/SourceReferenceFormatter.cpp index fa7bd47ad..4765b9fb2 100644 --- a/liblangutil/SourceReferenceFormatter.cpp +++ b/liblangutil/SourceReferenceFormatter.cpp @@ -26,6 +26,7 @@ #include #include #include +#include using namespace std; using namespace solidity; @@ -175,16 +176,12 @@ void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref) } } -void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg, bool _printFullType) +void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg) { - errorColored(Error::errorSeverity(_msg.type)) << ( - _printFullType ? - Error::formatErrorType(_msg.type) : - Error::formatErrorSeverity(Error::errorSeverity(_msg.type)) - ); + errorColored(Error::errorSeverityOrType(_msg._typeOrSeverity)) << Error::formatTypeOrSeverity(_msg._typeOrSeverity); if (m_withErrorIds && _msg.errorId.has_value()) - errorColored(Error::errorSeverity(_msg.type)) << " (" << _msg.errorId.value().error << ")"; + errorColored(Error::errorSeverityOrType(_msg._typeOrSeverity)) << " (" << _msg.errorId.value().error << ")"; messageColored() << ": " << _msg.primary.message << '\n'; printSourceLocation(_msg.primary); @@ -199,9 +196,14 @@ void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtracto m_stream << '\n'; } -void SourceReferenceFormatter::printExceptionInformation(util::Exception const& _exception, Error::Type _type, bool _printFullType) +void SourceReferenceFormatter::printExceptionInformation(util::Exception const& _exception, Error::Type _type) { - printExceptionInformation(SourceReferenceExtractor::extract(m_charStreamProvider, _exception, _type), _printFullType); + printExceptionInformation(SourceReferenceExtractor::extract(m_charStreamProvider, _exception, _type)); +} + +void SourceReferenceFormatter::printExceptionInformation(util::Exception const& _exception, Error::Severity _severity) +{ + printExceptionInformation(SourceReferenceExtractor::extract(m_charStreamProvider, _exception, _severity)); } void SourceReferenceFormatter::printErrorInformation(ErrorList const& _errors) @@ -212,5 +214,11 @@ void SourceReferenceFormatter::printErrorInformation(ErrorList const& _errors) void SourceReferenceFormatter::printErrorInformation(Error const& _error) { - printExceptionInformation(SourceReferenceExtractor::extract(m_charStreamProvider, _error)); + SourceReferenceExtractor::Message message = + SourceReferenceExtractor::extract( + m_charStreamProvider, + _error, + Error::errorSeverity(_error.type()) + ); + printExceptionInformation(message); } diff --git a/liblangutil/SourceReferenceFormatter.h b/liblangutil/SourceReferenceFormatter.h index b44898915..def216efa 100644 --- a/liblangutil/SourceReferenceFormatter.h +++ b/liblangutil/SourceReferenceFormatter.h @@ -51,8 +51,9 @@ public: /// Prints source location if it is given. void printSourceLocation(SourceReference const& _ref); - void printExceptionInformation(SourceReferenceExtractor::Message const& _msg, bool _printFullType=false); - void printExceptionInformation(util::Exception const& _exception, Error::Type _type, bool _printFullType=false); + void printExceptionInformation(SourceReferenceExtractor::Message const& _msg); + void printExceptionInformation(util::Exception const& _exception, Error::Type _type); + void printExceptionInformation(util::Exception const& _exception, Error::Severity _severity); void printErrorInformation(langutil::ErrorList const& _errors); void printErrorInformation(Error const& _error); @@ -61,13 +62,26 @@ public: Error::Type _type, CharStreamProvider const& _charStreamProvider, bool _colored = false, - bool _withErrorIds = false, - bool _printFullType = false + bool _withErrorIds = false ) { std::ostringstream errorOutput; SourceReferenceFormatter formatter(errorOutput, _charStreamProvider, _colored, _withErrorIds); - formatter.printExceptionInformation(_exception, _type, _printFullType); + formatter.printExceptionInformation(_exception, _type); + return errorOutput.str(); + } + + static std::string formatExceptionInformation( + util::Exception const& _exception, + Error::Severity _severity, + CharStreamProvider const& _charStreamProvider, + bool _colored = false, + bool _withErrorIds = false + ) + { + std::ostringstream errorOutput; + SourceReferenceFormatter formatter(errorOutput, _charStreamProvider, _colored, _withErrorIds); + formatter.printExceptionInformation(_exception, _severity); return errorOutput.str(); } @@ -78,7 +92,7 @@ public: { return formatExceptionInformation( _error, - _error.type(), + Error::errorSeverity(_error.type()), _charStreamProvider ); } diff --git a/libsolidity/interface/StandardCompiler.cpp b/libsolidity/interface/StandardCompiler.cpp index f393694c6..551bd6af7 100644 --- a/libsolidity/interface/StandardCompiler.cpp +++ b/libsolidity/interface/StandardCompiler.cpp @@ -126,8 +126,7 @@ Json::Value formatErrorWithException( _type, _charStreamProvider, false, // colored - false, // _withErrorIds - true // _printFullType + false // _withErrorIds ); if (string const* description = _exception.comment()) diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index ebe76f8f0..ccaa8df94 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -765,7 +765,10 @@ void CommandLineInterface::compile() catch (CompilerError const& _exception) { m_hasOutput = true; - formatter.printExceptionInformation(_exception, Error::Type::CompilerError); + formatter.printExceptionInformation( + _exception, + Error::errorSeverity(Error::Type::CompilerError) + ); solThrow(CommandLineExecutionError, ""); } catch (Error const& _error) @@ -778,7 +781,7 @@ void CommandLineInterface::compile() else { m_hasOutput = true; - formatter.printExceptionInformation(_error, _error.type()); + formatter.printExceptionInformation(_error, Error::errorSeverity(_error.type())); solThrow(CommandLineExecutionError, ""); } } diff --git a/test/cmdlineTests/standard_urls_existing_and_missing/args b/test/cmdlineTests/standard_urls_existing_and_missing/args deleted file mode 100644 index a905f1fe6..000000000 --- a/test/cmdlineTests/standard_urls_existing_and_missing/args +++ /dev/null @@ -1 +0,0 @@ ---pretty-json --json-indent 4 diff --git a/test/cmdlineTests/standard_urls_existing_and_missing/output.json b/test/cmdlineTests/standard_urls_existing_and_missing/output.json index 2089fc90e..8cb0cc4c7 100644 --- a/test/cmdlineTests/standard_urls_existing_and_missing/output.json +++ b/test/cmdlineTests/standard_urls_existing_and_missing/output.json @@ -1,18 +1 @@ -{ - "errors": - [ - { - "component": "general", - "formattedMessage": "Cannot import url (\"standard_urls_existing_and_missing/non-existent-contract-1.sol\"): File not found. Searched the following locations: \"\".", - "message": "Cannot import url (\"standard_urls_existing_and_missing/non-existent-contract-1.sol\"): File not found. Searched the following locations: \"\".", - "severity": "warning", - "type": "Warning" - }], - "sources": - { - "url_not_found.sol": - { - "id": 0 - } - } -} +{"errors":[{"component":"general","formattedMessage":"Cannot import url (\"standard_urls_existing_and_missing/non-existent-contract-1.sol\"): File not found. Searched the following locations: \"\".","message":"Cannot import url (\"standard_urls_existing_and_missing/non-existent-contract-1.sol\"): File not found. Searched the following locations: \"\".","severity":"warning","type":"Warning"}],"sources":{"url_not_found.sol":{"id":0}}} diff --git a/test/cmdlineTests/standard_urls_missing/args b/test/cmdlineTests/standard_urls_missing/args deleted file mode 100644 index a905f1fe6..000000000 --- a/test/cmdlineTests/standard_urls_missing/args +++ /dev/null @@ -1 +0,0 @@ ---pretty-json --json-indent 4 diff --git a/test/cmdlineTests/standard_urls_missing/output.json b/test/cmdlineTests/standard_urls_missing/output.json index 49d93ab75..2bead4831 100644 --- a/test/cmdlineTests/standard_urls_missing/output.json +++ b/test/cmdlineTests/standard_urls_missing/output.json @@ -1,26 +1 @@ -{ - "errors": - [ - { - "component": "general", - "formattedMessage": "Cannot import url (\"standard_urls_missing/non-existent-contract-1.sol\"): File not found. Searched the following locations: \"\".", - "message": "Cannot import url (\"standard_urls_missing/non-existent-contract-1.sol\"): File not found. Searched the following locations: \"\".", - "severity": "error", - "type": "IOError" - }, - { - "component": "general", - "formattedMessage": "Cannot import url (\"standard_urls_missing/non-existent-contract.sol\"): File not found. Searched the following locations: \"\".", - "message": "Cannot import url (\"standard_urls_missing/non-existent-contract.sol\"): File not found. Searched the following locations: \"\".", - "severity": "error", - "type": "IOError" - }, - { - "component": "general", - "formattedMessage": "Cannot import url (\"standard_urls_missing/non-existent-contract-2.sol\"): File not found. Searched the following locations: \"\".", - "message": "Cannot import url (\"standard_urls_missing/non-existent-contract-2.sol\"): File not found. Searched the following locations: \"\".", - "severity": "error", - "type": "IOError" - }], - "sources": {} -} +{"errors":[{"component":"general","formattedMessage":"Cannot import url (\"standard_urls_missing/non-existent-contract-1.sol\"): File not found. Searched the following locations: \"\".","message":"Cannot import url (\"standard_urls_missing/non-existent-contract-1.sol\"): File not found. Searched the following locations: \"\".","severity":"error","type":"IOError"},{"component":"general","formattedMessage":"Cannot import url (\"standard_urls_missing/non-existent-contract.sol\"): File not found. Searched the following locations: \"\".","message":"Cannot import url (\"standard_urls_missing/non-existent-contract.sol\"): File not found. Searched the following locations: \"\".","severity":"error","type":"IOError"},{"component":"general","formattedMessage":"Cannot import url (\"standard_urls_missing/non-existent-contract-2.sol\"): File not found. Searched the following locations: \"\".","message":"Cannot import url (\"standard_urls_missing/non-existent-contract-2.sol\"): File not found. Searched the following locations: \"\".","severity":"error","type":"IOError"}],"sources":{}} diff --git a/test/tools/ossfuzz/yulProto_diff_ossfuzz.cpp b/test/tools/ossfuzz/yulProto_diff_ossfuzz.cpp index 395db4c0f..2335bfbf5 100644 --- a/test/tools/ossfuzz/yulProto_diff_ossfuzz.cpp +++ b/test/tools/ossfuzz/yulProto_diff_ossfuzz.cpp @@ -79,7 +79,7 @@ DEFINE_PROTO_FUZZER(Program const& _input) SourceReferenceFormatter formatter(std::cout, stack, false, false); for (auto const& error: stack.errors()) - formatter.printExceptionInformation(*error, error->type()); + formatter.printExceptionInformation(*error, Error::errorSeverity(error->type())); yulAssert(false, "Proto fuzzer generated malformed program"); }