Add SourceLocation.length() and use it in AsmJsonConverter

This commit is contained in:
Kamil Śliwak 2021-09-22 12:39:13 +02:00
parent 628e37c7a0
commit a7e5f6b52b
3 changed files with 22 additions and 5 deletions

View File

@ -79,6 +79,10 @@ struct SourceLocation
bool hasText() const { return sourceName && 0 <= start && start <= end; }
/// @returns the length of text covered by the location if both @a start and @a end are defined.
/// Otherwise returns -1.
int length() const { return (start >= 0 && end >= 0 ? end - start : -1); }
/// @returns the smallest SourceLocation that contains both @param _a and @param _b.
/// Assumes that @param _a and @param _b refer to the same source (exception: if the source of either one
/// is unset, the source of the other will be used for the result, even if that is unset as well).

View File

@ -27,6 +27,7 @@
#include <libsolutil/UTF8.h>
using namespace std;
using namespace solidity::langutil;
namespace solidity::yul
{
@ -183,14 +184,23 @@ Json::Value AsmJsonConverter::operator()(Leave const& _node) const
return createAstNode(nativeLocationOf(_node), "YulLeave");
}
Json::Value AsmJsonConverter::createAstNode(langutil::SourceLocation const& _location, string _nodeType) const
string AsmJsonConverter::formatLocation(SourceLocation const& _location, optional<size_t> const& _sourceIndex)
{
return
to_string(_location.start) +
":" +
to_string(_location.length()) +
":" +
(_sourceIndex.has_value() ? to_string(_sourceIndex.value()) : "-1");
}
Json::Value AsmJsonConverter::createAstNode(SourceLocation const& _location, string _nodeType) const
{
Json::Value ret{Json::objectValue};
ret["nodeType"] = std::move(_nodeType);
int length = -1;
if (_location.start >= 0 && _location.end >= 0)
length = _location.end - _location.start;
ret["src"] = to_string(_location.start) + ":" + to_string(length) + ":" + (m_sourceIndex.has_value() ? to_string(m_sourceIndex.value()) : "-1");
ret["src"] = formatLocation(_location, m_sourceIndex);
return ret;
}

View File

@ -62,7 +62,10 @@ public:
Json::Value operator()(Label const& _node) const;
private:
static std::string formatLocation(langutil::SourceLocation const& _location, std::optional<size_t> const& m_sourceIndex);
Json::Value createAstNode(langutil::SourceLocation const& _location, std::string _nodeType) const;
template <class T>
Json::Value vectorOfVariantsToJson(std::vector<T> const& vec) const;