Skip to content

[solc] Refactor valid input modes. #13577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 43 additions & 31 deletions solc/CommandLineInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ using namespace solidity;
using namespace solidity::util;
using namespace solidity::langutil;

namespace
{

set<frontend::InputMode> const CompilerInputModes{
frontend::InputMode::Compiler,
frontend::InputMode::CompilerWithASTImport
};

} // anonymous namespace

namespace solidity::frontend
{

Expand Down Expand Up @@ -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)
{
Expand All @@ -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));
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -424,13 +434,15 @@ void CommandLineInterface::handleGasEstimation(string const& _contract)

void CommandLineInterface::readInputFiles()
{
solAssert(!m_standardJsonInput.has_value(), "");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious because I've seen it all over the place - is this equivalent to your altered version where you don't pass ""? I.e. will this just terminate with an empty message?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, this is equivalent to an empty message.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Until quite recently you could not skip the message (#12019). And it's just a tiny detail, so I don't think it makes sense to spend time replacing it all everywhere. We just sometimes remove the empty messages when we clean up code.

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<frontend::InputMode> 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);
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -527,7 +539,7 @@ void CommandLineInterface::readInputFiles()

map<string, Json::Value> CommandLineInterface::parseAstFromInput()
{
solAssert(m_options.input.mode == InputMode::CompilerWithASTImport, "");
solAssert(m_options.input.mode == InputMode::CompilerWithASTImport);

map<string, Json::Value> sourceJsons;
map<string, string> tmpSources;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<CompilerStack>(m_fileReader.reader());

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, h160> librariesReplacements;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<string, yul::YulStack> 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,
Expand Down Expand Up @@ -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();

Expand Down