aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/CommandLine.cpp
diff options
context:
space:
mode:
authorNicolai Hähnle <nicolai.haehnle@amd.com>2022-06-29 12:32:45 +0200
committerNicolai Hähnle <nicolai.haehnle@amd.com>2022-08-02 23:49:16 +0200
commitf7872cdce1102a5c12633dad462a1d9763a578d3 (patch)
tree2866aa0bdcc13049c2fd7fda2bb50ae8d616f646 /llvm/lib/Support/CommandLine.cpp
parente8468ddebc6d2fff1a008777300cfd3df0f87524 (diff)
downloadllvm-f7872cdce1102a5c12633dad462a1d9763a578d3.zip
llvm-f7872cdce1102a5c12633dad462a1d9763a578d3.tar.gz
llvm-f7872cdce1102a5c12633dad462a1d9763a578d3.tar.bz2
CommandLine: add and use cl::SubCommand::get{All,TopLevel}
Prefer using these accessors to access the special sub-commands corresponding to the top-level (no subcommand) and all sub-commands. This is a preparatory step towards removing the use of ManagedStatic: with a subsequent change, these global instances will be moved to be regular function-scope statics. It is split up to give downstream projects a (albeit short) window in which they can switch to using the accessors in a forward-compatible way. Differential Revision: https://reviews.llvm.org/D129118
Diffstat (limited to 'llvm/lib/Support/CommandLine.cpp')
-rw-r--r--llvm/lib/Support/CommandLine.cpp60
1 files changed, 32 insertions, 28 deletions
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp
index 5e7d631..b86be5e 100644
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/lib/Support/CommandLine.cpp
@@ -167,8 +167,8 @@ public:
SmallPtrSet<SubCommand *, 4> RegisteredSubCommands;
CommandLineParser() {
- registerSubCommand(&*TopLevelSubCommand);
- registerSubCommand(&*AllSubCommands);
+ registerSubCommand(&SubCommand::getTopLevel());
+ registerSubCommand(&SubCommand::getAll());
}
void ResetAllOptionOccurrences();
@@ -188,7 +188,7 @@ public:
// If we're adding this to all sub-commands, add it to the ones that have
// already been registered.
- if (SC == &*AllSubCommands) {
+ if (SC == &SubCommand::getAll()) {
for (auto *Sub : RegisteredSubCommands) {
if (SC == Sub)
continue;
@@ -199,7 +199,7 @@ public:
void addLiteralOption(Option &Opt, StringRef Name) {
if (Opt.Subs.empty())
- addLiteralOption(Opt, &*TopLevelSubCommand, Name);
+ addLiteralOption(Opt, &SubCommand::getTopLevel(), Name);
else {
for (auto *SC : Opt.Subs)
addLiteralOption(Opt, SC, Name);
@@ -244,7 +244,7 @@ public:
// If we're adding this to all sub-commands, add it to the ones that have
// already been registered.
- if (SC == &*AllSubCommands) {
+ if (SC == &SubCommand::getAll()) {
for (auto *Sub : RegisteredSubCommands) {
if (SC == Sub)
continue;
@@ -260,7 +260,7 @@ public:
}
if (O->Subs.empty()) {
- addOption(O, &*TopLevelSubCommand);
+ addOption(O, &SubCommand::getTopLevel());
} else {
for (auto *SC : O->Subs)
addOption(O, SC);
@@ -302,7 +302,7 @@ public:
void removeOption(Option *O) {
if (O->Subs.empty())
- removeOption(O, &*TopLevelSubCommand);
+ removeOption(O, &SubCommand::getTopLevel());
else {
if (O->isInAllSubCommands()) {
for (auto *SC : RegisteredSubCommands)
@@ -341,7 +341,7 @@ public:
void updateArgStr(Option *O, StringRef NewName) {
if (O->Subs.empty())
- updateArgStr(O, NewName, &*TopLevelSubCommand);
+ updateArgStr(O, NewName, &SubCommand::getTopLevel());
else {
if (O->isInAllSubCommands()) {
for (auto *SC : RegisteredSubCommands)
@@ -376,8 +376,8 @@ public:
// For all options that have been registered for all subcommands, add the
// option to this subcommand now.
- if (sub != &*AllSubCommands) {
- for (auto &E : AllSubCommands->OptionsMap) {
+ if (sub != &SubCommand::getAll()) {
+ for (auto &E : SubCommand::getAll().OptionsMap) {
Option *O = E.second;
if ((O->isPositional() || O->isSink() || O->isConsumeAfter()) ||
O->hasArgStr())
@@ -409,10 +409,10 @@ public:
ResetAllOptionOccurrences();
RegisteredSubCommands.clear();
- TopLevelSubCommand->reset();
- AllSubCommands->reset();
- registerSubCommand(&*TopLevelSubCommand);
- registerSubCommand(&*AllSubCommands);
+ SubCommand::getTopLevel().reset();
+ SubCommand::getAll().reset();
+ registerSubCommand(&SubCommand::getTopLevel());
+ registerSubCommand(&SubCommand::getAll());
DefaultOptions.clear();
}
@@ -491,6 +491,10 @@ ManagedStatic<SubCommand> llvm::cl::TopLevelSubCommand;
// A special subcommand that can be used to put an option into all subcommands.
ManagedStatic<SubCommand> llvm::cl::AllSubCommands;
+SubCommand &SubCommand::getTopLevel() { return *TopLevelSubCommand; }
+
+SubCommand &SubCommand::getAll() { return *AllSubCommands; }
+
void SubCommand::registerSubCommand() {
GlobalParser->registerSubCommand(this);
}
@@ -523,7 +527,7 @@ Option *CommandLineParser::LookupOption(SubCommand &Sub, StringRef &Arg,
// Reject all dashes.
if (Arg.empty())
return nullptr;
- assert(&Sub != &*AllSubCommands);
+ assert(&Sub != &SubCommand::getAll());
size_t EqualPos = Arg.find('=');
@@ -551,9 +555,9 @@ Option *CommandLineParser::LookupOption(SubCommand &Sub, StringRef &Arg,
SubCommand *CommandLineParser::LookupSubCommand(StringRef Name) {
if (Name.empty())
- return &*TopLevelSubCommand;
+ return &SubCommand::getTopLevel();
for (auto *S : RegisteredSubCommands) {
- if (S == &*AllSubCommands)
+ if (S == &SubCommand::getAll())
continue;
if (S->getName().empty())
continue;
@@ -561,7 +565,7 @@ SubCommand *CommandLineParser::LookupSubCommand(StringRef Name) {
if (StringRef(S->getName()) == StringRef(Name))
return S;
}
- return &*TopLevelSubCommand;
+ return &SubCommand::getTopLevel();
}
/// LookupNearestOption - Lookup the closest match to the option specified by
@@ -1452,12 +1456,12 @@ bool CommandLineParser::ParseCommandLineOptions(int argc,
bool HasUnlimitedPositionals = false;
int FirstArg = 1;
- SubCommand *ChosenSubCommand = &*TopLevelSubCommand;
+ SubCommand *ChosenSubCommand = &SubCommand::getTopLevel();
if (argc >= 2 && argv[FirstArg][0] != '-') {
// If the first argument specifies a valid subcommand, start processing
// options from the second argument.
ChosenSubCommand = LookupSubCommand(StringRef(argv[FirstArg]));
- if (ChosenSubCommand != &*TopLevelSubCommand)
+ if (ChosenSubCommand != &SubCommand::getTopLevel())
FirstArg = 2;
}
GlobalParser->ActiveSubCommand = ChosenSubCommand;
@@ -2289,7 +2293,7 @@ public:
if (!GlobalParser->ProgramOverview.empty())
outs() << "OVERVIEW: " << GlobalParser->ProgramOverview << "\n";
- if (Sub == &*TopLevelSubCommand) {
+ if (Sub == &SubCommand::getTopLevel()) {
outs() << "USAGE: " << GlobalParser->ProgramName;
if (Subs.size() > 2)
outs() << " [subcommand]";
@@ -2313,7 +2317,7 @@ public:
if (ConsumeAfterOpt)
outs() << " " << ConsumeAfterOpt->HelpStr;
- if (Sub == &*TopLevelSubCommand && !Subs.empty()) {
+ if (Sub == &SubCommand::getTopLevel() && !Subs.empty()) {
// Compute the maximum subcommand length...
size_t MaxSubLen = 0;
for (size_t i = 0, e = Subs.size(); i != e; ++i)
@@ -2519,7 +2523,7 @@ struct CommandLineCommonOptions {
cl::Hidden,
cl::ValueDisallowed,
cl::cat(GenericCategory),
- cl::sub(*AllSubCommands)};
+ cl::sub(SubCommand::getAll())};
cl::opt<HelpPrinter, true, parser<bool>> HLHOp{
"help-list-hidden",
@@ -2528,7 +2532,7 @@ struct CommandLineCommonOptions {
cl::Hidden,
cl::ValueDisallowed,
cl::cat(GenericCategory),
- cl::sub(*AllSubCommands)};
+ cl::sub(SubCommand::getAll())};
// Define uncategorized/categorized help printers. These printers change their
// behaviour at runtime depending on whether one or more Option categories
@@ -2539,7 +2543,7 @@ struct CommandLineCommonOptions {
cl::location(WrappedNormalPrinter),
cl::ValueDisallowed,
cl::cat(GenericCategory),
- cl::sub(*AllSubCommands)};
+ cl::sub(SubCommand::getAll())};
cl::alias HOpA{"h", cl::desc("Alias for --help"), cl::aliasopt(HOp),
cl::DefaultOption};
@@ -2551,7 +2555,7 @@ struct CommandLineCommonOptions {
cl::Hidden,
cl::ValueDisallowed,
cl::cat(GenericCategory),
- cl::sub(*AllSubCommands)};
+ cl::sub(SubCommand::getAll())};
cl::opt<bool> PrintOptions{
"print-options",
@@ -2559,7 +2563,7 @@ struct CommandLineCommonOptions {
cl::Hidden,
cl::init(false),
cl::cat(GenericCategory),
- cl::sub(*AllSubCommands)};
+ cl::sub(SubCommand::getAll())};
cl::opt<bool> PrintAllOptions{
"print-all-options",
@@ -2567,7 +2571,7 @@ struct CommandLineCommonOptions {
cl::Hidden,
cl::init(false),
cl::cat(GenericCategory),
- cl::sub(*AllSubCommands)};
+ cl::sub(SubCommand::getAll())};
VersionPrinterTy OverrideVersionPrinter = nullptr;