Skip to content

Commit 199ed76

Browse files
committed
[solc] Refactor valid input modes.
1 parent 2201526 commit 199ed76

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

solc/CommandLineInterface.cpp

Lines changed: 37 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+
std::set<frontend::InputMode> ValidInputModes{
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(ValidInputModes.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(ValidInputModes.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(ValidInputModes.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(ValidInputModes.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(ValidInputModes.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(ValidInputModes.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(ValidInputModes.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(ValidInputModes.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(ValidInputModes.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(ValidInputModes.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(ValidInputModes.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(ValidInputModes.count(m_options.input.mode) == 1);
386396

387397
Json::Value estimates = m_compiler->gasEstimates(_contract);
388398
sout() << "Gas estimation:" << endl;
@@ -424,13 +434,9 @@ 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+
if (std::set<InputMode>{InputMode::Help, InputMode::License, InputMode::Version}.count(m_options.input.mode) == 1)
434440
return;
435441

436442
m_fileReader.setBasePath(m_options.input.basePath);
@@ -496,7 +502,7 @@ void CommandLineInterface::readInputFiles()
496502
string fileContent = readFileAsString(infile);
497503
if (m_options.input.mode == InputMode::StandardJson)
498504
{
499-
solAssert(!m_standardJsonInput.has_value(), "");
505+
solAssert(!m_standardJsonInput.has_value());
500506
m_standardJsonInput = std::move(fileContent);
501507
}
502508
else
@@ -510,7 +516,7 @@ void CommandLineInterface::readInputFiles()
510516
{
511517
if (m_options.input.mode == InputMode::StandardJson)
512518
{
513-
solAssert(!m_standardJsonInput.has_value(), "");
519+
solAssert(!m_standardJsonInput.has_value());
514520
m_standardJsonInput = readUntilEnd(m_sin);
515521
}
516522
else
@@ -527,7 +533,7 @@ void CommandLineInterface::readInputFiles()
527533

528534
map<string, Json::Value> CommandLineInterface::parseAstFromInput()
529535
{
530-
solAssert(m_options.input.mode == InputMode::CompilerWithASTImport, "");
536+
solAssert(m_options.input.mode == InputMode::CompilerWithASTImport);
531537

532538
map<string, Json::Value> sourceJsons;
533539
map<string, string> tmpSources;
@@ -559,7 +565,7 @@ void CommandLineInterface::createFile(string const& _fileName, string const& _da
559565
{
560566
namespace fs = boost::filesystem;
561567

562-
solAssert(!m_options.output.dir.empty(), "");
568+
solAssert(!m_options.output.dir.empty());
563569

564570
// NOTE: create_directories() raises an exception if the path consists solely of '.' or '..'
565571
// (or equivalent such as './././.'). Paths like 'a/b/.' and 'a/b/..' are fine though.
@@ -639,7 +645,7 @@ void CommandLineInterface::processInput()
639645
break;
640646
case InputMode::StandardJson:
641647
{
642-
solAssert(m_standardJsonInput.has_value(), "");
648+
solAssert(m_standardJsonInput.has_value());
643649

644650
StandardCompiler compiler(m_fileReader.reader(), m_options.formatting.json);
645651
sout() << compiler.compile(std::move(m_standardJsonInput.value())) << endl;
@@ -678,7 +684,7 @@ void CommandLineInterface::printLicense()
678684

679685
void CommandLineInterface::compile()
680686
{
681-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
687+
solAssert(ValidInputModes.count(m_options.input.mode) == 1);
682688

683689
m_compiler = make_unique<CompilerStack>(m_fileReader.reader());
684690

@@ -788,7 +794,7 @@ void CommandLineInterface::compile()
788794

789795
void CommandLineInterface::handleCombinedJSON()
790796
{
791-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
797+
solAssert(ValidInputModes.count(m_options.input.mode) == 1);
792798

793799
if (!m_options.compiler.combinedJsonRequests.has_value())
794800
return;
@@ -880,7 +886,7 @@ void CommandLineInterface::handleCombinedJSON()
880886

881887
void CommandLineInterface::handleAst()
882888
{
883-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
889+
solAssert(ValidInputModes.count(m_options.input.mode) == 1);
884890

885891
if (!m_options.compiler.outputs.astCompactJson)
886892
return;
@@ -921,7 +927,7 @@ void CommandLineInterface::serveLSP()
921927

922928
void CommandLineInterface::link()
923929
{
924-
solAssert(m_options.input.mode == InputMode::Linker, "");
930+
solAssert(m_options.input.mode == InputMode::Linker);
925931

926932
// Map from how the libraries will be named inside the bytecode to their addresses.
927933
map<string, h160> librariesReplacements;
@@ -984,7 +990,7 @@ void CommandLineInterface::link()
984990

985991
void CommandLineInterface::writeLinkedFiles()
986992
{
987-
solAssert(m_options.input.mode == InputMode::Linker, "");
993+
solAssert(m_options.input.mode == InputMode::Linker);
988994

989995
for (auto const& src: m_fileReader.sourceUnits())
990996
if (src.first == g_stdinFileName)
@@ -1018,14 +1024,14 @@ string CommandLineInterface::objectWithLinkRefsHex(evmasm::LinkerObject const& _
10181024

10191025
void CommandLineInterface::assemble(yul::YulStack::Language _language, yul::YulStack::Machine _targetMachine)
10201026
{
1021-
solAssert(m_options.input.mode == InputMode::Assembler, "");
1027+
solAssert(m_options.input.mode == InputMode::Assembler);
10221028

10231029
bool successful = true;
10241030
map<string, yul::YulStack> yulStacks;
10251031
for (auto const& src: m_fileReader.sourceUnits())
10261032
{
10271033
// --no-optimize-yul option is not accepted in assembly mode.
1028-
solAssert(!m_options.optimizer.noOptimizeYul, "");
1034+
solAssert(!m_options.optimizer.noOptimizeYul);
10291035

10301036
auto& stack = yulStacks[src.first] = yul::YulStack(
10311037
m_options.output.evmVersion,
@@ -1122,7 +1128,7 @@ void CommandLineInterface::assemble(yul::YulStack::Language _language, yul::YulS
11221128

11231129
void CommandLineInterface::outputCompilationResults()
11241130
{
1125-
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
1131+
solAssert(ValidInputModes.count(m_options.input.mode) == 1);
11261132

11271133
handleCombinedJSON();
11281134

0 commit comments

Comments
 (0)