Yul: Adds parsing @src comment in AsmParser to customize the AST's sourcer locations.

This commit is contained in:
Christian Parpart
2021-07-09 11:48:00 +02:00
parent 43cde4e175
commit 132fa46faa
6 changed files with 513 additions and 17 deletions
+368 -1
View File
@@ -51,12 +51,25 @@ namespace solidity::yul::test
namespace
{
string_view constexpr g_strAlternateSourceText = "{}";
shared_ptr<Block> parse(string const& _source, Dialect const& _dialect, ErrorReporter& errorReporter)
{
try
{
auto scanner = make_shared<Scanner>(CharStream(_source, ""));
auto parserResult = yul::Parser(errorReporter, _dialect).parse(scanner, false);
map<unsigned, shared_ptr<CharStream>> indicesToCharStreams;
indicesToCharStreams[0] = scanner->charStream();
indicesToCharStreams[1] = make_shared<CharStream>(
string(g_strAlternateSourceText.data(), g_strAlternateSourceText.size()),
"alternate.sol"
);
auto parserResult = yul::Parser(
errorReporter,
_dialect,
move(indicesToCharStreams)
).parse(scanner, false);
if (parserResult)
{
yul::AsmAnalysisInfo analysisInfo;
@@ -187,7 +200,361 @@ BOOST_AUTO_TEST_CASE(default_types_set)
);
}
#define CHECK_LOCATION(_actual, _sourceText, _start, _end) \
do { \
BOOST_CHECK_EQUAL((_sourceText), ((_actual).source ? (_actual).source->source() : "")); \
BOOST_CHECK_EQUAL((_start), (_actual).start); \
BOOST_CHECK_EQUAL((_end), (_actual).end); \
} while (0)
BOOST_AUTO_TEST_CASE(customSourceLocations_empty_block)
{
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText =
"/// @src 0:234:543\n"
"{}\n";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
CHECK_LOCATION(result->debugData->location, sourceText, 234, 543);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_block_with_children)
{
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText =
"/// @src 0:234:543\n"
"{\n"
"let x:bool := true:bool\n"
"/// @src 0:123:432\n"
"let z:bool := true\n"
"let y := add(1, 2)\n"
"}\n";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
CHECK_LOCATION(result->debugData->location, sourceText, 234, 543);
BOOST_REQUIRE_EQUAL(3, result->statements.size());
CHECK_LOCATION(locationOf(result->statements.at(0)), sourceText, 234, 543);
CHECK_LOCATION(locationOf(result->statements.at(1)), sourceText, 123, 432);
// [2] is inherited source location
CHECK_LOCATION(locationOf(result->statements.at(2)), sourceText, 123, 432);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_block_different_sources)
{
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText =
"/// @src 0:234:543\n"
"{\n"
"let x:bool := true:bool\n"
"/// @src 1:123:432\n"
"let z:bool := true\n"
"let y := add(1, 2)\n"
"}\n";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
CHECK_LOCATION(result->debugData->location, sourceText, 234, 543);
BOOST_REQUIRE_EQUAL(3, result->statements.size());
CHECK_LOCATION(locationOf(result->statements.at(0)), sourceText, 234, 543);
CHECK_LOCATION(locationOf(result->statements.at(1)), g_strAlternateSourceText, 123, 432);
// [2] is inherited source location
CHECK_LOCATION(locationOf(result->statements.at(2)), g_strAlternateSourceText, 123, 432);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_block_nested)
{
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText =
"/// @src 0:234:543\n"
"{\n"
"let y := add(1, 2)\n"
"/// @src 0:343:434\n"
"switch y case 0 {} default {}\n"
"}\n";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
CHECK_LOCATION(result->debugData->location, sourceText, 234, 543);
BOOST_REQUIRE_EQUAL(2, result->statements.size());
CHECK_LOCATION(locationOf(result->statements.at(1)), sourceText, 343, 434);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_block_switch_case)
{
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText =
"/// @src 0:234:543\n"
"{\n"
"let y := add(1, 2)\n"
"/// @src 0:343:434\n"
"switch y\n"
"/// @src 0:3141:59265\n"
"case 0 {\n"
" /// @src 0:271:828\n"
" let z := add(3, 4)\n"
"}\n"
"}\n";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
CHECK_LOCATION(result->debugData->location, sourceText, 234, 543);
BOOST_REQUIRE_EQUAL(2, result->statements.size());
BOOST_REQUIRE(holds_alternative<Switch>(result->statements.at(1)));
auto const& switchStmt = get<Switch>(result->statements.at(1));
CHECK_LOCATION(switchStmt.debugData->location, sourceText, 343, 434);
BOOST_REQUIRE_EQUAL(1, switchStmt.cases.size());
CHECK_LOCATION(switchStmt.cases.at(0).debugData->location, sourceText, 3141, 59265);
auto const& caseBody = switchStmt.cases.at(0).body;
BOOST_REQUIRE_EQUAL(1, caseBody.statements.size());
CHECK_LOCATION(locationOf(caseBody.statements.at(0)), sourceText, 271, 828);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_inherit_into_outer_scope)
{
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText =
"/// @src 0:1:100\n"
"{\n"
"{\n"
"/// @src 0:123:432\n"
"let x:bool := true:bool\n"
"}\n"
"let z:bool := true\n"
"let y := add(1, 2)\n"
"}\n";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
CHECK_LOCATION(result->debugData->location, sourceText, 1, 100);
BOOST_REQUIRE_EQUAL(3, result->statements.size());
CHECK_LOCATION(locationOf(result->statements.at(0)), sourceText, 1, 100);
// First child element must be a block itself with one statement.
BOOST_REQUIRE(holds_alternative<Block>(result->statements.at(0)));
BOOST_REQUIRE_EQUAL(get<Block>(result->statements.at(0)).statements.size(), 1);
CHECK_LOCATION(locationOf(get<Block>(result->statements.at(0)).statements.at(0)), sourceText, 123, 432);
// The next two elements have an inherited source location from the prior inner scope.
CHECK_LOCATION(locationOf(result->statements.at(1)), sourceText, 123, 432);
CHECK_LOCATION(locationOf(result->statements.at(2)), sourceText, 123, 432);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_assign_empty)
{
// Tests single AST node (e.g. VariableDeclaration) with different source locations for each child.
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText =
"{\n"
"/// @src 0:123:432\n"
"let a:bool\n"
"/// @src 1:1:10\n"
"a := true:bool\n"
"}\n";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result); // should still parse
BOOST_REQUIRE_EQUAL(2, result->statements.size());
CHECK_LOCATION(locationOf(result->statements.at(0)), sourceText, 123, 432);
CHECK_LOCATION(locationOf(result->statements.at(1)), g_strAlternateSourceText, 1, 10);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_invalid_source_index)
{
// Tests single AST node (e.g. VariableDeclaration) with different source locations for each child.
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText =
"{\n"
"/// @src 1:123:432\n"
"let a:bool := true:bool\n"
"/// @src 2345:0:8\n"
"let b:bool := true:bool\n"
"\n"
"}\n";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result); // should still parse
}
BOOST_AUTO_TEST_CASE(customSourceLocations_mixed_locations_1)
{
// Tests single AST node (e.g. VariableDeclaration) with different source locations for each child.
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText =
"{\n"
"/// @src 0:123:432\n"
"let x:bool \n"
"/// @src 0:234:2026\n"
":= true:bool\n"
"}\n";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
BOOST_REQUIRE_EQUAL(1, result->statements.size());
CHECK_LOCATION(locationOf(result->statements.at(0)), sourceText, 123, 432);
BOOST_REQUIRE(holds_alternative<VariableDeclaration>(result->statements.at(0)));
VariableDeclaration const& varDecl = get<VariableDeclaration>(result->statements.at(0));
CHECK_LOCATION(locationOf(*varDecl.value), sourceText, 234, 2026);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_mixed_locations_2)
{
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText = R"(
/// @src 0:0:5
{
let x := /// @src 1:2:3
add(1, /// @src 0:4:8
2)
}
)";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
BOOST_REQUIRE_EQUAL(1, result->statements.size());
CHECK_LOCATION(result->debugData->location, sourceText, 0, 5);
// `let x := add(1, `
BOOST_REQUIRE(holds_alternative<VariableDeclaration>(result->statements.at(0)));
VariableDeclaration const& varDecl = get<VariableDeclaration>(result->statements.at(0));
CHECK_LOCATION(varDecl.debugData->location, sourceText, 0, 5);
BOOST_REQUIRE(!!varDecl.value);
BOOST_REQUIRE(holds_alternative<FunctionCall>(*varDecl.value));
FunctionCall const& call = get<FunctionCall>(*varDecl.value);
CHECK_LOCATION(call.debugData->location, g_strAlternateSourceText, 2, 3);
// `2`
BOOST_REQUIRE_EQUAL(2, call.arguments.size());
CHECK_LOCATION(locationOf(call.arguments.at(1)), sourceText, 4, 8);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_mixed_locations_3)
{
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText = R"(
/// @src 1:23:45
{ // Block
{ // Block
sstore(0, 1) // FunctionCall
/// @src 0:420:680
}
mstore(1, 2) // FunctionCall
}
)";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
BOOST_REQUIRE_EQUAL(2, result->statements.size());
CHECK_LOCATION(result->debugData->location, g_strAlternateSourceText, 23, 45);
BOOST_REQUIRE(holds_alternative<Block>(result->statements.at(0)));
Block const& innerBlock = get<Block>(result->statements.at(0));
CHECK_LOCATION(innerBlock.debugData->location, g_strAlternateSourceText, 23, 45);
BOOST_REQUIRE_EQUAL(1, innerBlock.statements.size());
BOOST_REQUIRE(holds_alternative<ExpressionStatement>(result->statements.at(1)));
ExpressionStatement const& sstoreStmt = get<ExpressionStatement>(innerBlock.statements.at(0));
BOOST_REQUIRE(holds_alternative<FunctionCall>(sstoreStmt.expression));
FunctionCall const& sstoreCall = get<FunctionCall>(sstoreStmt.expression);
CHECK_LOCATION(sstoreCall.debugData->location, g_strAlternateSourceText, 23, 45);
BOOST_REQUIRE(holds_alternative<ExpressionStatement>(result->statements.at(1)));
ExpressionStatement mstoreStmt = get<ExpressionStatement>(result->statements.at(1));
BOOST_REQUIRE(holds_alternative<FunctionCall>(mstoreStmt.expression));
FunctionCall const& mstoreCall = get<FunctionCall>(mstoreStmt.expression);
CHECK_LOCATION(mstoreCall.debugData->location, sourceText, 420, 680);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_invalid_comments_after_valid)
{
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText = R"(
/// @src 1:23:45
{
/// @src 0:420:680
/// @invalid
let a:bool := true
}
)";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
BOOST_REQUIRE_EQUAL(1, result->statements.size());
CHECK_LOCATION(result->debugData->location, g_strAlternateSourceText, 23, 45);
BOOST_REQUIRE(holds_alternative<VariableDeclaration>(result->statements.at(0)));
VariableDeclaration const& varDecl = get<VariableDeclaration>(result->statements.at(0));
CHECK_LOCATION(varDecl.debugData->location, sourceText, 420, 680);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_invalid_suffix)
{
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText = R"(
/// @src 0:420:680foo
{}
)";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
CHECK_LOCATION(result->debugData->location, "", -1, -1);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_unspecified)
{
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText = R"(
/// @src -1:-1:-1
{}
)";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
CHECK_LOCATION(result->debugData->location, "", -1, -1);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_ensure_last_match)
{
ErrorList errorList;
ErrorReporter reporter(errorList);
auto const sourceText = R"(
/// @src 0:123:432
{
/// @src 1:10:20
/// @src 0:30:40
let x:bool := true
}
)";
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
BOOST_REQUIRE(holds_alternative<VariableDeclaration>(result->statements.at(0)));
VariableDeclaration const& varDecl = get<VariableDeclaration>(result->statements.at(0));
// Ensure the latest @src per documentation-comment is used (0:30:40).
CHECK_LOCATION(varDecl.debugData->location, sourceText, 30, 40);
}
BOOST_AUTO_TEST_SUITE_END()