Merge pull request #9033 from a3d4/partfix-5819-add-errorid-to-error-class

Add unique ID to Error class
This commit is contained in:
chriseth 2020-05-28 16:24:47 +02:00 committed by GitHub
commit 65d8b6cf75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 136 additions and 118 deletions

View File

@ -28,8 +28,6 @@ using namespace std;
using namespace solidity; using namespace solidity;
using namespace solidity::langutil; using namespace solidity::langutil;
ErrorId solidity::langutil::operator"" _error(unsigned long long _error) { return ErrorId{ _error }; }
ErrorReporter& ErrorReporter::operator=(ErrorReporter const& _errorReporter) ErrorReporter& ErrorReporter::operator=(ErrorReporter const& _errorReporter)
{ {
if (&_errorReporter == this) if (&_errorReporter == this)
@ -62,12 +60,12 @@ void ErrorReporter::warning(
error(_error, Error::Type::Warning, _location, _secondaryLocation, _description); error(_error, Error::Type::Warning, _location, _secondaryLocation, _description);
} }
void ErrorReporter::error(ErrorId, Error::Type _type, SourceLocation const& _location, string const& _description) void ErrorReporter::error(ErrorId _errorId, Error::Type _type, SourceLocation const& _location, string const& _description)
{ {
if (checkForExcessiveErrors(_type)) if (checkForExcessiveErrors(_type))
return; return;
auto err = make_shared<Error>(_type); auto err = make_shared<Error>(_errorId, _type);
*err << *err <<
errinfo_sourceLocation(_location) << errinfo_sourceLocation(_location) <<
util::errinfo_comment(_description); util::errinfo_comment(_description);
@ -75,12 +73,12 @@ void ErrorReporter::error(ErrorId, Error::Type _type, SourceLocation const& _loc
m_errorList.push_back(err); m_errorList.push_back(err);
} }
void ErrorReporter::error(ErrorId, Error::Type _type, SourceLocation const& _location, SecondarySourceLocation const& _secondaryLocation, string const& _description) void ErrorReporter::error(ErrorId _errorId, Error::Type _type, SourceLocation const& _location, SecondarySourceLocation const& _secondaryLocation, string const& _description)
{ {
if (checkForExcessiveErrors(_type)) if (checkForExcessiveErrors(_type))
return; return;
auto err = make_shared<Error>(_type); auto err = make_shared<Error>(_errorId, _type);
*err << *err <<
errinfo_sourceLocation(_location) << errinfo_sourceLocation(_location) <<
errinfo_secondarySourceLocation(_secondaryLocation) << errinfo_secondarySourceLocation(_secondaryLocation) <<
@ -102,7 +100,7 @@ bool ErrorReporter::checkForExcessiveErrors(Error::Type _type)
if (m_warningCount == c_maxWarningsAllowed) if (m_warningCount == c_maxWarningsAllowed)
{ {
auto err = make_shared<Error>(Error::Type::Warning); auto err = make_shared<Error>(4591_error, Error::Type::Warning);
*err << util::errinfo_comment("There are more than 256 warnings. Ignoring the rest."); *err << util::errinfo_comment("There are more than 256 warnings. Ignoring the rest.");
m_errorList.push_back(err); m_errorList.push_back(err);
} }
@ -116,7 +114,7 @@ bool ErrorReporter::checkForExcessiveErrors(Error::Type _type)
if (m_errorCount > c_maxErrorsAllowed) if (m_errorCount > c_maxErrorsAllowed)
{ {
auto err = make_shared<Error>(Error::Type::Warning); auto err = make_shared<Error>(4013_error, Error::Type::Warning);
*err << util::errinfo_comment("There are more than 256 errors. Aborting."); *err << util::errinfo_comment("There are more than 256 errors. Aborting.");
m_errorList.push_back(err); m_errorList.push_back(err);
BOOST_THROW_EXCEPTION(FatalError()); BOOST_THROW_EXCEPTION(FatalError());

View File

@ -23,6 +23,7 @@
#pragma once #pragma once
#include <libsolutil/CommonData.h> #include <libsolutil/CommonData.h>
#include <libsolutil/Exceptions.h>
#include <liblangutil/Exceptions.h> #include <liblangutil/Exceptions.h>
#include <liblangutil/SourceLocation.h> #include <liblangutil/SourceLocation.h>
@ -33,17 +34,6 @@
namespace solidity::langutil namespace solidity::langutil
{ {
/**
* Unique identifiers are used to tag and track individual error cases.
* They are passed as the first parameter of error reporting functions.
* Suffix _error helps to find them in the sources.
* The struct ErrorId prevents incidental calls like typeError(3141) instead of typeError(3141_error).
* To create a new ID, one can add 0000_error and then run "python ./scripts/correct_error_ids.py"
* from the root of the repo.
*/
struct ErrorId { unsigned long long error = 0; };
ErrorId operator"" _error(unsigned long long error);
class ErrorReporter class ErrorReporter
{ {
public: public:

View File

@ -26,7 +26,8 @@ using namespace std;
using namespace solidity; using namespace solidity;
using namespace solidity::langutil; using namespace solidity::langutil;
Error::Error(Type _type, SourceLocation const& _location, string const& _description): Error::Error(ErrorId _errorId, Type _type, SourceLocation const& _location, string const& _description):
m_errorId(_errorId),
m_type(_type) m_type(_type)
{ {
switch (m_type) switch (m_type)
@ -57,8 +58,8 @@ Error::Error(Type _type, SourceLocation const& _location, string const& _descrip
*this << util::errinfo_comment(_description); *this << util::errinfo_comment(_description);
} }
Error::Error(Error::Type _type, std::string const& _description, SourceLocation const& _location): Error::Error(ErrorId _errorId, Error::Type _type, std::string const& _description, SourceLocation const& _location):
Error(_type) Error(_errorId, _type)
{ {
if (_location.isValid()) if (_location.isValid())
*this << errinfo_sourceLocation(_location); *this << errinfo_sourceLocation(_location);

View File

@ -56,6 +56,17 @@ struct InvalidAstError: virtual util::Exception {};
#define astAssert(CONDITION, DESCRIPTION) \ #define astAssert(CONDITION, DESCRIPTION) \
assertThrow(CONDITION, ::solidity::langutil::InvalidAstError, DESCRIPTION) assertThrow(CONDITION, ::solidity::langutil::InvalidAstError, DESCRIPTION)
/**
* Unique identifiers are used to tag and track individual error cases.
* They are passed as the first parameter of error reporting functions.
* Suffix _error helps to find them in the sources.
* The struct ErrorId prevents incidental calls like typeError(3141) instead of typeError(3141_error).
* To create a new ID, one can add 0000_error and then run "python ./scripts/correct_error_ids.py"
* from the root of the repo.
*/
struct ErrorId { unsigned long long error = 0; };
constexpr ErrorId operator"" _error(unsigned long long _error) { return ErrorId{ _error }; }
class Error: virtual public util::Exception class Error: virtual public util::Exception
{ {
public: public:
@ -69,14 +80,16 @@ public:
Warning Warning
}; };
explicit Error( Error(
ErrorId _errorId,
Type _type, Type _type,
SourceLocation const& _location = SourceLocation(), SourceLocation const& _location = SourceLocation(),
std::string const& _description = std::string() std::string const& _description = std::string()
); );
Error(Type _type, std::string const& _description, SourceLocation const& _location = SourceLocation()); Error(ErrorId _errorId, Type _type, std::string const& _description, SourceLocation const& _location = SourceLocation());
ErrorId errorId() const { return m_errorId; }
Type type() const { return m_type; } Type type() const { return m_type; }
std::string const& typeName() const { return m_typeName; } std::string const& typeName() const { return m_typeName; }
@ -100,6 +113,7 @@ public:
return true; return true;
} }
private: private:
ErrorId m_errorId;
Type m_type; Type m_type;
std::string m_typeName; std::string m_typeName;
}; };

View File

@ -172,8 +172,7 @@ void StorageOffsets::computeOffsets(TypePointers const& _types)
++slotOffset; ++slotOffset;
byteOffset = 0; byteOffset = 0;
} }
if (slotOffset >= bigint(1) << 256) solAssert(slotOffset < bigint(1) << 256 ,"Object too large for storage.");
BOOST_THROW_EXCEPTION(Error(Error::Type::TypeError) << util::errinfo_comment("Object too large for storage."));
offsets[i] = make_pair(u256(slotOffset), byteOffset); offsets[i] = make_pair(u256(slotOffset), byteOffset);
solAssert(type->storageSize() >= 1, "Invalid storage size."); solAssert(type->storageSize() >= 1, "Invalid storage size.");
if (type->storageSize() == 1 && byteOffset + type->storageBytes() <= 32) if (type->storageSize() == 1 && byteOffset + type->storageBytes() <= 32)
@ -186,8 +185,7 @@ void StorageOffsets::computeOffsets(TypePointers const& _types)
} }
if (byteOffset > 0) if (byteOffset > 0)
++slotOffset; ++slotOffset;
if (slotOffset >= bigint(1) << 256) solAssert(slotOffset < bigint(1) << 256, "Object too large for storage.");
BOOST_THROW_EXCEPTION(Error(Error::Type::TypeError) << util::errinfo_comment("Object too large for storage."));
m_storageSize = u256(slotOffset); m_storageSize = u256(slotOffset);
swap(m_offsets, offsets); swap(m_offsets, offsets);
} }
@ -1776,8 +1774,7 @@ u256 ArrayType::storageSize() const
} }
else else
size = bigint(length()) * baseType()->storageSize(); size = bigint(length()) * baseType()->storageSize();
if (size >= bigint(1) << 256) solAssert(size < bigint(1) << 256, "Array too large for storage.");
BOOST_THROW_EXCEPTION(Error(Error::Type::TypeError) << util::errinfo_comment("Array too large for storage."));
return max<u256>(1, u256(size)); return max<u256>(1, u256(size));
} }

