From a7e5f6b52b286a7644849dd7d8b6df5160d964f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Wed, 22 Sep 2021 12:39:13 +0200 Subject: [PATCH] Add SourceLocation.length() and use it in AsmJsonConverter --- liblangutil/SourceLocation.h | 4 ++++ libyul/AsmJsonConverter.cpp | 20 +++++++++++++++----- libyul/AsmJsonConverter.h | 3 +++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/liblangutil/SourceLocation.h b/liblangutil/SourceLocation.h index 66cef52fb..b35d03c67 100644 --- a/liblangutil/SourceLocation.h +++ b/liblangutil/SourceLocation.h @@ -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). diff --git a/libyul/AsmJsonConverter.cpp b/libyul/AsmJsonConverter.cpp index 1807396c5..27251b6cc 100644 --- a/libyul/AsmJsonConverter.cpp +++ b/libyul/AsmJsonConverter.cpp @@ -27,6 +27,7 @@ #include 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 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; } diff --git a/libyul/AsmJsonConverter.h b/libyul/AsmJsonConverter.h index 6bb50002c..24b5512e1 100644 --- a/libyul/AsmJsonConverter.h +++ b/libyul/AsmJsonConverter.h @@ -62,7 +62,10 @@ public: Json::Value operator()(Label const& _node) const; private: + static std::string formatLocation(langutil::SourceLocation const& _location, std::optional const& m_sourceIndex); + Json::Value createAstNode(langutil::SourceLocation const& _location, std::string _nodeType) const; + template Json::Value vectorOfVariantsToJson(std::vector const& vec) const;