From 3698cd54a589ed6d10d51930a551cdd784168a9c Mon Sep 17 00:00:00 2001 From: a3d4 Date: Mon, 28 Sep 2020 17:39:40 +0200 Subject: [PATCH] Complete error coverage of Parser and SyntaxChecker --- scripts/error_codes.py | 4 +-- scripts/test_antlr_grammar.sh | 2 +- test/libsolidity/AnalysisFramework.cpp | 17 ++++++++++- test/libsolidity/AnalysisFramework.h | 1 + test/libsolidity/SolidityParser.cpp | 29 ------------------- .../wrong_compiler_recovers_1.sol | 3 ++ .../wrong_compiler_recovers_2.sol | 4 +++ .../wrong_compiler_recovers_3.sol | 7 +++++ .../wrong_compiler_recovers_4.sol | 7 +++++ .../syntaxTests/parsing/wrong_compiler_1.sol | 3 ++ .../syntaxTests/parsing/wrong_compiler_2.sol | 4 +++ .../syntaxTests/parsing/wrong_compiler_3.sol | 6 ++++ .../syntaxTests/parsing/wrong_compiler_4.sol | 6 ++++ 13 files changed, 60 insertions(+), 33 deletions(-) create mode 100644 test/libsolidity/errorRecoveryTests/wrong_compiler_recovers_1.sol create mode 100644 test/libsolidity/errorRecoveryTests/wrong_compiler_recovers_2.sol create mode 100644 test/libsolidity/errorRecoveryTests/wrong_compiler_recovers_3.sol create mode 100644 test/libsolidity/errorRecoveryTests/wrong_compiler_recovers_4.sol create mode 100644 test/libsolidity/syntaxTests/parsing/wrong_compiler_1.sol create mode 100644 test/libsolidity/syntaxTests/parsing/wrong_compiler_2.sol create mode 100644 test/libsolidity/syntaxTests/parsing/wrong_compiler_3.sol create mode 100644 test/libsolidity/syntaxTests/parsing/wrong_compiler_4.sol diff --git a/scripts/error_codes.py b/scripts/error_codes.py index e806165a7..2f7c0619a 100755 --- a/scripts/error_codes.py +++ b/scripts/error_codes.py @@ -223,8 +223,8 @@ def examine_id_coverage(top_dir, source_id_to_file_names, new_ids_only=False): "1123", "1133", "1220", "1584", "1823", "1950", "1988", "2418", "2461", "2512", "2592", "2657", "2800", "2842", "2856", "3263", "3356", "3441", "3682", "3876", - "3893", "3997", "4010", "4281", "4802", "4805", "4828", - "4904", "4990", "5052", "5073", "5170", "5188", "5272", "5333", "5347", "5473", + "3893", "4010", "4281", "4802", "4805", "4828", + "4904", "4990", "5052", "5073", "5170", "5188", "5272", "5347", "5473", "5622", "6041", "6052", "6272", "6708", "6792", "6931", "7110", "7128", "7186", "7589", "7593", "7653", "7812", "7885", "8065", "8084", "8140", "8261", "8312", "8592", "8758", "9011", diff --git a/scripts/test_antlr_grammar.sh b/scripts/test_antlr_grammar.sh index a41387ee9..801f8184f 100755 --- a/scripts/test_antlr_grammar.sh +++ b/scripts/test_antlr_grammar.sh @@ -114,7 +114,7 @@ do SOL_FILES+=("$line") done < <( grep -riL -E \ - "^\/\/ (Syntax|Type|Declaration)Error|^\/\/ ParserError (6275|3716|6281|2837|6933)|^==== Source:" \ + "^\/\/ (Syntax|Type|Declaration)Error|^\/\/ ParserError (2837|3716|3997|5333|6275|6281|6933)|^==== Source:" \ "${ROOT_DIR}/test/libsolidity/syntaxTests" \ "${ROOT_DIR}/test/libsolidity/semanticTests" \ ) diff --git a/test/libsolidity/AnalysisFramework.cpp b/test/libsolidity/AnalysisFramework.cpp index bd28de240..a55ed68c9 100644 --- a/test/libsolidity/AnalysisFramework.cpp +++ b/test/libsolidity/AnalysisFramework.cpp @@ -94,7 +94,22 @@ ErrorList AnalysisFramework::filterErrors(ErrorList const& _errorList, bool _inc continue; } - errors.emplace_back(currentError); + std::shared_ptr newError = currentError; + for (auto const& messagePrefix: m_messagesToCut) + if (currentError->comment()->find(messagePrefix) == 0) + { + SourceLocation const* location = boost::get_error_info(*currentError); + // sufficient for now, but in future we might clone the error completely, including the secondary location + newError = make_shared( + currentError->errorId(), + currentError->type(), + location ? *location : SourceLocation(), + messagePrefix + " ...." + ); + break; + } + + errors.emplace_back(newError); } return errors; diff --git a/test/libsolidity/AnalysisFramework.h b/test/libsolidity/AnalysisFramework.h index 293e0e17c..f6b9d92da 100644 --- a/test/libsolidity/AnalysisFramework.h +++ b/test/libsolidity/AnalysisFramework.h @@ -71,6 +71,7 @@ protected: langutil::ErrorList filterErrors(langutil::ErrorList const& _errorList, bool _includeWarnings) const; std::vector m_warningsToFilter = {"This is a pre-release compiler version"}; + std::vector m_messagesToCut = {"Source file requires different compiler version (current compiler is"}; /// @returns reference to lazy-instanciated CompilerStack. solidity::frontend::CompilerStack& compiler() diff --git a/test/libsolidity/SolidityParser.cpp b/test/libsolidity/SolidityParser.cpp index 41fe4b53b..2393ceeca 100644 --- a/test/libsolidity/SolidityParser.cpp +++ b/test/libsolidity/SolidityParser.cpp @@ -124,35 +124,6 @@ BOOST_AUTO_TEST_CASE(reserved_keywords) BOOST_CHECK(!TokenTraits::isReservedKeyword(Token::Illegal)); } -BOOST_AUTO_TEST_CASE(unsatisfied_version) -{ - char const* text = R"( - pragma solidity ^99.99.0; - )"; - CHECK_PARSE_ERROR(text, "Source file requires different compiler version"); -} - -BOOST_AUTO_TEST_CASE(unsatisfied_version_followed_by_invalid_syntax) -{ - char const* text = R"( - pragma solidity ^99.99.0; - this is surely invalid - )"; - CHECK_PARSE_ERROR(text, "Source file requires different compiler version"); -} - -BOOST_AUTO_TEST_CASE(unsatisfied_version_with_recovery) -{ - char const* text = R"( - pragma solidity ^99.99.0; - contract test { - uint ; - } - )"; - Error err = getError(text, true); - BOOST_CHECK(searchErrorMessage(err, "Expected identifier but got ';'")); -} - BOOST_AUTO_TEST_CASE(function_natspec_documentation) { char const* text = R"( diff --git a/test/libsolidity/errorRecoveryTests/wrong_compiler_recovers_1.sol b/test/libsolidity/errorRecoveryTests/wrong_compiler_recovers_1.sol new file mode 100644 index 000000000..9baa5638e --- /dev/null +++ b/test/libsolidity/errorRecoveryTests/wrong_compiler_recovers_1.sol @@ -0,0 +1,3 @@ +pragma solidity ^99.99.0; +// ---- +// SyntaxError 3997: (0-25): Source file requires different compiler version (current compiler is .... diff --git a/test/libsolidity/errorRecoveryTests/wrong_compiler_recovers_2.sol b/test/libsolidity/errorRecoveryTests/wrong_compiler_recovers_2.sol new file mode 100644 index 000000000..82f782f7e --- /dev/null +++ b/test/libsolidity/errorRecoveryTests/wrong_compiler_recovers_2.sol @@ -0,0 +1,4 @@ +pragma solidity ^99.99.0; +this is surely invalid +// ---- +// ParserError 7858: (26-30): Expected pragma, import directive or contract/interface/library/struct/enum/function definition. diff --git a/test/libsolidity/errorRecoveryTests/wrong_compiler_recovers_3.sol b/test/libsolidity/errorRecoveryTests/wrong_compiler_recovers_3.sol new file mode 100644 index 000000000..40fae8116 --- /dev/null +++ b/test/libsolidity/errorRecoveryTests/wrong_compiler_recovers_3.sol @@ -0,0 +1,7 @@ +pragma solidity ^99.99.0; +contract C { + uint ; +} +// ---- +// ParserError 6635: (48-49): Expected identifier but got ';' +// ParserError 6635: (50-51): Expected ';' but got '}' diff --git a/test/libsolidity/errorRecoveryTests/wrong_compiler_recovers_4.sol b/test/libsolidity/errorRecoveryTests/wrong_compiler_recovers_4.sol new file mode 100644 index 000000000..fadf021c7 --- /dev/null +++ b/test/libsolidity/errorRecoveryTests/wrong_compiler_recovers_4.sol @@ -0,0 +1,7 @@ +pragma solidity ^99.99.0; +contract C { + function f() {} +} +// ---- +// SyntaxError 3997: (0-25): Source file requires different compiler version (current compiler is .... +// SyntaxError 4937: (43-58): No visibility specified. Did you intend to add "public"? diff --git a/test/libsolidity/syntaxTests/parsing/wrong_compiler_1.sol b/test/libsolidity/syntaxTests/parsing/wrong_compiler_1.sol new file mode 100644 index 000000000..ea05513de --- /dev/null +++ b/test/libsolidity/syntaxTests/parsing/wrong_compiler_1.sol @@ -0,0 +1,3 @@ +pragma solidity ^99.99.0; +// ---- +// ParserError 5333: (0-25): Source file requires different compiler version (current compiler is .... diff --git a/test/libsolidity/syntaxTests/parsing/wrong_compiler_2.sol b/test/libsolidity/syntaxTests/parsing/wrong_compiler_2.sol new file mode 100644 index 000000000..6e97cd06c --- /dev/null +++ b/test/libsolidity/syntaxTests/parsing/wrong_compiler_2.sol @@ -0,0 +1,4 @@ +pragma solidity ^99.99.0; +this is surely invalid +// ---- +// ParserError 5333: (0-25): Source file requires different compiler version (current compiler is .... diff --git a/test/libsolidity/syntaxTests/parsing/wrong_compiler_3.sol b/test/libsolidity/syntaxTests/parsing/wrong_compiler_3.sol new file mode 100644 index 000000000..06d7c4dda --- /dev/null +++ b/test/libsolidity/syntaxTests/parsing/wrong_compiler_3.sol @@ -0,0 +1,6 @@ +pragma solidity ^99.99.0; +contract C { + uint ; +} +// ---- +// ParserError 5333: (0-25): Source file requires different compiler version (current compiler is .... diff --git a/test/libsolidity/syntaxTests/parsing/wrong_compiler_4.sol b/test/libsolidity/syntaxTests/parsing/wrong_compiler_4.sol new file mode 100644 index 000000000..0d052123e --- /dev/null +++ b/test/libsolidity/syntaxTests/parsing/wrong_compiler_4.sol @@ -0,0 +1,6 @@ +pragma solidity ^99.99.0; +contract C { + function f() {} +} +// ---- +// ParserError 5333: (0-25): Source file requires different compiler version (current compiler is ....