diff options
author | Mehdi Amini <joker.eph@gmail.com> | 2021-07-16 07:34:41 +0000 |
---|---|---|
committer | Mehdi Amini <joker.eph@gmail.com> | 2021-07-16 07:35:13 +0000 |
commit | 8d051d854619956de633047409149cdab1e3319a (patch) | |
tree | e51ddd9e2fdb3237526b74ab09484bec6b4ca44a /llvm/lib/Support/CommandLine.cpp | |
parent | 3d3dc9523f9b2ec9d83d37bd8a757726f37b6ee5 (diff) | |
download | llvm-8d051d854619956de633047409149cdab1e3319a.zip llvm-8d051d854619956de633047409149cdab1e3319a.tar.gz llvm-8d051d854619956de633047409149cdab1e3319a.tar.bz2 |
Revert "Use ManagedStatic and lazy initialization of cl::opt in libSupport to make it free of global initializer"
This reverts commit af9321739b20becf170e6bb5060b8d780e1dc8dd.
Still some specific config broken in some way that requires more
investigation.
Diffstat (limited to 'llvm/lib/Support/CommandLine.cpp')
-rw-r--r-- | llvm/lib/Support/CommandLine.cpp | 337 |
1 files changed, 143 insertions, 194 deletions
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp index 4ae3ad4..8cf7d5b 100644 --- a/llvm/lib/Support/CommandLine.cpp +++ b/llvm/lib/Support/CommandLine.cpp @@ -16,9 +16,6 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/CommandLine.h" - -#include "DebugOptions.h" - #include "llvm-c/Support.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/Optional.h" @@ -465,7 +462,7 @@ void Option::addCategory(OptionCategory &C) { // Maintain backward compatibility by replacing the default GeneralCategory // if it's still set. Otherwise, just add the new one. The GeneralCategory // must be explicitly added if you want multiple categories that include it. - if (&C != &getGeneralCategory() && Categories[0] == &getGeneralCategory()) + if (&C != &GeneralCategory && Categories[0] == &GeneralCategory) Categories[0] = &C; else if (!is_contained(Categories, &C)) Categories.push_back(&C); @@ -478,6 +475,9 @@ void Option::reset() { removeArgument(); } +// Initialise the general option category. +OptionCategory llvm::cl::GeneralCategory("General options"); + void OptionCategory::registerCategory() { GlobalParser->registerCategory(this); } @@ -1293,12 +1293,10 @@ bool cl::readConfigFile(StringRef CfgFile, StringSaver &Saver, /*MarkEOLs=*/false, /*RelativeNames=*/true); } -static void initCommonOptions(); bool cl::ParseCommandLineOptions(int argc, const char *const *argv, StringRef Overview, raw_ostream *Errs, const char *EnvVar, bool LongOptionsUseDoubleDash) { - initCommonOptions(); SmallVector<const char *, 20> NewArgv; BumpPtrAllocator A; StringSaver Saver(A); @@ -1939,9 +1937,7 @@ unsigned generic_parser_base::findOption(StringRef Name) { static StringRef EqValue = "=<value>"; static StringRef EmptyOption = "<empty>"; static StringRef OptionPrefix = " ="; -static size_t getOptionPrefixesSize() { - return OptionPrefix.size() + ArgHelpPrefix.size(); -} +static size_t OptionPrefixesSize = OptionPrefix.size() + ArgHelpPrefix.size(); static bool shouldPrintOption(StringRef Name, StringRef Description, const Option &O) { @@ -1959,7 +1955,7 @@ size_t generic_parser_base::getOptionWidth(const Option &O) const { if (!shouldPrintOption(Name, getDescription(i), O)) continue; size_t NameSize = Name.empty() ? EmptyOption.size() : Name.size(); - Size = std::max(Size, NameSize + getOptionPrefixesSize()); + Size = std::max(Size, NameSize + OptionPrefixesSize); } return Size; } else { @@ -1998,7 +1994,7 @@ void generic_parser_base::printOptionInfo(const Option &O, StringRef Description = getDescription(i); if (!shouldPrintOption(OptionName, Description, O)) continue; - size_t FirstLineIndent = OptionName.size() + getOptionPrefixesSize(); + size_t FirstLineIndent = OptionName.size() + OptionPrefixesSize; outs() << OptionPrefix << OptionName; if (OptionName.empty()) { outs() << EmptyOption; @@ -2378,6 +2374,105 @@ public: } // End anonymous namespace +// Declare the four HelpPrinter instances that are used to print out help, or +// help-hidden as an uncategorized list or in categories. +static HelpPrinter UncategorizedNormalPrinter(false); +static HelpPrinter UncategorizedHiddenPrinter(true); +static CategorizedHelpPrinter CategorizedNormalPrinter(false); +static CategorizedHelpPrinter CategorizedHiddenPrinter(true); + +// Declare HelpPrinter wrappers that will decide whether or not to invoke +// a categorizing help printer +static HelpPrinterWrapper WrappedNormalPrinter(UncategorizedNormalPrinter, + CategorizedNormalPrinter); +static HelpPrinterWrapper WrappedHiddenPrinter(UncategorizedHiddenPrinter, + CategorizedHiddenPrinter); + +// Define a category for generic options that all tools should have. +static cl::OptionCategory GenericCategory("Generic Options"); + +// Define uncategorized help printers. +// --help-list is hidden by default because if Option categories are being used +// then --help behaves the same as --help-list. +static cl::opt<HelpPrinter, true, parser<bool>> HLOp( + "help-list", + cl::desc("Display list of available options (--help-list-hidden for more)"), + cl::location(UncategorizedNormalPrinter), cl::Hidden, cl::ValueDisallowed, + cl::cat(GenericCategory), cl::sub(*AllSubCommands)); + +static cl::opt<HelpPrinter, true, parser<bool>> + HLHOp("help-list-hidden", cl::desc("Display list of all available options"), + cl::location(UncategorizedHiddenPrinter), cl::Hidden, + cl::ValueDisallowed, cl::cat(GenericCategory), + cl::sub(*AllSubCommands)); + +// Define uncategorized/categorized help printers. These printers change their +// behaviour at runtime depending on whether one or more Option categories have +// been declared. +static cl::opt<HelpPrinterWrapper, true, parser<bool>> + HOp("help", cl::desc("Display available options (--help-hidden for more)"), + cl::location(WrappedNormalPrinter), cl::ValueDisallowed, + cl::cat(GenericCategory), cl::sub(*AllSubCommands)); + +static cl::alias HOpA("h", cl::desc("Alias for --help"), cl::aliasopt(HOp), + cl::DefaultOption); + +static cl::opt<HelpPrinterWrapper, true, parser<bool>> + HHOp("help-hidden", cl::desc("Display all available options"), + cl::location(WrappedHiddenPrinter), cl::Hidden, cl::ValueDisallowed, + cl::cat(GenericCategory), cl::sub(*AllSubCommands)); + +static cl::opt<bool> PrintOptions( + "print-options", + cl::desc("Print non-default options after command line parsing"), + cl::Hidden, cl::init(false), cl::cat(GenericCategory), + cl::sub(*AllSubCommands)); + +static cl::opt<bool> PrintAllOptions( + "print-all-options", + cl::desc("Print all option values after command line parsing"), cl::Hidden, + cl::init(false), cl::cat(GenericCategory), cl::sub(*AllSubCommands)); + +void HelpPrinterWrapper::operator=(bool Value) { + if (!Value) + return; + + // Decide which printer to invoke. If more than one option category is + // registered then it is useful to show the categorized help instead of + // uncategorized help. + if (GlobalParser->RegisteredOptionCategories.size() > 1) { + // unhide --help-list option so user can have uncategorized output if they + // want it. + HLOp.setHiddenFlag(NotHidden); + + CategorizedPrinter = true; // Invoke categorized printer + } else + UncategorizedPrinter = true; // Invoke uncategorized printer +} + +// Print the value of each option. +void cl::PrintOptionValues() { GlobalParser->printOptionValues(); } + +void CommandLineParser::printOptionValues() { + if (!PrintOptions && !PrintAllOptions) + return; + + SmallVector<std::pair<const char *, Option *>, 128> Opts; + sortOpts(ActiveSubCommand->OptionsMap, Opts, /*ShowHidden*/ true); + + // Compute the maximum argument length... + size_t MaxArgLen = 0; + for (size_t i = 0, e = Opts.size(); i != e; ++i) + MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth()); + + for (size_t i = 0, e = Opts.size(); i != e; ++i) + Opts[i].second->printOptionValue(MaxArgLen, PrintAllOptions); +} + +static VersionPrinterTy OverrideVersionPrinter = nullptr; + +static std::vector<VersionPrinterTy> *ExtraVersionPrinters = nullptr; + #if defined(__GNUC__) // GCC and GCC-compatible compilers define __OPTIMIZE__ when optimizations are // enabled. @@ -2433,203 +2528,59 @@ public: #endif OS << '\n'; } - void operator=(bool OptionWasSpecified); -}; - -struct CommandLineCommonOptions { - // Declare the four HelpPrinter instances that are used to print out help, or - // help-hidden as an uncategorized list or in categories. - HelpPrinter UncategorizedNormalPrinter{false}; - HelpPrinter UncategorizedHiddenPrinter{true}; - CategorizedHelpPrinter CategorizedNormalPrinter{false}; - CategorizedHelpPrinter CategorizedHiddenPrinter{true}; - // Declare HelpPrinter wrappers that will decide whether or not to invoke - // a categorizing help printer - HelpPrinterWrapper WrappedNormalPrinter{UncategorizedNormalPrinter, - CategorizedNormalPrinter}; - HelpPrinterWrapper WrappedHiddenPrinter{UncategorizedHiddenPrinter, - CategorizedHiddenPrinter}; - // Define a category for generic options that all tools should have. - cl::OptionCategory GenericCategory{"Generic Options"}; - - // Define uncategorized help printers. - // --help-list is hidden by default because if Option categories are being - // used then --help behaves the same as --help-list. - cl::opt<HelpPrinter, true, parser<bool>> HLOp{ - "help-list", - cl::desc( - "Display list of available options (--help-list-hidden for more)"), - cl::location(UncategorizedNormalPrinter), - cl::Hidden, - cl::ValueDisallowed, - cl::cat(GenericCategory), - cl::sub(*AllSubCommands)}; - - cl::opt<HelpPrinter, true, parser<bool>> HLHOp{ - "help-list-hidden", - cl::desc("Display list of all available options"), - cl::location(UncategorizedHiddenPrinter), - cl::Hidden, - cl::ValueDisallowed, - cl::cat(GenericCategory), - cl::sub(*AllSubCommands)}; - - // Define uncategorized/categorized help printers. These printers change their - // behaviour at runtime depending on whether one or more Option categories - // have been declared. - cl::opt<HelpPrinterWrapper, true, parser<bool>> HOp{ - "help", - cl::desc("Display available options (--help-hidden for more)"), - cl::location(WrappedNormalPrinter), - cl::ValueDisallowed, - cl::cat(GenericCategory), - cl::sub(*AllSubCommands)}; - - cl::alias HOpA{"h", cl::desc("Alias for --help"), cl::aliasopt(HOp), - cl::DefaultOption}; - - cl::opt<HelpPrinterWrapper, true, parser<bool>> HHOp{ - "help-hidden", - cl::desc("Display all available options"), - cl::location(WrappedHiddenPrinter), - cl::Hidden, - cl::ValueDisallowed, - cl::cat(GenericCategory), - cl::sub(*AllSubCommands)}; - - cl::opt<bool> PrintOptions{ - "print-options", - cl::desc("Print non-default options after command line parsing"), - cl::Hidden, - cl::init(false), - cl::cat(GenericCategory), - cl::sub(*AllSubCommands)}; - - cl::opt<bool> PrintAllOptions{ - "print-all-options", - cl::desc("Print all option values after command line parsing"), - cl::Hidden, - cl::init(false), - cl::cat(GenericCategory), - cl::sub(*AllSubCommands)}; - - VersionPrinterTy OverrideVersionPrinter = nullptr; - - std::vector<VersionPrinterTy> ExtraVersionPrinters; - - // Define the --version option that prints out the LLVM version for the tool - VersionPrinter VersionPrinterInstance; - - cl::opt<VersionPrinter, true, parser<bool>> VersOp{ - "version", cl::desc("Display the version of this program"), - cl::location(VersionPrinterInstance), cl::ValueDisallowed, - cl::cat(GenericCategory)}; -}; -} // End anonymous namespace - -// Lazy-initialized global instance of options controlling the command-line -// parser and general handling. -static ManagedStatic<CommandLineCommonOptions> CommonOptions; - -static void initCommonOptions() { - *CommonOptions; - initDebugCounterOptions(); - initGraphWriterOptions(); - initSignalsOptions(); - initStatisticOptions(); - initTimerOptions(); - initTypeSizeOptions(); - initWithColorOptions(); - initDebugOptions(); - initRandomSeedOptions(); -} - -OptionCategory &cl::getGeneralCategory() { - // Initialise the general option category. - static OptionCategory GeneralCategory{"General options"}; - return GeneralCategory; -} + void operator=(bool OptionWasSpecified) { + if (!OptionWasSpecified) + return; -void VersionPrinter::operator=(bool OptionWasSpecified) { - if (!OptionWasSpecified) - return; + if (OverrideVersionPrinter != nullptr) { + OverrideVersionPrinter(outs()); + exit(0); + } + print(); + + // Iterate over any registered extra printers and call them to add further + // information. + if (ExtraVersionPrinters != nullptr) { + outs() << '\n'; + for (const auto &I : *ExtraVersionPrinters) + I(outs()); + } - if (CommonOptions->OverrideVersionPrinter != nullptr) { - CommonOptions->OverrideVersionPrinter(outs()); exit(0); } - print(); - - // Iterate over any registered extra printers and call them to add further - // information. - if (!CommonOptions->ExtraVersionPrinters.empty()) { - outs() << '\n'; - for (const auto &I : CommonOptions->ExtraVersionPrinters) - I(outs()); - } - - exit(0); -} - -void HelpPrinterWrapper::operator=(bool Value) { - if (!Value) - return; - - // Decide which printer to invoke. If more than one option category is - // registered then it is useful to show the categorized help instead of - // uncategorized help. - if (GlobalParser->RegisteredOptionCategories.size() > 1) { - // unhide --help-list option so user can have uncategorized output if they - // want it. - CommonOptions->HLOp.setHiddenFlag(NotHidden); - - CategorizedPrinter = true; // Invoke categorized printer - } else - UncategorizedPrinter = true; // Invoke uncategorized printer -} - -// Print the value of each option. -void cl::PrintOptionValues() { GlobalParser->printOptionValues(); } - -void CommandLineParser::printOptionValues() { - if (!CommonOptions->PrintOptions && !CommonOptions->PrintAllOptions) - return; - - SmallVector<std::pair<const char *, Option *>, 128> Opts; - sortOpts(ActiveSubCommand->OptionsMap, Opts, /*ShowHidden*/ true); +}; +} // End anonymous namespace - // Compute the maximum argument length... - size_t MaxArgLen = 0; - for (size_t i = 0, e = Opts.size(); i != e; ++i) - MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth()); +// Define the --version option that prints out the LLVM version for the tool +static VersionPrinter VersionPrinterInstance; - for (size_t i = 0, e = Opts.size(); i != e; ++i) - Opts[i].second->printOptionValue(MaxArgLen, CommonOptions->PrintAllOptions); -} +static cl::opt<VersionPrinter, true, parser<bool>> + VersOp("version", cl::desc("Display the version of this program"), + cl::location(VersionPrinterInstance), cl::ValueDisallowed, + cl::cat(GenericCategory)); // Utility function for printing the help message. void cl::PrintHelpMessage(bool Hidden, bool Categorized) { if (!Hidden && !Categorized) - CommonOptions->UncategorizedNormalPrinter.printHelp(); + UncategorizedNormalPrinter.printHelp(); else if (!Hidden && Categorized) - CommonOptions->CategorizedNormalPrinter.printHelp(); + CategorizedNormalPrinter.printHelp(); else if (Hidden && !Categorized) - CommonOptions->UncategorizedHiddenPrinter.printHelp(); + UncategorizedHiddenPrinter.printHelp(); else - CommonOptions->CategorizedHiddenPrinter.printHelp(); + CategorizedHiddenPrinter.printHelp(); } /// Utility function for printing version number. -void cl::PrintVersionMessage() { - CommonOptions->VersionPrinterInstance.print(); -} +void cl::PrintVersionMessage() { VersionPrinterInstance.print(); } -void cl::SetVersionPrinter(VersionPrinterTy func) { - CommonOptions->OverrideVersionPrinter = func; -} +void cl::SetVersionPrinter(VersionPrinterTy func) { OverrideVersionPrinter = func; } void cl::AddExtraVersionPrinter(VersionPrinterTy func) { - CommonOptions->ExtraVersionPrinters.push_back(func); + if (!ExtraVersionPrinters) + ExtraVersionPrinters = new std::vector<VersionPrinterTy>; + + ExtraVersionPrinters->push_back(func); } StringMap<Option *> &cl::getRegisteredOptions(SubCommand &Sub) { @@ -2645,10 +2596,10 @@ cl::getRegisteredSubcommands() { } void cl::HideUnrelatedOptions(cl::OptionCategory &Category, SubCommand &Sub) { - initCommonOptions(); for (auto &I : Sub.OptionsMap) { for (auto &Cat : I.second->Categories) { - if (Cat != &Category && Cat != &CommonOptions->GenericCategory) + if (Cat != &Category && + Cat != &GenericCategory) I.second->setHiddenFlag(cl::ReallyHidden); } } @@ -2656,11 +2607,9 @@ void cl::HideUnrelatedOptions(cl::OptionCategory &Category, SubCommand &Sub) { void cl::HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *> Categories, SubCommand &Sub) { - initCommonOptions(); for (auto &I : Sub.OptionsMap) { for (auto &Cat : I.second->Categories) { - if (!is_contained(Categories, Cat) && - Cat != &CommonOptions->GenericCategory) + if (!is_contained(Categories, Cat) && Cat != &GenericCategory) I.second->setHiddenFlag(cl::ReallyHidden); } } |