Add errorId to Error class

This commit is contained in:
a3d4 2020-05-28 04:32:46 +02:00
parent be83c54d79
commit c959341720
5 changed files with 30 additions and 30 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));
} }