From b708f244c78580fbae95029c90e88b82f065e98a Mon Sep 17 00:00:00 2001 From: Mehdi Amini Date: Fri, 16 Jul 2021 07:34:41 +0000 Subject: [PATCH] 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. --- docs/CommandLine.rst | 2 +- include/llvm/Support/ARMAttributeParser.h | 4 +- include/llvm/Support/ARMBuildAttributes.h | 2 +- include/llvm/Support/CommandLine.h | 4 +- include/llvm/Support/RISCVAttributeParser.h | 4 +- include/llvm/Support/RISCVAttributes.h | 2 +- include/llvm/Support/ScopedPrinter.h | 5 +- include/llvm/Support/WithColor.h | 2 +- lib/Support/ARMBuildAttrs.cpp | 7 +- lib/Support/CommandLine.cpp | 335 ++++++++---------- lib/Support/Debug.cpp | 73 ++-- lib/Support/DebugCounter.cpp | 31 +- lib/Support/DebugOptions.h | 29 -- lib/Support/ELFAttributeParser.cpp | 2 +- lib/Support/GraphWriter.cpp | 20 +- lib/Support/RISCVAttributes.cpp | 9 +- lib/Support/RandomNumberGenerator.cpp | 24 +- lib/Support/Signals.cpp | 39 +- lib/Support/Statistic.cpp | 29 +- lib/Support/TimeProfiler.cpp | 18 +- lib/Support/Timer.cpp | 53 +-- lib/Support/TypeSize.cpp | 32 +- lib/Support/Windows/Signals.inc | 2 +- lib/Support/WithColor.cpp | 30 +- lib/Target/ARM/AsmParser/ARMAsmParser.cpp | 4 +- .../ARM/MCTargetDesc/ARMELFStreamer.cpp | 8 +- lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp | 2 +- tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp | 2 +- tools/llvm-dwarfdump/llvm-dwarfdump.cpp | 3 +- .../llvm-libtool-darwin.cpp | 2 +- unittests/Support/ARMAttributeParser.cpp | 2 +- unittests/Support/CommandLineTest.cpp | 20 +- .../Support/RISCVAttributeParserTest.cpp | 2 +- 33 files changed, 283 insertions(+), 520 deletions(-) delete mode 100644 lib/Support/DebugOptions.h diff --git a/docs/CommandLine.rst b/docs/CommandLine.rst index fef7e39ad86..e549d49bd90 100644 --- a/docs/CommandLine.rst +++ b/docs/CommandLine.rst @@ -661,7 +661,7 @@ declared, the command line option ``-help-list`` becomes visible which will print the command line options as uncategorized list. Note that Options that are not explicitly categorized will be placed in the -``cl::getGeneralCategory()`` category. +``cl::GeneralCategory`` category. .. _Reference Guide: diff --git a/include/llvm/Support/ARMAttributeParser.h b/include/llvm/Support/ARMAttributeParser.h index 5d12b7e08d5..bf85ea14cfe 100644 --- a/include/llvm/Support/ARMAttributeParser.h +++ b/include/llvm/Support/ARMAttributeParser.h @@ -71,9 +71,9 @@ class ARMAttributeParser : public ELFAttributeParser { public: ARMAttributeParser(ScopedPrinter *sw) - : ELFAttributeParser(sw, ARMBuildAttrs::getARMAttributeTags(), "aeabi") {} + : ELFAttributeParser(sw, ARMBuildAttrs::ARMAttributeTags, "aeabi") {} ARMAttributeParser() - : ELFAttributeParser(ARMBuildAttrs::getARMAttributeTags(), "aeabi") {} + : ELFAttributeParser(ARMBuildAttrs::ARMAttributeTags, "aeabi") {} }; } diff --git a/include/llvm/Support/ARMBuildAttributes.h b/include/llvm/Support/ARMBuildAttributes.h index 37c37522fd2..5a06fd6ca7b 100644 --- a/include/llvm/Support/ARMBuildAttributes.h +++ b/include/llvm/Support/ARMBuildAttributes.h @@ -23,7 +23,7 @@ namespace llvm { namespace ARMBuildAttrs { -const TagNameMap &getARMAttributeTags(); +extern const TagNameMap ARMAttributeTags; enum SpecialAttr { // This is for the .cpu asm attr. It translates into one or more diff --git a/include/llvm/Support/CommandLine.h b/include/llvm/Support/CommandLine.h index 14d7e21f78b..bcbc4f993d5 100644 --- a/include/llvm/Support/CommandLine.h +++ b/include/llvm/Support/CommandLine.h @@ -202,7 +202,7 @@ public: }; // The general Option Category (used as default category). -OptionCategory &getGeneralCategory(); +extern OptionCategory GeneralCategory; //===----------------------------------------------------------------------===// // SubCommand class @@ -342,7 +342,7 @@ protected: : NumOccurrences(0), Occurrences(OccurrencesFlag), Value(0), HiddenFlag(Hidden), Formatting(NormalFormatting), Misc(0), FullyInitialized(false), Position(0), AdditionalVals(0) { - Categories.push_back(&getGeneralCategory()); + Categories.push_back(&GeneralCategory); } inline void setNumAdditionalVals(unsigned n) { AdditionalVals = n; } diff --git a/include/llvm/Support/RISCVAttributeParser.h b/include/llvm/Support/RISCVAttributeParser.h index 305adffbe85..3e629419a7e 100644 --- a/include/llvm/Support/RISCVAttributeParser.h +++ b/include/llvm/Support/RISCVAttributeParser.h @@ -27,9 +27,9 @@ class RISCVAttributeParser : public ELFAttributeParser { public: RISCVAttributeParser(ScopedPrinter *sw) - : ELFAttributeParser(sw, RISCVAttrs::getRISCVAttributeTags(), "riscv") {} + : ELFAttributeParser(sw, RISCVAttrs::RISCVAttributeTags, "riscv") {} RISCVAttributeParser() - : ELFAttributeParser(RISCVAttrs::getRISCVAttributeTags(), "riscv") {} + : ELFAttributeParser(RISCVAttrs::RISCVAttributeTags, "riscv") {} }; } // namespace llvm diff --git a/include/llvm/Support/RISCVAttributes.h b/include/llvm/Support/RISCVAttributes.h index a8ce8f4d8da..caded9519b6 100644 --- a/include/llvm/Support/RISCVAttributes.h +++ b/include/llvm/Support/RISCVAttributes.h @@ -22,7 +22,7 @@ namespace llvm { namespace RISCVAttrs { -const TagNameMap &getRISCVAttributeTags(); +extern const TagNameMap RISCVAttributeTags; enum AttrType : unsigned { // Attribute types in ELF/.riscv.attributes. diff --git a/include/llvm/Support/ScopedPrinter.h b/include/llvm/Support/ScopedPrinter.h index 0dfe1245f7d..fe800e32404 100644 --- a/include/llvm/Support/ScopedPrinter.h +++ b/include/llvm/Support/ScopedPrinter.h @@ -32,9 +32,8 @@ template struct EnumEntry { // "Advanced Micro Devices X86-64" on GNU style StringRef AltName; T Value; - constexpr EnumEntry(StringRef N, StringRef A, T V) - : Name(N), AltName(A), Value(V) {} - constexpr EnumEntry(StringRef N, T V) : Name(N), AltName(N), Value(V) {} + EnumEntry(StringRef N, StringRef A, T V) : Name(N), AltName(A), Value(V) {} + EnumEntry(StringRef N, T V) : Name(N), AltName(N), Value(V) {} }; struct HexNumber { diff --git a/include/llvm/Support/WithColor.h b/include/llvm/Support/WithColor.h index e772ea667f4..1908c6eb8ea 100644 --- a/include/llvm/Support/WithColor.h +++ b/include/llvm/Support/WithColor.h @@ -20,7 +20,7 @@ namespace cl { class OptionCategory; } -extern cl::OptionCategory &getColorCategory(); +extern cl::OptionCategory ColorCategory; // Symbolic names for various syntax elements. enum class HighlightColor { diff --git a/lib/Support/ARMBuildAttrs.cpp b/lib/Support/ARMBuildAttrs.cpp index f20521f2a2d..5aaf0a4e7c6 100644 --- a/lib/Support/ARMBuildAttrs.cpp +++ b/lib/Support/ARMBuildAttrs.cpp @@ -63,7 +63,6 @@ static const TagNameItem tagData[] = { {ARMBuildAttrs::ABI_align_preserved, "Tag_ABI_align8_preserved"}, }; -constexpr TagNameMap ARMAttributeTags{tagData}; -const TagNameMap &llvm::ARMBuildAttrs::getARMAttributeTags() { - return ARMAttributeTags; -} +const TagNameMap llvm::ARMBuildAttrs::ARMAttributeTags(tagData, + sizeof(tagData) / + sizeof(TagNameItem)); diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp index 4ae3ad4c245..8cf7d5b1e57 100644 --- a/lib/Support/CommandLine.cpp +++ b/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 NewArgv; BumpPtrAllocator A; StringSaver Saver(A); @@ -1939,9 +1937,7 @@ unsigned generic_parser_base::findOption(StringRef Name) { static StringRef EqValue = "="; static StringRef EmptyOption = ""; 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> 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> + 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> + 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> + 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 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 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, 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 *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); -}; + void operator=(bool OptionWasSpecified) { + if (!OptionWasSpecified) + return; -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"}; + if (OverrideVersionPrinter != nullptr) { + OverrideVersionPrinter(outs()); + exit(0); + } + print(); - // 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> 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)}; + // 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()); + } - cl::opt> 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> 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> HHOp{ - "help-hidden", - cl::desc("Display all available options"), - cl::location(WrappedHiddenPrinter), - cl::Hidden, - cl::ValueDisallowed, - cl::cat(GenericCategory), - cl::sub(*AllSubCommands)}; - - cl::opt 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 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 ExtraVersionPrinters; - - // Define the --version option that prints out the LLVM version for the tool - VersionPrinter VersionPrinterInstance; - - cl::opt> VersOp{ - "version", cl::desc("Display the version of this program"), - cl::location(VersionPrinterInstance), cl::ValueDisallowed, - cl::cat(GenericCategory)}; + exit(0); + } }; } // End anonymous namespace -// Lazy-initialized global instance of options controlling the command-line -// parser and general handling. -static ManagedStatic CommonOptions; +// Define the --version option that prints out the LLVM version for the tool +static VersionPrinter VersionPrinterInstance; -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 VersionPrinter::operator=(bool OptionWasSpecified) { - if (!OptionWasSpecified) - return; - - 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, 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, CommonOptions->PrintAllOptions); -} +static cl::opt> + 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; + + ExtraVersionPrinters->push_back(func); } StringMap