Deduplicate code for printing source locations

This commit is contained in:
Kamil Śliwak
2021-09-14 11:46:57 +02:00
parent 49cde9d47b
commit 37f681c430
3 changed files with 50 additions and 24 deletions
+33 -16
View File
@@ -38,6 +38,7 @@
using namespace std;
using namespace solidity;
using namespace solidity::langutil;
using namespace solidity::util;
using namespace solidity::yul;
@@ -256,7 +257,33 @@ string AsmPrinter::appendTypeName(YulString _type, bool _isBoolLiteral) const
return ":" + _type.str();
}
string AsmPrinter::formatSourceLocationComment(shared_ptr<DebugData const> const& _debugData, bool _statement)
string AsmPrinter::formatSourceLocationComment(
SourceLocation const& _location,
map<string, unsigned> const& _nameToSourceIndex,
bool _statement
)
{
yulAssert(!_nameToSourceIndex.empty(), "");
string sourceIndex = "-1";
if (_location.sourceName)
sourceIndex = to_string(_nameToSourceIndex.at(*_location.sourceName));
string sourceLocation =
"@src " +
sourceIndex +
":" +
to_string(_location.start) +
":" +
to_string(_location.end);
return
_statement ?
"/// " + sourceLocation :
"/** " + sourceLocation + " */ ";
}
string AsmPrinter::formatSourceLocationComment(shared_ptr<DebugData const> const& _debugData, bool _statement)
{
if (
!_debugData ||
@@ -267,19 +294,9 @@ string AsmPrinter::formatSourceLocationComment(shared_ptr<DebugData const> const
m_lastLocation = _debugData->location;
string sourceIndex = "-1";
if (_debugData->location.sourceName)
sourceIndex = to_string(m_nameToSourceIndex.at(*_debugData->location.sourceName));
string sourceLocation =
"@src " +
sourceIndex +
":" +
to_string(_debugData->location.start) +
":" +
to_string(_debugData->location.end);
return
_statement ?
"/// " + sourceLocation + "\n" :
"/** " + sourceLocation + " */ ";
return formatSourceLocationComment(
_debugData->location,
m_nameToSourceIndex,
_statement
) + (_statement ? "\n" : "");
}