[solc] CommandLineParser: introduction of any_of / none_of.

This commit is contained in:
Alexander Arlt 2022-08-16 15:52:33 +02:00
parent f065760194
commit 65d09e7995
3 changed files with 72 additions and 86 deletions

View File

@ -89,6 +89,17 @@ using namespace solidity;
using namespace solidity::util; using namespace solidity::util;
using namespace solidity::langutil; using namespace solidity::langutil;
namespace
{
std::vector<frontend::InputMode> ValidInputModes{
frontend::InputMode::Compiler,
frontend::InputMode::CompilerWithASTImport,
frontend::InputMode::CompilerWithEvmAssemblyJsonImport
};
} // anonymous namespace
namespace solidity::frontend namespace solidity::frontend
{ {
@ -161,11 +172,7 @@ static bool coloredOutput(CommandLineOptions const& _options)
void CommandLineInterface::handleBinary(string const& _contract) void CommandLineInterface::handleBinary(string const& _contract)
{ {
solAssert( solAssert(any_of(m_options.input.mode, ValidInputModes));
m_options.input.mode == InputMode::Compiler ||
m_options.input.mode == InputMode::CompilerWithASTImport ||
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
);
if (m_options.compiler.outputs.binary) if (m_options.compiler.outputs.binary)
{ {
@ -191,11 +198,7 @@ void CommandLineInterface::handleBinary(string const& _contract)
void CommandLineInterface::handleOpcode(string const& _contract) void CommandLineInterface::handleOpcode(string const& _contract)
{ {
solAssert( solAssert(any_of(m_options.input.mode, ValidInputModes));
m_options.input.mode == InputMode::Compiler ||
m_options.input.mode == InputMode::CompilerWithASTImport ||
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
);
if (!m_options.output.dir.empty()) if (!m_options.output.dir.empty())
createFile(m_compiler->filesystemFriendlyName(_contract) + ".opcode", evmasm::disassemble(m_compiler->object(_contract).bytecode)); 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) void CommandLineInterface::handleIR(string const& _contractName)
{ {
solAssert( solAssert(any_of(m_options.input.mode, ValidInputModes));
m_options.input.mode == InputMode::Compiler ||
m_options.input.mode == InputMode::CompilerWithASTImport ||
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
);
if (!m_options.compiler.outputs.ir) if (!m_options.compiler.outputs.ir)
return; return;
@ -229,11 +228,7 @@ void CommandLineInterface::handleIR(string const& _contractName)
void CommandLineInterface::handleIROptimized(string const& _contractName) void CommandLineInterface::handleIROptimized(string const& _contractName)
{ {
solAssert( solAssert(any_of(m_options.input.mode, ValidInputModes));
m_options.input.mode == InputMode::Compiler ||
m_options.input.mode == InputMode::CompilerWithASTImport ||
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
);
if (!m_options.compiler.outputs.irOptimized) if (!m_options.compiler.outputs.irOptimized)
return; return;
@ -249,11 +244,7 @@ void CommandLineInterface::handleIROptimized(string const& _contractName)
void CommandLineInterface::handleEwasm(string const& _contractName) void CommandLineInterface::handleEwasm(string const& _contractName)
{ {
solAssert( solAssert(any_of(m_options.input.mode, ValidInputModes));
m_options.input.mode == InputMode::Compiler ||
m_options.input.mode == InputMode::CompilerWithASTImport ||
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
);
if (!m_options.compiler.outputs.ewasm) if (!m_options.compiler.outputs.ewasm)
return; return;
@ -276,11 +267,7 @@ void CommandLineInterface::handleEwasm(string const& _contractName)
void CommandLineInterface::handleBytecode(string const& _contract) void CommandLineInterface::handleBytecode(string const& _contract)
{ {
solAssert( solAssert(any_of(m_options.input.mode, ValidInputModes));
m_options.input.mode == InputMode::Compiler ||
m_options.input.mode == InputMode::CompilerWithASTImport ||
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
);
if (m_options.compiler.outputs.opcodes) if (m_options.compiler.outputs.opcodes)
handleOpcode(_contract); handleOpcode(_contract);
@ -290,11 +277,7 @@ void CommandLineInterface::handleBytecode(string const& _contract)
void CommandLineInterface::handleSignatureHashes(string const& _contract) void CommandLineInterface::handleSignatureHashes(string const& _contract)
{ {
solAssert( solAssert(any_of(m_options.input.mode, ValidInputModes));
m_options.input.mode == InputMode::Compiler ||
m_options.input.mode == InputMode::CompilerWithASTImport ||
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
);
if (!m_options.compiler.outputs.signatureHashes) if (!m_options.compiler.outputs.signatureHashes)
return; return;
@ -326,11 +309,7 @@ void CommandLineInterface::handleSignatureHashes(string const& _contract)
void CommandLineInterface::handleMetadata(string const& _contract) void CommandLineInterface::handleMetadata(string const& _contract)
{ {
solAssert( solAssert(any_of(m_options.input.mode, ValidInputModes));
m_options.input.mode == InputMode::Compiler ||
m_options.input.mode == InputMode::CompilerWithASTImport ||
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
);
if (!m_options.compiler.outputs.metadata) if (!m_options.compiler.outputs.metadata)
return; return;
@ -344,11 +323,7 @@ void CommandLineInterface::handleMetadata(string const& _contract)
void CommandLineInterface::handleABI(string const& _contract) void CommandLineInterface::handleABI(string const& _contract)
{ {
solAssert( solAssert(any_of(m_options.input.mode, ValidInputModes));
m_options.input.mode == InputMode::Compiler ||
m_options.input.mode == InputMode::CompilerWithASTImport ||
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
);
if (!m_options.compiler.outputs.abi) if (!m_options.compiler.outputs.abi)
return; return;
@ -362,11 +337,7 @@ void CommandLineInterface::handleABI(string const& _contract)
void CommandLineInterface::handleStorageLayout(string const& _contract) void CommandLineInterface::handleStorageLayout(string const& _contract)
{ {
solAssert( solAssert(any_of(m_options.input.mode, ValidInputModes));
m_options.input.mode == InputMode::Compiler ||
m_options.input.mode == InputMode::CompilerWithASTImport ||
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
);
if (!m_options.compiler.outputs.storageLayout) if (!m_options.compiler.outputs.storageLayout)
return; return;
@ -380,11 +351,7 @@ void CommandLineInterface::handleStorageLayout(string const& _contract)
void CommandLineInterface::handleNatspec(bool _natspecDev, string const& _contract) void CommandLineInterface::handleNatspec(bool _natspecDev, string const& _contract)
{ {
solAssert( solAssert(any_of(m_options.input.mode, ValidInputModes));
m_options.input.mode == InputMode::Compiler ||
m_options.input.mode == InputMode::CompilerWithASTImport ||
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
);
bool enabled = false; bool enabled = false;
std::string suffix; std::string suffix;
@ -427,11 +394,7 @@ void CommandLineInterface::handleNatspec(bool _natspecDev, string const& _contra
void CommandLineInterface::handleGasEstimation(string const& _contract) void CommandLineInterface::handleGasEstimation(string const& _contract)
{ {
solAssert( solAssert(any_of(m_options.input.mode, ValidInputModes));
m_options.input.mode == InputMode::Compiler ||
m_options.input.mode == InputMode::CompilerWithASTImport ||
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
);
Json::Value estimates = m_compiler->gasEstimates(_contract); Json::Value estimates = m_compiler->gasEstimates(_contract);
sout() << "Gas estimation:" << endl; sout() << "Gas estimation:" << endl;
@ -475,11 +438,7 @@ void CommandLineInterface::readInputFiles()
{ {
solAssert(!m_standardJsonInput.has_value()); solAssert(!m_standardJsonInput.has_value());
if ( if (any_of(m_options.input.mode, {InputMode::Help, InputMode::License, InputMode::Version}))
m_options.input.mode == InputMode::Help ||
m_options.input.mode == InputMode::License ||
m_options.input.mode == InputMode::Version
)
return; return;
m_fileReader.setBasePath(m_options.input.basePath); m_fileReader.setBasePath(m_options.input.basePath);
@ -747,11 +706,7 @@ void CommandLineInterface::printLicense()
void CommandLineInterface::compile() void CommandLineInterface::compile()
{ {
solAssert( solAssert(any_of(m_options.input.mode, ValidInputModes));
m_options.input.mode == InputMode::Compiler ||
m_options.input.mode == InputMode::CompilerWithASTImport ||
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
);
m_compiler = make_unique<CompilerStack>(m_fileReader.reader()); m_compiler = make_unique<CompilerStack>(m_fileReader.reader());
@ -869,11 +824,7 @@ void CommandLineInterface::compile()
void CommandLineInterface::handleCombinedJSON() void CommandLineInterface::handleCombinedJSON()
{ {
solAssert( solAssert(any_of(m_options.input.mode, ValidInputModes));
m_options.input.mode == InputMode::Compiler ||
m_options.input.mode == InputMode::CompilerWithASTImport ||
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
);
if (!m_options.compiler.combinedJsonRequests.has_value()) if (!m_options.compiler.combinedJsonRequests.has_value())
return; return;
@ -965,11 +916,7 @@ void CommandLineInterface::handleCombinedJSON()
void CommandLineInterface::handleAst() void CommandLineInterface::handleAst()
{ {
solAssert( solAssert(any_of(m_options.input.mode, ValidInputModes));
m_options.input.mode == InputMode::Compiler ||
m_options.input.mode == InputMode::CompilerWithASTImport ||
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
);
if (!m_options.compiler.outputs.astCompactJson) if (!m_options.compiler.outputs.astCompactJson)
return; return;
@ -1194,7 +1141,7 @@ void CommandLineInterface::assemble(yul::YulStack::Language _language, yul::YulS
serr() << "No binary representation found." << endl; 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 ( if (
(_targetMachine == yul::YulStack::Machine::EVM && m_options.compiler.outputs.asm_) || (_targetMachine == yul::YulStack::Machine::EVM && m_options.compiler.outputs.asm_) ||
(_targetMachine == yul::YulStack::Machine::Ewasm && m_options.compiler.outputs.ewasm) (_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() void CommandLineInterface::outputCompilationResults()
{ {
solAssert( solAssert(any_of(m_options.input.mode, ValidInputModes));
m_options.input.mode == InputMode::Compiler ||
m_options.input.mode == InputMode::CompilerWithASTImport ||
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
);
handleCombinedJSON(); handleCombinedJSON();

View File

@ -294,4 +294,19 @@ private:
boost::program_options::variables_map m_args; 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);
}
} }

View File

@ -224,6 +224,34 @@ 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_AUTO_TEST_CASE(via_ir_options)
{ {
BOOST_TEST(!parseCommandLine({"solc", "contract.sol"}).output.viaIR); BOOST_TEST(!parseCommandLine({"solc", "contract.sol"}).output.viaIR);