Skip to content

Commit fc224f7

Browse files
Marenzchriseth
authored andcommitted
Evaluate absolute paths for imports in 'parsing' stage
1 parent cede369 commit fc224f7

File tree

7 files changed

+101
-11
lines changed

7 files changed

+101
-11
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Compiler Features:
88
* Commandline Interface: Accept nested brackets in step sequences passed to ``--yul-optimizations``.
99
* Commandline Interface: Add ``--debug-info`` option for selecting how much extra debug information should be included in the produced EVM assembly and Yul code.
1010
* Commandline Interface: Use different colors when printing errors, warnings and infos.
11+
* JSON AST: Set absolute paths of imports earlier, in the ``parsing`` stage.
1112
* SMTChecker: Output values for ``block.*``, ``msg.*`` and ``tx.*`` variables that are present in the called functions.
1213
* SMTChecker: Report contract invariants and reentrancy properties. This can be enabled via the CLI option ``--model-checker-invariants`` or the Standard JSON option ``settings.modelChecker.invariants``.
1314
* Standard JSON: Accept nested brackets in step sequences passed to ``settings.optimizer.details.yulDetails.optimizerSteps``.

libsolidity/interface/CompilerStack.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,22 @@ bool CompilerStack::parse()
350350
else
351351
{
352352
source.ast->annotation().path = path;
353+
354+
for (auto const& import: ASTNode::filteredNodes<ImportDirective>(source.ast->nodes()))
355+
{
356+
solAssert(!import->path().empty(), "Import path cannot be empty.");
357+
358+
// The current value of `path` is the absolute path as seen from this source file.
359+
// We first have to apply remappings before we can store the actual absolute path
360+
// as seen globally.
361+
import->annotation().absolutePath = applyRemapping(util::absolutePath(
362+
import->path(),
363+
path
364+
), path);
365+
}
366+
353367
if (m_stopAfter >= ParsedAndImported)
354-
for (auto const& newSource: loadMissingSources(*source.ast, path))
368+
for (auto const& newSource: loadMissingSources(*source.ast))
355369
{
356370
string const& newPath = newSource.first;
357371
string const& newContents = newSource.second;
@@ -1091,7 +1105,7 @@ string const& CompilerStack::Source::ipfsUrl() const
10911105
return ipfsUrlCached;
10921106
}
10931107

1094-
StringMap CompilerStack::loadMissingSources(SourceUnit const& _ast, std::string const& _sourcePath)
1108+
StringMap CompilerStack::loadMissingSources(SourceUnit const& _ast)
10951109
{
10961110
solAssert(m_stackState < ParsedAndImported, "");
10971111
StringMap newSources;
@@ -1100,14 +1114,8 @@ StringMap CompilerStack::loadMissingSources(SourceUnit const& _ast, std::string
11001114
for (auto const& node: _ast.nodes())
11011115
if (ImportDirective const* import = dynamic_cast<ImportDirective*>(node.get()))
11021116
{
1103-
solAssert(!import->path().empty(), "Import path cannot be empty.");
1117+
string const& importPath = *import->annotation().absolutePath;
11041118

1105-
string importPath = util::absolutePath(import->path(), _sourcePath);
1106-
// The current value of `path` is the absolute path as seen from this source file.
1107-
// We first have to apply remappings before we can store the actual absolute path
1108-
// as seen globally.
1109-
importPath = applyRemapping(importPath, _sourcePath);
1110-
import->annotation().absolutePath = importPath;
11111119
if (m_sources.count(importPath) || newSources.count(importPath))
11121120
continue;
11131121

libsolidity/interface/CompilerStack.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,9 @@ class CompilerStack: public langutil::CharStreamProvider
392392
void findAndReportCyclicContractDependencies();
393393

394394
/// Loads the missing sources from @a _ast (named @a _path) using the callback
395-
/// @a m_readFile and stores the absolute paths of all imports in the AST annotations.
395+
/// @a m_readFile
396396
/// @returns the newly loaded sources.
397-
StringMap loadMissingSources(SourceUnit const& _ast, std::string const& _path);
397+
StringMap loadMissingSources(SourceUnit const& _ast);
398398
std::string applyRemapping(std::string const& _path, std::string const& _context);
399399
void resolveImports();
400400

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--pretty-json --json-indent 4
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"language": "Solidity",
3+
"sources": {
4+
"/project/../C.sol": {"content": "//SPDX-License-Identifier: GPL-2.0\npragma solidity >=0.0;\nimport \"../L.sol\";"},
5+
"/lib/L.sol": {"content": "//SPDX-License-Identifier: GPL-2.0\npragma solidity >=0.0;\n"}
6+
},
7+
"settings": {
8+
"stopAfter": "parsing",
9+
"remappings": [":/project/=/lib/"],
10+
"outputSelection": {"*": {"": ["ast"]}}
11+
}
12+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"sources":
3+
{
4+
"/lib/L.sol":
5+
{
6+
"ast":
7+
{
8+
"absolutePath": "/lib/L.sol",
9+
"id": 2,
10+
"license": "GPL-2.0",
11+
"nodeType": "SourceUnit",
12+
"nodes":
13+
[
14+
{
15+
"id": 1,
16+
"literals":
17+
[
18+
"solidity",
19+
">=",
20+
"0.0"
21+
],
22+
"nodeType": "PragmaDirective",
23+
"src": "35:22:0"
24+
}
25+
],
26+
"src": "35:23:0"
27+
},
28+
"id": 0
29+
},
30+
"/project/../C.sol":
31+
{
32+
"ast":
33+
{
34+
"absolutePath": "/project/../C.sol",
35+
"id": 5,
36+
"license": "GPL-2.0",
37+
"nodeType": "SourceUnit",
38+
"nodes":
39+
[
40+
{
41+
"id": 3,
42+
"literals":
43+
[
44+
"solidity",
45+
">=",
46+
"0.0"
47+
],
48+
"nodeType": "PragmaDirective",
49+
"src": "35:22:1"
50+
},
51+
{
52+
"absolutePath": "/lib/L.sol",
53+
"file": "../L.sol",
54+
"id": 4,
55+
"nameLocation": "-1:-1:-1",
56+
"nodeType": "ImportDirective",
57+
"src": "58:18:1",
58+
"symbolAliases": [],
59+
"unitAlias": ""
60+
}
61+
],
62+
"src": "35:41:1"
63+
},
64+
"id": 1
65+
}
66+
}
67+
}

test/libsolidity/ASTJSON/not_existing_import_parseOnly.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"nodes":
66
[
77
{
8+
"absolutePath": "notexisting.sol",
89
"file": "notexisting.sol",
910
"id": 1,
1011
"nameLocation": "28:11:1",

0 commit comments

Comments
 (0)