Skip to content

Commit 01dc77e

Browse files
committed
Properly assign source names for AST import.
1 parent f75b550 commit 01dc77e

File tree

6 files changed

+24
-29
lines changed

6 files changed

+24
-29
lines changed

liblangutil/SourceLocation.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,31 @@
2222
#include <boost/algorithm/string.hpp>
2323

2424
using namespace solidity;
25-
namespace solidity::langutil
26-
{
25+
using namespace solidity::langutil;
26+
using namespace std;
2727

28-
SourceLocation const parseSourceLocation(std::string const& _input, std::string const& _sourceName, size_t _maxIndex)
28+
SourceLocation solidity::langutil::parseSourceLocation(string const& _input, vector<shared_ptr<string const>> const& _sourceNames)
2929
{
3030
// Expected input: "start:length:sourceindex"
31-
enum SrcElem : size_t { Start, Length, Index };
31+
enum SrcElem: size_t { Start, Length, Index };
3232

33-
std::vector<std::string> pos;
33+
vector<string> pos;
3434

3535
boost::algorithm::split(pos, _input, boost::is_any_of(":"));
3636

37-
// TODO What to do with sourceIndex?
3837
solAssert(pos.size() == 3, "SourceLocation string must have 3 colon separated numeric fields.");
3938
auto const sourceIndex = stoi(pos[Index]);
4039

4140
astAssert(
42-
sourceIndex == -1 || _maxIndex >= static_cast<size_t>(sourceIndex),
41+
sourceIndex == -1 || (0 <= sourceIndex && static_cast<size_t>(sourceIndex) < _sourceNames.size()),
4342
"'src'-field ill-formatted or src-index too high"
4443
);
4544

4645
int start = stoi(pos[Start]);
4746
int end = start + stoi(pos[Length]);
4847

49-
return SourceLocation{start, end, std::make_shared<std::string>(_sourceName)};
50-
}
51-
48+
SourceLocation result{start, end, {}};
49+
if (sourceIndex != -1)
50+
result.sourceName = _sourceNames.at(static_cast<size_t>(sourceIndex));
51+
return result;
5252
}

liblangutil/SourceLocation.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,9 @@ struct SourceLocation
106106
std::shared_ptr<std::string const> sourceName;
107107
};
108108

