Skip to content

Commit 1633e36

Browse files
authored
Merge pull request #12199 from ethereum/expose-parsingAndImporting
Expose "parsingAndImporting" setting to user
2 parents 44f7065 + fc224f7 commit 1633e36

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
@@ -10,6 +10,7 @@ Compiler Features:
1010
* Commandline Interface: Add ``--debug-info`` option for selecting how much extra debug information should be included in the produced EVM assembly and Yul code.
1111
* Commandline Interface: Support ``--asm``, ``--bin``, ``--ir-optimized``, ``--ewasm`` and ``--ewasm-ir`` output selection options in assembler mode.
1212
* Commandline Interface: Use different colors when printing errors, warnings and infos.
13+
* JSON AST: Set absolute paths of imports earlier, in the ``parsing`` stage.
1314
* SMTChecker: Output values for ``block.*``, ``msg.*`` and ``tx.*`` variables that are present in the called functions.
1415
* 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``.
1516
* 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
@@ -351,8 +351,22 @@ bool CompilerStack::parse()
351351
else
352352
{
353353
source.ast->annotation().path = path;
354+
355+
for (auto const& import: ASTNode::filteredNodes<ImportDirective>(source.ast->nodes()))
356+
{
357+
solAssert(!import->path().empty(), "Import path cannot be empty.");
358+
359+
// The current value of `path` is the absolute path as seen from this source file.
360+
// We first have to apply remappings before we can store the actual absolute path
361+
// as seen globally.
362+
import->annotation().absolutePath = applyRemapping(util::absolutePath(
363+
import->path(),
364+
path
365+
), path);
366+
}
367+
354368
if (m_stopAfter >= ParsedAndImported)
355-
for (auto const& newSource: loadMissingSources(*source.ast, path))
369+
for (auto const& newSource: loadMissingSources(*source.ast))
356370
{
357371
string const& newPath = newSource.first;
358372
string const& newContents = newSource.second;
@@ -1092,7 +1106,7 @@ string const& CompilerStack::Source::ipfsUrl() const
10921106
return ipfsUrlCached;
10931107
}
10941108

1095-
StringMap CompilerStack::loadMissingSources(SourceUnit const& _ast, std::string const& _sourcePath)
1109+
StringMap CompilerStack::loadMissingSources(SourceUnit const& _ast)
10961110
{
10971111
solAssert(m_stackState < ParsedAndImported, "");
10981112
StringMap newSources;
@@ -1101,14 +1115,8 @@ StringMap CompilerStack::loadMissingSources(SourceUnit const& _ast, std::string
11011115
for (auto const& node: _ast.nodes())
11021116
if (ImportDirective const* import = dynamic_cast<ImportDirective*>(node.get()))
11031117
{
1104-
solAssert(!import->path().empty(), "Import path cannot be empty.");
1118+
string const& importPath = *import->annotation().absolutePath;
11051119

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

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)