Browse Source

Improved exceptions and reporting exceptions for command-line compiler.

cl-refactor
Christian 10 years ago
parent
commit
bf3a76f5af
  1. 3
      libdevcore/Exceptions.h
  2. 2
      libethcore/Exceptions.cpp
  3. 37
      libsolidity/AST.cpp
  4. 47
      libsolidity/Exceptions.cpp
  5. 43
      libsolidity/Exceptions.h
  6. 9
      libsolidity/NameAndTypeResolver.cpp
  7. 11
      libsolidity/Parser.cpp
  8. 2
      libsolidity/Types.cpp
  9. 93
      solc/main.cpp

3
libdevcore/Exceptions.h

@ -28,6 +28,9 @@
#include "CommonData.h"
#include "FixedHash.h"
// for use in what() implementations
#define ETH_RETURN_STRING(S) static std::string s_what; s_what = S; return s_what.c_str();
namespace dev
{
// base class for all exceptions

2
libethcore/Exceptions.cpp

@ -26,8 +26,6 @@ using namespace std;
using namespace dev;
using namespace dev::eth;
#define ETH_RETURN_STRING(S) static string s_what; s_what = S; return s_what.c_str();
const char* InvalidBlockFormat::what() const noexcept { ETH_RETURN_STRING("Invalid block format: Bad field " + toString(m_f) + " (" + toHex(m_d) + ")"); }
const char* UncleInChain::what() const noexcept { ETH_RETURN_STRING("Uncle in block already mentioned: Uncles " + toString(m_uncles) + " (" + m_block.abridged() + ")"); }
const char* InvalidTransactionsHash::what() const noexcept { ETH_RETURN_STRING("Invalid transactions hash: header says: " + toHex(m_head.ref()) + " block is:" + toHex(m_real.ref())); }

37
libsolidity/AST.cpp

@ -252,8 +252,8 @@ void Statement::expectType(Expression& _expression, const Type& _expectedType)
{
_expression.checkTypeRequirements();
if (!_expression.getType()->isImplicitlyConvertibleTo(_expectedType))
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Type not implicitly convertible "
"to expected type."));
BOOST_THROW_EXCEPTION(TypeError(_expression.getLocation(),
"Type not implicitly convertible to expected type."));
//@todo provide more information to the exception
}
@ -289,9 +289,9 @@ void Return::checkTypeRequirements()
{
BOOST_ASSERT(m_returnParameters != nullptr);
if (m_returnParameters->getParameters().size() != 1)
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Different number of arguments in "
"return statement than in returns "
"declaration."));
BOOST_THROW_EXCEPTION(TypeError(getLocation(), "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 +327,7 @@ void Assignment::checkTypeRequirements()
{
// complex assignment
if (!m_type->acceptsBinaryOperator(Token::AssignmentToBinaryOp(m_assigmentOperator)))
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Operator not compatible with type."));
BOOST_THROW_EXCEPTION(TypeError(getLocation(), "Operator not compatible with type."));
}
}
@ -337,7 +337,7 @@ void UnaryOperation::checkTypeRequirements()
m_subExpression->checkTypeRequirements();
m_type = m_subExpression->getType();
if (m_type->acceptsUnaryOperator(m_operator))
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Unary operator not compatible with type."));
BOOST_THROW_EXCEPTION(TypeError(getLocation(), "Unary operator not compatible with type."));
}
void BinaryOperation::checkTypeRequirements()
@ -349,7 +349,7 @@ void BinaryOperation::checkTypeRequirements()
else if (m_left->getType()->isImplicitlyConvertibleTo(*m_right->getType()))
m_commonType = m_right->getType();
else
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("No common type found in binary operation."));
BOOST_THROW_EXCEPTION(TypeError(getLocation(), "No common type found in binary operation."));
if (Token::isCompareOp(m_operator))
m_type = std::make_shared<BoolType>();
else
@ -357,7 +357,7 @@ void BinaryOperation::checkTypeRequirements()
BOOST_ASSERT(Token::isBinaryOp(m_operator));
m_type = m_commonType;
if (!m_commonType->acceptsBinaryOperator(m_operator))
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Operator not compatible with type."));
BOOST_THROW_EXCEPTION(TypeError(getLocation(), "Operator not compatible with type."));
}
}
@ -375,11 +375,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() << errinfo_comment("More than one argument for "
"explicit type conersion."));
BOOST_THROW_EXCEPTION(TypeError(getLocation(), "More than one argument for "
"explicit type conersion."));
if (!m_arguments.front()->getType()->isExplicitlyConvertibleTo(*type->getActualType()))
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Explicit type conversion not "
"allowed."));
BOOST_THROW_EXCEPTION(TypeError(getLocation(), "Explicit type conversion not allowed."));
m_type = type->getActualType();
}
else if (category == Type::Category::FUNCTION)
@ -392,12 +391,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() << errinfo_comment("Wrong argument count for "
"function call."));
BOOST_THROW_EXCEPTION(TypeError(getLocation(), "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() << errinfo_comment("Invalid type for argument in "
"function call."));
BOOST_THROW_EXCEPTION(TypeError(getLocation(), "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())
@ -407,7 +404,7 @@ void FunctionCall::checkTypeRequirements()
}
else
{
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Type does not support invocation."));
BOOST_THROW_EXCEPTION(TypeError(getLocation(), "Type does not support invocation."));
}
}
@ -438,8 +435,8 @@ void Identifier::checkTypeRequirements()
if (variable != nullptr)
{
if (!variable->getType())
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Variable referenced before type "
"could be determined."));
BOOST_THROW_EXCEPTION(TypeError(getLocation(), "Variable referenced before type "
"could be determined."));
m_type = variable->getType();
return;
}

