From 1774de0b3f8cb64762de6aa529376cda2062885a Mon Sep 17 00:00:00 2001 From: Alexander Arlt Date: Fri, 19 Aug 2022 14:21:37 +0200 Subject: [PATCH] CommandLineParser: Remove any_of / none_of. --- solc/CommandLineInterface.cpp | 38 ++++++++++++++++----------------- solc/CommandLineParser.h | 17 +-------------- test/solc/CommandLineParser.cpp | 28 ------------------------ 3 files changed, 20 insertions(+), 63 deletions(-) diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index 15bd24119..c81b65186 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -92,7 +92,7 @@ using namespace solidity::langutil; namespace { -std::vector ValidInputModes{ +std::set ValidInputModes{ frontend::InputMode::Compiler, frontend::InputMode::CompilerWithASTImport, frontend::InputMode::CompilerWithEvmAssemblyJsonImport @@ -172,7 +172,7 @@ static bool coloredOutput(CommandLineOptions const& _options) void CommandLineInterface::handleBinary(string const& _contract) { - solAssert(any_of(m_options.input.mode, ValidInputModes)); + solAssert(ValidInputModes.count(m_options.input.mode) == 1); if (m_options.compiler.outputs.binary) { @@ -198,7 +198,7 @@ void CommandLineInterface::handleBinary(string const& _contract) void CommandLineInterface::handleOpcode(string const& _contract) { - solAssert(any_of(m_options.input.mode, ValidInputModes)); + solAssert(ValidInputModes.count(m_options.input.mode) == 1); if (!m_options.output.dir.empty()) createFile(m_compiler->filesystemFriendlyName(_contract) + ".opcode", evmasm::disassemble(m_compiler->object(_contract).bytecode)); @@ -212,7 +212,7 @@ void CommandLineInterface::handleOpcode(string const& _contract) void CommandLineInterface::handleIR(string const& _contractName) { - solAssert(any_of(m_options.input.mode, ValidInputModes)); + solAssert(ValidInputModes.count(m_options.input.mode) == 1); if (!m_options.compiler.outputs.ir) return; @@ -228,7 +228,7 @@ void CommandLineInterface::handleIR(string const& _contractName) void CommandLineInterface::handleIROptimized(string const& _contractName) { - solAssert(any_of(m_options.input.mode, ValidInputModes)); + solAssert(ValidInputModes.count(m_options.input.mode) == 1); if (!m_options.compiler.outputs.irOptimized) return; @@ -244,7 +244,7 @@ void CommandLineInterface::handleIROptimized(string const& _contractName) void CommandLineInterface::handleEwasm(string const& _contractName) { - solAssert(any_of(m_options.input.mode, ValidInputModes)); + solAssert(ValidInputModes.count(m_options.input.mode) == 1); if (!m_options.compiler.outputs.ewasm) return; @@ -267,7 +267,7 @@ void CommandLineInterface::handleEwasm(string const& _contractName) void CommandLineInterface::handleBytecode(string const& _contract) { - solAssert(any_of(m_options.input.mode, ValidInputModes)); + solAssert(ValidInputModes.count(m_options.input.mode) == 1); if (m_options.compiler.outputs.opcodes) handleOpcode(_contract); @@ -277,7 +277,7 @@ void CommandLineInterface::handleBytecode(string const& _contract) void CommandLineInterface::handleSignatureHashes(string const& _contract) { - solAssert(any_of(m_options.input.mode, ValidInputModes)); + solAssert(ValidInputModes.count(m_options.input.mode) == 1); if (!m_options.compiler.outputs.signatureHashes) return; @@ -309,7 +309,7 @@ void CommandLineInterface::handleSignatureHashes(string const& _contract) void CommandLineInterface::handleMetadata(string const& _contract) { - solAssert(any_of(m_options.input.mode, ValidInputModes)); + solAssert(ValidInputModes.count(m_options.input.mode) == 1); if (!m_options.compiler.outputs.metadata) return; @@ -323,7 +323,7 @@ void CommandLineInterface::handleMetadata(string const& _contract) void CommandLineInterface::handleABI(string const& _contract) { - solAssert(any_of(m_options.input.mode, ValidInputModes)); + solAssert(ValidInputModes.count(m_options.input.mode) == 1); if (!m_options.compiler.outputs.abi) return; @@ -337,7 +337,7 @@ void CommandLineInterface::handleABI(string const& _contract) void CommandLineInterface::handleStorageLayout(string const& _contract) { - solAssert(any_of(m_options.input.mode, ValidInputModes)); + solAssert(ValidInputModes.count(m_options.input.mode) == 1); if (!m_options.compiler.outputs.storageLayout) return; @@ -351,7 +351,7 @@ void CommandLineInterface::handleStorageLayout(string const& _contract) void CommandLineInterface::handleNatspec(bool _natspecDev, string const& _contract) { - solAssert(any_of(m_options.input.mode, ValidInputModes)); + solAssert(ValidInputModes.count(m_options.input.mode) == 1); bool enabled = false; std::string suffix; @@ -394,7 +394,7 @@ void CommandLineInterface::handleNatspec(bool _natspecDev, string const& _contra void CommandLineInterface::handleGasEstimation(string const& _contract) { - solAssert(any_of(m_options.input.mode, ValidInputModes)); + solAssert(ValidInputModes.count(m_options.input.mode) == 1); Json::Value estimates = m_compiler->gasEstimates(_contract); sout() << "Gas estimation:" << endl; @@ -438,7 +438,7 @@ void CommandLineInterface::readInputFiles() { solAssert(!m_standardJsonInput.has_value()); - if (any_of(m_options.input.mode, {InputMode::Help, InputMode::License, InputMode::Version})) + if (std::set{InputMode::Help, InputMode::License, InputMode::Version}.count(m_options.input.mode) == 1) return; m_fileReader.setBasePath(m_options.input.basePath); @@ -706,7 +706,7 @@ void CommandLineInterface::printLicense() void CommandLineInterface::compile() { - solAssert(any_of(m_options.input.mode, ValidInputModes)); + solAssert(ValidInputModes.count(m_options.input.mode) == 1); m_compiler = make_unique(m_fileReader.reader()); @@ -824,7 +824,7 @@ void CommandLineInterface::compile() void CommandLineInterface::handleCombinedJSON() { - solAssert(any_of(m_options.input.mode, ValidInputModes)); + solAssert(ValidInputModes.count(m_options.input.mode) == 1); if (!m_options.compiler.combinedJsonRequests.has_value()) return; @@ -916,7 +916,7 @@ void CommandLineInterface::handleCombinedJSON() void CommandLineInterface::handleAst() { - solAssert(any_of(m_options.input.mode, ValidInputModes)); + solAssert(ValidInputModes.count(m_options.input.mode) == 1); if (!m_options.compiler.outputs.astCompactJson) return; @@ -1141,7 +1141,7 @@ void CommandLineInterface::assemble(yul::YulStack::Language _language, yul::YulS serr() << "No binary representation found." << endl; } - solAssert(any_of(_targetMachine, {yul::YulStack::Machine::Ewasm, yul::YulStack::Machine::EVM})); + solAssert(_targetMachine == yul::YulStack::Machine::Ewasm || _targetMachine == 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) @@ -1158,7 +1158,7 @@ void CommandLineInterface::assemble(yul::YulStack::Language _language, yul::YulS void CommandLineInterface::outputCompilationResults() { - solAssert(any_of(m_options.input.mode, ValidInputModes)); + solAssert(ValidInputModes.count(m_options.input.mode) == 1); handleCombinedJSON(); diff --git a/solc/CommandLineParser.h b/solc/CommandLineParser.h index f5c13196c..12fec17b1 100644 --- a/solc/CommandLineParser.h +++ b/solc/CommandLineParser.h @@ -294,19 +294,4 @@ 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); -} - -} +} // namespace solidity::frontend diff --git a/test/solc/CommandLineParser.cpp b/test/solc/CommandLineParser.cpp index f584b34b0..544021886 100644 --- a/test/solc/CommandLineParser.cpp +++ b/test/solc/CommandLineParser.cpp @@ -224,34 +224,6 @@ 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);