Skip to content

Commit 028c26d

Browse files
committed
[solc] CommandLineParser: introduction of any_of / none_of.
1 parent 4c20e18 commit 028c26d

File tree

3 files changed

+72
-86
lines changed

3 files changed

+72
-86
lines changed

solc/CommandLineInterface.cpp

Lines changed: 29 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ using namespace solidity;
8989
using namespace solidity::util;
9090
using namespace solidity::langutil;
9191

92+
namespace
93+
{
94+
95+
std::vector<frontend::InputMode> ValidInputModes{
96+
frontend::InputMode::Compiler,
97+
frontend::InputMode::CompilerWithASTImport,
98+
frontend::InputMode::CompilerWithEvmAssemblyJsonImport
99+
};
100+
101+
} // anonymous namespace
102+
92103
namespace solidity::frontend
93104
{
94105

@@ -161,11 +172,7 @@ static bool coloredOutput(CommandLineOptions const& _options)
161172

162173
void CommandLineInterface::handleBinary(string const& _contract)
163174
{
164-
solAssert(
165-
m_options.input.mode == InputMode::Compiler ||
166-
m_options.input.mode == InputMode::CompilerWithASTImport ||
167-
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
168-
);
175+
solAssert(any_of(m_options.input.mode, ValidInputModes));
169176

170177
if (m_options.compiler.outputs.binary)
171178
{
@@ -191,11 +198,7 @@ void CommandLineInterface::handleBinary(string const& _contract)
191198

192199
void CommandLineInterface::handleOpcode(string const& _contract)
193200
{
194-
solAssert(
195-
m_options.input.mode == InputMode::Compiler ||
196-
m_options.input.mode == InputMode::CompilerWithASTImport ||
197-
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
198-
);
201+
solAssert(any_of(m_options.input.mode, ValidInputModes));
199202

200203
if (!m_options.output.dir.empty())
201204
createFile(m_compiler->filesystemFriendlyName(_contract) + ".opcode", evmasm::disassemble(m_compiler->object(_contract).bytecode));
@@ -209,11 +212,7 @@ void CommandLineInterface::handleOpcode(string const& _contract)
209212

210213
void CommandLineInterface::handleIR(string const& _contractName)
211214
{
212-
solAssert(
213-
m_options.input.mode == InputMode::Compiler ||
214-
m_options.input.mode == InputMode::CompilerWithASTImport ||
215-
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
216-
);
215+
solAssert(any_of(m_options.input.mode, ValidInputModes));
217216

218217
if (!m_options.compiler.outputs.ir)
219218
return;
@@ -229,11 +228,7 @@ void CommandLineInterface::handleIR(string const& _contractName)
229228

230229
void CommandLineInterface::handleIROptimized(string const& _contractName)
231230
{
232-
solAssert(
233-
m_options.input.mode == InputMode::Compiler ||
234-
m_options.input.mode == InputMode::CompilerWithASTImport ||
235-
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
236-
);
231+
solAssert(any_of(m_options.input.mode, ValidInputModes));
237232

238233
if (!m_options.compiler.outputs.irOptimized)
239234
return;
@@ -249,11 +244,7 @@ void CommandLineInterface::handleIROptimized(string const& _contractName)
249244

250245
void CommandLineInterface::handleEwasm(string const& _contractName)
251246
{
252-
solAssert(
253-
m_options.input.mode == InputMode::Compiler ||
254-
m_options.input.mode == InputMode::CompilerWithASTImport ||
255-
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
256-
);
247+
solAssert(any_of(m_options.input.mode, ValidInputModes));
257248

258249
if (!m_options.compiler.outputs.ewasm)
259250
return;
@@ -276,11 +267,7 @@ void CommandLineInterface::handleEwasm(string const& _contractName)
276267

277268
void CommandLineInterface::handleBytecode(string const& _contract)
278269
{
279-
solAssert(
280-
m_options.input.mode == InputMode::Compiler ||
281-
m_options.input.mode == InputMode::CompilerWithASTImport ||
282-
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
283-
);
270+
solAssert(any_of(m_options.input.mode, ValidInputModes));
284271

285272
if (m_options.compiler.outputs.opcodes)
286273
handleOpcode(_contract);
@@ -290,11 +277,7 @@ void CommandLineInterface::handleBytecode(string const& _contract)
290277

291278
void CommandLineInterface::handleSignatureHashes(string const& _contract)
292279
{
293-
solAssert(
294-
m_options.input.mode == InputMode::Compiler ||
295-
m_options.input.mode == InputMode::CompilerWithASTImport ||
296-
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
297-
);
280+
solAssert(any_of(m_options.input.mode, ValidInputModes));
298281

299282
if (!m_options.compiler.outputs.signatureHashes)
300283
return;
@@ -326,11 +309,7 @@ void CommandLineInterface::handleSignatureHashes(string const& _contract)
326309

327310
void CommandLineInterface::handleMetadata(string const& _contract)
328311
{
329-
solAssert(
330-
m_options.input.mode == InputMode::Compiler ||
331-
m_options.input.mode == InputMode::CompilerWithASTImport ||
332-
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
333-
);
312+
solAssert(any_of(m_options.input.mode, ValidInputModes));
334313

335314
if (!m_options.compiler.outputs.metadata)
336315
return;
@@ -344,11 +323,7 @@ void CommandLineInterface::handleMetadata(string const& _contract)
344323

345324
void CommandLineInterface::handleABI(string const& _contract)
346325
{
347-
solAssert(
348-
m_options.input.mode == InputMode::Compiler ||
349-
m_options.input.mode == InputMode::CompilerWithASTImport ||
350-
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
351-
);
326+
solAssert(any_of(m_options.input.mode, ValidInputModes));
352327

353328
if (!m_options.compiler.outputs.abi)
354329
return;
@@ -362,11 +337,7 @@ void CommandLineInterface::handleABI(string const& _contract)
362337

363338
void CommandLineInterface::handleStorageLayout(string const& _contract)
364339
{
365-
solAssert(
366-
m_options.input.mode == InputMode::Compiler ||
367-
m_options.input.mode == InputMode::CompilerWithASTImport ||
368-
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
369-
);
340+
solAssert(any_of(m_options.input.mode, ValidInputModes));
370341

371342
if (!m_options.compiler.outputs.storageLayout)
372343
return;
@@ -380,11 +351,7 @@ void CommandLineInterface::handleStorageLayout(string const& _contract)
380351

381352
void CommandLineInterface::handleNatspec(bool _natspecDev, string const& _contract)
382353
{
383-
solAssert(
384-
m_options.input.mode == InputMode::Compiler ||
385-
m_options.input.mode == InputMode::CompilerWithASTImport ||
386-
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
387-
);
354+
solAssert(any_of(m_options.input.mode, ValidInputModes));
388355

389356
bool enabled = false;
390357
std::string suffix;
@@ -427,11 +394,7 @@ void CommandLineInterface::handleNatspec(bool _natspecDev, string const& _contra
427394

428395
void CommandLineInterface::handleGasEstimation(string const& _contract)
429396
{
430-
solAssert(
431-
m_options.input.mode == InputMode::Compiler ||
432-
m_options.input.mode == InputMode::CompilerWithASTImport ||
433-
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
434-
);
397+
solAssert(any_of(m_options.input.mode, ValidInputModes));
435398

436399
Json::Value estimates = m_compiler->gasEstimates(_contract);
437400
sout() << "Gas estimation:" << endl;
@@ -475,11 +438,7 @@ void CommandLineInterface::readInputFiles()
475438
{
476439
solAssert(!m_standardJsonInput.has_value());
477440

478-
if (
479-
m_options.input.mode == InputMode::Help ||
480-
m_options.input.mode == InputMode::License ||
481-
m_options.input.mode == InputMode::Version
482-
)
441+
if (any_of(m_options.input.mode, {InputMode::Help, InputMode::License, InputMode::Version}))
483442
return;
484443

485444
m_fileReader.setBasePath(m_options.input.basePath);
@@ -747,11 +706,7 @@ void CommandLineInterface::printLicense()
747706

748707
void CommandLineInterface::compile()
749708
{
750-
solAssert(
751-
m_options.input.mode == InputMode::Compiler ||
752-
m_options.input.mode == InputMode::CompilerWithASTImport ||
753-
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
754-
);
709+
solAssert(any_of(m_options.input.mode, ValidInputModes));
755710

756711
m_compiler = make_unique<CompilerStack>(m_fileReader.reader());
757712

@@ -869,11 +824,7 @@ void CommandLineInterface::compile()
869824

870825
void CommandLineInterface::handleCombinedJSON()
871826
{
872-
solAssert(
873-
m_options.input.mode == InputMode::Compiler ||
874-
m_options.input.mode == InputMode::CompilerWithASTImport ||
875-
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
876-
);
827+
solAssert(any_of(m_options.input.mode, ValidInputModes));
877828

878829
if (!m_options.compiler.combinedJsonRequests.has_value())
879830
return;
@@ -965,11 +916,7 @@ void CommandLineInterface::handleCombinedJSON()
965916

966917
void CommandLineInterface::handleAst()
967918
{
968-
solAssert(
969-
m_options.input.mode == InputMode::Compiler ||
970-
m_options.input.mode == InputMode::CompilerWithASTImport ||
971-
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
972-
);
919+
solAssert(any_of(m_options.input.mode, ValidInputModes));
973920

974921
if (!m_options.compiler.outputs.astCompactJson)
975922
return;
@@ -1194,7 +1141,7 @@ void CommandLineInterface::assemble(yul::YulStack::Language _language, yul::YulS
11941141
serr() << "No binary representation found." << endl;
11951142
}
11961143

1197-
solAssert(_targetMachine == yul::YulStack::Machine::Ewasm || _targetMachine == yul::YulStack::Machine::EVM);
1144+
solAssert(any_of(_targetMachine, {yul::YulStack::Machine::Ewasm, yul::YulStack::Machine::EVM}));
11981145
if (
11991146
(_targetMachine == yul::YulStack::Machine::EVM && m_options.compiler.outputs.asm_) ||
12001147
(_targetMachine == yul::YulStack::Machine::Ewasm && m_options.compiler.outputs.ewasm)
@@ -1211,11 +1158,7 @@ void CommandLineInterface::assemble(yul::YulStack::Language _language, yul::YulS
12111158

12121159
void CommandLineInterface::outputCompilationResults()
12131160
{
1214-
solAssert(
1215-
m_options.input.mode == InputMode::Compiler ||
1216-
m_options.input.mode == InputMode::CompilerWithASTImport ||
1217-
m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport
1218-
);
1161+
solAssert(any_of(m_options.input.mode, ValidInputModes));
12191162

12201163
handleCombinedJSON();
12211164

solc/CommandLineParser.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,19 @@ class CommandLineParser
294294
boost::program_options::variables_map m_args;
295295
};
296296

297+
template<typename T>
298+
bool any_of(T& _what, std::vector<T> _elements)
299+
{
300+
for (auto const& element: _elements)
301+
if (_what == element)
302+
return true;
303+
return false;
304+
}
305+
306+
template<typename T>
307+
bool none_of(T& _what, std::vector<T> _elements)
308+
{
309+
return !any_of(_what, _elements);
310+
}
311+
297312
}

test/solc/CommandLineParser.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,34 @@ BOOST_AUTO_TEST_CASE(cli_mode_options)
224224
}
225225
}
226226

227+
BOOST_AUTO_TEST_CASE(CommandLineOptions_any_of)
228+
{
229+
std::vector<InputMode> help_license{InputMode::Help, InputMode::License};
230+
CommandLineOptions help = parseCommandLine({"solc", "--help"});
231+
CommandLineOptions license = parseCommandLine({"solc", "--license"});
232+
CommandLineOptions version = parseCommandLine({"solc", "--version"});
233+
BOOST_TEST(any_of(help.input.mode, {InputMode::Help, InputMode::License}));
234+
BOOST_TEST(any_of(license.input.mode, {InputMode::Help, InputMode::License}));
235+
BOOST_TEST(any_of(help.input.mode, help_license));
236+
BOOST_TEST(any_of(license.input.mode, help_license));
237+
BOOST_TEST(!any_of(version.input.mode, help_license));
238+
BOOST_TEST(any_of(version.input.mode, {InputMode::Version}));
239+
}
240+
241+
BOOST_AUTO_TEST_CASE(CommandLineOptions_none_of)
242+
{
243+
std::vector<InputMode> help_license{InputMode::Help, InputMode::License};
244+
CommandLineOptions help = parseCommandLine({"solc", "--help"});
245+
CommandLineOptions license = parseCommandLine({"solc", "--license"});
246+
CommandLineOptions version = parseCommandLine({"solc", "--version"});
247+
BOOST_TEST(!none_of(help.input.mode, {InputMode::Help, InputMode::License}));
248+
BOOST_TEST(!none_of(license.input.mode, {InputMode::Help, InputMode::License}));
249+
BOOST_TEST(!none_of(help.input.mode, help_license));
250+
BOOST_TEST(!none_of(license.input.mode, help_license));
251+
BOOST_TEST(none_of(version.input.mode, help_license));
252+
BOOST_TEST(!none_of(version.input.mode, {InputMode::Version}));
253+
}
254+
227255
BOOST_AUTO_TEST_CASE(via_ir_options)
228256
{
229257
BOOST_TEST(!parseCommandLine({"solc", "contract.sol"}).output.viaIR);

0 commit comments

Comments
 (0)