47
libsolidity/Exceptions.cpp

@ -0,0 +1,47 @@
/*
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
* Solidity exception hierarchy.
*/
#include <libsolidity/Exceptions.h>
namespace dev
{
namespace solidity
{
const char* ParserError::what() const noexcept
{
ETH_RETURN_STRING("Parser error: " + m_description);
}
const char* DeclarationError::what() const noexcept
{
ETH_RETURN_STRING("Declaration error: " + m_description);
}
const char* TypeError::what() const noexcept
{
ETH_RETURN_STRING("Type error: " + m_description);
}
}
}

43
libsolidity/Exceptions.h

@ -22,16 +22,53 @@
#pragma once
#include <string>
#include <libdevcore/Exceptions.h>
#include <libsolidity/BaseTypes.h>
namespace dev
{
namespace solidity
{
struct ParserError: virtual Exception {};
struct TypeError: virtual Exception {};
struct DeclarationError: virtual Exception {};
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; }
private:
Location m_location;
std::string m_description;
};
}
}

9
libsolidity/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() << errinfo_comment("Identifier already declared."));
BOOST_THROW_EXCEPTION(DeclarationError(_declaration.getLocation(), "Identifier already declared."));
//@todo the exception should also contain the location of the first declaration
if (_opensScope)
enterNewSubScope(_declaration);
}
@ -175,11 +176,11 @@ bool ReferencesResolver::visit(UserDefinedTypeName& _typeName)
{
Declaration* declaration = m_resolver.getNameFromCurrentScope(_typeName.getName());
if (declaration == nullptr)
BOOST_THROW_EXCEPTION(DeclarationError() << errinfo_comment("Undeclared identifier."));
BOOST_THROW_EXCEPTION(DeclarationError(_typeName.getLocation(), "Undeclared identifier."));
StructDefinition* referencedStruct = dynamic_cast<StructDefinition*>(declaration);
//@todo later, contracts are also valid types
if (referencedStruct == nullptr)
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Identifier does not name a type name."));
BOOST_THROW_EXCEPTION(TypeError(_typeName.getLocation(), "Identifier does not name a type name."));
_typeName.setReferencedStruct(*referencedStruct);
return false;
}
@ -188,7 +189,7 @@ bool ReferencesResolver::visit(Identifier& _identifier)
{
Declaration* declaration = m_resolver.getNameFromCurrentScope(_identifier.getName());
if (declaration == nullptr)
BOOST_THROW_EXCEPTION(DeclarationError() << errinfo_comment("Undeclared identifier."));
BOOST_THROW_EXCEPTION(DeclarationError(_identifier.getLocation(), "Undeclared identifier."));
_identifier.setReferencedDeclaration(*declaration);
return false;
}

11
libsolidity/Parser.cpp

