Merge pull request #11518 from ethereum/command-line-parser

CommandLineParser
This commit is contained in:
Kamil Śliwak 2021-07-07 12:51:09 +02:00 committed by GitHub
commit ce11ebb687
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 2113 additions and 1198 deletions

View File

@ -44,6 +44,9 @@ struct ModelCheckerContracts
return has(_source) && contracts.at(_source).count(_contract);
}
bool operator!=(ModelCheckerContracts const& _other) const noexcept { return !(*this == _other); }
bool operator==(ModelCheckerContracts const& _other) const noexcept { return contracts == _other.contracts; }
/// Represents which contracts should be analyzed by the SMTChecker
/// as the most derived.
/// The key is the source file. If the map is empty, all sources must be analyzed.
@ -79,6 +82,9 @@ struct ModelCheckerEngine
return engineMap.at(_engine);
return {};
}
bool operator!=(ModelCheckerEngine const& _other) const noexcept { return !(*this == _other); }
bool operator==(ModelCheckerEngine const& _other) const noexcept { return bmc == _other.bmc && chc == _other.chc; }
};
enum class VerificationTargetType { ConstantCondition, Underflow, Overflow, UnderOverflow, DivByZero, Balance, Assert, PopEmptyArray, OutOfBounds };
@ -97,6 +103,9 @@ struct ModelCheckerTargets
static std::map<std::string, VerificationTargetType> const targetStrings;
bool operator!=(ModelCheckerTargets const& _other) const noexcept { return !(*this == _other); }
bool operator==(ModelCheckerTargets const& _other) const noexcept { return targets == _other.targets; }
std::set<VerificationTargetType> targets;
};
@ -106,6 +115,16 @@ struct ModelCheckerSettings
ModelCheckerEngine engine = ModelCheckerEngine::None();
ModelCheckerTargets targets = ModelCheckerTargets::Default();
std::optional<unsigned> timeout;
bool operator!=(ModelCheckerSettings const& _other) const noexcept { return !(*this == _other); }
bool operator==(ModelCheckerSettings const& _other) const noexcept
{
return
contracts == _other.contracts &&
engine == _other.engine &&
targets == _other.targets &&
timeout == _other.timeout;
}
};
}

View File

