Print code snippets next to source locations in IR output

This commit is contained in:
Kamil Śliwak
2021-09-14 12:09:59 +02:00
parent d78522b08b
commit 14639efc5d
38 changed files with 989 additions and 333 deletions
+22 -4
View File
@@ -27,6 +27,7 @@
#include <libyul/Dialect.h>
#include <libsolutil/CommonData.h>
#include <libsolutil/StringUtils.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/replace.hpp>
@@ -260,15 +261,31 @@ string AsmPrinter::appendTypeName(YulString _type, bool _isBoolLiteral) const
string AsmPrinter::formatSourceLocationComment(
SourceLocation const& _location,
map<string, unsigned> const& _nameToSourceIndex,
bool _statement
bool _statement,
CharStreamProvider const* _soliditySourceProvider
)
{
yulAssert(!_nameToSourceIndex.empty(), "");
string sourceIndex = "-1";
string solidityCodeSnippet = "";
if (_location.sourceName)
{
sourceIndex = to_string(_nameToSourceIndex.at(*_location.sourceName));
if (_soliditySourceProvider)
{
solidityCodeSnippet = escapeAndQuoteString(
_soliditySourceProvider->charStream(*_location.sourceName).singleLineSnippet(_location)
);
// On top of escaping quotes we also escape the slash inside any `*/` to guard against
// it prematurely terminating multi-line comment blocks. We do not escape all slashes
// because the ones without `*` are not dangerous and ignoring them reduces visual noise.
boost::replace_all(solidityCodeSnippet, "*/", "*\\/");
}
}
string sourceLocation =
"@src " +
sourceIndex +
@@ -279,8 +296,8 @@ string AsmPrinter::formatSourceLocationComment(
return
_statement ?
"/// " + sourceLocation :
"/** " + sourceLocation + " */ ";
"/// " + joinHumanReadable(vector<string>{sourceLocation, solidityCodeSnippet}, " ") :
"/** " + joinHumanReadable(vector<string>{sourceLocation, solidityCodeSnippet}, " ") + " */ ";
}
string AsmPrinter::formatSourceLocationComment(shared_ptr<DebugData const> const& _debugData, bool _statement)
@@ -297,6 +314,7 @@ string AsmPrinter::formatSourceLocationComment(shared_ptr<DebugData const> const
return formatSourceLocationComment(
_debugData->location,
m_nameToSourceIndex,
_statement
_statement,
m_soliditySourceProvider
) + (_statement ? "\n" : "");
}