From cd440fc5f964129d5e99a1ab2033c3b04752be03 Mon Sep 17 00:00:00 2001 From: Alexander Arlt Date: Wed, 28 Sep 2022 00:07:35 -0300 Subject: [PATCH] [solc] Refactor valid input modes. --- solc/CommandLineInterface.cpp | 74 ++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 31 deletions(-) diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index 0902e13f8..3e44f22c0 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -88,6 +88,16 @@ using namespace solidity; using namespace solidity::util; using namespace solidity::langutil; +namespace +{ + +set const CompilerInputModes{ + frontend::InputMode::Compiler, + frontend::InputMode::CompilerWithASTImport +}; + +} // anonymous namespace + namespace solidity::frontend { @@ -160,7 +170,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, ""); + solAssert(CompilerInputModes.count(m_options.input.mode) == 1); if (m_options.compiler.outputs.binary) { @@ -186,7 +196,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, ""); + solAssert(CompilerInputModes.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)); @@ -200,7 +210,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, ""); + solAssert(CompilerInputModes.count(m_options.input.mode) == 1); if (!m_options.compiler.outputs.ir) return; @@ -216,7 +226,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, ""); + solAssert(CompilerInputModes.count(m_options.input.mode) == 1); if (!m_options.compiler.outputs.irOptimized) return; @@ -232,7 +242,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, ""); + solAssert(CompilerInputModes.count(m_options.input.mode) == 1); if (!m_options.compiler.outputs.ewasm) return; @@ -255,7 +265,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, ""); + solAssert(CompilerInputModes.count(m_options.input.mode) == 1); if (m_options.compiler.outputs.opcodes) handleOpcode(_contract); @@ -265,7 +275,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, ""); + solAssert(CompilerInputModes.count(m_options.input.mode) == 1); if (!m_options.compiler.outputs.signatureHashes) return; @@ -297,7 +307,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, ""); + solAssert(CompilerInputModes.count(m_options.input.mode) == 1); if (!m_options.compiler.outputs.metadata) return; @@ -311,7 +321,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, ""); + solAssert(CompilerInputModes.count(m_options.input.mode) == 1); if (!m_options.compiler.outputs.abi) return; @@ -325,7 +335,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, ""); + solAssert(CompilerInputModes.count(m_options.input.mode) == 1); if (!m_options.compiler.outputs.storageLayout) return; @@ -339,7 +349,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, ""); + solAssert(CompilerInputModes.count(m_options.input.mode) == 1); bool enabled = false; std::string suffix; @@ -382,7 +392,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, ""); + solAssert(CompilerInputModes.count(m_options.input.mode) == 1); Json::Value estimates = m_compiler->gasEstimates(_contract); sout() << "Gas estimation:" << endl; @@ -424,13 +434,15 @@ void CommandLineInterface::handleGasEstimation(string const& _contract) void CommandLineInterface::readInputFiles() { - solAssert(!m_standardJsonInput.has_value(), ""); + solAssert(!m_standardJsonInput.has_value()); - if ( - m_options.input.mode == InputMode::Help || - m_options.input.mode == InputMode::License || - m_options.input.mode == InputMode::Version - ) + static set const noInputFiles{ + frontend::InputMode::Help, + frontend::InputMode::License, + frontend::InputMode::Version + }; + + if (noInputFiles.count(m_options.input.mode) == 1) return; m_fileReader.setBasePath(m_options.input.basePath); @@ -496,7 +508,7 @@ void CommandLineInterface::readInputFiles() string fileContent = readFileAsString(infile); if (m_options.input.mode == InputMode::StandardJson) { - solAssert(!m_standardJsonInput.has_value(), ""); + solAssert(!m_standardJsonInput.has_value()); m_standardJsonInput = std::move(fileContent); } else @@ -510,7 +522,7 @@ void CommandLineInterface::readInputFiles() { if (m_options.input.mode == InputMode::StandardJson) { - solAssert(!m_standardJsonInput.has_value(), ""); + solAssert(!m_standardJsonInput.has_value()); m_standardJsonInput = readUntilEnd(m_sin); } else @@ -527,7 +539,7 @@ void CommandLineInterface::readInputFiles() map CommandLineInterface::parseAstFromInput() { - solAssert(m_options.input.mode == InputMode::CompilerWithASTImport, ""); + solAssert(m_options.input.mode == InputMode::CompilerWithASTImport); map sourceJsons; map tmpSources; @@ -559,7 +571,7 @@ void CommandLineInterface::createFile(string const& _fileName, string const& _da { namespace fs = boost::filesystem; - solAssert(!m_options.output.dir.empty(), ""); + solAssert(!m_options.output.dir.empty()); // NOTE: create_directories() raises an exception if the path consists solely of '.' or '..' // (or equivalent such as './././.'). Paths like 'a/b/.' and 'a/b/..' are fine though. @@ -639,7 +651,7 @@ void CommandLineInterface::processInput() break; case InputMode::StandardJson: { - solAssert(m_standardJsonInput.has_value(), ""); + solAssert(m_standardJsonInput.has_value()); StandardCompiler compiler(m_fileReader.reader(), m_options.formatting.json); sout() << compiler.compile(std::move(m_standardJsonInput.value())) << endl; @@ -678,7 +690,7 @@ void CommandLineInterface::printLicense() void CommandLineInterface::compile() { - solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); + solAssert(CompilerInputModes.count(m_options.input.mode) == 1); m_compiler = make_unique(m_fileReader.reader()); @@ -789,7 +801,7 @@ void CommandLineInterface::compile() void CommandLineInterface::handleCombinedJSON() { - solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); + solAssert(CompilerInputModes.count(m_options.input.mode) == 1); if (!m_options.compiler.combinedJsonRequests.has_value()) return; @@ -881,7 +893,7 @@ void CommandLineInterface::handleCombinedJSON() void CommandLineInterface::handleAst() { - solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); + solAssert(CompilerInputModes.count(m_options.input.mode) == 1); if (!m_options.compiler.outputs.astCompactJson) return; @@ -922,7 +934,7 @@ void CommandLineInterface::serveLSP() void CommandLineInterface::link() { - solAssert(m_options.input.mode == InputMode::Linker, ""); + solAssert(m_options.input.mode == InputMode::Linker); // Map from how the libraries will be named inside the bytecode to their addresses. map librariesReplacements; @@ -985,7 +997,7 @@ void CommandLineInterface::link() void CommandLineInterface::writeLinkedFiles() { - solAssert(m_options.input.mode == InputMode::Linker, ""); + solAssert(m_options.input.mode == InputMode::Linker); for (auto const& src: m_fileReader.sourceUnits()) if (src.first == g_stdinFileName) @@ -1019,14 +1031,14 @@ string CommandLineInterface::objectWithLinkRefsHex(evmasm::LinkerObject const& _ void CommandLineInterface::assemble(yul::YulStack::Language _language, yul::YulStack::Machine _targetMachine) { - solAssert(m_options.input.mode == InputMode::Assembler, ""); + solAssert(m_options.input.mode == InputMode::Assembler); bool successful = true; map yulStacks; for (auto const& src: m_fileReader.sourceUnits()) { // --no-optimize-yul option is not accepted in assembly mode. - solAssert(!m_options.optimizer.noOptimizeYul, ""); + solAssert(!m_options.optimizer.noOptimizeYul); auto& stack = yulStacks[src.first] = yul::YulStack( m_options.output.evmVersion, @@ -1123,7 +1135,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, ""); + solAssert(CompilerInputModes.count(m_options.input.mode) == 1); handleCombinedJSON();