Skip to content

Commit e7fc139

Browse files
committed
CommandLineParser: Validate compiler output selection
1 parent 8a76957 commit e7fc139

File tree

17 files changed

+64
-21
lines changed

17 files changed

+64
-21
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Compiler Features:
77

88

99
Bugfixes:
10+
* Commandline Interface: Report output selection options unsupported by the selected input mode instead of ignoring them.
1011

1112

1213

solc/CommandLineParser.cpp

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ static set<string> const g_metadataHashArgs
137137
g_strNone
138138
};
139139

140+
static map<InputMode, string> const g_inputModeName = {
141+
{InputMode::Compiler, "compiler"},
142+
{InputMode::CompilerWithASTImport, "compiler (AST import)"},
143+
{InputMode::Assembler, "assembler"},
144+
{InputMode::StandardJson, "standard JSON"},
145+
{InputMode::Linker, "linker"},
146+
};
147+
140148
void CommandLineParser::printVersionAndExit()
141149
{
142150
sout() <<
@@ -460,6 +468,47 @@ bool CommandLineParser::parseLibraryOption(string const& _input)
460468
return true;
461469
}
462470

471+
bool CommandLineParser::parseOutputSelection()
472+
{
473+
static auto outputSupported = [](InputMode _mode, string_view _outputName)
474+
{
475+
static set<string> const compilerModeOutputs =
476+
CompilerOutputs::componentMap() |
477+
ranges::views::keys |
478+
ranges::to<set>();
479+
480+
switch (_mode)
481+
{
482+
case InputMode::Compiler:
483+
case InputMode::CompilerWithASTImport:
484+
return contains(compilerModeOutputs, _outputName);
485+
case InputMode::Assembler:
486+
case InputMode::StandardJson:
487+
case InputMode::Linker:
488+
return false;
489+
}
490+
491+
solAssert(false, "");
492+
};
493+
494+
for (auto&& [optionName, outputComponent]: CompilerOutputs::componentMap())
495+
m_options.compiler.outputs.*outputComponent = (m_args.count(optionName) > 0);
496+
497+
vector<string> unsupportedOutputs;
498+
for (auto&& [optionName, outputComponent]: CompilerOutputs::componentMap())
499+
if (m_options.compiler.outputs.*outputComponent && !outputSupported(m_options.input.mode, optionName))
500+
unsupportedOutputs.push_back(optionName);
501+
502+
if (!unsupportedOutputs.empty())
503+
{
504+
serr() << "The following outputs are not supported in " << g_inputModeName.at(m_options.input.mode) << " mode: ";
505+
serr() << joinOptionNames(unsupportedOutputs) << ".";
506+
return false;
507+
}
508+
509+
return true;
510+
}
511+
463512
po::options_description CommandLineParser::optionsDescription()
464513
{
465514
// Declare the supported options.
@@ -930,8 +979,8 @@ bool CommandLineParser::processArgs()
930979
m_options.formatting.json.indent = m_args[g_strJsonIndent].as<uint32_t>();
931980
}
932981

933-
for (auto&& [optionName, outputComponent]: CompilerOutputs::componentMap())
934-
m_options.compiler.outputs.*outputComponent = (m_args.count(optionName) > 0);
982+
if (!parseOutputSelection())
983+
return false;
935984

936985
m_options.compiler.estimateGas = (m_args.count(g_strGas) > 0);
937986

solc/CommandLineParser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ class CommandLineParser
286286
/// @return false if there are any validation errors, true otherwise.
287287
bool parseLibraryOption(std::string const& _input);
288288

289+
bool parseOutputSelection();
290+
289291
bool checkMutuallyExclusive(std::vector<std::string> const& _optionNames);
290292
[[noreturn]] void printVersionAndExit();
291293
[[noreturn]] void printLicenseAndExit();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--link --asm --asm-json --opcodes --bin --bin-runtime --abi --ir --ir-optimized --ewasm --hashes --userdoc --devdoc --metadata --storage-layout
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The following outputs are not supported in linker mode: --abi, --asm, --asm-json, --bin, --bin-runtime, --devdoc, --ewasm, --hashes, --ir, --ir-optimized, --metadata, --opcodes, --storage-layout, --userdoc.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1

test/cmdlineTests/linker_mode_output_selection_invalid/input.bin

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--ast-compact-json --asm --asm-json --opcodes --bin --bin-runtime --abi --ir --ir-optimized --ewasm --hashes --userdoc --devdoc --metadata --storage-layout
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The following outputs are not supported in standard JSON mode: --abi, --asm, --asm-json, --ast-compact-json, --bin, --bin-runtime, --devdoc, --ewasm, --hashes, --ir, --ir-optimized, --metadata, --opcodes, --storage-layout, --userdoc.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1

0 commit comments

Comments
 (0)