From 65d09e799532bf1d66990962987cd344e7f27eac Mon Sep 17 00:00:00 2001 From: Alexander Arlt Date: Tue, 16 Aug 2022 15:52:33 +0200 Subject: [PATCH] [solc] CommandLineParser: introduction of any_of / none_of. --- solc/CommandLineInterface.cpp | 115 ++++++++------------------------ solc/CommandLineParser.h | 15 +++++ test/solc/CommandLineParser.cpp | 28 ++++++++ 3 files changed, 72 insertions(+), 86 deletions(-) diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index 64d1913e8..15bd24119 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -89,6 +89,17 @@ using namespace solidity; using namespace solidity::util; using namespace solidity::langutil; +namespace +{ + +std::vector ValidInputModes{ + frontend::InputMode::Compiler, + frontend::InputMode::CompilerWithASTImport, + frontend::InputMode::CompilerWithEvmAssemblyJsonImport +}; + +} // anonymous namespace + namespace solidity::frontend { @@ -161,11 +172,7 @@ static bool coloredOutput(CommandLineOptions const& _options) void CommandLineInterface::handleBinary(string const& _contract) { - solAssert( - m_options.input.mode == InputMode::Compiler || - m_options.input.mode == InputMode::CompilerWithASTImport || - m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport - ); + solAssert(any_of(m_options.input.mode, ValidInputModes)); if (m_options.compiler.outputs.binary) { @@ -191,11 +198,7 @@ void CommandLineInterface::handleBinary(string const& _contract) void CommandLineInterface::handleOpcode(string const& _contract) { - solAssert( - m_options.input.mode == InputMode::Compiler || - m_options.input.mode == InputMode::CompilerWithASTImport || - m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport - ); + solAssert(any_of(m_options.input.mode, ValidInputModes)); if (!m_options.output.dir.empty()) createFile(m_compiler->filesystemFriendlyName(_contract) + ".opcode", evmasm::disassemble(m_compiler->object(_contract).bytecode)); @@ -209,11 +212,7 @@ void CommandLineInterface::handleOpcode(string const& _contract) void CommandLineInterface::handleIR(string const& _contractName) { - solAssert( - m_options.input.mode == InputMode::Compiler || - m_options.input.mode == InputMode::CompilerWithASTImport || - m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport - ); + solAssert(any_of(m_options.input.mode, ValidInputModes)); if (!m_options.compiler.outputs.ir) return; @@ -229,11 +228,7 @@ void CommandLineInterface::handleIR(string const& _contractName) void CommandLineInterface::handleIROptimized(string const& _contractName) { - solAssert( - m_options.input.mode == InputMode::Compiler || - m_options.input.mode == InputMode::CompilerWithASTImport || - m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport - ); + solAssert(any_of(m_options.input.mode, ValidInputModes)); if (!m_options.compiler.outputs.irOptimized) return; @@ -249,11 +244,7 @@ void CommandLineInterface::handleIROptimized(string const& _contractName) void CommandLineInterface::handleEwasm(string const& _contractName) { - solAssert( - m_options.input.mode == InputMode::Compiler || - m_options.input.mode == InputMode::CompilerWithASTImport || - m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport - ); + solAssert(any_of(m_options.input.mode, ValidInputModes)); if (!m_options.compiler.outputs.ewasm) return; @@ -276,11 +267,7 @@ void CommandLineInterface::handleEwasm(string const& _contractName) void CommandLineInterface::handleBytecode(string const& _contract) { - solAssert( - m_options.input.mode == InputMode::Compiler || - m_options.input.mode == InputMode::CompilerWithASTImport || - m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport - ); + solAssert(any_of(m_options.input.mode, ValidInputModes)); if (m_options.compiler.outputs.opcodes) handleOpcode(_contract); @@ -290,11 +277,7 @@ void CommandLineInterface::handleBytecode(string const& _contract) void CommandLineInterface::handleSignatureHashes(string const& _contract) { - solAssert( - m_options.input.mode == InputMode::Compiler || - m_options.input.mode == InputMode::CompilerWithASTImport || - m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport - ); + solAssert(any_of(m_options.input.mode, ValidInputModes)); if (!m_options.compiler.outputs.signatureHashes) return; @@ -326,11 +309,7 @@ void CommandLineInterface::handleSignatureHashes(string const& _contract) void CommandLineInterface::handleMetadata(string const& _contract) { - solAssert( - m_options.input.mode == InputMode::Compiler || - m_options.input.mode == InputMode::CompilerWithASTImport || - m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport - ); + solAssert(any_of(m_options.input.mode, ValidInputModes)); if (!m_options.compiler.outputs.metadata) return; @@ -344,11 +323,7 @@ void CommandLineInterface::handleMetadata(string const& _contract) void CommandLineInterface::handleABI(string const& _contract) { - solAssert( - m_options.input.mode == InputMode::Compiler || - m_options.input.mode == InputMode::CompilerWithASTImport || - m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport - ); + solAssert(any_of(m_options.input.mode, ValidInputModes)); if (!m_options.compiler.outputs.abi) return; @@ -362,11 +337,7 @@ void CommandLineInterface::handleABI(string const& _contract) void CommandLineInterface::handleStorageLayout(string const& _contract) { - solAssert( - m_options.input.mode == InputMode::Compiler || - m_options.input.mode == InputMode::CompilerWithASTImport || - m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport - ); + solAssert(any_of(m_options.input.mode, ValidInputModes)); if (!m_options.compiler.outputs.storageLayout) return; @@ -380,11 +351,7 @@ void CommandLineInterface::handleStorageLayout(string const& _contract) void CommandLineInterface::handleNatspec(bool _natspecDev, string const& _contract) { - solAssert( - m_options.input.mode == InputMode::Compiler || - m_options.input.mode == InputMode::CompilerWithASTImport || - m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport - ); + solAssert(any_of(m_options.input.mode, ValidInputModes)); bool enabled = false; std::string suffix; @@ -427,11 +394,7 @@ void CommandLineInterface::handleNatspec(bool _natspecDev, string const& _contra void CommandLineInterface::handleGasEstimation(string const& _contract) { - solAssert( - m_options.input.mode == InputMode::Compiler || - m_options.input.mode == InputMode::CompilerWithASTImport || - m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport - ); + solAssert(any_of(m_options.input.mode, ValidInputModes)); Json::Value estimates = m_compiler->gasEstimates(_contract); sout() << "Gas estimation:" << endl; @@ -475,11 +438,7 @@ void CommandLineInterface::readInputFiles() { solAssert(!m_standardJsonInput.has_value()); - if ( - m_options.input.mode == InputMode::Help || - m_options.input.mode == InputMode::License || - m_options.input.mode == InputMode::Version - ) + if (any_of(m_options.input.mode, {InputMode::Help, InputMode::License, InputMode::Version})) return; m_fileReader.setBasePath(m_options.input.basePath); @@ -747,11 +706,7 @@ void CommandLineInterface::printLicense() void CommandLineInterface::compile() { - solAssert( - m_options.input.mode == InputMode::Compiler || - m_options.input.mode == InputMode::CompilerWithASTImport || - m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport - ); + solAssert(any_of(m_options.input.mode, ValidInputModes)); m_compiler = make_unique(m_fileReader.reader()); @@ -869,11 +824,7 @@ void CommandLineInterface::compile() void CommandLineInterface::handleCombinedJSON() { - solAssert( - m_options.input.mode == InputMode::Compiler || - m_options.input.mode == InputMode::CompilerWithASTImport || - m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport - ); + solAssert(any_of(m_options.input.mode, ValidInputModes)); if (!m_options.compiler.combinedJsonRequests.has_value()) return; @@ -965,11 +916,7 @@ void CommandLineInterface::handleCombinedJSON() void CommandLineInterface::handleAst() { - solAssert( - m_options.input.mode == InputMode::Compiler || - m_options.input.mode == InputMode::CompilerWithASTImport || - m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport - ); + solAssert(any_of(m_options.input.mode, ValidInputModes)); if (!m_options.compiler.outputs.astCompactJson) return; @@ -1194,7 +1141,7 @@ void CommandLineInterface::assemble(yul::YulStack::Language _language, yul::YulS serr() << "No binary representation found." << endl; } - solAssert(_targetMachine == yul::YulStack::Machine::Ewasm || _targetMachine == yul::YulStack::Machine::EVM); + solAssert(any_of(_targetMachine, {yul::YulStack::Machine::Ewasm, yul::YulStack::Machine::EVM})); if ( (_targetMachine == yul::YulStack::Machine::EVM && m_options.compiler.outputs.asm_) || (_targetMachine == yul::YulStack::Machine::Ewasm && m_options.compiler.outputs.ewasm) @@ -1211,11 +1158,7 @@ void CommandLineInterface::assemble(yul::YulStack::Language _language, yul::YulS void CommandLineInterface::outputCompilationResults() { - solAssert( - m_options.input.mode == InputMode::Compiler || - m_options.input.mode == InputMode::CompilerWithASTImport || - m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport - ); + solAssert(any_of(m_options.input.mode, ValidInputModes)); handleCombinedJSON(); diff --git a/solc/CommandLineParser.h b/solc/CommandLineParser.h index c16d92a6a..f5c13196c 100644 --- a/solc/CommandLineParser.h +++ b/solc/CommandLineParser.h @@ -294,4 +294,19 @@ private: boost::program_options::variables_map m_args; }; +template +bool any_of(T& _what, std::vector _elements) +{ + for (auto const& element: _elements) + if (_what == element) + return true; + return false; +} + +template +bool none_of(T& _what, std::vector _elements) +{ + return !any_of(_what, _elements); +} + } diff --git a/test/solc/CommandLineParser.cpp b/test/solc/CommandLineParser.cpp index 544021886..f584b34b0 100644 --- a/test/solc/CommandLineParser.cpp +++ b/test/solc/CommandLineParser.cpp @@ -224,6 +224,34 @@ BOOST_AUTO_TEST_CASE(cli_mode_options) } } +BOOST_AUTO_TEST_CASE(CommandLineOptions_any_of) +{ + std::vector help_license{InputMode::Help, InputMode::License}; + CommandLineOptions help = parseCommandLine({"solc", "--help"}); + CommandLineOptions license = parseCommandLine({"solc", "--license"}); + CommandLineOptions version = parseCommandLine({"solc", "--version"}); + BOOST_TEST(any_of(help.input.mode, {InputMode::Help, InputMode::License})); + BOOST_TEST(any_of(license.input.mode, {InputMode::Help, InputMode::License})); + BOOST_TEST(any_of(help.input.mode, help_license)); + BOOST_TEST(any_of(license.input.mode, help_license)); + BOOST_TEST(!any_of(version.input.mode, help_license)); + BOOST_TEST(any_of(version.input.mode, {InputMode::Version})); +} + +BOOST_AUTO_TEST_CASE(CommandLineOptions_none_of) +{ + std::vector help_license{InputMode::Help, InputMode::License}; + CommandLineOptions help = parseCommandLine({"solc", "--help"}); + CommandLineOptions license = parseCommandLine({"solc", "--license"}); + CommandLineOptions version = parseCommandLine({"solc", "--version"}); + BOOST_TEST(!none_of(help.input.mode, {InputMode::Help, InputMode::License})); + BOOST_TEST(!none_of(license.input.mode, {InputMode::Help, InputMode::License})); + BOOST_TEST(!none_of(help.input.mode, help_license)); + BOOST_TEST(!none_of(license.input.mode, help_license)); + BOOST_TEST(none_of(version.input.mode, help_license)); + BOOST_TEST(!none_of(version.input.mode, {InputMode::Version})); +} + BOOST_AUTO_TEST_CASE(via_ir_options) { BOOST_TEST(!parseCommandLine({"solc", "contract.sol"}).output.viaIR);