mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
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:
commit
f957820351
@ -130,37 +130,73 @@ void Parser::fetchSourceLocationFromComment()
|
||||
if (m_scanner->currentCommentLiteral().empty())
|
||||
return;
|
||||
|
||||
static regex const lineRE = std::regex(
|
||||
R"~~~((^|\s*)@src\s+(-1|\d+):(-1|\d+):(-1|\d+)(\s+|$))~~~",
|
||||
std::regex_constants::ECMAScript | std::regex_constants::optimize
|
||||
static regex const tagRegex = regex(
|
||||
R"~~((?:^|\s+)(@[a-zA-Z0-9\-_]+)(?:\s+|$))~~", // tag, e.g: @src
|
||||
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();
|
||||
auto from = sregex_iterator(text.begin(), text.end(), lineRE);
|
||||
auto to = sregex_iterator();
|
||||
string const commentLiteral = m_scanner->currentCommentLiteral();
|
||||
SourceLocation const commentLocation = m_scanner->currentCommentLocation();
|
||||
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());
|
||||
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
|
||||
if (tagMatch[1] == "@src")
|
||||
{
|
||||
shared_ptr<string const> sourceName = m_sourceNames->at(static_cast<unsigned>(*sourceIndex));
|
||||
solAssert(sourceName, "");
|
||||
m_debugDataOverride = DebugData::create(SourceLocation{*start, *end, move(sourceName)});
|
||||
smatch srcTagArgsMatch;
|
||||
if (!regex_search(position, commentLiteral.end(), srcTagArgsMatch, srcTagArgsRegex))
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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.
|
||||
"2674",
|
||||
"6367",
|
||||
"8387",
|
||||
"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.
|
||||
"4591", # "There are more than 256 warnings. Ignoring the rest."
|
||||
|
@ -44,6 +44,9 @@ using namespace solidity;
|
||||
using namespace solidity::util;
|
||||
using namespace solidity::langutil;
|
||||
|
||||
BOOST_TEST_DONT_PRINT_LOG_VALUE(ErrorId)
|
||||
BOOST_TEST_DONT_PRINT_LOG_VALUE(Error::Type)
|
||||
|
||||
namespace solidity::yul::test
|
||||
{
|
||||
|
||||
@ -165,7 +168,7 @@ BOOST_AUTO_TEST_CASE(default_types_set)
|
||||
EVMDialectTyped::instance(EVMVersion{}),
|
||||
reporter
|
||||
);
|
||||
BOOST_REQUIRE(!!result);
|
||||
BOOST_REQUIRE(!!result && errorList.size() == 0);
|
||||
|
||||
// Use no dialect so that all types are printed.
|
||||
// This tests that the default types are properly assigned.
|
||||
@ -210,7 +213,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_empty_block)
|
||||
"{}\n";
|
||||
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
|
||||
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);
|
||||
}
|
||||
|
||||
@ -251,7 +254,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_block_different_sources)
|
||||
"}\n";
|
||||
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
|
||||
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);
|
||||
BOOST_REQUIRE_EQUAL(3, result->statements.size());
|
||||
CHECK_LOCATION(locationOf(result->statements.at(0)), "source0", 234, 543);
|
||||
@ -273,7 +276,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_block_nested)
|
||||
"}\n";
|
||||
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
|
||||
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);
|
||||
BOOST_REQUIRE_EQUAL(2, result->statements.size());
|
||||
CHECK_LOCATION(locationOf(result->statements.at(1)), "source0", 343, 434);
|
||||
@ -297,7 +300,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_block_switch_case)
|
||||
"}\n";
|
||||
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
|
||||
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);
|
||||
|
||||
BOOST_REQUIRE_EQUAL(2, result->statements.size());
|
||||
@ -329,7 +332,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_inherit_into_outer_scope)
|
||||
"}\n";
|
||||
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
|
||||
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);
|
||||
|
||||
@ -360,7 +363,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_assign_empty)
|
||||
"}\n";
|
||||
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
|
||||
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());
|
||||
CHECK_LOCATION(locationOf(result->statements.at(0)), "source0", 123, 432);
|
||||
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{});
|
||||
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
|
||||
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)
|
||||
@ -398,7 +404,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_mixed_locations_1)
|
||||
"}\n";
|
||||
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
|
||||
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
|
||||
BOOST_REQUIRE(!!result);
|
||||
BOOST_REQUIRE(!!result && errorList.size() == 0);
|
||||
|
||||
BOOST_REQUIRE_EQUAL(1, result->statements.size());
|
||||
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{});
|
||||
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
|
||||
BOOST_REQUIRE(!!result);
|
||||
BOOST_REQUIRE(!!result && errorList.size() == 0);
|
||||
BOOST_REQUIRE_EQUAL(1, result->statements.size());
|
||||
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{});
|
||||
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
|
||||
BOOST_REQUIRE(!!result);
|
||||
BOOST_REQUIRE(!!result && errorList.size() == 0);
|
||||
BOOST_REQUIRE_EQUAL(2, result->statements.size());
|
||||
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{});
|
||||
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
|
||||
BOOST_REQUIRE(!!result);
|
||||
BOOST_REQUIRE(!!result && errorList.size() == 0);
|
||||
BOOST_REQUIRE_EQUAL(1, result->statements.size());
|
||||
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{});
|
||||
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_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);
|
||||
}
|
||||
|
||||
@ -524,7 +547,41 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_unspecified)
|
||||
)";
|
||||
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);
|
||||
}
|
||||
|
||||
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(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);
|
||||
}
|
||||
|
||||
@ -542,7 +599,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_ensure_last_match)
|
||||
)";
|
||||
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
|
||||
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)));
|
||||
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);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
ErrorList errorList;
|
||||
@ -563,7 +648,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_reference_original_sloc)
|
||||
)";
|
||||
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
|
||||
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)));
|
||||
VariableDeclaration const& varDecl = get<VariableDeclaration>(result->statements.at(0));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user