cl::Optional,
cl::cat(BoltOutputCategory));
-cl::opt<std::string>
-PerfData("perfdata",
- cl::desc("<data file>"),
- cl::Optional,
- cl::cat(AggregatorCategory),
- cl::sub(*cl::AllSubCommands));
+cl::opt<std::string> PerfData("perfdata", cl::desc("<data file>"), cl::Optional,
+ cl::cat(AggregatorCategory),
+ cl::sub(cl::SubCommand::getAll()));
static cl::alias
PerfDataA("p",
cl::cat(BoltCategory));
cl::opt<unsigned>
-Verbosity("v",
- cl::desc("set verbosity level for diagnostic output"),
- cl::init(0),
- cl::ZeroOrMore,
- cl::cat(BoltCategory),
- cl::sub(*cl::AllSubCommands));
+ Verbosity("v", cl::desc("set verbosity level for diagnostic output"),
+ cl::init(0), cl::ZeroOrMore, cl::cat(BoltCategory),
+ cl::sub(cl::SubCommand::getAll()));
bool processAllFunctions() {
if (opts::AggregateOnly)
static cl::opt<std::string> InputFilename(cl::Positional,
cl::desc("<executable>"),
cl::Required, cl::cat(BoltCategory),
- cl::sub(*cl::AllSubCommands));
+ cl::sub(cl::SubCommand::getAll()));
static cl::opt<std::string>
InputDataFilename("data",
static cl::opt<std::string> BuildPath("p", cl::desc("Build path"),
cl::Optional, cl::cat(Category),
- cl::sub(*cl::AllSubCommands));
+ cl::sub(cl::SubCommand::getAll()));
static cl::list<std::string> SourcePaths(
cl::Positional, cl::desc("<source0> [... <sourceN>]"), OccurrencesFlag,
- cl::cat(Category), cl::sub(*cl::AllSubCommands));
+ cl::cat(Category), cl::sub(cl::SubCommand::getAll()));
static cl::list<std::string> ArgsAfter(
"extra-arg",
cl::desc("Additional argument to append to the compiler command line"),
- cl::cat(Category), cl::sub(*cl::AllSubCommands));
+ cl::cat(Category), cl::sub(cl::SubCommand::getAll()));
static cl::list<std::string> ArgsBefore(
"extra-arg-before",
cl::desc("Additional argument to prepend to the compiler command line"),
- cl::cat(Category), cl::sub(*cl::AllSubCommands));
+ cl::cat(Category), cl::sub(cl::SubCommand::getAll()));
cl::ResetAllOptionOccurrences();
static cl::opt<bool> Verbose("v", cl::desc("Use verbose output"),
cl::cat(cl::getGeneralCategory()),
- cl::sub(*cl::AllSubCommands));
+ cl::sub(cl::SubCommand::getAll()));
static cl::opt<bool> Inplace("i", cl::desc("Inplace edit <file>s"),
cl::cat(cl::getGeneralCategory()),
- cl::sub(*cl::AllSubCommands));
+ cl::sub(cl::SubCommand::getAll()));
} // end namespace opts
}
SubCommand() = default;
+ // Get the special subcommand representing no subcommand.
+ static SubCommand &getTopLevel();
+
+ // Get the special subcommand that can be used to put an option into all
+ // subcomands.
+ static SubCommand &getAll();
+
void reset();
explicit operator bool() const;
}
bool isInAllSubCommands() const {
- return llvm::is_contained(Subs, &*AllSubCommands);
+ return llvm::is_contained(Subs, &SubCommand::getAll());
}
//-------------------------------------------------------------------------===
/// Hopefully this API can be deprecated soon. Any situation where options need
/// to be modified by tools or libraries should be handled by sane APIs rather
/// than just handing around a global list.
-StringMap<Option *> &getRegisteredOptions(SubCommand &Sub = *TopLevelSubCommand);
+StringMap<Option *> &
+getRegisteredOptions(SubCommand &Sub = SubCommand::getTopLevel());
/// Use this to get all registered SubCommands from the provided parser.
///
/// not specific to the tool. This function allows a tool to specify a single
/// option category to display in the -help output.
void HideUnrelatedOptions(cl::OptionCategory &Category,
- SubCommand &Sub = *TopLevelSubCommand);
+ SubCommand &Sub = SubCommand::getTopLevel());
/// Mark all options not part of the categories as cl::ReallyHidden.
///
/// not specific to the tool. This function allows a tool to specify a single
/// option category to display in the -help output.
void HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *> Categories,
- SubCommand &Sub = *TopLevelSubCommand);
+ SubCommand &Sub = SubCommand::getTopLevel());
/// Reset all command line options to a state that looks as if they have
/// never appeared on the command line. This is useful for being able to parse
SmallPtrSet<SubCommand *, 4> RegisteredSubCommands;
CommandLineParser() {
- registerSubCommand(&*TopLevelSubCommand);
- registerSubCommand(&*AllSubCommands);
+ registerSubCommand(&SubCommand::getTopLevel());
+ registerSubCommand(&SubCommand::getAll());
}
void ResetAllOptionOccurrences();
// 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;
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);
// 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;
}
if (O->Subs.empty()) {
- addOption(O, &*TopLevelSubCommand);
+ addOption(O, &SubCommand::getTopLevel());
} else {
for (auto *SC : O->Subs)
addOption(O, SC);
void removeOption(Option *O) {
if (O->Subs.empty())
- removeOption(O, &*TopLevelSubCommand);
+ removeOption(O, &SubCommand::getTopLevel());
else {
if (O->isInAllSubCommands()) {
for (auto *SC : RegisteredSubCommands)
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)
// 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())
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();
}
// 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);
}
// Reject all dashes.
if (Arg.empty())
return nullptr;
- assert(&Sub != &*AllSubCommands);
+ assert(&Sub != &SubCommand::getAll());
size_t EqualPos = Arg.find('=');
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;
if (StringRef(S->getName()) == StringRef(Name))
return S;
}
- return &*TopLevelSubCommand;
+ return &SubCommand::getTopLevel();
}
/// LookupNearestOption - Lookup the closest match to the option specified by
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;
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]";
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)
cl::Hidden,
cl::ValueDisallowed,
cl::cat(GenericCategory),
- cl::sub(*AllSubCommands)};
+ cl::sub(SubCommand::getAll())};
cl::opt<HelpPrinter, true, parser<bool>> HLHOp{
"help-list-hidden",
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
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};
cl::Hidden,
cl::ValueDisallowed,
cl::cat(GenericCategory),
- cl::sub(*AllSubCommands)};
+ cl::sub(SubCommand::getAll())};
cl::opt<bool> PrintOptions{
"print-options",
cl::Hidden,
cl::init(false),
cl::cat(GenericCategory),
- cl::sub(*AllSubCommands)};
+ cl::sub(SubCommand::getAll())};
cl::opt<bool> PrintAllOptions{
"print-all-options",
cl::Hidden,
cl::init(false),
cl::cat(GenericCategory),
- cl::sub(*AllSubCommands)};
+ cl::sub(SubCommand::getAll())};
VersionPrinterTy OverrideVersionPrinter = nullptr;
if (*SC) {
// If no subcommand was provided, we need to explicitly check if this is
// the top-level subcommand.
- if (SC == &*cl::TopLevelSubCommand) {
+ if (SC == &cl::SubCommand::getTopLevel()) {
cl::PrintHelpMessage(false, true);
return 0;
}
TEST(CommandLineInitTest, GetPresetOptions) {
StringMap<cl::Option *> &Map =
- cl::getRegisteredOptions(*cl::TopLevelSubCommand);
+ cl::getRegisteredOptions(cl::SubCommand::getTopLevel());
for (auto *Str :
{"help", "help-hidden", "help-list", "help-list-hidden", "version"})
static const char ValueString[] = "Integer";
StringMap<cl::Option *> &Map =
- cl::getRegisteredOptions(*cl::TopLevelSubCommand);
+ cl::getRegisteredOptions(cl::SubCommand::getTopLevel());
ASSERT_EQ(Map.count("test-option"), 1u) << "Could not find option in map.";
<< "Hid extra option that should be visable.";
StringMap<cl::Option *> &Map =
- cl::getRegisteredOptions(*cl::TopLevelSubCommand);
+ cl::getRegisteredOptions(cl::SubCommand::getTopLevel());
ASSERT_TRUE(Map.count("help") == (size_t)0 ||
cl::NotHidden == Map["help"]->getOptionHiddenFlag())
<< "Hid default option that should be visable.";
<< "Hid extra option that should be visable.";
StringMap<cl::Option *> &Map =
- cl::getRegisteredOptions(*cl::TopLevelSubCommand);
+ cl::getRegisteredOptions(cl::SubCommand::getTopLevel());
ASSERT_TRUE(Map.count("help") == (size_t)0 ||
cl::NotHidden == Map["help"]->getOptionHiddenFlag())
<< "Hid default option that should be visable.";
cl::ResetCommandLineParser();
StackSubCommand SC1("sc1", "First subcommand");
- StackOption<bool> AllOpt("everywhere", cl::sub(*cl::AllSubCommands),
+ StackOption<bool> AllOpt("everywhere", cl::sub(cl::SubCommand::getAll()),
cl::init(false));
StackSubCommand SC2("sc2", "Second subcommand");
TEST(CommandLineTest, ReparseCommandLineOptions) {
cl::ResetCommandLineParser();
- StackOption<bool> TopLevelOpt("top-level", cl::sub(*cl::TopLevelSubCommand),
- cl::init(false));
+ StackOption<bool> TopLevelOpt(
+ "top-level", cl::sub(cl::SubCommand::getTopLevel()), cl::init(false));
const char *args[] = {"prog", "-top-level"};
TEST(CommandLineTest, RemoveFromTopLevelSubCommand) {
cl::ResetCommandLineParser();
- StackOption<bool> TopLevelRemove(
- "top-level-remove", cl::sub(*cl::TopLevelSubCommand), cl::init(false));
- StackOption<bool> TopLevelKeep(
- "top-level-keep", cl::sub(*cl::TopLevelSubCommand), cl::init(false));
+ StackOption<bool> TopLevelRemove("top-level-remove",
+ cl::sub(cl::SubCommand::getTopLevel()),
+ cl::init(false));
+ StackOption<bool> TopLevelKeep("top-level-keep",
+ cl::sub(cl::SubCommand::getTopLevel()),
+ cl::init(false));
const char *args[] = {"prog", "-top-level-remove"};
StackSubCommand SC1("sc1", "First Subcommand");
StackSubCommand SC2("sc2", "Second Subcommand");
- StackOption<bool> RemoveOption("remove-option", cl::sub(*cl::AllSubCommands),
- cl::init(false));
- StackOption<bool> KeepOption("keep-option", cl::sub(*cl::AllSubCommands),
+ StackOption<bool> RemoveOption(
+ "remove-option", cl::sub(cl::SubCommand::getAll()), cl::init(false));
+ StackOption<bool> KeepOption("keep-option", cl::sub(cl::SubCommand::getAll()),
cl::init(false));
const char *args0[] = {"prog", "-remove-option"};
TEST(CommandLineTest, DefaultOptions) {
cl::ResetCommandLineParser();
- StackOption<std::string> Bar("bar", cl::sub(*cl::AllSubCommands),
+ StackOption<std::string> Bar("bar", cl::sub(cl::SubCommand::getAll()),
cl::DefaultOption);
StackOption<std::string, cl::alias> Bar_Alias(
"b", cl::desc("Alias for -bar"), cl::aliasopt(Bar), cl::DefaultOption);
- StackOption<bool> Foo("foo", cl::init(false), cl::sub(*cl::AllSubCommands),
- cl::DefaultOption);
+ StackOption<bool> Foo("foo", cl::init(false),
+ cl::sub(cl::SubCommand::getAll()), cl::DefaultOption);
StackOption<bool, cl::alias> Foo_Alias("f", cl::desc("Alias for -foo"),
cl::aliasopt(Foo), cl::DefaultOption);
cl::ResetAllOptionOccurrences();
- for (auto &OM : cl::getRegisteredOptions(*cl::TopLevelSubCommand)) {
+ for (auto &OM : cl::getRegisteredOptions(cl::SubCommand::getTopLevel())) {
cl::Option *O = OM.second;
if (O->ArgStr == "opt2") {
continue;