Skip to content

Commit cd440fc

Browse files
committed
[solc] Refactor valid input modes.
1 parent 2cb618a commit cd440fc

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

solc/CommandLineInterface.cpp

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ using namespace solidity;
8888
using namespace solidity::util;
8989
using namespace solidity::langutil;
9090

91+
namespace
92+
{
93+
94+
set<frontend::InputMode> const CompilerInputModes{
95+
frontend::InputMode::Compiler,
96+
frontend::InputMode::CompilerWithASTImport
97+
};
98+
99+
} // anonymous namespace
100+
91101
namespace solidity::frontend
92102
{
93103

@@ -160,7 +170,7 @@ static bool coloredOutput(CommandLineOptions const& _options)
160170

161171
void CommandLineInterface::handleBinary(string const& _contract)
162172
{
163-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
173+
solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
164174

165175
if (m_options.compiler.outputs.binary)
166176
{
@@ -186,7 +196,7 @@ void CommandLineInterface::handleBinary(string const& _contract)
186196

187197
void CommandLineInterface::handleOpcode(string const& _contract)
188198
{
189-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
199+
solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
190200

191201
if (!m_options.output.dir.empty())
192202
createFile(m_compiler->filesystemFriendlyName(_contract) + ".opcode", evmasm::disassemble(m_compiler->object(_contract).bytecode));
@@ -200,7 +210,7 @@ void CommandLineInterface::handleOpcode(string const& _contract)
200210

201211
void CommandLineInterface::handleIR(string const& _contractName)
202212
{
203-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
213+
solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
204214

205215
if (!m_options.compiler.outputs.ir)
206216
return;
@@ -216,7 +226,7 @@ void CommandLineInterface::handleIR(string const& _contractName)
216226

217227
void CommandLineInterface::handleIROptimized(string const& _contractName)
218228
{
219-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
229+
solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
220230

221231
if (!m_options.compiler.outputs.irOptimized)
222232
return;
@@ -232,7 +242,7 @@ void CommandLineInterface::handleIROptimized(string const& _contractName)
232242

233243
void CommandLineInterface::handleEwasm(string const& _contractName)
234244
{
235-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
245+
solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
236246

237247
if (!m_options.compiler.outputs.ewasm)
238248
return;
@@ -255,7 +265,7 @@ void CommandLineInterface::handleEwasm(string const& _contractName)
255265

256266
void CommandLineInterface::handleBytecode(string const& _contract)
257267
{
258-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
268+
solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
259269

260270
if (m_options.compiler.outputs.opcodes)
261271
handleOpcode(_contract);
@@ -265,7 +275,7 @@ void CommandLineInterface::handleBytecode(string const& _contract)
265275

266276
void CommandLineInterface::handleSignatureHashes(string const& _contract)
267277
{
268-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
278+
solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
269279

270280
if (!m_options.compiler.outputs.signatureHashes)
271281
return;
@@ -297,7 +307,7 @@ void CommandLineInterface::handleSignatureHashes(string const& _contract)
297307

298308
void CommandLineInterface::handleMetadata(string const& _contract)
299309
{
300-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
310+
solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
301311

302312
if (!m_options.compiler.outputs.metadata)
303313
return;
@@ -311,7 +321,7 @@ void CommandLineInterface::handleMetadata(string const& _contract)
311321

312322
void CommandLineInterface::handleABI(string const& _contract)
313323
{
314-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
324+
solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
315325

316326
if (!m_options.compiler.outputs.abi)
317327
return;
@@ -325,7 +335,7 @@ void CommandLineInterface::handleABI(string const& _contract)
325335

326336
void CommandLineInterface::handleStorageLayout(string const& _contract)
327337
{
328-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
338+
solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
329339

330340
if (!m_options.compiler.outputs.storageLayout)
331341
return;
@@ -339,7 +349,7 @@ void CommandLineInterface::handleStorageLayout(string const& _contract)
339349

340350
void CommandLineInterface::handleNatspec(bool _natspecDev, string const& _contract)
341351
{
342-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
352+
solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
343353

344354
bool enabled = false;
345355
std::string suffix;
@@ -382,7 +392,7 @@ void CommandLineInterface::handleNatspec(bool _natspecDev, string const& _contra
382392

383393
void CommandLineInterface::handleGasEstimation(string const& _contract)
384394
{
385-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
395+
solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
386396

387397
Json::Value estimates = m_compiler->gasEstimates(_contract);
388398
sout() << "Gas estimation:" << endl;
@@ -424,13 +434,15 @@ void CommandLineInterface::handleGasEstimation(string const& _contract)
424434

425435
void CommandLineInterface::readInputFiles()
426436
{
427-
solAssert(!m_standardJsonInput.has_value(), "");
437+
solAssert(!m_standardJsonInput.has_value());
428438

429-
if (
430-
m_options.input.mode == InputMode::Help ||
431-
m_options.input.mode == InputMode::License ||
432-
m_options.input.mode == InputMode::Version
433-
)
439+
static set<frontend::InputMode> const noInputFiles{
440+
frontend::InputMode::Help,
441+
frontend::InputMode::License,
442+
frontend::InputMode::Version
443+
};
444+
445+
if (noInputFiles.count(m_options.input.mode) == 1)
434446
return;
435447

436448
m_fileReader.setBasePath(m_options.input.basePath);
@@ -496,7 +508,7 @@ void CommandLineInterface::readInputFiles()
496508
string fileContent = readFileAsString(infile);
497509
if (m_options.input.mode == InputMode::StandardJson)
498510
{
499-
solAssert(!m_standardJsonInput.has_value(), "");
511+
solAssert(!m_standardJsonInput.has_value());
500512
m_standardJsonInput = std::move(fileContent);
501513
}
502514
else
@@ -510,7 +522,7 @@ void CommandLineInterface::readInputFiles()
510522
{
511523
if (m_options.input.mode == InputMode::StandardJson)
512524
{
513-
solAssert(!m_standardJsonInput.has_value(), "");
525+
solAssert(!m_standardJsonInput.has_value());
514526
m_standardJsonInput = readUntilEnd(m_sin);
515527
}
516528
else
@@ -527,7 +539,7 @@ void CommandLineInterface::readInputFiles()
527539

528540
map<string, Json::Value> CommandLineInterface::parseAstFromInput()
529541
{
530-
solAssert(m_options.input.mode == InputMode::CompilerWithASTImport, "");
542+
solAssert(m_options.input.mode == InputMode::CompilerWithASTImport);
531543

532544
map<string, Json::Value> sourceJsons;
533545
map<string, string> tmpSources;
@@ -559,7 +571,7 @@ void CommandLineInterface::createFile(string const& _fileName, string const& _da
559571
{
560572
namespace fs = boost::filesystem;
561573

562-
solAssert(!m_options.output.dir.empty(), "");
574+
solAssert(!m_options.output.dir.empty());
563575

564576
// NOTE: create_directories() raises an exception if the path consists solely of '.' or '..'
565577
// (or equivalent such as './././.'). Paths like 'a/b/.' and 'a/b/..' are fine though.
@@ -639,7 +651,7 @@ void CommandLineInterface::processInput()
639651
break;
640652
case InputMode::StandardJson:
641653
{
642-
solAssert(m_standardJsonInput.has_value(), "");
654+
solAssert(m_standardJsonInput.has_value());
643655

644656
StandardCompiler compiler(m_fileReader.reader(), m_options.formatting.json);
645657
sout() << compiler.compile(std::move(m_standardJsonInput.value())) << endl;
@@ -678,7 +690,7 @@ void CommandLineInterface::printLicense()
678690

679691
void CommandLineInterface::compile()
680692
{
681-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
693+
solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
682694

683695
m_compiler = make_unique<CompilerStack>(m_fileReader.reader());
684696

@@ -789,7 +801,7 @@ void CommandLineInterface::compile()
789801

790802
void CommandLineInterface::handleCombinedJSON()
791803
{
792-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
804+
solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
793805

794806
if (!m_options.compiler.combinedJsonRequests.has_value())
795807
return;
@@ -881,7 +893,7 @@ void CommandLineInterface::handleCombinedJSON()
881893

882894
void CommandLineInterface::handleAst()
883895
{
884-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
896+
solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
885897

886898
if (!m_options.compiler.outputs.astCompactJson)
887899
return;
@@ -922,7 +934,7 @@ void CommandLineInterface::serveLSP()
922934

923935
void CommandLineInterface::link()
924936
{
925-
solAssert(m_options.input.mode == InputMode::Linker, "");
937+
solAssert(m_options.input.mode == InputMode::Linker);
926938

927939
// Map from how the libraries will be named inside the bytecode to their addresses.
928940
map<string, h160> librariesReplacements;
@@ -985,7 +997,7 @@ void CommandLineInterface::link()
985997

986998
void CommandLineInterface::writeLinkedFiles()
987999
{
988-
solAssert(m_options.input.mode == InputMode::Linker, "");
1000+
solAssert(m_options.input.mode == InputMode::Linker);
9891001

9901002
for (auto const& src: m_fileReader.sourceUnits())
9911003
if (src.first == g_stdinFileName)
@@ -1019,14 +1031,14 @@ string CommandLineInterface::objectWithLinkRefsHex(evmasm::LinkerObject const& _
10191031

10201032
void CommandLineInterface::assemble(yul::YulStack::Language _language, yul::YulStack::Machine _targetMachine)
10211033
{
1022-
solAssert(m_options.input.mode == InputMode::Assembler, "");
1034+
solAssert(m_options.input.mode == InputMode::Assembler);
10231035

10241036
bool successful = true;
10251037
map<string, yul::YulStack> yulStacks;
10261038
for (auto const& src: m_fileReader.sourceUnits())
10271039
{
10281040
// --no-optimize-yul option is not accepted in assembly mode.
1029-
solAssert(!m_options.optimizer.noOptimizeYul, "");
1041+
solAssert(!m_options.optimizer.noOptimizeYul);
10301042

10311043
auto& stack = yulStacks[src.first] = yul::YulStack(
10321044
m_options.output.evmVersion,
@@ -1123,7 +1135,7 @@ void CommandLineInterface::assemble(yul::YulStack::Language _language, yul::YulS
11231135

11241136
void CommandLineInterface::outputCompilationResults()
11251137
{
1126-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
1138+
solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
11271139

11281140
handleCombinedJSON();
11291141

0 commit comments

Comments
 (0)