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

View File

@ -38,6 +38,7 @@
using namespace std; using namespace std;
using namespace solidity; using namespace solidity;
using namespace solidity::langutil;
using namespace solidity::util; using namespace solidity::util;
using namespace solidity::yul; using namespace solidity::yul;
@ -256,7 +257,33 @@ string AsmPrinter::appendTypeName(YulString _type, bool _isBoolLiteral) const
return ":" + _type.str(); 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 ( if (
!_debugData || !_debugData ||
@ -267,19 +294,9 @@ string AsmPrinter::formatSourceLocationComment(shared_ptr<DebugData const> const
m_lastLocation = _debugData->location; m_lastLocation = _debugData->location;
string sourceIndex = "-1"; return formatSourceLocationComment(
if (_debugData->location.sourceName) _debugData->location,
sourceIndex = to_string(m_nameToSourceIndex.at(*_debugData->location.sourceName)); m_nameToSourceIndex,
_statement
string sourceLocation = ) + (_statement ? "\n" : "");
"@src " +
sourceIndex +
":" +
to_string(_debugData->location.start) +
":" +
to_string(_debugData->location.end);
return
_statement ?
"/// " + sourceLocation + "\n" :
"/** " + sourceLocation + " */ ";
} }

View File

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