View File

@ -81,17 +81,19 @@ vector<YulString> AsmAnalyzer::operator()(Literal const& _literal)
{ {
expectValidType(_literal.type, _literal.location); expectValidType(_literal.type, _literal.location);
if (_literal.kind == LiteralKind::String && _literal.value.str().size() > 32) if (_literal.kind == LiteralKind::String && _literal.value.str().size() > 32)
typeError( m_errorReporter.typeError(
3069_error,
_literal.location, _literal.location,
"String literal too long (" + to_string(_literal.value.str().size()) + " > 32)" "String literal too long (" + to_string(_literal.value.str().size()) + " > 32)"
); );
else if (_literal.kind == LiteralKind::Number && bigint(_literal.value.str()) > u256(-1)) else if (_literal.kind == LiteralKind::Number && bigint(_literal.value.str()) > u256(-1))
typeError(_literal.location, "Number literal too large (> 256 bits)"); m_errorReporter.typeError(6708_error, _literal.location, "Number literal too large (> 256 bits)");
else if (_literal.kind == LiteralKind::Boolean) else if (_literal.kind == LiteralKind::Boolean)
yulAssert(_literal.value == "true"_yulstring || _literal.value == "false"_yulstring, ""); yulAssert(_literal.value == "true"_yulstring || _literal.value == "false"_yulstring, "");
if (!m_dialect.validTypeForLiteral(_literal.kind, _literal.value, _literal.type)) if (!m_dialect.validTypeForLiteral(_literal.kind, _literal.value, _literal.type))
typeError( m_errorReporter.typeError(
5170_error,
_literal.location, _literal.location,
"Invalid type \"" + _literal.type.str() + "\" for literal \"" + _literal.value.str() + "\"." "Invalid type \"" + _literal.type.str() + "\" for literal \"" + _literal.value.str() + "\"."
); );
@ -110,7 +112,8 @@ vector<YulString> AsmAnalyzer::operator()(Identifier const& _identifier)
[&](Scope::Variable const& _var) [&](Scope::Variable const& _var)
{ {
if (!m_activeVariables.count(&_var)) if (!m_activeVariables.count(&_var))
declarationError( m_errorReporter.declarationError(
4990_error,
_identifier.location, _identifier.location,
"Variable " + _identifier.name.str() + " used before it was declared." "Variable " + _identifier.name.str() + " used before it was declared."
); );
@ -118,7 +121,8 @@ vector<YulString> AsmAnalyzer::operator()(Identifier const& _identifier)
}, },
[&](Scope::Function const&) [&](Scope::Function const&)
{ {
typeError( m_errorReporter.typeError(
6041_error,
_identifier.location, _identifier.location,
"Function " + _identifier.name.str() + " used without being called." "Function " + _identifier.name.str() + " used without being called."
); );
@ -141,7 +145,7 @@ vector<YulString> AsmAnalyzer::operator()(Identifier const& _identifier)
} }
if (!found && watcher.ok()) if (!found && watcher.ok())
// Only add an error message if the callback did not do it. // Only add an error message if the callback did not do it.
declarationError(_identifier.location, "Identifier not found."); m_errorReporter.declarationError(8198_error, _identifier.location, "Identifier not found.");
} }
return {type}; return {type};
@ -152,7 +156,9 @@ void AsmAnalyzer::operator()(ExpressionStatement const& _statement)
auto watcher = m_errorReporter.errorWatcher(); auto watcher = m_errorReporter.errorWatcher();
vector<YulString> types = std::visit(*this, _statement.expression); vector<YulString> types = std::visit(*this, _statement.expression);
if (watcher.ok() && !types.empty()) if (watcher.ok() && !types.empty())
typeError(_statement.location, m_errorReporter.typeError(
3083_error,
_statement.location,
"Top-level expressions are not supposed to return values (this expression returns " + "Top-level expressions are not supposed to return values (this expression returns " +
to_string(types.size()) + to_string(types.size()) +
" value" + " value" +
@ -170,7 +176,8 @@ void AsmAnalyzer::operator()(Assignment const& _assignment)
vector<YulString> types = std::visit(*this, *_assignment.value); vector<YulString> types = std::visit(*this, *_assignment.value);
if (types.size() != numVariables) if (types.size() != numVariables)
declarationError( m_errorReporter.declarationError(
8678_error,
_assignment.location, _assignment.location,
"Variable count does not match number of values (" + "Variable count does not match number of values (" +
to_string(numVariables) + to_string(numVariables) +
@ -202,7 +209,9 @@ void AsmAnalyzer::operator()(VariableDeclaration const& _varDecl)
{ {
vector<YulString> types = std::visit(*this, *_varDecl.value); vector<YulString> types = std::visit(*this, *_varDecl.value);
if (types.size() != numVariables) if (types.size() != numVariables)
declarationError(_varDecl.location, m_errorReporter.declarationError(
3812_error,
_varDecl.location,
"Variable count mismatch: " + "Variable count mismatch: " +
to_string(numVariables) + to_string(numVariables) +
" variables and " + " variables and " +
@ -217,7 +226,8 @@ void AsmAnalyzer::operator()(VariableDeclaration const& _varDecl)
givenType = types[i]; givenType = types[i];
TypedName const& variable = _varDecl.variables[i]; TypedName const& variable = _varDecl.variables[i];
if (variable.type != givenType) if (variable.type != givenType)
typeError( m_errorReporter.typeError(
3947_error,
variable.location, variable.location,
"Assigning value of type \"" + givenType.str() + "\" to variable of type \"" + variable.type.str() + "." "Assigning value of type \"" + givenType.str() + "\" to variable of type \"" + variable.type.str() + "."
); );
@ -265,7 +275,8 @@ vector<YulString> AsmAnalyzer::operator()(FunctionCall const& _funCall)
else if (!m_currentScope->lookup(_funCall.functionName.name, GenericVisitor{ else if (!m_currentScope->lookup(_funCall.functionName.name, GenericVisitor{
[&](Scope::Variable const&) [&](Scope::Variable const&)
{ {
typeError( m_errorReporter.typeError(
4202_error,
_funCall.functionName.location, _funCall.functionName.location,
"Attempt to call variable instead of function." "Attempt to call variable instead of function."
); );
@ -278,12 +289,13 @@ vector<YulString> AsmAnalyzer::operator()(FunctionCall const& _funCall)
})) }))
{ {
if (!warnOnInstructions(_funCall)) if (!warnOnInstructions(_funCall))
declarationError(_funCall.functionName.location, "Function not found."); m_errorReporter.declarationError(4619_error, _funCall.functionName.location, "Function not found.");
yulAssert(!watcher.ok(), "Expected a reported error."); yulAssert(!watcher.ok(), "Expected a reported error.");
} }
if (parameterTypes && _funCall.arguments.size() != parameterTypes->size()) if (parameterTypes && _funCall.arguments.size() != parameterTypes->size())
typeError( m_errorReporter.typeError(
7000_error,
_funCall.functionName.location, _funCall.functionName.location,
"Function expects " + "Function expects " +
to_string(parameterTypes->size()) + to_string(parameterTypes->size()) +
@ -301,7 +313,8 @@ vector<YulString> AsmAnalyzer::operator()(FunctionCall const& _funCall)
if (needsLiteralArguments && (*needsLiteralArguments)[i - 1]) if (needsLiteralArguments && (*needsLiteralArguments)[i - 1])
{ {
if (!holds_alternative<Literal>(arg)) if (!holds_alternative<Literal>(arg))
typeError( m_errorReporter.typeError(
9114_error,
_funCall.functionName.location, _funCall.functionName.location,
"Function expects direct literals as arguments." "Function expects direct literals as arguments."
); );
@ -310,7 +323,8 @@ vector<YulString> AsmAnalyzer::operator()(FunctionCall const& _funCall)
_funCall.functionName.name.str() == "dataoffset" _funCall.functionName.name.str() == "dataoffset"
) )
if (!m_dataNames.count(std::get<Literal>(arg).value)) if (!m_dataNames.count(std::get<Literal>(arg).value))
typeError( m_errorReporter.typeError(
3517_error,
_funCall.functionName.location, _funCall.functionName.location,
"Unknown data object \"" + std::get<Literal>(arg).value.str() + "\"." "Unknown data object \"" + std::get<Literal>(arg).value.str() + "\"."
); );
@ -362,7 +376,7 @@ void AsmAnalyzer::operator()(Switch const& _switch)
/// Note: the parser ensures there is only one default case /// Note: the parser ensures there is only one default case
if (watcher.ok() && !cases.insert(valueOfLiteral(*_case.value)).second) if (watcher.ok() && !cases.insert(valueOfLiteral(*_case.value)).second)
declarationError(_case.location, "Duplicate case defined."); m_errorReporter.declarationError(6792_error, _case.location, "Duplicate case defined.");
} }
(*this)(_case.body); (*this)(_case.body);
@ -408,7 +422,8 @@ YulString AsmAnalyzer::expectExpression(Expression const& _expr)
{ {
vector<YulString> types = std::visit(*this, _expr); vector<YulString> types = std::visit(*this, _expr);
if (types.size() != 1) if (types.size() != 1)
typeError( m_errorReporter.typeError(
3950_error,
locationOf(_expr), locationOf(_expr),
"Expected expression to evaluate to one value, but got " + "Expected expression to evaluate to one value, but got " +
to_string(types.size()) + to_string(types.size()) +
@ -421,7 +436,9 @@ void AsmAnalyzer::expectBoolExpression(Expression const& _expr)
{ {
YulString type = expectExpression(_expr); YulString type = expectExpression(_expr);
if (type != m_dialect.boolType) if (type != m_dialect.boolType)
typeError(locationOf(_expr), m_errorReporter.typeError(
1733_error,
locationOf(_expr),
"Expected a value of boolean type \"" + "Expected a value of boolean type \"" +
m_dialect.boolType.str() + m_dialect.boolType.str() +
"\" but got \"" + "\" but got \"" +
@ -440,9 +457,10 @@ void AsmAnalyzer::checkAssignment(Identifier const& _variable, YulString _valueT
{ {
// Check that it is a variable // Check that it is a variable
if (!holds_alternative<Scope::Variable>(*var)) if (!holds_alternative<Scope::Variable>(*var))
typeError(_variable.location, "Assignment requires variable."); m_errorReporter.typeError(2657_error, _variable.location, "Assignment requires variable.");
else if (!m_activeVariables.count(&std::get<Scope::Variable>(*var))) else if (!m_activeVariables.count(&std::get<Scope::Variable>(*var)))
declarationError( m_errorReporter.declarationError(
1133_error,
_variable.location, _variable.location,
"Variable " + _variable.name.str() + " used before it was declared." "Variable " + _variable.name.str() + " used before it was declared."
); );
@ -464,9 +482,11 @@ void AsmAnalyzer::checkAssignment(Identifier const& _variable, YulString _valueT
if (!found && watcher.ok()) if (!found && watcher.ok())
// Only add message if the callback did not. // Only add message if the callback did not.
declarationError(_variable.location, "Variable not found or variable not lvalue."); m_errorReporter.declarationError(4634_error, _variable.location, "Variable not found or variable not lvalue.");
if (variableType && *variableType != _valueType) if (variableType && *variableType != _valueType)
typeError(_variable.location, m_errorReporter.typeError(
9547_error,
_variable.location,
"Assigning a value of type \"" + "Assigning a value of type \"" +
_valueType.str() + _valueType.str() +
"\" to a variable of type \"" + "\" to a variable of type \"" +
@ -488,7 +508,8 @@ Scope& AsmAnalyzer::scope(Block const* _block)
void AsmAnalyzer::expectValidType(YulString _type, SourceLocation const& _location) void AsmAnalyzer::expectValidType(YulString _type, SourceLocation const& _location)
{ {
if (!m_dialect.types.count(_type)) if (!m_dialect.types.count(_type))
typeError( m_errorReporter.typeError(
5473_error,
_location, _location,
"\"" + _type.str() + "\" is not a valid type (user defined types are not yet supported)." "\"" + _type.str() + "\" is not a valid type (user defined types are not yet supported)."
); );
@ -497,7 +518,9 @@ void AsmAnalyzer::expectValidType(YulString _type, SourceLocation const& _locati
void AsmAnalyzer::expectType(YulString _expectedType, YulString _givenType, SourceLocation const& _location) void AsmAnalyzer::expectType(YulString _expectedType, YulString _givenType, SourceLocation const& _location)
{ {
if (_expectedType != _givenType) if (_expectedType != _givenType)
typeError(_location, m_errorReporter.typeError(
3781_error,
_location,
"Expected a value of type \"" + "Expected a value of type \"" +
_expectedType.str() + _expectedType.str() +
"\" but got \"" + "\" but got \"" +
@ -524,7 +547,8 @@ bool AsmAnalyzer::warnOnInstructions(evmasm::Instruction _instr, SourceLocation
yulAssert(m_evmVersion.hasBitwiseShifting() == m_evmVersion.hasCreate2(), ""); yulAssert(m_evmVersion.hasBitwiseShifting() == m_evmVersion.hasCreate2(), "");
auto errorForVM = [&](string const& vmKindMessage) { auto errorForVM = [&](string const& vmKindMessage) {
typeError( m_errorReporter.typeError(
7079_error,
_location, _location,
"The \"" + "The \"" +
boost::to_lower_copy(instructionInfo(_instr).name) boost::to_lower_copy(instructionInfo(_instr).name)
@ -584,13 +608,3 @@ bool AsmAnalyzer::warnOnInstructions(evmasm::Instruction _instr, SourceLocation
return true; return true;
} }
void AsmAnalyzer::typeError(SourceLocation const& _location, string const& _description)
{
m_errorReporter.typeError(7569_error, _location, _description);
}
void AsmAnalyzer::declarationError(SourceLocation const& _location, string const& _description)
{
m_errorReporter.declarationError(9595_error, _location, _description);
}

View File

@ -117,9 +117,6 @@ private:
return warnOnInstructions(_functionCall.functionName.name.str(), _functionCall.functionName.location); return warnOnInstructions(_functionCall.functionName.name.str(), _functionCall.functionName.location);
} }
void typeError(langutil::SourceLocation const& _location, std::string const& _description);
void declarationError(langutil::SourceLocation const& _location, std::string const& _description);
yul::ExternalIdentifierAccess::Resolver m_resolver; yul::ExternalIdentifierAccess::Resolver m_resolver;
Scope* m_currentScope = nullptr; Scope* m_currentScope = nullptr;
/// Variables that are active at the current point in assembly (as opposed to /// Variables that are active at the current point in assembly (as opposed to

View File

@ -81,7 +81,7 @@ bytes BytesUtils::convertBoolean(string const& _literal)
else if (_literal == "false") else if (_literal == "false")
return bytes{false}; return bytes{false};
else else
throw Error(Error::Type::ParserError, "Boolean literal invalid."); throw TestParserError("Boolean literal invalid.");
} }
bytes BytesUtils::convertNumber(string const& _literal) bytes BytesUtils::convertNumber(string const& _literal)
@ -92,7 +92,7 @@ bytes BytesUtils::convertNumber(string const& _literal)
} }
catch (std::exception const&) catch (std::exception const&)
{ {
throw Error(Error::Type::ParserError, "Number encoding invalid."); throw TestParserError("Number encoding invalid.");
} }
} }
@ -104,7 +104,7 @@ bytes BytesUtils::convertHexNumber(string const& _literal)
} }
catch (std::exception const&) catch (std::exception const&)
{ {
throw Error(Error::Type::ParserError, "Hex number encoding invalid."); throw TestParserError("Hex number encoding invalid.");
} }
} }
@ -116,7 +116,7 @@ bytes BytesUtils::convertString(string const& _literal)
} }
catch (std::exception const&) catch (std::exception const&)
{ {
throw Error(Error::Type::ParserError, "String encoding invalid."); throw TestParserError("String encoding invalid.");
} }
} }

View File

@ -30,6 +30,15 @@ namespace solidity::frontend::test
while (false) while (false)
class TestParserError: virtual public util::Exception
{
public:
explicit TestParserError(std::string const& _description)
{
*this << util::errinfo_comment(_description);
}
};
/** /**
* Representation of a notice, warning or error that can occur while * Representation of a notice, warning or error that can occur while
* formatting and therefore updating an interactive function call test. * formatting and therefore updating an interactive function call test.

View File

@ -18,6 +18,7 @@
#include <test/libsolidity/util/TestFileParser.h> #include <test/libsolidity/util/TestFileParser.h>
#include <test/libsolidity/util/BytesUtils.h> #include <test/libsolidity/util/BytesUtils.h>
#include <test/libsolidity/util/SoltestErrors.h>
#include <test/Common.h> #include <test/Common.h>
#include <liblangutil/Common.h> #include <liblangutil/Common.h>
@ -128,9 +129,9 @@ vector<solidity::frontend::test::FunctionCall> TestFileParser::parseFunctionCall
calls.emplace_back(std::move(call)); calls.emplace_back(std::move(call));
} }
catch (Error const& _e) catch (TestParserError const& _e)
{ {
throw Error{_e.type(), "Line " + to_string(_lineOffset + m_lineNumber) + ": " + _e.what()}; throw TestParserError("Line " + to_string(_lineOffset + m_lineNumber) + ": " + _e.what());
} }
} }
} }
@ -150,8 +151,7 @@ bool TestFileParser::accept(soltest::Token _token, bool const _expect)
bool TestFileParser::expect(soltest::Token _token, bool const _advance) bool TestFileParser::expect(soltest::Token _token, bool const _advance)
{ {
if (m_scanner.currentToken() != _token || m_scanner.currentToken() == Token::Invalid) if (m_scanner.currentToken() != _token || m_scanner.currentToken() == Token::Invalid)
throw Error( throw TestParserError(
Error::Type::ParserError,
"Unexpected " + formatToken(m_scanner.currentToken()) + ": \"" + "Unexpected " + formatToken(m_scanner.currentToken()) + ": \"" +
m_scanner.currentLiteral() + "\". " + m_scanner.currentLiteral() + "\". " +
"Expected \"" + formatToken(_token) + "\"." "Expected \"" + formatToken(_token) + "\"."
@ -187,10 +187,10 @@ pair<string, bool> TestFileParser::parseFunctionSignature()
parameters += parseIdentifierOrTuple(); parameters += parseIdentifierOrTuple();
} }
if (accept(Token::Arrow, true)) if (accept(Token::Arrow, true))
throw Error(Error::Type::ParserError, "Invalid signature detected: " + signature); throw TestParserError("Invalid signature detected: " + signature);
if (!hasName && !parameters.empty()) if (!hasName && !parameters.empty())
throw Error(Error::Type::ParserError, "Signatures without a name cannot have parameters: " + signature); throw TestParserError("Signatures without a name cannot have parameters: " + signature);
else else
signature += parameters; signature += parameters;
@ -207,7 +207,7 @@ FunctionValue TestFileParser::parseFunctionCallValue()
u256 value{ parseDecimalNumber() }; u256 value{ parseDecimalNumber() };
Token token = m_scanner.currentToken(); Token token = m_scanner.currentToken();
if (token != Token::Ether && token != Token::Wei) if (token != Token::Ether && token != Token::Wei)
throw Error(Error::Type::ParserError, "Invalid value unit provided. Coins can be wei or ether."); throw TestParserError("Invalid value unit provided. Coins can be wei or ether.");
m_scanner.scanNextToken(); m_scanner.scanNextToken();
@ -216,7 +216,7 @@ FunctionValue TestFileParser::parseFunctionCallValue()
} }
catch (std::exception const&) catch (std::exception const&)
{ {
throw Error(Error::Type::ParserError, "Ether value encoding invalid."); throw TestParserError("Ether value encoding invalid.");
} }
} }
@ -226,7 +226,7 @@ FunctionCallArgs TestFileParser::parseFunctionCallArguments()
auto param = parseParameter(); auto param = parseParameter();
if (param.abiType.type == ABIType::None) if (param.abiType.type == ABIType::None)
throw Error(Error::Type::ParserError, "No argument provided."); throw TestParserError("No argument provided.");
arguments.parameters.emplace_back(param); arguments.parameters.emplace_back(param);
while (accept(Token::Comma, true)) while (accept(Token::Comma, true))
@ -290,7 +290,7 @@ Parameter TestFileParser::parseParameter()
if (accept(Token::Boolean)) if (accept(Token::Boolean))
{ {
if (isSigned) if (isSigned)
throw Error(Error::Type::ParserError, "Invalid boolean literal."); throw TestParserError("Invalid boolean literal.");
parameter.abiType = ABIType{ABIType::Boolean, ABIType::AlignRight, 32}; parameter.abiType = ABIType{ABIType::Boolean, ABIType::AlignRight, 32};
string parsed = parseBoolean(); string parsed = parseBoolean();
@ -304,7 +304,7 @@ Parameter TestFileParser::parseParameter()
else if (accept(Token::HexNumber)) else if (accept(Token::HexNumber))
{ {
if (isSigned) if (isSigned)
throw Error(Error::Type::ParserError, "Invalid hex number literal."); throw TestParserError("Invalid hex number literal.");
parameter.abiType = ABIType{ABIType::Hex, ABIType::AlignRight, 32}; parameter.abiType = ABIType{ABIType::Hex, ABIType::AlignRight, 32};
string parsed = parseHexNumber(); string parsed = parseHexNumber();
@ -318,9 +318,9 @@ Parameter TestFileParser::parseParameter()
else if (accept(Token::Hex, true)) else if (accept(Token::Hex, true))
{ {
if (isSigned) if (isSigned)
throw Error(Error::Type::ParserError, "Invalid hex string literal."); throw TestParserError("Invalid hex string literal.");
if (parameter.alignment != Parameter::Alignment::None) if (parameter.alignment != Parameter::Alignment::None)
throw Error(Error::Type::ParserError, "Hex string literals cannot be aligned or padded."); throw TestParserError("Hex string literals cannot be aligned or padded.");
string parsed = parseString(); string parsed = parseString();
parameter.rawString += "hex\"" + parsed + "\""; parameter.rawString += "hex\"" + parsed + "\"";
@ -332,9 +332,9 @@ Parameter TestFileParser::parseParameter()
else if (accept(Token::String)) else if (accept(Token::String))
{ {
if (isSigned) if (isSigned)
throw Error(Error::Type::ParserError, "Invalid string literal."); throw TestParserError("Invalid string literal.");
if (parameter.alignment != Parameter::Alignment::None) if (parameter.alignment != Parameter::Alignment::None)
throw Error(Error::Type::ParserError, "String literals cannot be aligned or padded."); throw TestParserError("String literals cannot be aligned or padded.");
string parsed = parseString(); string parsed = parseString();
parameter.abiType = ABIType{ABIType::String, ABIType::AlignLeft, parsed.size()}; parameter.abiType = ABIType{ABIType::String, ABIType::AlignLeft, parsed.size()};
@ -364,7 +364,7 @@ Parameter TestFileParser::parseParameter()
else if (accept(Token::Failure, true)) else if (accept(Token::Failure, true))
{ {
if (isSigned) if (isSigned)
throw Error(Error::Type::ParserError, "Invalid failure literal."); throw TestParserError("Invalid failure literal.");
parameter.abiType = ABIType{ABIType::Failure, ABIType::AlignRight, 0}; parameter.abiType = ABIType{ABIType::Failure, ABIType::AlignRight, 0};
parameter.rawBytes = bytes{}; parameter.rawBytes = bytes{};
@ -555,10 +555,7 @@ void TestFileParser::Scanner::scanNextToken()
else if (isEndOfLine()) else if (isEndOfLine())
token = make_pair(Token::EOS, "EOS"); token = make_pair(Token::EOS, "EOS");
else else
throw Error( throw TestParserError("Unexpected character: '" + string{current()} + "'");
Error::Type::ParserError,
"Unexpected character: '" + string{current()} + "'"
);
break; break;
} }
} }
@ -651,7 +648,7 @@ string TestFileParser::Scanner::scanString()
str += scanHexPart(); str += scanHexPart();
break; break;
default: default:
throw Error(Error::Type::ParserError, "Invalid or escape sequence found in string literal."); throw TestParserError("Invalid or escape sequence found in string literal.");
} }
} }
else else
@ -673,7 +670,7 @@ char TestFileParser::Scanner::scanHexPart()
else if (tolower(current()) >= 'a' && tolower(current()) <= 'f') else if (tolower(current()) >= 'a' && tolower(current()) <= 'f')
value = tolower(current()) - 'a' + 10; value = tolower(current()) - 'a' + 10;
else else
throw Error(Error::Type::ParserError, "\\x used with no following hex digits."); throw TestParserError("\\x used with no following hex digits.");
advance(); advance();
if (current() == '"') if (current() == '"')

View File

@ -25,6 +25,7 @@
#include <liblangutil/Exceptions.h> #include <liblangutil/Exceptions.h>
#include <test/ExecutionFramework.h> #include <test/ExecutionFramework.h>
#include <test/libsolidity/util/SoltestErrors.h>
#include <test/libsolidity/util/TestFileParser.h> #include <test/libsolidity/util/TestFileParser.h>
using namespace std; using namespace std;
@ -365,7 +366,7 @@ BOOST_AUTO_TEST_CASE(scanner_hex_values_invalid1)
char const* source = R"( char const* source = R"(
// f(uint256): "\x" -> // f(uint256): "\x" ->
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(scanner_hex_values_invalid2) BOOST_AUTO_TEST_CASE(scanner_hex_values_invalid2)
@ -383,7 +384,7 @@ BOOST_AUTO_TEST_CASE(scanner_hex_values_invalid3)
char const* source = R"( char const* source = R"(
// f(uint256): "\xZ" -> // f(uint256): "\xZ" ->
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(scanner_hex_values_invalid4) BOOST_AUTO_TEST_CASE(scanner_hex_values_invalid4)
@ -391,7 +392,7 @@ BOOST_AUTO_TEST_CASE(scanner_hex_values_invalid4)
char const* source = R"( char const* source = R"(
// f(uint256): "\xZZ" -> // f(uint256): "\xZZ" ->
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arguments_hex_string) BOOST_AUTO_TEST_CASE(call_arguments_hex_string)
@ -741,7 +742,7 @@ BOOST_AUTO_TEST_CASE(call_arguments_hex_string_left_align)
char const* source = R"( char const* source = R"(
// f(bytes): left(hex"4200ef") -> // f(bytes): left(hex"4200ef") ->
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arguments_hex_string_right_align) BOOST_AUTO_TEST_CASE(call_arguments_hex_string_right_align)
@ -749,7 +750,7 @@ BOOST_AUTO_TEST_CASE(call_arguments_hex_string_right_align)
char const* source = R"( char const* source = R"(
// f(bytes): right(hex"4200ef") -> // f(bytes): right(hex"4200ef") ->
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_newline_invalid) BOOST_AUTO_TEST_CASE(call_newline_invalid)
@ -757,7 +758,7 @@ BOOST_AUTO_TEST_CASE(call_newline_invalid)
char const* source = R"( char const* source = R"(
/ /
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_invalid) BOOST_AUTO_TEST_CASE(call_invalid)
@ -765,7 +766,7 @@ BOOST_AUTO_TEST_CASE(call_invalid)
char const* source = R"( char const* source = R"(
/ f() -> / f() ->
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_signature_invalid) BOOST_AUTO_TEST_CASE(call_signature_invalid)
@ -773,7 +774,7 @@ BOOST_AUTO_TEST_CASE(call_signature_invalid)
char const* source = R"( char const* source = R"(
// f(uint8,) -> FAILURE // f(uint8,) -> FAILURE
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arguments_tuple_invalid) BOOST_AUTO_TEST_CASE(call_arguments_tuple_invalid)
@ -781,7 +782,7 @@ BOOST_AUTO_TEST_CASE(call_arguments_tuple_invalid)
char const* source = R"( char const* source = R"(
// f((uint8,) -> FAILURE // f((uint8,) -> FAILURE
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arguments_tuple_invalid_empty) BOOST_AUTO_TEST_CASE(call_arguments_tuple_invalid_empty)
@ -789,7 +790,7 @@ BOOST_AUTO_TEST_CASE(call_arguments_tuple_invalid_empty)
char const* source = R"( char const* source = R"(
// f(uint8, ()) -> FAILURE // f(uint8, ()) -> FAILURE
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arguments_tuple_invalid_parantheses) BOOST_AUTO_TEST_CASE(call_arguments_tuple_invalid_parantheses)
@ -797,14 +798,14 @@ BOOST_AUTO_TEST_CASE(call_arguments_tuple_invalid_parantheses)
char const* source = R"( char const* source = R"(
// f((uint8,() -> FAILURE // f((uint8,() -> FAILURE
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_ether_value_expectations_missing) BOOST_AUTO_TEST_CASE(call_ether_value_expectations_missing)
{ {
char const* source = R"( char const* source = R"(
// f(), 0)"; // f(), 0)";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arguments_invalid) BOOST_AUTO_TEST_CASE(call_arguments_invalid)
@ -812,7 +813,7 @@ BOOST_AUTO_TEST_CASE(call_arguments_invalid)
char const* source = R"( char const* source = R"(
// f(uint256): abc -> 1 // f(uint256): abc -> 1
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arguments_invalid_decimal) BOOST_AUTO_TEST_CASE(call_arguments_invalid_decimal)
@ -820,7 +821,7 @@ BOOST_AUTO_TEST_CASE(call_arguments_invalid_decimal)
char const* source = R"( char const* source = R"(
// sig(): 0.h3 -> // sig(): 0.h3 ->
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_ether_value_invalid) BOOST_AUTO_TEST_CASE(call_ether_value_invalid)
@ -828,7 +829,7 @@ BOOST_AUTO_TEST_CASE(call_ether_value_invalid)
char const* source = R"( char const* source = R"(
// f(uint256), abc : 1 -> 1 // f(uint256), abc : 1 -> 1
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_ether_value_invalid_decimal) BOOST_AUTO_TEST_CASE(call_ether_value_invalid_decimal)
@ -836,7 +837,7 @@ BOOST_AUTO_TEST_CASE(call_ether_value_invalid_decimal)
char const* source = R"( char const* source = R"(
// sig(): 0.1hd ether -> // sig(): 0.1hd ether ->
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_ether_type_invalid) BOOST_AUTO_TEST_CASE(call_ether_type_invalid)
@ -844,7 +845,7 @@ BOOST_AUTO_TEST_CASE(call_ether_type_invalid)
char const* source = R"( char const* source = R"(
// f(uint256), 2 btc : 1 -> 1 // f(uint256), 2 btc : 1 -> 1
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_signed_bool_invalid) BOOST_AUTO_TEST_CASE(call_signed_bool_invalid)
@ -852,7 +853,7 @@ BOOST_AUTO_TEST_CASE(call_signed_bool_invalid)
char const* source = R"( char const* source = R"(
// f() -> -true // f() -> -true
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_signed_failure_invalid) BOOST_AUTO_TEST_CASE(call_signed_failure_invalid)
@ -860,7 +861,7 @@ BOOST_AUTO_TEST_CASE(call_signed_failure_invalid)
char const* source = R"( char const* source = R"(
// f() -> -FAILURE // f() -> -FAILURE
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_signed_hex_number_invalid) BOOST_AUTO_TEST_CASE(call_signed_hex_number_invalid)
@ -868,7 +869,7 @@ BOOST_AUTO_TEST_CASE(call_signed_hex_number_invalid)
char const* source = R"( char const* source = R"(
// f() -> -0x42 // f() -> -0x42
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arguments_colon) BOOST_AUTO_TEST_CASE(call_arguments_colon)
@ -877,7 +878,7 @@ BOOST_AUTO_TEST_CASE(call_arguments_colon)
// h256(): // h256():
// -> 1 // -> 1
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arguments_newline_colon) BOOST_AUTO_TEST_CASE(call_arguments_newline_colon)
@ -887,7 +888,7 @@ BOOST_AUTO_TEST_CASE(call_arguments_newline_colon)
// : // :
// -> 1 // -> 1
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arrow_missing) BOOST_AUTO_TEST_CASE(call_arrow_missing)
@ -895,7 +896,7 @@ BOOST_AUTO_TEST_CASE(call_arrow_missing)
char const* source = R"( char const* source = R"(
// h256() FAILURE // h256() FAILURE
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_unexpected_character) BOOST_AUTO_TEST_CASE(call_unexpected_character)
@ -903,7 +904,7 @@ BOOST_AUTO_TEST_CASE(call_unexpected_character)
char const* source = R"( char const* source = R"(
// f() -> ?? // f() -> ??
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(constructor) BOOST_AUTO_TEST_CASE(constructor)