Deduplicate code for printing source locations

This commit is contained in:
Kamil Śliwak 2021-08-31 15:08:16 +02:00
parent 49cde9d47b
commit 37f681c430
3 changed files with 50 additions and 24 deletions

View File

@ -22,9 +22,13 @@
#include <libsolutil/CommonIO.h>
#include <libyul/AsmPrinter.h>
using namespace std;
using namespace solidity::util;
using namespace solidity::langutil;
using namespace solidity::frontend;
using namespace solidity::util;
using namespace solidity::yul;
namespace solidity::frontend
{
@ -131,12 +135,11 @@ string dispenseLocationComment(langutil::SourceLocation const& _location, IRGene
{
solAssert(_location.sourceName, "");
_context.markSourceUsed(*_location.sourceName);
return "/// @src "
+ to_string(_context.sourceIndices().at(*_location.sourceName))
+ ":"
+ to_string(_location.start)
+ ":"
+ to_string(_location.end);
return AsmPrinter::formatSourceLocationComment(
_location,
_context.sourceIndices(),
true /* _statement */
);
}
string dispenseLocationComment(ASTNode const& _node, IRGenerationContext& _context)

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,6 +257,32 @@ string AsmPrinter::appendTypeName(YulString _type, bool _isBoolLiteral) const
return ":" + _type.str();
}
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 (
@ -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" : "");
}

View File

@ -76,6 +76,12 @@ public:
std::string operator()(Leave const& _continue);
std::string operator()(Block const& _block);
static std::string formatSourceLocationComment(
langutil::SourceLocation const& _location,
std::map<std::string, unsigned> const& _nameToSourceIndex,
bool _statement
);
private:
std::string formatTypedName(TypedName _variable);
std::string appendTypeName(YulString _type, bool _isBoolLiteral = false) const;
@ -88,7 +94,7 @@ private:
}
Dialect const* const m_dialect = nullptr;
std::map<std::string const, unsigned> m_nameToSourceIndex;
std::map<std::string, unsigned> m_nameToSourceIndex;
langutil::SourceLocation m_lastLocation = {};
};