Merge pull request #11913 from ethereum/source-location-parsing-extra-tests-and-regex-refactor

More generic source location parsing + more errors
This commit is contained in:
chriseth 2021-09-13 19:06:29 +02:00 committed by GitHub
commit f957820351
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 159 additions and 37 deletions

View File

@ -130,37 +130,73 @@ void Parser::fetchSourceLocationFromComment()
if (m_scanner->currentCommentLiteral().empty()) if (m_scanner->currentCommentLiteral().empty())
return; return;
static regex const lineRE = std::regex( static regex const tagRegex = regex(
R"~~~((^|\s*)@src\s+(-1|\d+):(-1|\d+):(-1|\d+)(\s+|$))~~~", R"~~((?:^|\s+)(@[a-zA-Z0-9\-_]+)(?:\s+|$))~~", // tag, e.g: @src
std::regex_constants::ECMAScript | std::regex_constants::optimize regex_constants::ECMAScript | regex_constants::optimize
);
static regex const srcTagArgsRegex = regex(
R"~~(^(-1|\d+):(-1|\d+):(-1|\d+)(?:\s+|$))~~", // index and location, e.g.: 1:234:-1
regex_constants::ECMAScript | regex_constants::optimize
); );
string const text = m_scanner->currentCommentLiteral(); string const commentLiteral = m_scanner->currentCommentLiteral();
auto from = sregex_iterator(text.begin(), text.end(), lineRE); SourceLocation const commentLocation = m_scanner->currentCommentLocation();
auto to = sregex_iterator(); smatch tagMatch;
string::const_iterator position = commentLiteral.begin();
for (auto const& matchResult: ranges::make_subrange(from, to)) while (regex_search(position, commentLiteral.end(), tagMatch, tagRegex))
{ {
solAssert(matchResult.size() == 6, ""); solAssert(tagMatch.size() == 2, "");
position += tagMatch.position() + tagMatch.length();
auto const sourceIndex = toInt(matchResult[2].str()); if (tagMatch[1] == "@src")
auto const start = toInt(matchResult[3].str());
auto const end = toInt(matchResult[4].str());
auto const commentLocation = m_scanner->currentCommentLocation();
m_debugDataOverride = DebugData::create();
if (!sourceIndex || !start || !end)
m_errorReporter.syntaxError(6367_error, commentLocation, "Invalid value in source location mapping. Could not parse location specification.");
else if (sourceIndex == -1)
m_debugDataOverride = DebugData::create(SourceLocation{*start, *end, nullptr});
else if (!(sourceIndex >= 0 && m_sourceNames->count(static_cast<unsigned>(*sourceIndex))))
m_errorReporter.syntaxError(2674_error, commentLocation, "Invalid source mapping. Source index not defined via @use-src.");
else
{ {
shared_ptr<string const> sourceName = m_sourceNames->at(static_cast<unsigned>(*sourceIndex)); smatch srcTagArgsMatch;
solAssert(sourceName, ""); if (!regex_search(position, commentLiteral.end(), srcTagArgsMatch, srcTagArgsRegex))
m_debugDataOverride = DebugData::create(SourceLocation{*start, *end, move(sourceName)}); {
m_errorReporter.syntaxError(
8387_error,
commentLocation,
"Invalid values in source location mapping. Could not parse location specification."
);
// If the arguments to @src are malformed, we don't know where they end so we can't continue.
return;
}
solAssert(srcTagArgsMatch.size() == 4, "");
position += srcTagArgsMatch.position() + srcTagArgsMatch.length();
optional<int> const sourceIndex = toInt(srcTagArgsMatch[1].str());
optional<int> const start = toInt(srcTagArgsMatch[2].str());
optional<int> const end = toInt(srcTagArgsMatch[3].str());
m_debugDataOverride = DebugData::create();
if (!sourceIndex.has_value() || !start.has_value() || !end.has_value())
m_errorReporter.syntaxError(
6367_error,
commentLocation,
"Invalid value in source location mapping. "
"Expected non-negative integer values or -1 for source index and location."
);
else if (sourceIndex == -1)
m_debugDataOverride = DebugData::create(SourceLocation{start.value(), end.value(), nullptr});
else if (!(sourceIndex >= 0 && m_sourceNames->count(static_cast<unsigned>(sourceIndex.value()))))
m_errorReporter.syntaxError(
2674_error,
commentLocation,
"Invalid source mapping. Source index not defined via @use-src."
);
else
{
shared_ptr<string const> sourceName = m_sourceNames->at(static_cast<unsigned>(sourceIndex.value()));
solAssert(sourceName, "");
m_debugDataOverride = DebugData::create(SourceLocation{start.value(), end.value(), move(sourceName)});
}
} }
else
// Ignore unrecognized tags.
continue;
} }
} }