@ -536,16 +536,7 @@ ASTPointer<ASTString> Parser::getLiteralAndAdvance()
void Parser::throwExpectationError(std::string const& _description)
{
//@todo put some of this stuff into ParserError
int line, column;
std::tie(line, column) = m_scanner->translatePositionToLineColumn(getPosition());
std::stringstream buf;
buf << "Solidity parser error: " << _description
<< " at line " << (line + 1)
<< ", column " << (column + 1) << "\n"
<< m_scanner->getLineAtPosition(getPosition()) << "\n"
<< std::string(column, ' ') << "^";
BOOST_THROW_EXCEPTION(ParserError() << errinfo_comment(buf.str()));
BOOST_THROW_EXCEPTION(ParserError(getPosition(), _description));
}

2
libsolidity/Types.cpp

@ -88,9 +88,9 @@ std::shared_ptr<IntegerType> IntegerType::smallestTypeForLiteral(std::string con
IntegerType::IntegerType(int _bits, IntegerType::Modifier _modifier):
m_bits(_bits), m_modifier(_modifier)
{
BOOST_ASSERT(_bits > 0 && _bits <= 256 && _bits % 8 == 0);
if (isAddress())
_bits = 160;
BOOST_ASSERT(_bits > 0 && _bits <= 256 && _bits % 8 == 0);
}
bool IntegerType::isImplicitlyConvertibleTo(Type const& _convertTo) const

93
solc/main.cpp

@ -9,21 +9,10 @@
#include <libsolidity/Parser.h>
#include <libsolidity/ASTPrinter.h>
#include <libsolidity/NameAndTypeResolver.h>
#include <libsolidity/Exceptions.h>
namespace dev
{
namespace solidity
{
ASTPointer<ContractDefinition> parseAST(std::string const& _source)
{
ASTPointer<Scanner> scanner = std::make_shared<Scanner>(CharStream(_source));
Parser parser;
return parser.parse(scanner);
}
}
} // end namespaces
using namespace dev;
using namespace solidity;
void help()
{
@ -44,6 +33,33 @@ void version()
exit(0);
}
void printSourcePart(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";
}
}
int main(int argc, char** argv)
{
std::string infile;
@ -57,28 +73,57 @@ int main(int argc, char** argv)
else
infile = argv[i];
}
std::string src;
std::string sourceCode;
if (infile.empty())
{
std::string s;
while (!std::cin.eof())
{
getline(std::cin, s);
src.append(s);
sourceCode.append(s);
}
}
else
sourceCode = asString(dev::contents(infile));
ASTPointer<ContractDefinition> ast;
std::shared_ptr<Scanner> scanner = std::make_shared<Scanner>(CharStream(sourceCode));
Parser parser;
try
{
ast = parser.parse(scanner);
}
catch (ParserError const& exc)
{
src = dev::asString(dev::contents(infile));
int line;
int column;
std::tie(line, column) = scanner->translatePositionToLineColumn(exc.getPosition());
std::cerr << exc.what() << " at line " << (line + 1) << ", column " << (column + 1) << std::endl;
std::cerr << scanner->getLineAtPosition(exc.getPosition()) << std::endl;
std::cerr << std::string(column, ' ') << "^" << std::endl;
return -1;
}
std::cout << "Parsing..." << std::endl;
// @todo catch exception
dev::solidity::ASTPointer<dev::solidity::ContractDefinition> ast = dev::solidity::parseAST(src);
dev::solidity::NameAndTypeResolver resolver;
try
{
resolver.resolveNamesAndTypes(*ast.get());
}
catch (DeclarationError const& exc)
{
std::cerr << exc.what() << std::endl;
printSourcePart(std::cerr, exc.getLocation(), *scanner);
return -1;
}
catch (TypeError const& exc)
{
std::cerr << exc.what() << std::endl;
printSourcePart(std::cerr, exc.getLocation(), *scanner);
return -1;
}
std::cout << "Syntax tree for the contract:" << std::endl;
dev::solidity::ASTPrinter printer(ast, src);
dev::solidity::ASTPrinter printer(ast, sourceCode);
printer.print(std::cout);
std::cout << "Resolving identifiers..." << std::endl;
dev::solidity::NameAndTypeResolver resolver;
resolver.resolveNamesAndTypes(*ast.get());
return 0;
}

Loading…
Cancel
Save