diff --git a/llvm/include/llvm/Support/CommandLine.h b/llvm/include/llvm/Support/CommandLine.h --- a/llvm/include/llvm/Support/CommandLine.h +++ b/llvm/include/llvm/Support/CommandLine.h @@ -285,7 +285,9 @@ StringRef ValueStr; // String describing what the value of this option is SmallVector Categories; // The Categories this option belongs to - SmallPtrSet Subs; // The subcommands this option belongs to. + + // Return the set of SubCommands that this Option was added to. + SmallPtrSet getSubCommands() const; inline enum NumOccurrencesFlag getNumOccurrencesFlag() const { return (enum NumOccurrencesFlag)Occurrences; @@ -304,6 +306,7 @@ } inline unsigned getMiscFlags() const { return Misc; } + inline bool isFullyInitialized() const { return FullyInitialized; } inline unsigned getPosition() const { return Position; } inline unsigned getNumAdditionalVals() const { return AdditionalVals; } @@ -317,12 +320,6 @@ return getNumOccurrencesFlag() == cl::ConsumeAfter; } - bool isInAllSubCommands() const { - return any_of(Subs, [](const SubCommand *SC) { - return SC == &*AllSubCommands; - }); - } - //-------------------------------------------------------------------------=== // Accessor functions set by OptionModifiers // @@ -336,7 +333,6 @@ void setMiscFlag(enum MiscFlags M) { Misc |= M; } void setPosition(unsigned pos) { Position = pos; } void addCategory(OptionCategory &C); - void addSubCommand(SubCommand &S) { Subs.insert(&S); } protected: explicit Option(enum NumOccurrencesFlag OccurrencesFlag, @@ -354,7 +350,14 @@ // addArgument - Register this argument with the commandline system. // - void addArgument(); + void addArgument(SubCommand &SC); + + // addArgument - Only called in done() method to add default + // TopLevelSubCommand. + void addArgument() { + if (!isFullyInitialized()) + addArgument(*TopLevelSubCommand); + } /// Unregisters this option from the CommandLine system. /// @@ -465,7 +468,7 @@ sub(SubCommand &S) : Sub(S) {} - template void apply(Opt &O) const { O.addSubCommand(Sub); } + template void apply(Opt &O) const { O.addArgument(Sub); } }; //===----------------------------------------------------------------------===// @@ -1772,11 +1775,9 @@ error("cl::alias must have argument name specified!"); if (!AliasFor) error("cl::alias must have an cl::aliasopt(option) specified!"); - if (!Subs.empty()) - error("cl::alias must not have cl::sub(), aliased option's cl::sub() will be used!"); - Subs = AliasFor->Subs; Categories = AliasFor->Categories; - addArgument(); + for(SubCommand *SC: AliasFor->getSubCommands()) + addArgument(*SC); } public: diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp --- a/llvm/lib/Support/CommandLine.cpp +++ b/llvm/lib/Support/CommandLine.cpp @@ -142,7 +142,7 @@ // This collects Options added with the cl::DefaultOption flag. Since they can // be overridden, they are not added to the appropriate SubCommands until // ParseCommandLineOptions actually runs. - SmallVector DefaultOptions; + SmallVector, 4> DefaultOptions; // This collects the different option categories that have been registered. SmallPtrSet RegisteredOptionCategories; @@ -182,15 +182,16 @@ } void addLiteralOption(Option &Opt, StringRef Name) { - if (Opt.Subs.empty()) - addLiteralOption(Opt, &*TopLevelSubCommand, Name); - else { - for (auto SC : Opt.Subs) - addLiteralOption(Opt, SC, Name); - } + for(SubCommand *SC: Opt.getSubCommands()) + addLiteralOption(Opt, SC, Name); } - void addOption(Option *O, SubCommand *SC) { + void addOption(Option *O, SubCommand *SC, bool ProcessDefaultOptions = false) { + if (!ProcessDefaultOptions && O->isDefaultOption()) { + DefaultOptions.push_back(std::make_pair(O, SC)); + return; + } + bool HadErrors = false; if (O->hasArgStr()) { // If it's a DefaultOption, check to make sure it isn't already there. @@ -232,22 +233,14 @@ for (const auto &Sub : RegisteredSubCommands) { if (SC == Sub) continue; - addOption(O, Sub); + addOption(O, Sub, ProcessDefaultOptions); } } } - void addOption(Option *O, bool ProcessDefaultOption = false) { - if (!ProcessDefaultOption && O->isDefaultOption()) { - DefaultOptions.push_back(O); - return; - } - - if (O->Subs.empty()) { - addOption(O, &*TopLevelSubCommand); - } else { - for (auto SC : O->Subs) - addOption(O, SC); + void addDefaultOptions() { + for (std::pair