View File

@ -194,6 +194,7 @@ def examine_id_coverage(top_dir, source_id_to_file_names, new_ids_only=False):
"9804", # Tested in test/libyul/ObjectParser.cpp. "9804", # Tested in test/libyul/ObjectParser.cpp.
"2674", "2674",
"6367", "6367",
"8387",
"3805", # "This is a pre-release compiler version, please do not use it in production." "3805", # "This is a pre-release compiler version, please do not use it in production."
# The warning may or may not exist in a compiler build. # The warning may or may not exist in a compiler build.
"4591", # "There are more than 256 warnings. Ignoring the rest." "4591", # "There are more than 256 warnings. Ignoring the rest."

View File

@ -44,6 +44,9 @@ using namespace solidity;
using namespace solidity::util; using namespace solidity::util;
using namespace solidity::langutil; using namespace solidity::langutil;
BOOST_TEST_DONT_PRINT_LOG_VALUE(ErrorId)
BOOST_TEST_DONT_PRINT_LOG_VALUE(Error::Type)
namespace solidity::yul::test namespace solidity::yul::test
{ {
@ -165,7 +168,7 @@ BOOST_AUTO_TEST_CASE(default_types_set)
EVMDialectTyped::instance(EVMVersion{}), EVMDialectTyped::instance(EVMVersion{}),
reporter reporter
); );
BOOST_REQUIRE(!!result); BOOST_REQUIRE(!!result && errorList.size() == 0);
// Use no dialect so that all types are printed. // Use no dialect so that all types are printed.
// This tests that the default types are properly assigned. // This tests that the default types are properly assigned.
@ -210,7 +213,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_empty_block)
"{}\n"; "{}\n";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{}); EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter); shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result); BOOST_REQUIRE(!!result && errorList.size() == 0);
CHECK_LOCATION(result->debugData->location, "source0", 234, 543); CHECK_LOCATION(result->debugData->location, "source0", 234, 543);
} }
@ -251,7 +254,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_block_different_sources)
"}\n"; "}\n";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{}); EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter); shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result); BOOST_REQUIRE(!!result && errorList.size() == 0);
CHECK_LOCATION(result->debugData->location, "source0", 234, 543); CHECK_LOCATION(result->debugData->location, "source0", 234, 543);
BOOST_REQUIRE_EQUAL(3, result->statements.size()); BOOST_REQUIRE_EQUAL(3, result->statements.size());
CHECK_LOCATION(locationOf(result->statements.at(0)), "source0", 234, 543); CHECK_LOCATION(locationOf(result->statements.at(0)), "source0", 234, 543);
@ -273,7 +276,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_block_nested)
"}\n"; "}\n";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{}); EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter); shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result); BOOST_REQUIRE(!!result && errorList.size() == 0);
CHECK_LOCATION(result->debugData->location, "source0", 234, 543); CHECK_LOCATION(result->debugData->location, "source0", 234, 543);
BOOST_REQUIRE_EQUAL(2, result->statements.size()); BOOST_REQUIRE_EQUAL(2, result->statements.size());
CHECK_LOCATION(locationOf(result->statements.at(1)), "source0", 343, 434); CHECK_LOCATION(locationOf(result->statements.at(1)), "source0", 343, 434);
@ -297,7 +300,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_block_switch_case)
"}\n"; "}\n";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{}); EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter); shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result); BOOST_REQUIRE(!!result && errorList.size() == 0);
CHECK_LOCATION(result->debugData->location, "source0", 234, 543); CHECK_LOCATION(result->debugData->location, "source0", 234, 543);
BOOST_REQUIRE_EQUAL(2, result->statements.size()); BOOST_REQUIRE_EQUAL(2, result->statements.size());
@ -329,7 +332,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_inherit_into_outer_scope)
"}\n"; "}\n";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{}); EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter); shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result); BOOST_REQUIRE(!!result && errorList.size() == 0);
CHECK_LOCATION(result->debugData->location, "source0", 1, 100); CHECK_LOCATION(result->debugData->location, "source0", 1, 100);
@ -360,7 +363,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_assign_empty)
"}\n"; "}\n";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{}); EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter); shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result); // should still parse BOOST_REQUIRE(!!result && errorList.size() == 0); // should still parse
BOOST_REQUIRE_EQUAL(2, result->statements.size()); BOOST_REQUIRE_EQUAL(2, result->statements.size());
CHECK_LOCATION(locationOf(result->statements.at(0)), "source0", 123, 432); CHECK_LOCATION(locationOf(result->statements.at(0)), "source0", 123, 432);
CHECK_LOCATION(locationOf(result->statements.at(1)), "source1", 1, 10); CHECK_LOCATION(locationOf(result->statements.at(1)), "source1", 1, 10);
@ -382,6 +385,9 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_invalid_source_index)
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{}); EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter); shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result); // should still parse BOOST_REQUIRE(!!result); // should still parse
BOOST_REQUIRE(errorList.size() == 1);
BOOST_TEST(errorList[0]->type() == Error::Type::SyntaxError);
BOOST_TEST(errorList[0]->errorId() == 2674_error);
} }
BOOST_AUTO_TEST_CASE(customSourceLocations_mixed_locations_1) BOOST_AUTO_TEST_CASE(customSourceLocations_mixed_locations_1)
@ -398,7 +404,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_mixed_locations_1)
"}\n"; "}\n";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{}); EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter); shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result); BOOST_REQUIRE(!!result && errorList.size() == 0);
BOOST_REQUIRE_EQUAL(1, result->statements.size()); BOOST_REQUIRE_EQUAL(1, result->statements.size());
CHECK_LOCATION(locationOf(result->statements.at(0)), "source0", 123, 432); CHECK_LOCATION(locationOf(result->statements.at(0)), "source0", 123, 432);
@ -421,7 +427,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_mixed_locations_2)
)"; )";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{}); EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter); shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result); BOOST_REQUIRE(!!result && errorList.size() == 0);
BOOST_REQUIRE_EQUAL(1, result->statements.size()); BOOST_REQUIRE_EQUAL(1, result->statements.size());
CHECK_LOCATION(result->debugData->location, "source0", 0, 5); CHECK_LOCATION(result->debugData->location, "source0", 0, 5);
@ -455,7 +461,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_mixed_locations_3)
)"; )";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{}); EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter); shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result); BOOST_REQUIRE(!!result && errorList.size() == 0);
BOOST_REQUIRE_EQUAL(2, result->statements.size()); BOOST_REQUIRE_EQUAL(2, result->statements.size());
CHECK_LOCATION(result->debugData->location, "source1", 23, 45); CHECK_LOCATION(result->debugData->location, "source1", 23, 45);
@ -491,7 +497,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_invalid_comments_after_valid)
)"; )";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{}); EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter); shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result); BOOST_REQUIRE(!!result && errorList.size() == 0);
BOOST_REQUIRE_EQUAL(1, result->statements.size()); BOOST_REQUIRE_EQUAL(1, result->statements.size());
CHECK_LOCATION(result->debugData->location, "source1", 23, 45); CHECK_LOCATION(result->debugData->location, "source1", 23, 45);
@ -511,6 +517,23 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_invalid_suffix)
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{}); EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter); shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result); BOOST_REQUIRE(!!result);
BOOST_REQUIRE(errorList.size() == 1);
BOOST_TEST(errorList[0]->type() == Error::Type::SyntaxError);
BOOST_TEST(errorList[0]->errorId() == 8387_error);
CHECK_LOCATION(result->debugData->location, "", -1, -1);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_invalid_prefix)
{
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText = R"(
/// abc@src 0:111:222
{}
)";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result && errorList.size() == 0);
CHECK_LOCATION(result->debugData->location, "", -1, -1); CHECK_LOCATION(result->debugData->location, "", -1, -1);
} }
@ -524,7 +547,41 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_unspecified)
)"; )";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{}); EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter); shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result && errorList.size() == 0);
CHECK_LOCATION(result->debugData->location, "", -1, -1);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_non_integer)
{
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText = R"(
/// @src a:b:c
{}
)";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result); BOOST_REQUIRE(!!result);
BOOST_REQUIRE(errorList.size() == 1);
BOOST_TEST(errorList[0]->type() == Error::Type::SyntaxError);
BOOST_TEST(errorList[0]->errorId() == 8387_error);
CHECK_LOCATION(result->debugData->location, "", -1, -1);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_bad_integer)
{
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText = R"(
/// @src 111111111111111111111:222222222222222222222:333333333333333333333
{}
)";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
BOOST_REQUIRE(errorList.size() == 1);
BOOST_TEST(errorList[0]->type() == Error::Type::SyntaxError);
BOOST_TEST(errorList[0]->errorId() == 6367_error);
CHECK_LOCATION(result->debugData->location, "", -1, -1); CHECK_LOCATION(result->debugData->location, "", -1, -1);
} }
@ -542,7 +599,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_ensure_last_match)
)"; )";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{}); EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter); shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result); BOOST_REQUIRE(!!result && errorList.size() == 0);
BOOST_REQUIRE(holds_alternative<VariableDeclaration>(result->statements.at(0))); BOOST_REQUIRE(holds_alternative<VariableDeclaration>(result->statements.at(0)));
VariableDeclaration const& varDecl = get<VariableDeclaration>(result->statements.at(0)); VariableDeclaration const& varDecl = get<VariableDeclaration>(result->statements.at(0));
@ -550,6 +607,34 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_ensure_last_match)
CHECK_LOCATION(varDecl.debugData->location, "source0", 30, 40); CHECK_LOCATION(varDecl.debugData->location, "source0", 30, 40);
} }
BOOST_AUTO_TEST_CASE(customSourceLocations_two_locations_no_whitespace)
{
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText = R"(
/// @src 0:111:222@src 1:333:444
{}
)";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
BOOST_REQUIRE(errorList.size() == 1);
BOOST_TEST(errorList[0]->type() == Error::Type::SyntaxError);
BOOST_TEST(errorList[0]->errorId() == 8387_error);
CHECK_LOCATION(result->debugData->location, "", -1, -1);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_leading_trailing_whitespace)
{
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText = "/// @src 0:111:222 \n{}";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result && errorList.size() == 0);
CHECK_LOCATION(result->debugData->location, "source0", 111, 222);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_reference_original_sloc) BOOST_AUTO_TEST_CASE(customSourceLocations_reference_original_sloc)
{ {
ErrorList errorList; ErrorList errorList;
@ -563,7 +648,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_reference_original_sloc)
)"; )";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{}); EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter); shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result); BOOST_REQUIRE(!!result && errorList.size() == 0);
BOOST_REQUIRE(holds_alternative<VariableDeclaration>(result->statements.at(0))); BOOST_REQUIRE(holds_alternative<VariableDeclaration>(result->statements.at(0)));
VariableDeclaration const& varDecl = get<VariableDeclaration>(result->statements.at(0)); VariableDeclaration const& varDecl = get<VariableDeclaration>(result->statements.at(0));