109-
SourceLocation const parseSourceLocation(
109+
SourceLocation parseSourceLocation(
110110
std::string const& _input,
111-
std::string const& _sourceName,
112-
size_t _maxIndex = std::numeric_limits<size_t>::max()
111+
std::vector<std::shared_ptr<std::string const>> const& _sourceNames
113112
);
114113

115114
/// Stream output for Location (used e.g. in boost exceptions).

libsolidity/ast/ASTJsonImporter.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,12 @@ ASTPointer<T> ASTJsonImporter::nullOrCast(Json::Value const& _json)
5757

5858
map<string, ASTPointer<SourceUnit>> ASTJsonImporter::jsonToSourceUnit(map<string, Json::Value> const& _sourceList)
5959
{
60-
m_sourceList = _sourceList;
6160
for (auto const& src: _sourceList)
62-
m_sourceLocations.emplace_back(make_shared<string const>(src.first));
63-
for (auto const& srcPair: m_sourceList)
61+
m_sourceNames.emplace_back(make_shared<string const>(src.first));
62+
for (auto const& srcPair: _sourceList)
6463
{
6564
astAssert(!srcPair.second.isNull(), "");
6665
astAssert(member(srcPair.second,"nodeType") == "SourceUnit", "The 'nodeType' of the highest node must be 'SourceUnit'.");
67-
m_currentSourceName = srcPair.first;
6866
m_sourceUnits[srcPair.first] = createSourceUnit(srcPair.second, srcPair.first);
6967
}
7068
return m_sourceUnits;
@@ -94,14 +92,14 @@ SourceLocation const ASTJsonImporter::createSourceLocation(Json::Value const& _n
9492
{
9593
astAssert(member(_node, "src").isString(), "'src' must be a string");
9694

97-
return solidity::langutil::parseSourceLocation(_node["src"].asString(), m_currentSourceName, m_sourceLocations.size());
95+
return solidity::langutil::parseSourceLocation(_node["src"].asString(), m_sourceNames);
9896
}
9997

10098
SourceLocation ASTJsonImporter::createNameSourceLocation(Json::Value const& _node)
10199
{
102100
astAssert(member(_node, "nameLocation").isString(), "'nameLocation' must be a string");
103101

104-
return solidity::langutil::parseSourceLocation(_node["nameLocation"].asString(), m_currentSourceName, m_sourceLocations.size());
102+
return solidity::langutil::parseSourceLocation(_node["nameLocation"].asString(), m_sourceNames);
105103
}
106104

107105
template<class T>
@@ -616,7 +614,7 @@ ASTPointer<InlineAssembly> ASTJsonImporter::createInlineAssembly(Json::Value con
616614
astAssert(m_evmVersion == evmVersion, "Imported tree evm version differs from configured evm version!");
617615

618616
yul::Dialect const& dialect = yul::EVMDialect::strictAssemblyForEVM(evmVersion.value());
619-
shared_ptr<yul::Block> operations = make_shared<yul::Block>(yul::AsmJsonImporter(m_currentSourceName).createBlock(member(_node, "AST")));
617+
shared_ptr<yul::Block> operations = make_shared<yul::Block>(yul::AsmJsonImporter(m_sourceNames).createBlock(member(_node, "AST")));
620618
return createASTNode<InlineAssembly>(
621619
_node,
622620
nullOrASTString(_node, "documentation"),

libsolidity/ast/ASTJsonImporter.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,10 @@ class ASTJsonImporter
152152
///@}
153153

154154
// =========== member variables ===============
155-
/// Stores filepath as sourcenames to AST in JSON format
156-
std::map<std::string, Json::Value> m_sourceList;
157-
/// list of filepaths (used as sourcenames)
158-
std::vector<std::shared_ptr<std::string const>> m_sourceLocations;
155+
/// list of source names, order by source index
156+
std::vector<std::shared_ptr<std::string const>> m_sourceNames;
159157
/// filepath to AST
160158
std::map<std::string, ASTPointer<SourceUnit>> m_sourceUnits;
161-
std::string m_currentSourceName;
162159
/// IDs already used by the nodes
163160
std::set<int64_t> m_usedIDs;
164161
/// Configured EVM version

libyul/AsmJsonImporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ SourceLocation const AsmJsonImporter::createSourceLocation(Json::Value const& _n
4545
{
4646
yulAssert(member(_node, "src").isString(), "'src' must be a string");
4747

48-
return solidity::langutil::parseSourceLocation(_node["src"].asString(), m_sourceName);
48+
return solidity::langutil::parseSourceLocation(_node["src"].asString(), m_sourceNames);
4949
}
5050

5151
template <class T>

libyul/AsmJsonImporter.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ namespace solidity::yul
3838
class AsmJsonImporter
3939
{
4040
public:
41-
explicit AsmJsonImporter(std::string _sourceName) : m_sourceName(std::move(_sourceName)) {}
41+
explicit AsmJsonImporter(std::vector<std::shared_ptr<std::string const>> const& _sourceNames):
42+
m_sourceNames(_sourceNames)
43+
{}
4244
yul::Block createBlock(Json::Value const& _node);
4345

4446
private:
@@ -70,8 +72,7 @@ class AsmJsonImporter
7072
yul::Break createBreak(Json::Value const& _node);
7173
yul::Continue createContinue(Json::Value const& _node);
7274

73-
std::string m_sourceName;
74-
75+
std::vector<std::shared_ptr<std::string const>> const& m_sourceNames;
7576
};
7677

7778
}

0 commit comments

Comments
 (0)