Remove CharStream from SourceLocation.

This commit is contained in:
chriseth
2021-07-14 15:12:07 +02:00
parent 57d32ca252
commit f75b55071e
73 changed files with 613 additions and 560 deletions
+42 -46
View File
@@ -51,24 +51,19 @@ 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, ""));
map<unsigned, shared_ptr<CharStream>> indicesToCharStreams;
indicesToCharStreams[0] = scanner->charStream();
indicesToCharStreams[1] = make_shared<CharStream>(
string(g_strAlternateSourceText.data(), g_strAlternateSourceText.size()),
"alternate.sol"
);
map<unsigned, shared_ptr<string const>> indicesToSourceNames;
indicesToSourceNames[0] = make_shared<string const>("source0");
indicesToSourceNames[1] = make_shared<string const>("source1");
auto parserResult = yul::Parser(
errorReporter,
_dialect,
move(indicesToCharStreams)
move(indicesToSourceNames)
).parse(scanner, false);
if (parserResult)
{
@@ -200,9 +195,9 @@ BOOST_AUTO_TEST_CASE(default_types_set)
);
}
#define CHECK_LOCATION(_actual, _sourceText, _start, _end) \
#define CHECK_LOCATION(_actual, _sourceName, _start, _end) \
do { \
BOOST_CHECK_EQUAL((_sourceText), ((_actual).source ? (_actual).source->source() : "")); \
BOOST_CHECK_EQUAL((_sourceName), ((_actual).sourceName ? *(_actual).sourceName : "")); \
BOOST_CHECK_EQUAL((_start), (_actual).start); \
BOOST_CHECK_EQUAL((_end), (_actual).end); \
} while (0)
@@ -217,7 +212,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_empty_block)
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);
CHECK_LOCATION(result->debugData->location, "source0", 234, 543);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_block_with_children)
@@ -235,12 +230,12 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_block_with_children)
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);
CHECK_LOCATION(result->debugData->location, "source0", 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);
CHECK_LOCATION(locationOf(result->statements.at(0)), "source0", 234, 543);
CHECK_LOCATION(locationOf(result->statements.at(1)), "source0", 123, 432);
// [2] is inherited source location
CHECK_LOCATION(locationOf(result->statements.at(2)), sourceText, 123, 432);
CHECK_LOCATION(locationOf(result->statements.at(2)), "source0", 123, 432);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_block_different_sources)
@@ -258,12 +253,12 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_block_different_sources)
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);
CHECK_LOCATION(result->debugData->location, "source0", 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);
CHECK_LOCATION(locationOf(result->statements.at(0)), "source0", 234, 543);
CHECK_LOCATION(locationOf(result->statements.at(1)), "source1", 123, 432);
// [2] is inherited source location
CHECK_LOCATION(locationOf(result->statements.at(2)), g_strAlternateSourceText, 123, 432);
CHECK_LOCATION(locationOf(result->statements.at(2)), "source1", 123, 432);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_block_nested)
@@ -280,9 +275,9 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_block_nested)
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);
CHECK_LOCATION(result->debugData->location, "source0", 234, 543);
BOOST_REQUIRE_EQUAL(2, result->statements.size());
CHECK_LOCATION(locationOf(result->statements.at(1)), sourceText, 343, 434);
CHECK_LOCATION(locationOf(result->statements.at(1)), "source0", 343, 434);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_block_switch_case)
@@ -304,19 +299,19 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_block_switch_case)
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);
CHECK_LOCATION(result->debugData->location, "source0", 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);
CHECK_LOCATION(switchStmt.debugData->location, "source0", 343, 434);
BOOST_REQUIRE_EQUAL(1, switchStmt.cases.size());
CHECK_LOCATION(switchStmt.cases.at(0).debugData->location, sourceText, 3141, 59265);
CHECK_LOCATION(switchStmt.cases.at(0).debugData->location, "source0", 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);
CHECK_LOCATION(locationOf(caseBody.statements.at(0)), "source0", 271, 828);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_inherit_into_outer_scope)
@@ -337,19 +332,19 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_inherit_into_outer_scope)
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
BOOST_REQUIRE(!!result);
CHECK_LOCATION(result->debugData->location, sourceText, 1, 100);
CHECK_LOCATION(result->debugData->location, "source0", 1, 100);
BOOST_REQUIRE_EQUAL(3, result->statements.size());
CHECK_LOCATION(locationOf(result->statements.at(0)), sourceText, 1, 100);
CHECK_LOCATION(locationOf(result->statements.at(0)), "source0", 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);
CHECK_LOCATION(locationOf(get<Block>(result->statements.at(0)).statements.at(0)), "source0", 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);
CHECK_LOCATION(locationOf(result->statements.at(1)), "source0", 123, 432);
CHECK_LOCATION(locationOf(result->statements.at(2)), "source0", 123, 432);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_assign_empty)
@@ -368,8 +363,8 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_assign_empty)
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);
CHECK_LOCATION(locationOf(result->statements.at(0)), "source0", 123, 432);
CHECK_LOCATION(locationOf(result->statements.at(1)), "source1", 1, 10);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_invalid_source_index)
@@ -407,10 +402,10 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_mixed_locations_1)
BOOST_REQUIRE(!!result);
BOOST_REQUIRE_EQUAL(1, result->statements.size());
CHECK_LOCATION(locationOf(result->statements.at(0)), sourceText, 123, 432);
CHECK_LOCATION(locationOf(result->statements.at(0)), "source0", 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);
CHECK_LOCATION(locationOf(*varDecl.value), "source0", 234, 2026);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_mixed_locations_2)
@@ -429,20 +424,20 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_mixed_locations_2)
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);
CHECK_LOCATION(result->debugData->location, "source0", 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);
CHECK_LOCATION(varDecl.debugData->location, "source0", 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);
CHECK_LOCATION(call.debugData->location, "source1", 2, 3);
// `2`
BOOST_REQUIRE_EQUAL(2, call.arguments.size());
CHECK_LOCATION(locationOf(call.arguments.at(1)), sourceText, 4, 8);
CHECK_LOCATION(locationOf(call.arguments.at(1)), "source0", 4, 8);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_mixed_locations_3)
@@ -463,24 +458,24 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_mixed_locations_3)
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);
CHECK_LOCATION(result->debugData->location, "source1", 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);
CHECK_LOCATION(innerBlock.debugData->location, "source1", 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);
CHECK_LOCATION(sstoreCall.debugData->location, "source1", 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);
CHECK_LOCATION(mstoreCall.debugData->location, "source0", 420, 680);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_invalid_comments_after_valid)
@@ -499,11 +494,11 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_invalid_comments_after_valid)
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);
CHECK_LOCATION(result->debugData->location, "source1", 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);
CHECK_LOCATION(varDecl.debugData->location, "source0", 420, 680);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_invalid_suffix)
@@ -553,7 +548,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_ensure_last_match)
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);
CHECK_LOCATION(varDecl.debugData->location, "source0", 30, 40);
}
BOOST_AUTO_TEST_CASE(customSourceLocations_reference_original_sloc)
@@ -573,6 +568,7 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_reference_original_sloc)
BOOST_REQUIRE(holds_alternative<VariableDeclaration>(result->statements.at(0)));
VariableDeclaration const& varDecl = get<VariableDeclaration>(result->statements.at(0));
// -1 points to original source code, which in this case is `"source0"` (which is also
CHECK_LOCATION(varDecl.debugData->location, "", 10, 20);
}