refactored struct message to use std::variant for _typeOrSeverity

This commit is contained in:
nishant-sachdeva 2022-09-06 15:42:15 +05:30
parent c8011d8719
commit eafd7218b7
12 changed files with 80 additions and 73 deletions

View File

@ -37,6 +37,7 @@
#include <utility>
#include <vector>
#include <memory>
#include <variant>
namespace solidity::langutil
{
@ -228,6 +229,13 @@ public:
}
}
static constexpr Severity errorSeverityOrType(std::variant<Error::Type, Error::Severity> _typeOrSeverity)
{
if (std::holds_alternative<Error::Type>(_typeOrSeverity))
return errorSeverity(std::get<Error::Type>(_typeOrSeverity));
return std::get<Error::Severity>(_typeOrSeverity);
}
static bool isError(Severity _severity)
{
return _severity == Severity::Error;
@ -282,6 +290,13 @@ public:
util::unreachable();
}
static std::string formatTypeOrSeverity(std::variant<Error::Type, Error::Severity> _typeOrSeverity)
{
if (std::holds_alternative<Error::Type>(_typeOrSeverity))
return formatErrorType(std::get<Error::Type>(_typeOrSeverity));
return formatErrorSeverity(std::get<Error::Severity>(_typeOrSeverity));
}
static std::string formatErrorSeverityLowercase(Severity _severity)
{
std::string severityValue = formatErrorSeverity(_severity);

View File

@ -22,6 +22,7 @@
#include <algorithm>
#include <cmath>
#include <variant>
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<Error::Type, Error::Severity> _typeOrSeverity
)
{
SourceLocation const* location = boost::get_error_info<errinfo_sourceLocation>(_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<Error::Type, Error::Severity> _typeOrSeverity
)
{
Message message = extract(_charStreamProvider, _error, _error.type());
Message message = extract(_charStreamProvider, static_cast<util::Exception>(_error), _typeOrSeverity);
message.errorId = _error.errorId();
return message;
}

View File

@ -24,6 +24,7 @@
#include <string>
#include <tuple>
#include <vector>
#include <variant>
namespace solidity::langutil
{
@ -55,12 +56,21 @@ namespace SourceReferenceExtractor
struct Message
{
SourceReference primary;
Error::Type type;
std::variant<Error::Type, Error::Severity> _typeOrSeverity;
std::vector<SourceReference> secondary;
std::optional<ErrorId> errorId;
};
Message extract(CharStreamProvider const& _charStreamProvider, util::Exception const& _exception, Error::Type _type);
Message extract(
CharStreamProvider const& _charStreamProvider,
util::Exception const& _exception,
std::variant<Error::Type, Error::Severity> _typeOrSeverity
);
Message extract(
CharStreamProvider const& _charStreamProvider,
Error const& _error,
std::variant<Error::Type, Error::Severity> _typeOrSeverity
);
Message extract(CharStreamProvider const& _charStreamProvider, Error const& _error);
SourceReference extract(CharStreamProvider const& _charStreamProvider, SourceLocation const* _location, std::string message = "");
}

View File

@ -26,6 +26,7 @@
#include <libsolutil/UTF8.h>
#include <iomanip>
#include <string_view>
#include <variant>
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);
}

View File

@ -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
);
}

View File

@ -126,8 +126,7 @@ Json::Value formatErrorWithException(
_type,
_charStreamProvider,
false, // colored
false, // _withErrorIds
true // _printFullType
false // _withErrorIds
);
if (string const* description = _exception.comment())

View File

@ -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, "");
}
}

View File

@ -1 +0,0 @@
--pretty-json --json-indent 4

View File

@ -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}}}

View File

@ -1 +0,0 @@
--pretty-json --json-indent 4

View File

@ -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":{}}

View File

@ -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");
}