Skip to content

Commit

Permalink
Evaluate absolute paths for imports in 'parsing' stage
Browse files Browse the repository at this point in the history
  • Loading branch information
Marenz committed Nov 8, 2021
1 parent cede369 commit f860891
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Compiler Features:
* Commandline Interface: Accept nested brackets in step sequences passed to ``--yul-optimizations``.
* Commandline Interface: Add ``--debug-info`` option for selecting how much extra debug information should be included in the produced EVM assembly and Yul code.
* Commandline Interface: Use different colors when printing errors, warnings and infos.
* JSON AST: Set absolute paths of imports earlier, in the ``parsing`` stage.
* SMTChecker: Output values for ``block.*``, ``msg.*`` and ``tx.*`` variables that are present in the called functions.
* 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``.
* Standard JSON: Accept nested brackets in step sequences passed to ``settings.optimizer.details.yulDetails.optimizerSteps``.
Expand Down
26 changes: 17 additions & 9 deletions libsolidity/interface/CompilerStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,22 @@ bool CompilerStack::parse()
else
{
source.ast->annotation().path = path;

for (auto const& import: ASTNode::filteredNodes<ImportDirective>(source.ast->nodes()))
{
solAssert(!import->path().empty(), "Import path cannot be empty.");

// The current value of `path` is the absolute path as seen from this source file.
// We first have to apply remappings before we can store the actual absolute path
// as seen globally.
import->annotation().absolutePath = applyRemapping(util::absolutePath(
import->path(),
path
), path);
}

if (m_stopAfter >= ParsedAndImported)
for (auto const& newSource: loadMissingSources(*source.ast, path))
for (auto const& newSource: loadMissingSources(*source.ast))
{
string const& newPath = newSource.first;
string const& newContents = newSource.second;
Expand Down Expand Up @@ -1091,7 +1105,7 @@ string const& CompilerStack::Source::ipfsUrl() const
return ipfsUrlCached;
}

StringMap CompilerStack::loadMissingSources(SourceUnit const& _ast, std::string const& _sourcePath)
StringMap CompilerStack::loadMissingSources(SourceUnit const& _ast)
{
solAssert(m_stackState < ParsedAndImported, "");
StringMap newSources;
Expand All @@ -1100,14 +1114,8 @@ StringMap CompilerStack::loadMissingSources(SourceUnit const& _ast, std::string
for (auto const& node: _ast.nodes())
if (ImportDirective const* import = dynamic_cast<ImportDirective*>(node.get()))
{
solAssert(!import->path().empty(), "Import path cannot be empty.");
string const& importPath = *import->annotation().absolutePath;

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

Expand Down
4 changes: 2 additions & 2 deletions libsolidity/interface/CompilerStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,9 @@ class CompilerStack: public langutil::CharStreamProvider
void findAndReportCyclicContractDependencies();

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"language": "Solidity",
"sources": {
"/project/../C.sol": {"content": "import \"../L.sol\";"},
"/lib/L.sol": {"content": ""}
},
"settings": {
"stopAfter": "parsing",
"remappings": [":/project/=/lib/"],
"outputSelection": {"*": {"": ["ast"]}}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{"errors":[{"component":"general","errorCode":"1878","formattedMessage":"Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing \"SPDX-License-Identifier: <SPDX-License>\" to each source file. Use \"SPDX-License-Identifier: UNLICENSED\" for non-open-source code. Please see https://spdx.org for more information.
--> /lib/L.sol

","message":"SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing \"SPDX-License-Identifier: <SPDX-License>\" to each source file. Use \"SPDX-License-Identifier: UNLICENSED\" for non-open-source code. Please see https://spdx.org for more information.","severity":"warning","sourceLocation":{"end":-1,"file":"/lib/L.sol","start":-1},"type":"Warning"},{"component":"general","errorCode":"1878","formattedMessage":"Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing \"SPDX-License-Identifier: <SPDX-License>\" to each source file. Use \"SPDX-License-Identifier: UNLICENSED\" for non-open-source code. Please see https://spdx.org for more information.
--> /project/../C.sol

","message":"SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing \"SPDX-License-Identifier: <SPDX-License>\" to each source file. Use \"SPDX-License-Identifier: UNLICENSED\" for non-open-source code. Please see https://spdx.org for more information.","severity":"warning","sourceLocation":{"end":-1,"file":"/project/../C.sol","start":-1},"type":"Warning"}],"sources":{"/lib/L.sol":{"ast":{"absolutePath":"/lib/L.sol","id":1,"nodeType":"SourceUnit","nodes":[],"src":"0:0:0"},"id":0},"/project/../C.sol":{"ast":{"absolutePath":"/project/../C.sol","id":3,"nodeType":"SourceUnit","nodes":[{"absolutePath":"/lib/L.sol","file":"../L.sol","id":2,"nameLocation":"-1:-1:-1","nodeType":"ImportDirective","src":"0:18:1","symbolAliases":[],"unitAlias":""}],"src":"0:18:1"},"id":1}}}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"nodes":
[
{
"absolutePath": "notexisting.sol",
"file": "notexisting.sol",
"id": 1,
"nameLocation": "28:11:1",
Expand Down

0 comments on commit f860891

Please sign in to comment.