aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Kudrin <ikudrin@accesssoftek.com>2023-12-20 02:39:32 +0700
committerGitHub <noreply@github.com>2023-12-20 02:39:32 +0700
commit6a2a99fb4550bab4b64a8cb8d5c9d91ae4f37558 (patch)
treea2240b0b550e1ab15e3a0a591df866f49d076972
parent315a5cce89d8f15da5c47d85abbc8155b9c0f0b0 (diff)
downloadllvm-6a2a99fb4550bab4b64a8cb8d5c9d91ae4f37558.zip
llvm-6a2a99fb4550bab4b64a8cb8d5c9d91ae4f37558.tar.gz
llvm-6a2a99fb4550bab4b64a8cb8d5c9d91ae4f37558.tar.bz2
[CommandLine][NFCI] Simplify enumerating subcommands of an option (#75679)
The patch adds a helper method to iterate over all subcommands to which an option belongs. Duplicate code is removed and replaced with calls to this new method.
-rw-r--r--llvm/include/llvm/Support/CommandLine.h4
-rw-r--r--llvm/lib/Support/CommandLine.cpp79
2 files changed, 24 insertions, 59 deletions
diff --git a/llvm/include/llvm/Support/CommandLine.h b/llvm/include/llvm/Support/CommandLine.h
index 58ef176..5d733ee 100644
--- a/llvm/include/llvm/Support/CommandLine.h
+++ b/llvm/include/llvm/Support/CommandLine.h
@@ -314,10 +314,6 @@ public:
return getNumOccurrencesFlag() == cl::ConsumeAfter;
}
- bool isInAllSubCommands() const {
- return Subs.contains(&SubCommand::getAll());
- }
-
//-------------------------------------------------------------------------===
// Accessor functions set by OptionModifiers
//
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp
index 088b4e4..00179bc 100644
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/lib/Support/CommandLine.cpp
@@ -175,6 +175,24 @@ public:
StringRef Overview, raw_ostream *Errs = nullptr,
bool LongOptionsUseDoubleDash = false);
+ void forEachSubCommand(Option &Opt,
+ std::function<void(SubCommand &)> Action) {
+ if (Opt.Subs.empty()) {
+ Action(SubCommand::getTopLevel());
+ return;
+ }
+ if (Opt.Subs.size() == 1 && *Opt.Subs.begin() == &SubCommand::getAll()) {
+ for (auto *SC : RegisteredSubCommands)
+ Action(*SC);
+ return;
+ }
+ for (auto *SC : Opt.Subs) {
+ assert(SC != &SubCommand::getAll() &&
+ "SubCommand::getAll() should not be used with other subcommands");
+ Action(*SC);
+ }
+ }
+
void addLiteralOption(Option &Opt, SubCommand *SC, StringRef Name) {
if (Opt.hasArgStr())
return;
@@ -183,25 +201,11 @@ public:
<< "' registered more than once!\n";
report_fatal_error("inconsistency in registered CommandLine options");
}
-
- // If we're adding this to all sub-commands, add it to the ones that have
- // already been registered.
- if (SC == &SubCommand::getAll()) {
- for (auto *Sub : RegisteredSubCommands) {
- if (SC == Sub)
- continue;
- addLiteralOption(Opt, Sub, Name);
- }
- }
}
void addLiteralOption(Option &Opt, StringRef Name) {
- if (Opt.Subs.empty())
- addLiteralOption(Opt, &SubCommand::getTopLevel(), Name);
- else {
- for (auto *SC : Opt.Subs)
- addLiteralOption(Opt, SC, Name);
- }
+ forEachSubCommand(
+ Opt, [&](SubCommand &SC) { addLiteralOption(Opt, &SC, Name); });
}
void addOption(Option *O, SubCommand *SC) {
@@ -238,16 +242,6 @@ public:
// linked LLVM distribution.
if (HadErrors)
report_fatal_error("inconsistency in registered CommandLine options");
-
- // If we're adding this to all sub-commands, add it to the ones that have
- // already been registered.
- if (SC == &SubCommand::getAll()) {
- for (auto *Sub : RegisteredSubCommands) {
- if (SC == Sub)
- continue;
- addOption(O, Sub);
- }
- }
}
void addOption(Option *O, bool ProcessDefaultOption = false) {
@@ -255,13 +249,7 @@ public:
DefaultOptions.push_back(O);
return;
}
-
- if (O->Subs.empty()) {
- addOption(O, &SubCommand::getTopLevel());
- } else {
- for (auto *SC : O->Subs)
- addOption(O, SC);
- }
+ forEachSubCommand(*O, [&](SubCommand &SC) { addOption(O, &SC); });
}
void removeOption(Option *O, SubCommand *SC) {
@@ -298,17 +286,7 @@ public:
}
void removeOption(Option *O) {
- if (O->Subs.empty())
- removeOption(O, &SubCommand::getTopLevel());
- else {
- if (O->isInAllSubCommands()) {
- for (auto *SC : RegisteredSubCommands)
- removeOption(O, SC);
- } else {
- for (auto *SC : O->Subs)
- removeOption(O, SC);
- }
- }
+ forEachSubCommand(*O, [&](SubCommand &SC) { removeOption(O, &SC); });
}
bool hasOptions(const SubCommand &Sub) const {
@@ -344,17 +322,8 @@ public:
}
void updateArgStr(Option *O, StringRef NewName) {
- if (O->Subs.empty())
- updateArgStr(O, NewName, &SubCommand::getTopLevel());
- else {
- if (O->isInAllSubCommands()) {
- for (auto *SC : RegisteredSubCommands)
- updateArgStr(O, NewName, SC);
- } else {
- for (auto *SC : O->Subs)
- updateArgStr(O, NewName, SC);
- }
- }
+ forEachSubCommand(*O,
+ [&](SubCommand &SC) { updateArgStr(O, NewName, &SC); });
}
void printOptionValues();