@ -35,6 +35,15 @@ class ImportRemapper
public:
struct Remapping
{
bool operator!=(Remapping const& _other) const noexcept { return !(*this == _other); }
bool operator==(Remapping const& _other) const noexcept
{
return
context == _other.context &&
prefix == _other.prefix &&
target == _other.target;
}
std::string context;
std::string prefix;
std::string target;

View File

@ -1,5 +1,6 @@
set(libsolcli_sources
CommandLineInterface.cpp CommandLineInterface.h
CommandLineParser.cpp CommandLineParser.h
)
add_library(solcli ${libsolcli_sources})

File diff suppressed because it is too large Load Diff

View File

@ -22,36 +22,39 @@
*/
#pragma once
#include <solc/CommandLineParser.h>
#include <libsolidity/interface/CompilerStack.h>
#include <libsolidity/interface/DebugSettings.h>
#include <libsolidity/interface/FileReader.h>
#include <libsolidity/interface/ImportRemapper.h>
#include <libyul/AssemblyStack.h>
#include <liblangutil/EVMVersion.h>
#include <boost/program_options.hpp>
#include <boost/filesystem/path.hpp>
#include <memory>
#include <string>
namespace solidity::frontend
{
//forward declaration
enum class DocumentationType: uint8_t;
class CommandLineInterface
{
public:
explicit CommandLineInterface(CommandLineOptions const& _options = CommandLineOptions{}):
m_options(_options)
{}
/// Parse command line arguments and return false if we should not continue
bool parseArguments(int _argc, char** _argv);
bool parseArguments(int _argc, char const* const* _argv);
/// Parse the files and create source code objects
bool processInput();
/// Perform actions on the input depending on provided compiler arguments
/// @returns true on success.
bool actOnInput();
CommandLineOptions const& options() const { return m_options; }
FileReader const& fileReader() const { return m_fileReader; }
private:
bool compile();
bool link();
void writeLinkedFiles();
/// @returns the ``// <identifier> -> name`` hint for library placeholders.
@ -83,11 +86,9 @@ private:
void handleGasEstimation(std::string const& _contract);
void handleStorageLayout(std::string const& _contract);
/// Fills @a m_sourceCodes initially and @a m_redirects.
bool readInputFilesAndConfigureRemappings();
/// Tries to read from the file @a _input or interprets _input literally if that fails.
/// It then tries to parse the contents and appends to m_libraries.
bool parseLibraryOption(std::string const& _input);
/// Reads the content of input files specified on the command line and passes them to FileReader.
/// @return false if there are no input files or input files cannot be read.
bool readInputFilesAndConfigureFileReader();
/// Tries to read @ m_sourceCodes as a JSONs holding ASTs
/// such that they can be imported into the compiler (importASTs())
@ -105,38 +106,10 @@ private:
/// @arg _json json string to be written
void createJson(std::string const& _fileName, std::string const& _json);
size_t countEnabledOptions(std::vector<std::string> const& _optionNames) const;
static std::string joinOptionNames(std::vector<std::string> const& _optionNames, std::string _separator = ", ");
bool m_error = false; ///< If true, some error occurred.
bool m_onlyAssemble = false;
bool m_onlyLink = false;
FileReader m_fileReader;
/// Compiler arguments variable map
boost::program_options::variables_map m_args;
/// list of remappings
std::vector<ImportRemapper::Remapping> m_remappings;
/// map of library names to addresses
std::map<std::string, util::h160> m_libraries;
/// Solidity compiler stack
std::unique_ptr<frontend::CompilerStack> m_compiler;
CompilerStack::State m_stopAfter = CompilerStack::State::CompilationSuccessful;
/// EVM version to use
langutil::EVMVersion m_evmVersion;
/// How to handle revert strings
RevertStrings m_revertStrings = RevertStrings::Default;
/// Chosen hash method for the bytecode metadata.
CompilerStack::MetadataHash m_metadataHash = CompilerStack::MetadataHash::IPFS;
/// Model checker settings.
ModelCheckerSettings m_modelCheckerSettings;
/// Whether or not to colorize diagnostics output.
bool m_coloredOutput = true;
/// Whether or not to output error IDs.
bool m_withErrorIds = false;
CommandLineOptions m_options;
};
}

1245
solc/CommandLineParser.cpp Normal file

File diff suppressed because it is too large Load Diff

223
solc/CommandLineParser.h Normal file
View File

@ -0,0 +1,223 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
* Validates and parses command-line options into an internal representation.
*/
#pragma once
#include <libsolidity/interface/CompilerStack.h>
#include <libsolidity/interface/DebugSettings.h>
#include <libsolidity/interface/FileReader.h>
#include <libsolidity/interface/ImportRemapper.h>
#include <libyul/AssemblyStack.h>
#include <liblangutil/EVMVersion.h>
#include <boost/program_options.hpp>
#include <boost/filesystem/path.hpp>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <vector>
namespace solidity::frontend
{
enum class InputMode
{
Compiler,
CompilerWithASTImport,
StandardJson,
Linker,
Assembler,
};
struct CompilerOutputs
{
bool operator!=(CompilerOutputs const& _other) const noexcept { return !(*this == _other); }
bool operator==(CompilerOutputs const& _other) const noexcept;
bool astCompactJson = false;
bool asm_ = false;
bool asmJson = false;
bool opcodes = false;
bool binary = false;
bool binaryRuntime = false;
bool abi = false;
bool ir = false;
bool irOptimized = false;
bool ewasm = false;
bool signatureHashes = false;
bool natspecUser = false;
bool natspecDev = false;
bool metadata = false;
bool storageLayout = false;
};
struct CombinedJsonRequests
{
bool operator!=(CombinedJsonRequests const& _other) const noexcept { return !(*this == _other); }
bool operator==(CombinedJsonRequests const& _other) const noexcept;
bool abi = false;
bool metadata = false;
bool binary = false;
bool binaryRuntime = false;
bool opcodes = false;
bool asm_ = false;
bool storageLayout = false;
bool generatedSources = false;
bool generatedSourcesRuntime = false;
bool srcMap = false;
bool srcMapRuntime = false;
bool funDebug = false;
bool funDebugRuntime = false;
bool signatureHashes = false;
bool natspecDev = false;
bool natspecUser = false;
bool ast = false;
};
struct CommandLineOptions
{
bool operator==(CommandLineOptions const& _other) const noexcept;
bool operator!=(CommandLineOptions const& _other) const noexcept { return !(*this == _other); }
struct
{
InputMode mode = InputMode::Compiler;
std::set<boost::filesystem::path> paths;
std::string standardJsonFile;
std::vector<ImportRemapper::Remapping> remappings;
bool addStdin = false;
boost::filesystem::path basePath = "";
FileReader::FileSystemPathSet allowedDirectories;
bool ignoreMissingFiles = false;
bool errorRecovery = false;
} input;
struct
{
boost::filesystem::path dir;
bool overwriteFiles = false;
langutil::EVMVersion evmVersion;
bool experimentalViaIR = false;
RevertStrings revertStrings = RevertStrings::Default;
CompilerStack::State stopAfter = CompilerStack::State::CompilationSuccessful;
} output;
struct
{
yul::AssemblyStack::Machine targetMachine = yul::AssemblyStack::Machine::EVM;
yul::AssemblyStack::Language inputLanguage = yul::AssemblyStack::Language::StrictAssembly;
} assembly;
struct
{
std::map<std::string, util::h160> libraries; // library name -> address
} linker;
struct
{
bool prettyJson = false;
std::optional<bool> coloredOutput;
bool withErrorIds = false;
} formatting;
struct
{
CompilerOutputs outputs;
bool estimateGas = false;
std::optional<CombinedJsonRequests> combinedJsonRequests;
} compiler;
struct
{
CompilerStack::MetadataHash hash = CompilerStack::MetadataHash::IPFS;
bool literalSources = false;
} metadata;
struct
{
bool enabled = false;
unsigned expectedExecutionsPerDeployment = 0;
bool noOptimizeYul = false;
std::optional<std::string> yulSteps;
} optimizer;
struct
{
bool initialize = false;
ModelCheckerSettings settings;
} modelChecker;
};
/// Parses the command-line arguments and produces a filled-out CommandLineOptions structure.
/// Validates provided values and prints error messages in case of errors.
///
/// The class is also responsible for handling options that only result in printing informational
/// text, without the need to invoke the compiler - printing usage banner, version or license.
class CommandLineParser
{
public:
/// Parses the command-line arguments and fills out the internal CommandLineOptions structure.
/// Performs validation and prints error messages. If requested, prints usage banner, version
/// or license.
/// @param interactiveTerminal specifies whether the terminal is taking input from the user.
/// This is used to determine whether to provide more user-friendly output in some situations.
/// E.g. whether to print help text when no arguments are provided.
/// @return true if there were no validation errors when parsing options and the
/// CommandLineOptions structure has been fully initialized. false if there were errors - in
/// this case CommandLineOptions may be only partially filled out. May also return false if
/// there is not further processing necessary and the program should just exit.
bool parse(int _argc, char const* const* _argv, bool interactiveTerminal);
CommandLineOptions const& options() const { return m_options; }
/// Returns true if any parser instance has written anything to cout or cerr since the last
/// call to parse().
static bool hasOutput();
private:
/// Parses the value supplied to --combined-json.
/// @return false if there are any validation errors, true otherwise.
bool parseCombinedJsonOption();
/// Parses the names of the input files, remappings for all modes except for Standard JSON.
/// Does not check if files actually exist.
/// @return false if there are any validation errors, true otherwise.
bool parseInputPathsAndRemappings();
/// Tries to read from the file @a _input or interprets @a _input literally if that fails.
/// It then tries to parse the contents and appends to m_options.libraries.
/// @return false if there are any validation errors, true otherwise.
bool parseLibraryOption(std::string const& _input);
size_t countEnabledOptions(std::vector<std::string> const& _optionNames) const;
static std::string joinOptionNames(std::vector<std::string> const& _optionNames, std::string _separator = ", ");
CommandLineOptions m_options;
/// Map of command-line arguments produced by boost::program_options.
/// Serves as input for filling out m_options.
boost::program_options::variables_map m_args;
};
}

View File

@ -149,6 +149,11 @@ set(libyul_sources
)
detect_stray_source_files("${libyul_sources}" "libyul/")
set(solcli_sources
solc/CommandLineParser.cpp
)
detect_stray_source_files("${solcli_sources}" "solc/")
set(yul_phaser_sources
yulPhaser/TestHelpers.h
yulPhaser/TestHelpers.cpp
@ -177,9 +182,10 @@ add_executable(soltest ${sources}
${libyul_sources}
${libsolidity_sources}
${libsolidity_util_sources}
${solcli_sources}
${yul_phaser_sources}
)
target_link_libraries(soltest PRIVATE libsolc yul solidity smtutil solutil phaser Boost::boost yulInterpreter evmasm Boost::filesystem Boost::program_options Boost::unit_test_framework evmc)
target_link_libraries(soltest PRIVATE solcli libsolc yul solidity smtutil solutil phaser Boost::boost yulInterpreter evmasm Boost::filesystem Boost::program_options Boost::unit_test_framework evmc)
# Special compilation flag for Visual Studio (version 2019 at least affected)

View File

@ -0,0 +1,399 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
/// Unit tests for solc/CommandLineParser.h
#include <solc/CommandLineParser.h>
#include <test/Common.h>
#include <test/libsolidity/util/SoltestErrors.h>
#include <libsolutil/CommonData.h>
#include <liblangutil/EVMVersion.h>
#include <libsolidity/interface/Version.h>
#include <boost/algorithm/string.hpp>
#include <boost/test/unit_test.hpp>
#include <map>
#include <optional>
#include <ostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
using namespace solidity::frontend;
using namespace solidity::langutil;
using namespace solidity::util;
using namespace solidity::yul;
namespace
{
optional<CommandLineOptions> parseCommandLine(vector<string> const& commandLine)
{
size_t argc = commandLine.size();
vector<char const*> argv(argc + 1);
// argv[argc] typically contains NULL
argv[argc] = nullptr;
for (size_t i = 0; i < argc; ++i)
argv[i] = commandLine[i].c_str();
CommandLineParser cliParser;
bool success = cliParser.parse(
static_cast<int>(argc),
argv.data(),
false // interactiveTerminal
);
if (!success)
return nullopt;
else
return cliParser.options();
}
} // namespace
namespace solidity::frontend::test
{
BOOST_AUTO_TEST_SUITE(CommandLineParserTest)
BOOST_AUTO_TEST_CASE(no_options)
{
vector<string> commandLine = {"solc", "contract.sol"};
CommandLineOptions expectedOptions;
expectedOptions.input.paths = {"contract.sol"};
expectedOptions.optimizer.expectedExecutionsPerDeployment = 200;
expectedOptions.modelChecker.initialize = true;
expectedOptions.modelChecker.settings = {
ModelCheckerContracts::Default(),
ModelCheckerEngine::None(),
ModelCheckerTargets::Default(),
nullopt,
};
optional<CommandLineOptions> parsedOptions = parseCommandLine(commandLine);
BOOST_REQUIRE(parsedOptions.has_value());
BOOST_TEST((parsedOptions.value() == expectedOptions));
}
BOOST_AUTO_TEST_CASE(cli_mode_options)
{
for (InputMode inputMode: {InputMode::Compiler, InputMode::CompilerWithASTImport})
{
vector<string> commandLine = {
"solc",
"contract.sol", // Both modes do not care about file names, just about
"/tmp/projects/token.sol", // their content. They also both support stdin.
"/home/user/lib/dex.sol",
"file",
"input.json",
"-",
"/tmp=/usr/lib/",
"a:b=c/d",
":contract.sol=",
"--base-path=/home/user/",
"--allow-paths=/tmp,/home,project,../contracts",
"--ignore-missing",
"--error-recovery",
"--output-dir=/tmp/out",
"--overwrite",
"--evm-version=spuriousDragon",
"--experimental-via-ir",
"--revert-strings=strip",
"--pretty-json",
"--no-color",
"--error-codes",
"--libraries="
"dir1/file1.sol:L=0x1234567890123456789012345678901234567890,"
"dir2/file2.sol:L=0x1111122222333334444455555666667777788888",
"--ast-compact-json", "--asm", "--asm-json", "--opcodes", "--bin", "--bin-runtime", "--abi",
"--ir", "--ir-optimized", "--ewasm", "--hashes", "--userdoc", "--devdoc", "--metadata", "--storage-layout",
"--gas",
"--combined-json="
"abi,metadata,bin,bin-runtime,opcodes,asm,storage-layout,generated-sources,generated-sources-runtime,"
"srcmap,srcmap-runtime,function-debug,function-debug-runtime,hashes,devdoc,userdoc,ast",
"--metadata-hash=swarm",
"--metadata-literal",
"--optimize",
"--optimize-runs=1000",
"--yul-optimizations=agf",
"--model-checker-contracts=contract1.yul:A,contract2.yul:B",
"--model-checker-engine=bmc",
"--model-checker-targets=underflow,divByZero",
"--model-checker-timeout=5",
};
if (inputMode == InputMode::CompilerWithASTImport)
commandLine += vector<string>{
"--import-ast",
};
CommandLineOptions expectedOptions;
expectedOptions.input.mode = inputMode;
expectedOptions.input.paths = {"contract.sol", "/tmp/projects/token.sol", "/home/user/lib/dex.sol", "file", "input.json"};
expectedOptions.input.remappings = {
{"", "/tmp", "/usr/lib/"},
{"a", "b", "c/d"},
{"", "contract.sol", ""},
};
expectedOptions.input.addStdin = true;
expectedOptions.input.basePath = "/home/user/";
expectedOptions.input.allowedDirectories = {"/tmp", "/home", "project", "../contracts", "", "c", "/usr/lib"};
expectedOptions.input.ignoreMissingFiles = true;
expectedOptions.input.errorRecovery = (inputMode == InputMode::Compiler);
expectedOptions.output.dir = "/tmp/out";
expectedOptions.output.overwriteFiles = true;
expectedOptions.output.evmVersion = EVMVersion::spuriousDragon();
expectedOptions.output.experimentalViaIR = true;
expectedOptions.output.revertStrings = RevertStrings::Strip;
expectedOptions.linker.libraries = {
{"dir1/file1.sol:L", h160("1234567890123456789012345678901234567890")},
{"dir2/file2.sol:L", h160("1111122222333334444455555666667777788888")},
};
expectedOptions.formatting.prettyJson = true;
expectedOptions.formatting.coloredOutput = false;
expectedOptions.formatting.withErrorIds = true;
expectedOptions.compiler.outputs = {
true, true, true, true, true,
true, true, true, true, true,
true, true, true, true, true,
};
expectedOptions.compiler.estimateGas = true;
expectedOptions.compiler.combinedJsonRequests = {
true, true, true, true, true,
true, true, true, true, true,
true, true, true, true, true,
true, true,
};
expectedOptions.metadata.hash = CompilerStack::MetadataHash::Bzzr1;
expectedOptions.metadata.literalSources = true;
expectedOptions.optimizer.enabled = true;
expectedOptions.optimizer.expectedExecutionsPerDeployment = 1000;
expectedOptions.optimizer.yulSteps = "agf";
expectedOptions.modelChecker.initialize = true;
expectedOptions.modelChecker.settings = {
{{{"contract1.yul", {"A"}}, {"contract2.yul", {"B"}}}},
{true, false},
{{VerificationTargetType::Underflow, VerificationTargetType::DivByZero}},
5,
};
optional<CommandLineOptions> parsedOptions = parseCommandLine(commandLine);
BOOST_REQUIRE(parsedOptions.has_value());
BOOST_TEST((parsedOptions.value() == expectedOptions));
}
}
BOOST_AUTO_TEST_CASE(assembly_mode_options)
{
static vector<tuple<vector<string>, AssemblyStack::Machine, AssemblyStack::Language>> const allowedCombinations = {
{{"--machine=ewasm", "--yul-dialect=ewasm", "--assemble"}, AssemblyStack::Machine::Ewasm, AssemblyStack::Language::Ewasm},
{{"--machine=ewasm", "--yul-dialect=ewasm", "--yul"}, AssemblyStack::Machine::Ewasm, AssemblyStack::Language::Ewasm},
{{"--machine=ewasm", "--yul-dialect=ewasm", "--strict-assembly"}, AssemblyStack::Machine::Ewasm, AssemblyStack::Language::Ewasm},
{{"--machine=ewasm", "--yul-dialect=evm", "--assemble"}, AssemblyStack::Machine::Ewasm, AssemblyStack::Language::StrictAssembly},
{{"--machine=ewasm", "--yul-dialect=evm", "--yul"}, AssemblyStack::Machine::Ewasm, AssemblyStack::Language::StrictAssembly},
{{"--machine=ewasm", "--yul-dialect=evm", "--strict-assembly"}, AssemblyStack::Machine::Ewasm, AssemblyStack::Language::StrictAssembly},
{{"--machine=ewasm", "--strict-assembly"}, AssemblyStack::Machine::Ewasm, AssemblyStack::Language::Ewasm},
{{"--machine=evm", "--yul-dialect=evm", "--assemble"}, AssemblyStack::Machine::EVM, AssemblyStack::Language::StrictAssembly},
{{"--machine=evm", "--yul-dialect=evm", "--yul"}, AssemblyStack::Machine::EVM, AssemblyStack::Language::StrictAssembly},
{{"--machine=evm", "--yul-dialect=evm", "--strict-assembly"}, AssemblyStack::Machine::EVM, AssemblyStack::Language::StrictAssembly},
{{"--machine=evm", "--assemble"}, AssemblyStack::Machine::EVM, AssemblyStack::Language::Assembly},
{{"--machine=evm", "--yul"}, AssemblyStack::Machine::EVM, AssemblyStack::Language::Yul},
{{"--machine=evm", "--strict-assembly"}, AssemblyStack::Machine::EVM, AssemblyStack::Language::StrictAssembly},
{{"--assemble"}, AssemblyStack::Machine::EVM, AssemblyStack::Language::Assembly},
{{"--yul"}, AssemblyStack::Machine::EVM, AssemblyStack::Language::Yul},
{{"--strict-assembly"}, AssemblyStack::Machine::EVM, AssemblyStack::Language::StrictAssembly},
};
for (auto const& [assemblyOptions, expectedMachine, expectedLanguage]: allowedCombinations)
{
vector<string> commandLine = {
"solc",
"contract.yul",
"/tmp/projects/token.yul",
"/home/user/lib/dex.yul",
"file",
"input.json",
"-",
"/tmp=/usr/lib/",
"a:b=c/d",
":contract.yul=",
"--base-path=/home/user/",
"--allow-paths=/tmp,/home,project,../contracts",
"--ignore-missing",
"--error-recovery", // Ignored in assembly mode
"--overwrite",
"--evm-version=spuriousDragon",
"--experimental-via-ir", // Ignored in assembly mode
"--revert-strings=strip", // Accepted but has no effect in assembly mode
"--pretty-json",
"--no-color",
"--error-codes",
"--libraries="
"dir1/file1.sol:L=0x1234567890123456789012345678901234567890,"
"dir2/file2.sol:L=0x1111122222333334444455555666667777788888",
"--metadata-hash=swarm", // Ignored in assembly mode
"--metadata-literal", // Ignored in assembly mode
"--model-checker-contracts=" // Ignored in assembly mode
"contract1.yul:A,"
"contract2.yul:B",
"--model-checker-engine=bmc", // Ignored in assembly mode
"--model-checker-targets=" // Ignored in assembly mode
"underflow,"
"divByZero",
"--model-checker-timeout=5", // Ignored in assembly mode
// Accepted but has no effect in assembly mode
"--ast-compact-json", "--asm", "--asm-json", "--opcodes", "--bin", "--bin-runtime", "--abi",
"--ir", "--ir-optimized", "--ewasm", "--hashes", "--userdoc", "--devdoc", "--metadata", "--storage-layout",
};
commandLine += assemblyOptions;
if (expectedLanguage == AssemblyStack::Language::StrictAssembly || expectedLanguage == AssemblyStack::Language::Ewasm)
commandLine += vector<string>{
"--optimize",
"--optimize-runs=1000", // Ignored in assembly mode
"--yul-optimizations=agf",
};
CommandLineOptions expectedOptions;
expectedOptions.input.mode = InputMode::Assembler;
expectedOptions.input.paths = {"contract.yul", "/tmp/projects/token.yul", "/home/user/lib/dex.yul", "file", "input.json"};
expectedOptions.input.remappings = {
{"", "/tmp", "/usr/lib/"},
{"a", "b", "c/d"},
{"", "contract.yul", ""},
};
expectedOptions.input.addStdin = true;
expectedOptions.input.basePath = "/home/user/";
expectedOptions.input.allowedDirectories = {"/tmp", "/home", "project", "../contracts", "", "c", "/usr/lib"};
expectedOptions.input.ignoreMissingFiles = true;
expectedOptions.output.overwriteFiles = true;
expectedOptions.output.evmVersion = EVMVersion::spuriousDragon();
expectedOptions.output.revertStrings = RevertStrings::Strip;
expectedOptions.assembly.targetMachine = expectedMachine;
expectedOptions.assembly.inputLanguage = expectedLanguage;
expectedOptions.linker.libraries = {
{"dir1/file1.sol:L", h160("1234567890123456789012345678901234567890")},
{"dir2/file2.sol:L", h160("1111122222333334444455555666667777788888")},
};
expectedOptions.formatting.prettyJson = true;
expectedOptions.formatting.coloredOutput = false;
expectedOptions.formatting.withErrorIds = true;
expectedOptions.compiler.outputs = {
true, true, true, true, true,
true, true, true, true, true,
true, true, true, true, true,
};
if (expectedLanguage == AssemblyStack::Language::StrictAssembly || expectedLanguage == AssemblyStack::Language::Ewasm)
{
expectedOptions.optimizer.enabled = true;
expectedOptions.optimizer.yulSteps = "agf";
}
optional<CommandLineOptions> parsedOptions = parseCommandLine(commandLine);
BOOST_REQUIRE(parsedOptions.has_value());
BOOST_TEST((parsedOptions.value() == expectedOptions));
}
}
BOOST_AUTO_TEST_CASE(standard_json_mode_options)
{
vector<string> commandLine = {
"solc",
"input.json",
"--standard-json",
"--base-path=/home/user/",
"--allow-paths=/tmp,/home,project,../contracts",
"--ignore-missing", // Ignored in Standard JSON mode
"--error-recovery", // Ignored in Standard JSON mode
"--output-dir=/tmp/out", // Accepted but has no effect in Standard JSON mode
"--overwrite", // Accepted but has no effect in Standard JSON mode
"--evm-version=spuriousDragon", // Ignored in Standard JSON mode
"--experimental-via-ir", // Ignored in Standard JSON mode
"--revert-strings=strip", // Accepted but has no effect in Standard JSON mode
"--pretty-json", // Accepted but has no effect in Standard JSON mode
"--no-color", // Accepted but has no effect in Standard JSON mode
"--error-codes", // Accepted but has no effect in Standard JSON mode
"--libraries=" // Ignored in Standard JSON mode
"dir1/file1.sol:L=0x1234567890123456789012345678901234567890,"
"dir2/file2.sol:L=0x1111122222333334444455555666667777788888",
"--gas", // Accepted but has no effect in Standard JSON mode
"--combined-json=abi,bin", // Accepted but has no effect in Standard JSON mode
"--metadata-hash=swarm", // Ignored in Standard JSON mode
"--metadata-literal", // Ignored in Standard JSON mode
"--optimize", // Ignored in Standard JSON mode
"--optimize-runs=1000", // Ignored in Standard JSON mode
"--yul-optimizations=agf",
"--model-checker-contracts=" // Ignored in Standard JSON mode
"contract1.yul:A,"
"contract2.yul:B",
"--model-checker-engine=bmc", // Ignored in Standard JSON mode
"--model-checker-targets=" // Ignored in Standard JSON mode
"underflow,"
"divByZero",
"--model-checker-timeout=5", // Ignored in Standard JSON mode
// Accepted but has no effect in Standard JSON mode
"--ast-compact-json", "--asm", "--asm-json", "--opcodes", "--bin", "--bin-runtime", "--abi",
"--ir", "--ir-optimized", "--ewasm", "--hashes", "--userdoc", "--devdoc", "--metadata", "--storage-layout",
};
CommandLineOptions expectedOptions;
expectedOptions.input.mode = InputMode::StandardJson;
expectedOptions.input.paths = {};
expectedOptions.input.standardJsonFile = "input.json";
expectedOptions.input.basePath = "/home/user/";
expectedOptions.input.allowedDirectories = {"/tmp", "/home", "project", "../contracts"};
expectedOptions.output.dir = "/tmp/out";
expectedOptions.output.overwriteFiles = true;
expectedOptions.output.revertStrings = RevertStrings::Strip;
expectedOptions.formatting.prettyJson = true;
expectedOptions.formatting.coloredOutput = false;
expectedOptions.formatting.withErrorIds = true;
expectedOptions.compiler.outputs = {
true, true, true, true, true,
true, true, true, true, true,
true, true, true, true, true,
};
expectedOptions.compiler.estimateGas = true;
expectedOptions.compiler.combinedJsonRequests = CombinedJsonRequests{};
expectedOptions.compiler.combinedJsonRequests->abi = true;
expectedOptions.compiler.combinedJsonRequests->binary = true;
optional<CommandLineOptions> parsedOptions = parseCommandLine(commandLine);
BOOST_REQUIRE(parsedOptions.has_value());
BOOST_TEST((parsedOptions.value() == expectedOptions));
}
BOOST_AUTO_TEST_SUITE_END()
} // namespace solidity::frontend::test