From 37f681c430af65181b9c1a5f5dee774fd043f7ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Tue, 31 Aug 2021 15:08:16 +0200 Subject: [PATCH] Deduplicate code for printing source locations --- libsolidity/codegen/ir/Common.cpp | 17 ++++++----- libyul/AsmPrinter.cpp | 49 +++++++++++++++++++++---------- libyul/AsmPrinter.h | 8 ++++- 3 files changed, 50 insertions(+), 24 deletions(-) diff --git a/libsolidity/codegen/ir/Common.cpp b/libsolidity/codegen/ir/Common.cpp index 772e9b53e..aec812c12 100644 --- a/libsolidity/codegen/ir/Common.cpp +++ b/libsolidity/codegen/ir/Common.cpp @@ -22,9 +22,13 @@ #include +#include + 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) diff --git a/libyul/AsmPrinter.cpp b/libyul/AsmPrinter.cpp index 2a7b705f8..680dd89bc 100644 --- a/libyul/AsmPrinter.cpp +++ b/libyul/AsmPrinter.cpp @@ -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 const& _debugData, bool _statement) +string AsmPrinter::formatSourceLocationComment( + SourceLocation const& _location, + map 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 const& _debugData, bool _statement) { if ( !_debugData || @@ -267,19 +294,9 @@ string AsmPrinter::formatSourceLocationComment(shared_ptr 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" : ""); } diff --git a/libyul/AsmPrinter.h b/libyul/AsmPrinter.h index 940f663b4..1beb964aa 100644 --- a/libyul/AsmPrinter.h +++ b/libyul/AsmPrinter.h @@ -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 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 m_nameToSourceIndex; + std::map m_nameToSourceIndex; langutil::SourceLocation m_lastLocation = {}; };