diff --git a/libsolidity/Exceptions.cpp b/libsolidity/Exceptions.cpp new file mode 100644 index 000000000..37d25697b --- /dev/null +++ b/libsolidity/Exceptions.cpp @@ -0,0 +1,52 @@ +/* + 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 . +*/ +/** + * @author Christian + * @date 2015 + * Solidity exception hierarchy. + */ + +#include +#include + +using namespace dev; +using namespace dev::solidity; + +Error::Error(Error::Type _type): m_type(_type) +{ + switch(m_type) + { + case Type::DeclarationError: + m_typeName = "Declaration Error"; + break; + case Type::DocstringParsingError: + m_typeName = "Docstring Parsing Error"; + break; + case Type::ParserError: + m_typeName = "Parser Error"; + break; + case Type::TypeError: + m_typeName = "Type Error"; + break; + case Type::Warning: + m_typeName = "Warning"; + break; + default: + solAssert(false, ""); + break; + } +} diff --git a/libsolidity/Exceptions.h b/libsolidity/Exceptions.h index 92c009ef0..9bc22994a 100644 --- a/libsolidity/Exceptions.h +++ b/libsolidity/Exceptions.h @@ -26,7 +26,6 @@ #include #include #include -#include namespace dev { @@ -51,35 +50,11 @@ public: Warning }; - Error(Type _type) : m_type(_type) - { - switch(m_type) - { - case Type::DeclarationError: - m_typeName = "Declaration Error"; - break; - case Type::DocstringParsingError: - m_typeName = "Docstring Parsing Error"; - break; - case Type::ParserError: - m_typeName = "Parser Error"; - break; - case Type::TypeError: - m_typeName = "Type Error"; - break; - case Type::Warning: - m_typeName = "Warning"; - break; - default: - solAssert(false, ""); - break; - } - } + explicit Error(Type _type); Type type() const { return m_type; } std::string const& typeName() const { return m_typeName; } - /// helper functions static Error const* containsErrorOfType(ErrorList const& _list, Error::Type _type) { diff --git a/libsolidity/Utils.h b/libsolidity/Utils.h index 05c5fa6f0..48bb1e479 100644 --- a/libsolidity/Utils.h +++ b/libsolidity/Utils.h @@ -23,6 +23,15 @@ #pragma once #include +#include + +namespace dev +{ +namespace solidity +{ +struct InternalCompilerError; +} +} /// Assertion that throws an InternalCompilerError containing the given description if it is not met. #define solAssert(CONDITION, DESCRIPTION) \ diff --git a/solc/jsonCompiler.cpp b/solc/jsonCompiler.cpp index 1cf539e59..0746fc887 100644 --- a/solc/jsonCompiler.cpp +++ b/solc/jsonCompiler.cpp @@ -127,11 +127,14 @@ string compile(string _input, bool _optimize) { bool succ = compiler.compile(_input, _optimize); for (auto const& error: compiler.errors()) + { + auto err = dynamic_pointer_cast(error); errors.append(formatError( *error, - (dynamic_pointer_cast(error)) ? "Warning" : "Error", + (err->type() == Error::Type::Warning) ? "Warning" : "Error", compiler )); + } success = succ; // keep success false on exception } catch (Error const& error) diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 905b5ce19..01a65cd90 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -57,12 +57,11 @@ parseAnalyseAndReturnError(string const& _source, bool _reportWarnings = false) if(!sourceUnit) return make_pair(sourceUnit, nullptr); - NameAndTypeResolver resolver({}, errors); + std::shared_ptr globalContext = make_shared(); + NameAndTypeResolver resolver(globalContext->declarations(), errors); solAssert(Error::containsOnlyWarnings(errors), ""); resolver.registerDeclarations(*sourceUnit); - std::shared_ptr globalContext = make_shared(); - NameAndTypeResolver resolver(globalContext->declarations()); resolver.registerDeclarations(*sourceUnit); for (ASTPointer const& node: sourceUnit->nodes()) @@ -81,7 +80,7 @@ parseAnalyseAndReturnError(string const& _source, bool _reportWarnings = false) TypeChecker typeChecker(errors); bool success = typeChecker.checkTypeRequirements(*contract); - BOOST_CHECK(success || !typeChecker.errors().empty()); + BOOST_CHECK(success || !errors.empty()); for (auto const& currentError: errors) { @@ -1119,7 +1118,7 @@ BOOST_AUTO_TEST_CASE(anonymous_event_too_many_indexed) contract c { event e(uint indexed a, bytes3 indexed b, bool indexed c, uint indexed d, uint indexed e) anonymous; })"; - SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); + BOOST_CHECK(parseAndAnalyseReturnErrorType(text) == Error::Type::TypeError); } BOOST_AUTO_TEST_CASE(event_call) @@ -1282,13 +1281,8 @@ BOOST_AUTO_TEST_CASE(empty_name_return_parameter_with_named_one) BOOST_AUTO_TEST_CASE(disallow_declaration_of_void_type) { -<<<<<<< HEAD char const* sourceCode = "contract c { function f() { var (x) = f(); } }"; - SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(sourceCode), TypeError); -======= - char const* sourceCode = "contract c { function f() { var x = f(); } }"; BOOST_CHECK(parseAndAnalyseReturnErrorType(sourceCode) == Error::Type::TypeError); ->>>>>>> 2cd6509... errors instead of exceptions } BOOST_AUTO_TEST_CASE(overflow_caused_by_ether_units) @@ -2353,8 +2347,6 @@ BOOST_AUTO_TEST_CASE(non_initialized_references) } )"; - auto b = parseAndAnalyseReturnErrorType(text, true); - (void)b; BOOST_CHECK(parseAndAnalyseReturnErrorType(text, true) == Error::Type::Warning); } @@ -2366,7 +2358,7 @@ BOOST_AUTO_TEST_CASE(sha3_with_large_integer_constant) function f() { sha3(2**500); } } )"; - SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); + BOOST_CHECK(parseAndAnalyseReturnErrorType(text) == Error::Type::TypeError); } BOOST_AUTO_TEST_CASE(cyclic_binary_dependency) @@ -2376,7 +2368,7 @@ BOOST_AUTO_TEST_CASE(cyclic_binary_dependency) contract B { function f() { new C(); } } contract C { function f() { new A(); } } )"; - SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); + BOOST_CHECK(parseAndAnalyseReturnErrorType(text) == Error::Type::TypeError); } BOOST_AUTO_TEST_CASE(cyclic_binary_dependency_via_inheritance) @@ -2386,7 +2378,7 @@ BOOST_AUTO_TEST_CASE(cyclic_binary_dependency_via_inheritance) contract B { function f() { new C(); } } contract C { function f() { new A(); } } )"; - SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); + BOOST_CHECK(parseAndAnalyseReturnErrorType(text) == Error::Type::TypeError); } BOOST_AUTO_TEST_CASE(multi_variable_declaration_fail) @@ -2394,7 +2386,7 @@ BOOST_AUTO_TEST_CASE(multi_variable_declaration_fail) char const* text = R"( contract C { function f() { var (x,y); } } )"; - SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); + BOOST_CHECK(parseAndAnalyseReturnErrorType(text) == Error::Type::TypeError); } BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fine) @@ -2425,7 +2417,7 @@ BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_1) function f() { var (a, b, ) = one(); } } )"; - SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); + BOOST_CHECK(parseAndAnalyseReturnErrorType(text) == Error::Type::TypeError); } BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_2) { @@ -2435,7 +2427,7 @@ BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_2) function f() { var (a, , ) = one(); } } )"; - SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); + BOOST_CHECK(parseAndAnalyseReturnErrorType(text) == Error::Type::TypeError); } BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_3) @@ -2446,7 +2438,7 @@ BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_3) function f() { var (, , a) = one(); } } )"; - SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); + BOOST_CHECK(parseAndAnalyseReturnErrorType(text) == Error::Type::TypeError); } BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_4) @@ -2457,7 +2449,7 @@ BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_4) function f() { var (, a, b) = one(); } } )"; - SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); + BOOST_CHECK(parseAndAnalyseReturnErrorType(text) == Error::Type::TypeError); } BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_5) @@ -2468,7 +2460,7 @@ BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_5) function f() { var (,) = one(); } } )"; - SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); + BOOST_CHECK(parseAndAnalyseReturnErrorType(text) == Error::Type::TypeError); } BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_6) @@ -2479,7 +2471,7 @@ BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_6) function f() { var (a, b, c) = two(); } } )"; - SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); + BOOST_CHECK(parseAndAnalyseReturnErrorType(text) == Error::Type::TypeError); } BOOST_AUTO_TEST_SUITE_END() diff --git a/test/libsolidity/SolidityParser.cpp b/test/libsolidity/SolidityParser.cpp index 0bc4d4eee..3147c034f 100644 --- a/test/libsolidity/SolidityParser.cpp +++ b/test/libsolidity/SolidityParser.cpp @@ -997,7 +997,7 @@ BOOST_AUTO_TEST_CASE(multi_variable_declaration) function g() returns (uint, uint, uint) {} } )"; - BOOST_CHECK_NO_THROW(parseText(text)); + BOOST_CHECK(successParse(text)); } BOOST_AUTO_TEST_SUITE_END()