Skip to content

Commit 810c446

Browse files
authored
Merge pull request ethereum#14610 from ethereum/introduce-no-import-callback-cli-option
Introduce `--no-import-callback` CLI option
2 parents 766efb3 + db98eed commit 810c446

File tree

18 files changed

+115
-6
lines changed

18 files changed

+115
-6
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Language Features:
55

66

77
Compiler Features:
8+
* Commandline Interface: Add ``--no-import-callback`` option that prevents the compiler from loading source files not given explicitly on the CLI or in Standard JSON input.
89
* Commandline Interface: Use proper severity and coloring also for error messages produced outside of the compilation pipeline.
910
* Parser: Remove the experimental error recovery mode (``--error-recovery`` / ``settings.parserErrorRecovery``).
1011
* Yul Optimizer: If ``PUSH0`` is supported, favor zero literals over storing zero values in variables.

docs/path-resolution.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ source unit is assigned a unique *source unit name* which is an opaque and unstr
2121
When you use the :ref:`import statement <import>`, you specify an *import path* that references a
2222
source unit name.
2323

24-
.. index:: ! import callback, ! Host Filesystem Loader
24+
.. index:: ! import callback, ! Host Filesystem Loader, ! --no-import-callback
2525
.. _import-callback:
2626

2727
Import Callback
@@ -36,8 +36,9 @@ An import callback is free to interpret source unit names in an arbitrary way, n
3636
If there is no callback available when one is needed or if it fails to locate the source code,
3737
compilation fails.
3838

39-
The command-line compiler provides the *Host Filesystem Loader* - a rudimentary callback
39+
By default, the command-line compiler provides the *Host Filesystem Loader* - a rudimentary callback
4040
that interprets a source unit name as a path in the local filesystem.
41+
This callback can be disabled using the ``--no-import-callback`` command-line option.
4142
The `JavaScript interface <https://github.com/ethereum/solc-js>`_ does not provide any by default,
4243
but one can be provided by the user.
4344
This mechanism can be used to obtain source code from locations other then the local filesystem

libsolidity/interface/UniversalCallback.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace solidity::frontend
2929
class UniversalCallback
3030
{
3131
public:
32-
UniversalCallback(FileReader& _fileReader, SMTSolverCommand& _solver) :
32+
UniversalCallback(FileReader* _fileReader, SMTSolverCommand& _solver) :
3333
m_fileReader{_fileReader},
3434
m_solver{_solver}
3535
{}
@@ -38,15 +38,20 @@ class UniversalCallback
3838
{
3939
return [this](std::string const& _kind, std::string const& _data) -> ReadCallback::Result {
4040
if (_kind == ReadCallback::kindString(ReadCallback::Kind::ReadFile))
41-
return m_fileReader.readFile(_kind, _data);
41+
if (!m_fileReader)
42+
return ReadCallback::Result{false, "No import callback."};
43+
else
44+
return m_fileReader->readFile(_kind, _data);
4245
else if (_kind == ReadCallback::kindString(ReadCallback::Kind::SMTQuery))
4346
return m_solver.solve(_kind, _data);
4447
solAssert(false, "Unknown callback kind.");
4548
};
4649
}
4750

51+
void resetImportCallback() { m_fileReader = nullptr; }
52+
4853
private:
49-
FileReader& m_fileReader;
54+
FileReader* m_fileReader;
5055
SMTSolverCommand& m_solver;
5156
};
5257

solc/CommandLineInterface.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,9 @@ void CommandLineInterface::readInputFiles()
464464
{
465465
solAssert(!m_standardJsonInput.has_value());
466466

467+
if (m_options.input.noImportCallback)
468+
m_universalCallback.resetImportCallback();
469+
467470
static std::set<frontend::InputMode> const noInputFiles{
468471
frontend::InputMode::Help,
469472
frontend::InputMode::License,

solc/CommandLineInterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class CommandLineInterface
144144
bool m_hasOutput = false;
145145
FileReader m_fileReader;
146146
SMTSolverCommand m_solverCommand{"eld"};
147-
UniversalCallback m_universalCallback{m_fileReader, m_solverCommand};
147+
UniversalCallback m_universalCallback{&m_fileReader, m_solverCommand};
148148
std::optional<std::string> m_standardJsonInput;
149149
std::unique_ptr<frontend::CompilerStack> m_compiler;
150150
CommandLineOptions m_options;

solc/CommandLineParser.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ static std::string const g_strModelCheckerTimeout = "model-checker-timeout";
7878
static std::string const g_strModelCheckerBMCLoopIterations = "model-checker-bmc-loop-iterations";
7979
static std::string const g_strNone = "none";
8080
static std::string const g_strNoOptimizeYul = "no-optimize-yul";
81+
static std::string const g_strNoImportCallback = "no-import-callback";
8182
static std::string const g_strOptimize = "optimize";
8283
static std::string const g_strOptimizeRuns = "optimize-runs";
8384
static std::string const g_strOptimizeYul = "optimize-yul";
@@ -224,6 +225,7 @@ bool CommandLineOptions::operator==(CommandLineOptions const& _other) const noex
224225
input.includePaths == _other.input.includePaths &&
225226
input.allowedDirectories == _other.input.allowedDirectories &&
226227
input.ignoreMissingFiles == _other.input.ignoreMissingFiles &&
228+
input.noImportCallback == _other.input.noImportCallback &&
227229
output.dir == _other.output.dir &&
228230
output.overwriteFiles == _other.output.overwriteFiles &&
229231
output.evmVersion == _other.output.evmVersion &&
@@ -567,6 +569,11 @@ General Information)").c_str(),
567569
g_strIgnoreMissingFiles.c_str(),
568570
"Ignore missing files."
569571
)
572+
(
573+
g_strNoImportCallback.c_str(),
574+
"Disable the default import callback to prevent the compiler from loading any source "
575+
"files not listed on the command line or given in the Standard JSON input."
576+
)
570577
;
571578
desc.add(inputOptions);
572579

@@ -1104,6 +1111,11 @@ void CommandLineParser::processArgs()
11041111
}
11051112
}
11061113

1114+
checkMutuallyExclusive({g_strNoImportCallback, g_strAllowPaths});
1115+
1116+
if (m_args.count(g_strNoImportCallback))
1117+
m_options.input.noImportCallback = true;
1118+
11071119
if (m_args.count(g_strAllowPaths))
11081120
{
11091121
std::vector<std::string> paths;

solc/CommandLineParser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ struct CommandLineOptions
174174
std::vector<boost::filesystem::path> includePaths;
175175
FileReader::FileSystemPathSet allowedDirectories;
176176
bool ignoreMissingFiles = false;
177+
bool noImportCallback = false;
177178
} input;
178179

179180
struct
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--no-import-callback no_import_callback/contract_1.sol
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// SPDX-License-Identifier: GPL-3.0
2+
pragma solidity >=0.0;
3+
4+
import "contract_2.sol";

test/cmdlineTests/no_import_callback/contract_2.sol

Whitespace-only changes.

0 commit comments

Comments
 (0)