Skip to content

Commit

Permalink
Merge pull request ethereum#11903 from ethereum/printASTID
Browse files Browse the repository at this point in the history
Print @ast-id
  • Loading branch information
chriseth authored Sep 16, 2021
2 parents 2d75043 + a141589 commit bf60302
Show file tree
Hide file tree
Showing 20 changed files with 607 additions and 152 deletions.
3 changes: 1 addition & 2 deletions libsolidity/codegen/ir/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,9 @@ string dispenseLocationComment(langutil::SourceLocation const& _location, IRGene
{
solAssert(_location.sourceName, "");
_context.markSourceUsed(*_location.sourceName);
return AsmPrinter::formatSourceLocationComment(
return "/// " + AsmPrinter::formatSourceLocation(
_location,
_context.sourceIndices(),
true /* _statement */,
_context.soliditySourceProvider()
);
}
Expand Down
16 changes: 15 additions & 1 deletion libsolidity/codegen/ir/IRGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ string IRGenerator::generateFunction(FunctionDefinition const& _function)
return m_context.functionCollector().createFunction(functionName, [&]() {
m_context.resetLocalVariables();
Whiskers t(R"(
/// @ast-id <astID>
<sourceLocationComment>
function <functionName>(<params>)<?+retParams> -> <retParams></+retParams> {
<retInit>
Expand All @@ -348,6 +349,7 @@ string IRGenerator::generateFunction(FunctionDefinition const& _function)
<contractSourceLocationComment>
)");

t("astID", to_string(_function.id()));
t("sourceLocationComment", dispenseLocationComment(_function));
t(
"contractSourceLocationComment",
Expand Down Expand Up @@ -407,6 +409,7 @@ string IRGenerator::generateModifier(
return m_context.functionCollector().createFunction(functionName, [&]() {
m_context.resetLocalVariables();
Whiskers t(R"(
/// @ast-id <astID>
<sourceLocationComment>
function <functionName>(<params>)<?+retParams> -> <retParams></+retParams> {
<assignRetParams>
Expand Down Expand Up @@ -437,6 +440,7 @@ string IRGenerator::generateModifier(
_modifierInvocation.name().annotation().referencedDeclaration
);
solAssert(modifier, "");
t("astID", to_string(modifier->id()));
t("sourceLocationComment", dispenseLocationComment(*modifier));
t(
"contractSourceLocationComment",
Expand Down Expand Up @@ -542,12 +546,14 @@ string IRGenerator::generateGetter(VariableDeclaration const& _varDecl)
solAssert(paramTypes.empty(), "");
solUnimplementedAssert(type->sizeOnStack() == 1, "");
return Whiskers(R"(
/// @ast-id <astID>
<sourceLocationComment>
function <functionName>() -> rval {
rval := loadimmutable("<id>")
}
<contractSourceLocationComment>
)")
("astID", to_string(_varDecl.id()))
("sourceLocationComment", dispenseLocationComment(_varDecl))
(
"contractSourceLocationComment",
Expand All @@ -561,12 +567,14 @@ string IRGenerator::generateGetter(VariableDeclaration const& _varDecl)
{
solAssert(paramTypes.empty(), "");
return Whiskers(R"(
/// @ast-id <astID>
<sourceLocationComment>
function <functionName>() -> <ret> {
<ret> := <constantValueFunction>()
}
<contractSourceLocationComment>
)")
("astID", to_string(_varDecl.id()))
("sourceLocationComment", dispenseLocationComment(_varDecl))
(
"contractSourceLocationComment",
Expand Down Expand Up @@ -683,6 +691,7 @@ string IRGenerator::generateGetter(VariableDeclaration const& _varDecl)
}

return Whiskers(R"(
/// @ast-id <astID>
<sourceLocationComment>
function <functionName>(<params>) -> <retVariables> {
<code>
Expand All @@ -693,6 +702,7 @@ string IRGenerator::generateGetter(VariableDeclaration const& _varDecl)
("params", joinHumanReadable(parameters))
("retVariables", joinHumanReadable(returnVariables))
("code", std::move(code))
("astID", to_string(_varDecl.id()))
("sourceLocationComment", dispenseLocationComment(_varDecl))
(
"contractSourceLocationComment",
Expand Down Expand Up @@ -804,7 +814,7 @@ void IRGenerator::generateConstructors(ContractDefinition const& _contract)
m_context.resetLocalVariables();
m_context.functionCollector().createFunction(IRNames::constructor(*contract), [&]() {
Whiskers t(R"(
<sourceLocationComment>
<astIDComment><sourceLocationComment>
function <functionName>(<params><comma><baseParams>) {
<evalBaseArguments>
<sourceLocationComment>
Expand All @@ -819,6 +829,10 @@ void IRGenerator::generateConstructors(ContractDefinition const& _contract)
for (ASTPointer<VariableDeclaration> const& varDecl: contract->constructor()->parameters())
params += m_context.addLocalVariable(*varDecl).stackSlots();

if (contract->constructor())
t("astIDComment", "/// @ast-id " + to_string(contract->constructor()->id()) + "\n");
else
t("astIDComment", "");
t("sourceLocationComment", dispenseLocationComment(
contract->constructor() ?
dynamic_cast<ASTNode const&>(*contract->constructor()) :
Expand Down
76 changes: 43 additions & 33 deletions libyul/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ using namespace solidity::yul;

string AsmPrinter::operator()(Literal const& _literal)
{
string const locationComment = formatSourceLocationComment(_literal);
string const locationComment = formatDebugData(_literal);

switch (_literal.kind)
{
Expand All @@ -65,19 +65,19 @@ string AsmPrinter::operator()(Literal const& _literal)
string AsmPrinter::operator()(Identifier const& _identifier)
{
yulAssert(!_identifier.name.empty(), "Invalid identifier.");
return formatSourceLocationComment(_identifier) + _identifier.name.str();
return formatDebugData(_identifier) + _identifier.name.str();
}

string AsmPrinter::operator()(ExpressionStatement const& _statement)
{
string const locationComment = formatSourceLocationComment(_statement);
string const locationComment = formatDebugData(_statement);

return locationComment + std::visit(*this, _statement.expression);
}

string AsmPrinter::operator()(Assignment const& _assignment)
{
string const locationComment = formatSourceLocationComment(_assignment);
string const locationComment = formatDebugData(_assignment);

yulAssert(_assignment.variableNames.size() >= 1, "");
string variables = (*this)(_assignment.variableNames.front());
Expand All @@ -89,7 +89,7 @@ string AsmPrinter::operator()(Assignment const& _assignment)

string AsmPrinter::operator()(VariableDeclaration const& _variableDeclaration)
{
string out = formatSourceLocationComment(_variableDeclaration);
string out = formatDebugData(_variableDeclaration);

out += "let ";
out += boost::algorithm::join(
Expand All @@ -110,7 +110,7 @@ string AsmPrinter::operator()(FunctionDefinition const& _functionDefinition)
{
yulAssert(!_functionDefinition.name.empty(), "Invalid function name.");

string out = formatSourceLocationComment(_functionDefinition);
string out = formatDebugData(_functionDefinition);
out += "function " + _functionDefinition.name.str() + "(";
out += boost::algorithm::join(
_functionDefinition.parameters | ranges::views::transform(
Expand All @@ -135,7 +135,7 @@ string AsmPrinter::operator()(FunctionDefinition const& _functionDefinition)

string AsmPrinter::operator()(FunctionCall const& _functionCall)
{
string const locationComment = formatSourceLocationComment(_functionCall);
string const locationComment = formatDebugData(_functionCall);
string const functionName = (*this)(_functionCall.functionName);
return
locationComment +
Expand All @@ -150,7 +150,7 @@ string AsmPrinter::operator()(If const& _if)
{
yulAssert(_if.condition, "Invalid if condition.");

string out = formatSourceLocationComment(_if);
string out = formatDebugData(_if);
out += "if " + std::visit(*this, *_if.condition);

string body = (*this)(_if.body);
Expand All @@ -165,7 +165,7 @@ string AsmPrinter::operator()(Switch const& _switch)
{
yulAssert(_switch.expression, "Invalid expression pointer.");

string out = formatSourceLocationComment(_switch);
string out = formatDebugData(_switch);
out += "switch " + std::visit(*this, *_switch.expression);

for (auto const& _case: _switch.cases)
Expand All @@ -182,7 +182,7 @@ string AsmPrinter::operator()(Switch const& _switch)
string AsmPrinter::operator()(ForLoop const& _forLoop)
{
yulAssert(_forLoop.condition, "Invalid for loop condition.");
string const locationComment = formatSourceLocationComment(_forLoop);
string const locationComment = formatDebugData(_forLoop);

string pre = (*this)(_forLoop.pre);
string condition = std::visit(*this, *_forLoop.condition);
Expand All @@ -203,23 +203,23 @@ string AsmPrinter::operator()(ForLoop const& _forLoop)

string AsmPrinter::operator()(Break const& _break)
{
return formatSourceLocationComment(_break) + "break";
return formatDebugData(_break) + "break";
}

string AsmPrinter::operator()(Continue const& _continue)
{
return formatSourceLocationComment(_continue) + "continue";
return formatDebugData(_continue) + "continue";
}

// '_leave' and '__leave' is reserved in VisualStudio
string AsmPrinter::operator()(Leave const& leave_)
{
return formatSourceLocationComment(leave_) + "leave";
return formatDebugData(leave_) + "leave";
}

string AsmPrinter::operator()(Block const& _block)
{
string const locationComment = formatSourceLocationComment(_block);
string const locationComment = formatDebugData(_block);

if (_block.statements.empty())
return locationComment + "{ }";
Expand All @@ -239,7 +239,7 @@ string AsmPrinter::operator()(Block const& _block)
string AsmPrinter::formatTypedName(TypedName _variable)
{
yulAssert(!_variable.name.empty(), "Invalid variable name.");
return formatSourceLocationComment(_variable) + _variable.name.str() + appendTypeName(_variable.type);
return formatDebugData(_variable) + _variable.name.str() + appendTypeName(_variable.type);
}

string AsmPrinter::appendTypeName(YulString _type, bool _isBoolLiteral) const
Expand All @@ -258,10 +258,9 @@ string AsmPrinter::appendTypeName(YulString _type, bool _isBoolLiteral) const
return ":" + _type.str();
}

string AsmPrinter::formatSourceLocationComment(
string AsmPrinter::formatSourceLocation(
SourceLocation const& _location,
map<string, unsigned> const& _nameToSourceIndex,
bool _statement,
CharStreamProvider const* _soliditySourceProvider
)
{
Expand Down Expand Up @@ -294,27 +293,38 @@ string AsmPrinter::formatSourceLocationComment(
":" +
to_string(_location.end);

return
_statement ?
"/// " + joinHumanReadable(vector<string>{sourceLocation, solidityCodeSnippet}, " ") :
"/** " + joinHumanReadable(vector<string>{sourceLocation, solidityCodeSnippet}, " ") + " */ ";
return joinHumanReadable(vector<string>{sourceLocation, solidityCodeSnippet}, " ");
}

string AsmPrinter::formatSourceLocationComment(shared_ptr<DebugData const> const& _debugData, bool _statement)
string AsmPrinter::formatDebugData(shared_ptr<DebugData const> const& _debugData, bool _statement)
{
if (!_debugData)
return "";

vector<string> items;
if (auto id = _debugData->astID)
items.emplace_back("@ast-id " + to_string(*id));

if (
!_debugData ||
m_lastLocation == _debugData->location ||
m_nameToSourceIndex.empty()
m_lastLocation != _debugData->location &&
!m_nameToSourceIndex.empty()
)
return "";
{
m_lastLocation = _debugData->location;

m_lastLocation = _debugData->location;
items.emplace_back(formatSourceLocation(
_debugData->location,
m_nameToSourceIndex,
m_soliditySourceProvider
));
}

return formatSourceLocationComment(
_debugData->location,
m_nameToSourceIndex,
_statement,
m_soliditySourceProvider
) + (_statement ? "\n" : "");
string commentBody = joinHumanReadable(items, " ");
if (commentBody.empty())
return "";
else
return
_statement ?
"/// " + commentBody + "\n" :
"/** " + commentBody + " */ ";
}
9 changes: 4 additions & 5 deletions libyul/AsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,21 @@ class AsmPrinter
std::string operator()(Leave const& _continue);
std::string operator()(Block const& _block);

static std::string formatSourceLocationComment(
static std::string formatSourceLocation(
langutil::SourceLocation const& _location,
std::map<std::string, unsigned> const& _nameToSourceIndex,
bool _statement,
langutil::CharStreamProvider const* m_soliditySourceProvider = nullptr
);

private:
std::string formatTypedName(TypedName _variable);
std::string appendTypeName(YulString _type, bool _isBoolLiteral = false) const;
std::string formatSourceLocationComment(std::shared_ptr<DebugData const> const& _debugData, bool _statement);
std::string formatDebugData(std::shared_ptr<DebugData const> const& _debugData, bool _statement);
template <class T>
std::string formatSourceLocationComment(T const& _node)
std::string formatDebugData(T const& _node)
{
bool isExpression = std::is_constructible<Expression, T>::value;
return formatSourceLocationComment(_node.debugData, !isExpression);
return formatDebugData(_node.debugData, !isExpression);
}

Dialect const* const m_dialect = nullptr;
Expand Down
1 change: 1 addition & 0 deletions test/cmdlineTests/exp_base_literal/output
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ object "C_81" {
power := exp(1, exponent)
}

/// @ast-id 80
/// @src 0:96:368 "function f(uint a, uint b, uint c, uint d) public pure returns (uint, int, uint, uint) {..."
function fun_f_80(var_a_4, var_b_6, var_c_8, var_d_10) -> var__13, var__15, var__17, var__19 {
/// @src 0:160:164 "uint"
Expand Down
2 changes: 1 addition & 1 deletion test/cmdlineTests/name_simplifier/output
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ object "C_59" {
mstore(4, 0x32)
revert(0, 0x24)
}
/// @src 0:381:623 "function sumArray(S[] memory _s) public returns (uint, string memory) {..."
/// @ast-id 58 @src 0:381:623 "function sumArray(S[] memory _s) public returns (uint, string memory) {..."
function fun_sumArray(var_s_mpos) -> var, var_mpos
{
/// @src 0:346:625 "contract C {..."
Expand Down
1 change: 1 addition & 0 deletions test/cmdlineTests/revert_strings/output
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ object "C_15" {

}

/// @ast-id 14
/// @src 0:93:145 "function f(uint[][] memory, E e) public pure {..."
function fun_f_14(var__7_mpos, var_e_10) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ object \"C_7\" {
{ tail := add(headStart, 0) }
function revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74()
{ revert(0, 0) }
/// @src 0:92:119 \"function f() public pure {}\"
/// @ast-id 6 @src 0:92:119 \"function f() public pure {}\"
function fun_f_6()
{ }
}
Expand Down
1 change: 1 addition & 0 deletions test/cmdlineTests/standard_ir_requested/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ object \"C_7\" {
revert(0, 0)
}

/// @ast-id 6
/// @src 0:92:119 \"function f() public pure {}\"
function fun_f_6() {

Expand Down
1 change: 1 addition & 0 deletions test/cmdlineTests/standard_viair_requested/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ object \"D_16\" {
revert(pos, returndatasize())
}

/// @ast-id 15
/// @src 0:106:144 \"function f() public { C c = new C(); }\"
function fun_f_15() {

Expand Down
22 changes: 22 additions & 0 deletions test/cmdlineTests/standard_yul_source_locations/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"language": "Solidity",
"sources":
{
"C":
{
"content": "//SPDX-License-Identifier: GPL-2.0\npragma solidity >=0.0;\npragma abicoder v2;\n\ncontract C\n{\n int public constant constVar = 41;\n int immutable immutVar = 42;\n int public stateVar;\n\n constructor(int _init)\n {\n stateVar = _init;\n }\n\n function f() external pure returns (int)\n {\n return constVar + immutVar;\n }\n modifier m()\n {\n stateVar++;\n _;\n }\n function f2() m public returns (int)\n {\n return stateVar + this.f() + immutVar;\n }\n}\n"
},
"D":
{
"content": "//SPDX-License-Identifier: GPL-2.0\npragma solidity >=0.0;\npragma abicoder v2;\nimport \"C\";\n\ncontract D is C(3)\n{\n constructor(int _init2)\n {\n stateVar += _init2;\n }\n}\n"
}
},
"settings":
{
"outputSelection":
{
"*": { "*": ["ir", "irOptimized"] }
},
"optimizer": { "enabled": true }
}
}
Loading

0 comments on commit bf60302

Please sign in to comment.