aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian <c@ethdev.com>2014-10-24 01:22:30 +0800
committerChristian <c@ethdev.com>2014-10-24 03:37:57 +0800
commit094ee44f721054518ff384aef7cde1afe649636d (patch)
tree4b6935c867fd01c70ea8385778c2017067ec4dd6
parent781d7fd5149f6e24d60cd0327841ca9f2fca23af (diff)
downloaddexon-solidity-094ee44f721054518ff384aef7cde1afe649636d.tar.gz
dexon-solidity-094ee44f721054518ff384aef7cde1afe649636d.tar.zst
dexon-solidity-094ee44f721054518ff384aef7cde1afe649636d.zip
Use boost errinfo.
-rw-r--r--AST.cpp34
-rw-r--r--AST.h9
-rw-r--r--Exceptions.h42
-rw-r--r--NameAndTypeResolver.cpp12
-rw-r--r--Parser.cpp20
-rw-r--r--Parser.h5
-rw-r--r--SourceReferenceFormatter.cpp96
-rw-r--r--SourceReferenceFormatter.h (renamed from Exceptions.cpp)29
8 files changed, 164 insertions, 83 deletions
diff --git a/AST.cpp b/AST.cpp
index 0671ecd7..50c53bf3 100644
--- a/AST.cpp
+++ b/AST.cpp
@@ -248,12 +248,17 @@ void Literal::accept(ASTVisitor& _visitor)
_visitor.endVisit(*this);
}
+TypeError ASTNode::createTypeError(std::string const& _description)
+{
+ return TypeError() << errinfo_sourceLocation(getLocation()) << errinfo_comment(_description);
+}
+
void Statement::expectType(Expression& _expression, const Type& _expectedType)
{
_expression.checkTypeRequirements();
if (!_expression.getType()->isImplicitlyConvertibleTo(_expectedType))
- BOOST_THROW_EXCEPTION(TypeError(_expression.getLocation(),
- "Type not implicitly convertible to expected type."));
+ BOOST_THROW_EXCEPTION(TypeError() << errinfo_sourceLocation(_expression.getLocation())
+ << errinfo_comment("Type not implicitly convertible to expected type."));
//@todo provide more information to the exception
}
@@ -289,9 +294,8 @@ void Return::checkTypeRequirements()
{
BOOST_ASSERT(m_returnParameters != nullptr);
if (m_returnParameters->getParameters().size() != 1)
- BOOST_THROW_EXCEPTION(TypeError(getLocation(), "Different number of arguments in "
- "return statement than in returns "
- "declaration."));
+ BOOST_THROW_EXCEPTION(createTypeError("Different number of arguments in return statement "
+ "than in returns declaration."));
// this could later be changed such that the paramaters type is an anonymous struct type,
// but for now, we only allow one return parameter
expectType(*m_expression, *m_returnParameters->getParameters().front()->getType());
@@ -327,7 +331,7 @@ void Assignment::checkTypeRequirements()
{
// complex assignment
if (!m_type->acceptsBinaryOperator(Token::AssignmentToBinaryOp(m_assigmentOperator)))
- BOOST_THROW_EXCEPTION(TypeError(getLocation(), "Operator not compatible with type."));
+ BOOST_THROW_EXCEPTION(createTypeError("Operator not compatible with type."));
}
}
@@ -337,7 +341,7 @@ void UnaryOperation::checkTypeRequirements()
m_subExpression->checkTypeRequirements();
m_type = m_subExpression->getType();
if (m_type->acceptsUnaryOperator(m_operator))
- BOOST_THROW_EXCEPTION(TypeError(getLocation(), "Unary operator not compatible with type."));
+ BOOST_THROW_EXCEPTION(createTypeError("Unary operator not compatible with type."));
}
void BinaryOperation::checkTypeRequirements()
@@ -349,7 +353,7 @@ void BinaryOperation::checkTypeRequirements()
else if (m_left->getType()->isImplicitlyConvertibleTo(*m_right->getType()))
m_commonType = m_right->getType();
else
- BOOST_THROW_EXCEPTION(TypeError(getLocation(), "No common type found in binary operation."));
+ BOOST_THROW_EXCEPTION(createTypeError("No common type found in binary operation."));
if (Token::isCompareOp(m_operator))
m_type = std::make_shared<BoolType>();
else
@@ -357,7 +361,7 @@ void BinaryOperation::checkTypeRequirements()
BOOST_ASSERT(Token::isBinaryOp(m_operator));
m_type = m_commonType;
if (!m_commonType->acceptsBinaryOperator(m_operator))
- BOOST_THROW_EXCEPTION(TypeError(getLocation(), "Operator not compatible with type."));
+ BOOST_THROW_EXCEPTION(createTypeError("Operator not compatible with type."));
}
}
@@ -375,10 +379,10 @@ void FunctionCall::checkTypeRequirements()
//@todo for structs, we have to check the number of arguments to be equal to the
// number of non-mapping members
if (m_arguments.size() != 1)
- BOOST_THROW_EXCEPTION(TypeError(getLocation(), "More than one argument for "
+ BOOST_THROW_EXCEPTION(createTypeError("More than one argument for "
"explicit type conersion."));
if (!m_arguments.front()->getType()->isExplicitlyConvertibleTo(*type->getActualType()))
- BOOST_THROW_EXCEPTION(TypeError(getLocation(), "Explicit type conversion not allowed."));
+ BOOST_THROW_EXCEPTION(createTypeError("Explicit type conversion not allowed."));
m_type = type->getActualType();
}
else if (category == Type::Category::FUNCTION)
@@ -391,10 +395,10 @@ void FunctionCall::checkTypeRequirements()
FunctionDefinition const& fun = function->getFunction();
std::vector<ASTPointer<VariableDeclaration>> const& parameters = fun.getParameters();
if (parameters.size() != m_arguments.size())
- BOOST_THROW_EXCEPTION(TypeError(getLocation(), "Wrong argument count for function call."));
+ BOOST_THROW_EXCEPTION(createTypeError("Wrong argument count for function call."));
for (size_t i = 0; i < m_arguments.size(); ++i)
if (!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameters[i]->getType()))
- BOOST_THROW_EXCEPTION(TypeError(getLocation(), "Invalid type for argument in function call."));
+ BOOST_THROW_EXCEPTION(createTypeError("Invalid type for argument in function call."));
// @todo actually the return type should be an anonymous struct,
// but we change it to the type of the first return value until we have structs
if (fun.getReturnParameterList()->getParameters().empty())
@@ -404,7 +408,7 @@ void FunctionCall::checkTypeRequirements()
}
else
{
- BOOST_THROW_EXCEPTION(TypeError(getLocation(), "Type does not support invocation."));
+ BOOST_THROW_EXCEPTION(createTypeError("Type does not support invocation."));
}
}
@@ -435,7 +439,7 @@ void Identifier::checkTypeRequirements()
if (variable != nullptr)
{
if (!variable->getType())
- BOOST_THROW_EXCEPTION(TypeError(getLocation(), "Variable referenced before type "
+ BOOST_THROW_EXCEPTION(createTypeError("Variable referenced before type "
"could be determined."));
m_type = variable->getType();
return;
diff --git a/AST.h b/AST.h
index d5e1e066..5679ed83 100644
--- a/AST.h
+++ b/AST.h
@@ -22,16 +22,16 @@
#pragma once
-#include <boost/noncopyable.hpp>
#include <string>
#include <vector>
#include <memory>
-
+#include <boost/noncopyable.hpp>
#include <libsolidity/ASTForward.h>
#include <libsolidity/BaseTypes.h>
#include <libsolidity/Token.h>
#include <libsolidity/Types.h>
+#include <libsolidity/Exceptions.h>
namespace dev
{
@@ -57,6 +57,11 @@ public:
Location const& getLocation() const { return m_location; }
+protected:
+ /// Creates a @ref TypeError exception and decorates it with the current location and
+ /// the given description
+ TypeError createTypeError(std::string const& _description);
+
private:
Location m_location;
};
diff --git a/Exceptions.h b/Exceptions.h
index 075af2b9..330c3778 100644
--- a/Exceptions.h
+++ b/Exceptions.h
@@ -31,44 +31,12 @@ namespace dev
namespace solidity
{
-class ParserError: public virtual Exception
-{
-public:
- ParserError(int _position, std::string const& _description):
- m_position(_position), m_description(_description) {}
- virtual const char* what() const noexcept;
- int getPosition() const { return m_position; }
-
-private:
- int m_position;
- std::string m_description;
-};
-
-class TypeError: public virtual Exception
-{
-public:
- TypeError(Location const& _location, std::string const& _description):
- m_location(_location), m_description(_description) {}
- virtual const char* what() const noexcept;
- Location const& getLocation() const { return m_location; }
-
-private:
- Location m_location;
- std::string m_description;
-};
-
-class DeclarationError: public virtual Exception
-{
-public:
- DeclarationError(Location const& _location, std::string const& _description):
- m_location(_location), m_description(_description) {}
- virtual const char* what() const noexcept;
- Location const& getLocation() const { return m_location; }
+struct ParserError: public virtual Exception {};
+struct TypeError: public virtual Exception {};
+struct DeclarationError: public virtual Exception {};
-private:
- Location m_location;
- std::string m_description;
-};
+typedef boost::error_info<struct tag_sourcePosition, int> errinfo_sourcePosition;
+typedef boost::error_info<struct tag_sourceLocation, Location> errinfo_sourceLocation;
}
}
diff --git a/NameAndTypeResolver.cpp b/NameAndTypeResolver.cpp
index 8fe45281..a5650272 100644
--- a/NameAndTypeResolver.cpp
+++ b/NameAndTypeResolver.cpp
@@ -137,7 +137,8 @@ void DeclarationRegistrationHelper::registerDeclaration(Declaration& _declaratio
{
BOOST_ASSERT(m_currentScope != nullptr);
if (!m_currentScope->registerDeclaration(_declaration))
- BOOST_THROW_EXCEPTION(DeclarationError(_declaration.getLocation(), "Identifier already declared."));
+ BOOST_THROW_EXCEPTION(DeclarationError() << errinfo_sourceLocation(_declaration.getLocation())
+ << errinfo_comment("Identifier already declared."));
//@todo the exception should also contain the location of the first declaration
if (_opensScope)
enterNewSubScope(_declaration);
@@ -176,11 +177,13 @@ bool ReferencesResolver::visit(UserDefinedTypeName& _typeName)
{
Declaration* declaration = m_resolver.getNameFromCurrentScope(_typeName.getName());
if (declaration == nullptr)
- BOOST_THROW_EXCEPTION(DeclarationError(_typeName.getLocation(), "Undeclared identifier."));
+ BOOST_THROW_EXCEPTION(DeclarationError() << errinfo_sourceLocation(_typeName.getLocation())
+ << errinfo_comment("Undeclared identifier."));
StructDefinition* referencedStruct = dynamic_cast<StructDefinition*>(declaration);
//@todo later, contracts are also valid types
if (referencedStruct == nullptr)
- BOOST_THROW_EXCEPTION(TypeError(_typeName.getLocation(), "Identifier does not name a type name."));
+ BOOST_THROW_EXCEPTION(TypeError() << errinfo_sourceLocation(_typeName.getLocation())
+ << errinfo_comment("Identifier does not name a type name."));
_typeName.setReferencedStruct(*referencedStruct);
return false;
}
@@ -189,7 +192,8 @@ bool ReferencesResolver::visit(Identifier& _identifier)
{
Declaration* declaration = m_resolver.getNameFromCurrentScope(_identifier.getName());
if (declaration == nullptr)
- BOOST_THROW_EXCEPTION(DeclarationError(_identifier.getLocation(), "Undeclared identifier."));
+ BOOST_THROW_EXCEPTION(DeclarationError() << errinfo_sourceLocation(_identifier.getLocation())
+ << errinfo_comment("Undeclared identifier."));
_identifier.setReferencedDeclaration(*declaration);
return false;
}
diff --git a/Parser.cpp b/Parser.cpp
index 3887ac8f..1ea413ee 100644
--- a/Parser.cpp
+++ b/Parser.cpp
@@ -106,7 +106,7 @@ ASTPointer<ContractDefinition> Parser::parseContractDefinition()
expectToken(Token::SEMICOLON);
}
else
- throwExpectationError("Function, variable or struct declaration expected.");
+ BOOST_THROW_EXCEPTION(createParserError("Function, variable or struct declaration expected."));
}
nodeFactory.markEndPosition();
expectToken(Token::RBRACE);
@@ -184,7 +184,7 @@ ASTPointer<TypeName> Parser::parseTypeName(bool _allowVar)
else if (token == Token::VAR)
{
if (!_allowVar)
- throwExpectationError("Expected explicit type name.");
+ BOOST_THROW_EXCEPTION(createParserError("Expected explicit type name."));
m_scanner->next();
}
else if (token == Token::MAPPING)
@@ -198,7 +198,7 @@ ASTPointer<TypeName> Parser::parseTypeName(bool _allowVar)
type = nodeFactory.createNode<UserDefinedTypeName>(expectIdentifierToken());
}
else
- throwExpectationError("Expected type name");
+ BOOST_THROW_EXCEPTION(createParserError("Expected type name"));
return type;
}
@@ -208,7 +208,7 @@ ASTPointer<Mapping> Parser::parseMapping()
expectToken(Token::MAPPING);
expectToken(Token::LPAREN);
if (!Token::isElementaryTypeName(m_scanner->getCurrentToken()))
- throwExpectationError("Expected elementary type name for mapping key type");
+ BOOST_THROW_EXCEPTION(createParserError("Expected elementary type name for mapping key type"));
ASTPointer<ElementaryTypeName> keyType;
keyType = ASTNodeFactory(*this).createNode<ElementaryTypeName>(m_scanner->getCurrentToken());
m_scanner->next();
@@ -481,7 +481,7 @@ ASTPointer<Expression> Parser::parsePrimaryExpression()
}
else
{
- throwExpectationError("Expected primary expression.");
+ BOOST_THROW_EXCEPTION(createParserError("Expected primary expression."));
return ASTPointer<Expression>(); // this is not reached
}
break;
@@ -507,7 +507,7 @@ std::vector<ASTPointer<Expression>> Parser::parseFunctionCallArguments()
void Parser::expectToken(Token::Value _value)
{
if (m_scanner->getCurrentToken() != _value)
- throwExpectationError(std::string("Expected token ") + std::string(Token::getName(_value)));
+ BOOST_THROW_EXCEPTION(createParserError(std::string("Expected token ") + std::string(Token::getName(_value))));
m_scanner->next();
}
@@ -515,7 +515,7 @@ Token::Value Parser::expectAssignmentOperator()
{
Token::Value op = m_scanner->getCurrentToken();
if (!Token::isAssignmentOp(op))
- throwExpectationError(std::string("Expected assignment operator"));
+ BOOST_THROW_EXCEPTION(createParserError("Expected assignment operator"));
m_scanner->next();
return op;
}
@@ -523,7 +523,7 @@ Token::Value Parser::expectAssignmentOperator()
ASTPointer<ASTString> Parser::expectIdentifierToken()
{
if (m_scanner->getCurrentToken() != Token::IDENTIFIER)
- throwExpectationError("Expected identifier");
+ BOOST_THROW_EXCEPTION(createParserError("Expected identifier"));
return getLiteralAndAdvance();
}
@@ -534,9 +534,9 @@ ASTPointer<ASTString> Parser::getLiteralAndAdvance()
return identifier;
}
-void Parser::throwExpectationError(std::string const& _description)
+ParserError Parser::createParserError(std::string const& _description) const
{
- BOOST_THROW_EXCEPTION(ParserError(getPosition(), _description));
+ return ParserError() << errinfo_sourcePosition(getPosition()) << errinfo_comment(_description);
}
diff --git a/Parser.h b/Parser.h
index 7cc41513..14338dc2 100644
--- a/Parser.h
+++ b/Parser.h
@@ -73,9 +73,12 @@ private:
Token::Value expectAssignmentOperator();
ASTPointer<ASTString> expectIdentifierToken();
ASTPointer<ASTString> getLiteralAndAdvance();
- void throwExpectationError(std::string const& _description);
/// @}
+ /// Creates a @ref ParserError exception and annotates it with the current position and the
+ /// given @a _description.
+ ParserError createParserError(std::string const& _description) const;
+
std::shared_ptr<Scanner> m_scanner;
};
diff --git a/SourceReferenceFormatter.cpp b/SourceReferenceFormatter.cpp
new file mode 100644
index 00000000..6d291116
--- /dev/null
+++ b/SourceReferenceFormatter.cpp
@@ -0,0 +1,96 @@
+/*
+ This file is part of cpp-ethereum.
+
+ cpp-ethereum is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ cpp-ethereum is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * @author Christian <c@ethdev.com>
+ * @date 2014
+ * Formatting functions for errors referencing positions and locations in the source.
+ */
+
+#include <libsolidity/SourceReferenceFormatter.h>
+#include <libsolidity/Scanner.h>
+#include <libsolidity/Exceptions.h>
+
+namespace dev
+{
+namespace solidity
+{
+
+void SourceReferenceFormatter::printSourceLocation(std::ostream& _stream,
+ Location const& _location,
+ Scanner const& _scanner)
+{
+ int startLine;
+ int startColumn;
+ std::tie(startLine, startColumn) = _scanner.translatePositionToLineColumn(_location.start);
+ _stream << "starting at line " << (startLine + 1) << ", column " << (startColumn + 1) << "\n";
+ int endLine;
+ int endColumn;
+ std::tie(endLine, endColumn) = _scanner.translatePositionToLineColumn(_location.end);
+ if (startLine == endLine)
+ {
+ _stream << _scanner.getLineAtPosition(_location.start) << "\n"
+ << std::string(startColumn, ' ') << "^";
+ if (endColumn > startColumn + 2)
+ _stream << std::string(endColumn - startColumn - 2, '-');
+ if (endColumn > startColumn + 1)
+ _stream << "^";
+ _stream << "\n";
+ }
+ else
+ _stream << _scanner.getLineAtPosition(_location.start) << "\n"
+ << std::string(startColumn, ' ') << "^\n"
+ << "Spanning multiple lines.\n";
+}
+
+void SourceReferenceFormatter::printSourcePosition(std::ostream& _stream,
+ int _position,
+ const Scanner& _scanner)
+{
+ int line;
+ int column;
+ std::tie(line, column) = _scanner.translatePositionToLineColumn(_position);
+ _stream << "at line " << (line + 1) << ", column " << (column + 1) << "\n"
+ << _scanner.getLineAtPosition(_position) << "\n"
+ << std::string(column, ' ') << "^" << std::endl;
+}
+
+void SourceReferenceFormatter::printExceptionInformation(std::ostream& _stream,
+ Exception const& _exception,
+ std::string const& _name,
+ Scanner const& _scanner)
+{
+ std::cerr << _name;
+ std::string const* description = boost::get_error_info<errinfo_comment>(_exception);
+ if (description != nullptr)
+ std::cerr << ": " << *description;
+
+ int const* position = boost::get_error_info<errinfo_sourcePosition>(_exception);
+ if (position != nullptr)
+ {
+ std::cerr << " ";
+ printSourcePosition(std::cerr, *position, _scanner);
+ }
+ Location const* location = boost::get_error_info<errinfo_sourceLocation>(_exception);
+ if (location != nullptr)
+ {
+ std::cerr << " ";
+ printSourceLocation(_stream, *location, _scanner);
+ }
+}
+
+}
+}
diff --git a/Exceptions.cpp b/SourceReferenceFormatter.h
index 53380d77..4736066f 100644
--- a/Exceptions.cpp
+++ b/SourceReferenceFormatter.h
@@ -17,31 +17,32 @@
/**
* @author Christian <c@ethdev.com>
* @date 2014
- * Solidity exception hierarchy.
+ * Formatting functions for errors referencing positions and locations in the source.
*/
-#include <libsolidity/Exceptions.h>
+#pragma once
+#include <ostream>
+#include <libsolidity/BaseTypes.h>
namespace dev
{
-namespace solidity
-{
-const char* ParserError::what() const noexcept
-{
- ETH_RETURN_STRING("Parser error: " + m_description);
-}
+class Exception; // forward
-const char* DeclarationError::what() const noexcept
+namespace solidity
{
- ETH_RETURN_STRING("Declaration error: " + m_description);
-}
-const char* TypeError::what() const noexcept
+class Scanner; // forward
+
+struct SourceReferenceFormatter
{
- ETH_RETURN_STRING("Type error: " + m_description);
-}
+public:
+ static void printSourceLocation(std::ostream& _stream, Location const& _location, Scanner const& _scanner);
+ static void printSourcePosition(std::ostream& _stream, int _position, Scanner const& _scanner);
+ static void printExceptionInformation(std::ostream& _stream, Exception const& _exception,
+ std::string const& _name, Scanner const& _scanner);
+};
}
}