From 01dc77e5a2e301f798ff48ba41fb536ae4d32a72 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 1 Jul 2021 16:58:01 +0200 Subject: [PATCH] Properly assign source names for AST import. --- liblangutil/SourceLocation.cpp | 20 ++++++++++---------- liblangutil/SourceLocation.h | 5 ++--- libsolidity/ast/ASTJsonImporter.cpp | 12 +++++------- libsolidity/ast/ASTJsonImporter.h | 7 ++----- libyul/AsmJsonImporter.cpp | 2 +- libyul/AsmJsonImporter.h | 7 ++++--- 6 files changed, 24 insertions(+), 29 deletions(-) diff --git a/liblangutil/SourceLocation.cpp b/liblangutil/SourceLocation.cpp index 76270829d..058cc6b57 100644 --- a/liblangutil/SourceLocation.cpp +++ b/liblangutil/SourceLocation.cpp @@ -22,31 +22,31 @@ #include using namespace solidity; -namespace solidity::langutil -{ +using namespace solidity::langutil; +using namespace std; -SourceLocation const parseSourceLocation(std::string const& _input, std::string const& _sourceName, size_t _maxIndex) +SourceLocation solidity::langutil::parseSourceLocation(string const& _input, vector> const& _sourceNames) { // Expected input: "start:length:sourceindex" - enum SrcElem : size_t { Start, Length, Index }; + enum SrcElem: size_t { Start, Length, Index }; - std::vector pos; + vector pos; boost::algorithm::split(pos, _input, boost::is_any_of(":")); - // TODO What to do with sourceIndex? solAssert(pos.size() == 3, "SourceLocation string must have 3 colon separated numeric fields."); auto const sourceIndex = stoi(pos[Index]); astAssert( - sourceIndex == -1 || _maxIndex >= static_cast(sourceIndex), + sourceIndex == -1 || (0 <= sourceIndex && static_cast(sourceIndex) < _sourceNames.size()), "'src'-field ill-formatted or src-index too high" ); int start = stoi(pos[Start]); int end = start + stoi(pos[Length]); - return SourceLocation{start, end, std::make_shared(_sourceName)}; -} - + SourceLocation result{start, end, {}}; + if (sourceIndex != -1) + result.sourceName = _sourceNames.at(static_cast(sourceIndex)); + return result; } diff --git a/liblangutil/SourceLocation.h b/liblangutil/SourceLocation.h index 71e5ad711..845b269ca 100644 --- a/liblangutil/SourceLocation.h +++ b/liblangutil/SourceLocation.h @@ -106,10 +106,9 @@ struct SourceLocation std::shared_ptr sourceName; }; -SourceLocation const parseSourceLocation( +SourceLocation parseSourceLocation( std::string const& _input, - std::string const& _sourceName, - size_t _maxIndex = std::numeric_limits::max() + std::vector> const& _sourceNames ); /// Stream output for Location (used e.g. in boost exceptions). diff --git a/libsolidity/ast/ASTJsonImporter.cpp b/libsolidity/ast/ASTJsonImporter.cpp index 916dbac14..041bf49a6 100644 --- a/libsolidity/ast/ASTJsonImporter.cpp +++ b/libsolidity/ast/ASTJsonImporter.cpp @@ -57,14 +57,12 @@ ASTPointer ASTJsonImporter::nullOrCast(Json::Value const& _json) map> ASTJsonImporter::jsonToSourceUnit(map const& _sourceList) { - m_sourceList = _sourceList; for (auto const& src: _sourceList) - m_sourceLocations.emplace_back(make_shared(src.first)); - for (auto const& srcPair: m_sourceList) + m_sourceNames.emplace_back(make_shared(src.first)); + for (auto const& srcPair: _sourceList) { astAssert(!srcPair.second.isNull(), ""); astAssert(member(srcPair.second,"nodeType") == "SourceUnit", "The 'nodeType' of the highest node must be 'SourceUnit'."); - m_currentSourceName = srcPair.first; m_sourceUnits[srcPair.first] = createSourceUnit(srcPair.second, srcPair.first); } return m_sourceUnits; @@ -94,14 +92,14 @@ SourceLocation const ASTJsonImporter::createSourceLocation(Json::Value const& _n { astAssert(member(_node, "src").isString(), "'src' must be a string"); - return solidity::langutil::parseSourceLocation(_node["src"].asString(), m_currentSourceName, m_sourceLocations.size()); + return solidity::langutil::parseSourceLocation(_node["src"].asString(), m_sourceNames); } SourceLocation ASTJsonImporter::createNameSourceLocation(Json::Value const& _node) { astAssert(member(_node, "nameLocation").isString(), "'nameLocation' must be a string"); - return solidity::langutil::parseSourceLocation(_node["nameLocation"].asString(), m_currentSourceName, m_sourceLocations.size()); + return solidity::langutil::parseSourceLocation(_node["nameLocation"].asString(), m_sourceNames); } template @@ -616,7 +614,7 @@ ASTPointer ASTJsonImporter::createInlineAssembly(Json::Value con astAssert(m_evmVersion == evmVersion, "Imported tree evm version differs from configured evm version!"); yul::Dialect const& dialect = yul::EVMDialect::strictAssemblyForEVM(evmVersion.value()); - shared_ptr operations = make_shared(yul::AsmJsonImporter(m_currentSourceName).createBlock(member(_node, "AST"))); + shared_ptr operations = make_shared(yul::AsmJsonImporter(m_sourceNames).createBlock(member(_node, "AST"))); return createASTNode( _node, nullOrASTString(_node, "documentation"), diff --git a/libsolidity/ast/ASTJsonImporter.h b/libsolidity/ast/ASTJsonImporter.h index a4d43293f..3f1f5265c 100644 --- a/libsolidity/ast/ASTJsonImporter.h +++ b/libsolidity/ast/ASTJsonImporter.h @@ -152,13 +152,10 @@ private: ///@} // =========== member variables =============== - /// Stores filepath as sourcenames to AST in JSON format - std::map m_sourceList; - /// list of filepaths (used as sourcenames) - std::vector> m_sourceLocations; + /// list of source names, order by source index + std::vector> m_sourceNames; /// filepath to AST std::map> m_sourceUnits; - std::string m_currentSourceName; /// IDs already used by the nodes std::set m_usedIDs; /// Configured EVM version diff --git a/libyul/AsmJsonImporter.cpp b/libyul/AsmJsonImporter.cpp index de4e06870..87ce31497 100644 --- a/libyul/AsmJsonImporter.cpp +++ b/libyul/AsmJsonImporter.cpp @@ -45,7 +45,7 @@ SourceLocation const AsmJsonImporter::createSourceLocation(Json::Value const& _n { yulAssert(member(_node, "src").isString(), "'src' must be a string"); - return solidity::langutil::parseSourceLocation(_node["src"].asString(), m_sourceName); + return solidity::langutil::parseSourceLocation(_node["src"].asString(), m_sourceNames); } template diff --git a/libyul/AsmJsonImporter.h b/libyul/AsmJsonImporter.h index e491430b6..506352fa4 100644 --- a/libyul/AsmJsonImporter.h +++ b/libyul/AsmJsonImporter.h @@ -38,7 +38,9 @@ namespace solidity::yul class AsmJsonImporter { public: - explicit AsmJsonImporter(std::string _sourceName) : m_sourceName(std::move(_sourceName)) {} + explicit AsmJsonImporter(std::vector> const& _sourceNames): + m_sourceNames(_sourceNames) + {} yul::Block createBlock(Json::Value const& _node); private: @@ -70,8 +72,7 @@ private: yul::Break createBreak(Json::Value const& _node); yul::Continue createContinue(Json::Value const& _node); - std::string m_sourceName; - + std::vector> const& m_sourceNames; }; }