Skip to content

Commit 5e4868d

Browse files
Adapted tests due to more precise Yul source locations.
Also added support for -1 source index, referencing original scanner's source location.
1 parent 132fa46 commit 5e4868d

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

libyul/AsmParser.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,13 @@ void Parser::fetchSourceLocationFromComment()
131131
m_locationOverride = SourceLocation{};
132132
if (!sourceIndex || !start || !end)
133133
m_errorReporter.syntaxError(6367_error, commentLocation, "Invalid value in source location mapping. Could not parse location specification.");
134-
else if (!((start < 0 && end < 0) || (start >= 0 && *start <= *end)))
134+
else if (!((*start < 0 && *end < 0) || (*start >= 0 && *start <= *end)))
135135
m_errorReporter.syntaxError(5798_error, commentLocation, "Invalid value in source location mapping. Start offset larger than end offset.");
136+
else if (sourceIndex == -1 && (0 <= *start && *start <= *end)) // Use source index -1 to indicate original source.
137+
m_locationOverride = SourceLocation{*start, *end, ParserBase::currentLocation().source};
136138
else if (!(sourceIndex >= 0 && m_charStreamMap->count(static_cast<unsigned>(*sourceIndex))))
137139
m_errorReporter.syntaxError(2674_error, commentLocation, "Invalid source mapping. Source index not defined via @use-src.");
138-
else if (sourceIndex >= 0)
140+
else
139141
{
140142
shared_ptr<CharStream> charStream = m_charStreamMap->at(static_cast<unsigned>(*sourceIndex));
141143
solAssert(charStream, "");

test/libyul/Parser.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,28 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_ensure_last_match)
556556
CHECK_LOCATION(varDecl.debugData->location, sourceText, 30, 40);
557557
}
558558

559+
BOOST_AUTO_TEST_CASE(customSourceLocations_reference_original_sloc)
560+
{
561+
ErrorList errorList;
562+
ErrorReporter reporter(errorList);
563+
auto const sourceText = R"(
564+
/// @src 1:2:3
565+
{
566+
/// @src -1:10:20
567+
let x:bool := true
568+
}
569+
)";
570+
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
571+
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
572+
BOOST_REQUIRE(!!result);
573+
BOOST_REQUIRE(holds_alternative<VariableDeclaration>(result->statements.at(0)));
574+
VariableDeclaration const& varDecl = get<VariableDeclaration>(result->statements.at(0));
575+
576+
// -1 points to original source code, which in this case is `sourceText` (which is also
577+
// available via `0`, that's why the first @src is set to `1` instead.)
578+
CHECK_LOCATION(varDecl.debugData->location, sourceText, 10, 20);
579+
}
580+
559581
BOOST_AUTO_TEST_SUITE_END()
560582

561583
} // end namespaces

0 commit comments

Comments
 (0)