CommandLineParser: Remove any_of / none_of.

This commit is contained in:
Alexander Arlt 2022-08-19 14:21:37 +02:00
parent 145152038c
commit 1774de0b3f
3 changed files with 20 additions and 63 deletions

View File

@ -92,7 +92,7 @@ using namespace solidity::langutil;
namespace
{
std::vector<frontend::InputMode> ValidInputModes{
std::set<frontend::InputMode> 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>{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<CompilerStack>(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();

View File

@ -294,19 +294,4 @@ private:
boost::program_options::variables_map m_args;
};
template<typename T>
bool any_of(T& _what, std::vector<T> _elements)
{
for (auto const& element: _elements)
if (_what == element)
return true;
return false;
}
template<typename T>
bool none_of(T& _what, std::vector<T> _elements)
{
return !any_of(_what, _elements);
}
}
} // namespace solidity::frontend

View File

@ -224,34 +224,6 @@ BOOST_AUTO_TEST_CASE(cli_mode_options)
}
}
BOOST_AUTO_TEST_CASE(CommandLineOptions_any_of)
{
std::vector<InputMode> 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<InputMode> 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);