mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Parse @ast-id annotation.
This commit is contained in:
parent
42739b73b1
commit
05d20446bb
18
libyul/AST.h
18
libyul/AST.h
@ -38,14 +38,22 @@ using Type = YulString;
|
|||||||
|
|
||||||
struct DebugData
|
struct DebugData
|
||||||
{
|
{
|
||||||
explicit DebugData(langutil::SourceLocation _location): location(std::move(_location)) {}
|
explicit DebugData(langutil::SourceLocation _location, std::optional<int64_t> _astID = {}):
|
||||||
|
location(std::move(_location)),
|
||||||
|
astID(std::move(_astID))
|
||||||
|
{}
|
||||||
|
|
||||||
|
static std::shared_ptr<DebugData const> create(
|
||||||
|
langutil::SourceLocation _location = {},
|
||||||
|
std::optional<int64_t> _astID = {}
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return std::make_shared<DebugData const>(std::move(_location), std::move(_astID));
|
||||||
|
}
|
||||||
|
|
||||||
langutil::SourceLocation location;
|
langutil::SourceLocation location;
|
||||||
/// ID in the (Solidity) source AST.
|
/// ID in the (Solidity) source AST.
|
||||||
std::optional<int64_t> astID;
|
std::optional<int64_t> astID;
|
||||||
static std::shared_ptr<DebugData const> create(langutil::SourceLocation _location = {})
|
|
||||||
{
|
|
||||||
return std::make_shared<DebugData const>(_location);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TypedName { std::shared_ptr<DebugData const> debugData; YulString name; Type type; };
|
struct TypedName { std::shared_ptr<DebugData const> debugData; YulString name; Type type; };
|
||||||
|
@ -103,7 +103,7 @@ unique_ptr<Block> Parser::parseInline(std::shared_ptr<Scanner> const& _scanner)
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
m_scanner = _scanner;
|
m_scanner = _scanner;
|
||||||
if (m_sourceNames)
|
if (m_useSourceLocationFrom == UseSourceLocationFrom::Comments)
|
||||||
fetchDebugDataFromComment();
|
fetchDebugDataFromComment();
|
||||||
return make_unique<Block>(parseBlock());
|
return make_unique<Block>(parseBlock());
|
||||||
}
|
}
|
||||||
@ -136,6 +136,8 @@ void Parser::fetchDebugDataFromComment()
|
|||||||
match_results<string_view::const_iterator> match;
|
match_results<string_view::const_iterator> match;
|
||||||
|
|
||||||
langutil::SourceLocation sourceLocation = m_debugDataOverride->location;
|
langutil::SourceLocation sourceLocation = m_debugDataOverride->location;
|
||||||
|
// Empty for each new node.
|
||||||
|
optional<int> astID;
|
||||||
|
|
||||||
while (regex_search(commentLiteral.cbegin(), commentLiteral.cend(), match, tagRegex))
|
while (regex_search(commentLiteral.cbegin(), commentLiteral.cend(), match, tagRegex))
|
||||||
{
|
{
|
||||||
@ -149,12 +151,19 @@ void Parser::fetchDebugDataFromComment()
|
|||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
else if (match[1] == "@ast-id")
|
||||||
|
{
|
||||||
|
if (auto parseResult = parseASTIDComment(commentLiteral, m_scanner->currentCommentLocation()))
|
||||||
|
tie(commentLiteral, astID) = *parseResult;
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
// Ignore unrecognized tags.
|
// Ignore unrecognized tags.
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_debugDataOverride = DebugData::create(sourceLocation);
|
m_debugDataOverride = DebugData::create(sourceLocation, astID);
|
||||||
}
|
}
|
||||||
|
|
||||||
optional<pair<string_view, SourceLocation>> Parser::parseSrcComment(
|
optional<pair<string_view, SourceLocation>> Parser::parseSrcComment(
|
||||||
@ -222,6 +231,38 @@ optional<pair<string_view, SourceLocation>> Parser::parseSrcComment(
|
|||||||
return {{tail, SourceLocation{}}};
|
return {{tail, SourceLocation{}}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
optional<pair<string_view, optional<int>>> Parser::parseASTIDComment(
|
||||||
|
string_view _arguments,
|
||||||
|
langutil::SourceLocation const& _commentLocation
|
||||||
|
)
|
||||||
|
{
|
||||||
|
static regex const argRegex = regex(
|
||||||
|
R"~~(^(\d+)(?:\s|$))~~",
|
||||||
|
regex_constants::ECMAScript | regex_constants::optimize
|
||||||
|
);
|
||||||
|
match_results<string_view::const_iterator> match;
|
||||||
|
optional<int> astID;
|
||||||
|
bool matched = regex_search(_arguments.cbegin(), _arguments.cend(), match, argRegex);
|
||||||
|
string_view tail = _arguments;
|
||||||
|
if (matched)
|
||||||
|
{
|
||||||
|
solAssert(match.size() == 2, "");
|
||||||
|
tail = _arguments.substr(static_cast<size_t>(match.position() + match.length()));
|
||||||
|
|
||||||
|
astID = toInt(match[1].str());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!matched || !astID || *astID < 0 || static_cast<int64_t>(*astID) != *astID)
|
||||||
|
{
|
||||||
|
m_errorReporter.syntaxError(1749_error, _commentLocation, "Invalid argument for @ast-id.");
|
||||||
|
astID = nullopt;
|
||||||
|
}
|
||||||
|
if (matched)
|
||||||
|
return {{_arguments, astID}};
|
||||||
|
else
|
||||||
|
return nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
Block Parser::parseBlock()
|
Block Parser::parseBlock()
|
||||||
{
|
{
|
||||||
RecursionGuard recursionGuard(*this);
|
RecursionGuard recursionGuard(*this);
|
||||||
|
@ -108,8 +108,12 @@ protected:
|
|||||||
|
|
||||||
void fetchDebugDataFromComment();
|
void fetchDebugDataFromComment();
|
||||||
|
|
||||||
std::optional<std::pair<std::string_view, langutil::SourceLocation>>
|
std::optional<std::pair<std::string_view, langutil::SourceLocation>> parseSrcComment(
|
||||||
parseSrcComment(
|
std::string_view _arguments,
|
||||||
|
langutil::SourceLocation const& _commentLocation
|
||||||
|
);
|
||||||
|
|
||||||
|
std::optional<std::pair<std::string_view, std::optional<int>>> parseASTIDComment(
|
||||||
std::string_view _arguments,
|
std::string_view _arguments,
|
||||||
langutil::SourceLocation const& _commentLocation
|
langutil::SourceLocation const& _commentLocation
|
||||||
);
|
);
|
||||||
|
@ -804,6 +804,51 @@ BOOST_AUTO_TEST_CASE(customSourceLocations_with_code_snippets_with_nested_locati
|
|||||||
CHECK_LOCATION(literal128.debugData->location, "source1", 96, 165);
|
CHECK_LOCATION(literal128.debugData->location, "source1", 96, 165);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(astid)
|
||||||
|
{
|
||||||
|
ErrorList errorList;
|
||||||
|
ErrorReporter reporter(errorList);
|
||||||
|
auto const sourceText = R"(
|
||||||
|
/// @src -1:-1:-1 @ast-id 7
|
||||||
|
{
|
||||||
|
/** @ast-id 2 */
|
||||||
|
function f(x) -> y {}
|
||||||
|
mstore(1, 2)
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
|
||||||
|
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
|
||||||
|
BOOST_REQUIRE(!!result);
|
||||||
|
BOOST_CHECK(result->debugData->astID == int64_t(7));
|
||||||
|
auto const& funDef = get<FunctionDefinition>(result->statements.at(0));
|
||||||
|
BOOST_CHECK(funDef.debugData->astID == int64_t(2));
|
||||||
|
BOOST_CHECK(funDef.parameters.at(0).debugData->astID == nullopt);
|
||||||
|
BOOST_CHECK(debugDataOf(result->statements.at(1))->astID == nullopt);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(astid_reset)
|
||||||
|
{
|
||||||
|
ErrorList errorList;
|
||||||
|
ErrorReporter reporter(errorList);
|
||||||
|
auto const sourceText = R"(
|
||||||
|
/// @src -1:-1:-1 @ast-id 7 @src 1:1:1
|
||||||
|
{
|
||||||
|
/** @ast-id 2 */
|
||||||
|
function f(x) -> y {}
|
||||||
|
mstore(1, 2)
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
EVMDialectTyped const& dialect = EVMDialectTyped::instance(EVMVersion{});
|
||||||
|
shared_ptr<Block> result = parse(sourceText, dialect, reporter);
|
||||||
|
BOOST_REQUIRE(!!result);
|
||||||
|
BOOST_CHECK(result->debugData->astID == int64_t(7));
|
||||||
|
auto const& funDef = get<FunctionDefinition>(result->statements.at(0));
|
||||||
|
BOOST_CHECK(funDef.debugData->astID == int64_t(2));
|
||||||
|
BOOST_CHECK(funDef.parameters.at(0).debugData->astID == nullopt);
|
||||||
|
BOOST_CHECK(debugDataOf(result->statements.at(1))->astID == nullopt);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(customSourceLocations_multiple_src_tags_on_one_line)
|
BOOST_AUTO_TEST_CASE(customSourceLocations_multiple_src_tags_on_one_line)
|
||||||
{
|
{
|
||||||
ErrorList errorList;
|
ErrorList errorList;
|
||||||
|
Loading…
Reference in New Issue
Block a user