aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/CommandLine.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/Support/CommandLine.cpp')
-rw-r--r--llvm/lib/Support/CommandLine.cpp337
1 files changed, 194 insertions, 143 deletions
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp
index 8cf7d5b..4ae3ad4 100644
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/lib/Support/CommandLine.cpp
@@ -16,6 +16,9 @@
//===----------------------------------------------------------------------===//
#include "llvm/Support/CommandLine.h"
+
+#include "DebugOptions.h"
+
#include "llvm-c/Support.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Optional.h"
@@ -462,7 +465,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 != &GeneralCategory && Categories[0] == &GeneralCategory)
+ if (&C != &getGeneralCategory() && Categories[0] == &getGeneralCategory())
Categories[0] = &C;
else if (!is_contained(Categories, &C))
Categories.push_back(&C);
@@ -475,9 +478,6 @@ void Option::reset() {
removeArgument();
}
-// Initialise the general option category.
-OptionCategory llvm::cl::GeneralCategory("General options");
-
void OptionCategory::registerCategory() {
GlobalParser->registerCategory(this);
}
@@ -1293,10 +1293,12 @@ 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);
@@ -1937,7 +1939,9 @@ unsigned generic_parser_base::findOption(StringRef Name) {
static StringRef EqValue = "=<value>";
static StringRef EmptyOption = "<empty>";
static StringRef OptionPrefix = " =";
-static size_t OptionPrefixesSize = OptionPrefix.size() + ArgHelpPrefix.size();
+static size_t getOptionPrefixesSize() {
+ return OptionPrefix.size() + ArgHelpPrefix.size();
+}
static bool shouldPrintOption(StringRef Name, StringRef Description,
const Option &O) {
@@ -1955,7 +1959,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 + OptionPrefixesSize);
+ Size = std::max(Size, NameSize + getOptionPrefixesSize());
}
return Size;
} else {
@@ -1994,7 +1998,7 @@ void generic_parser_base::printOptionInfo(const Option &O,
StringRef Description = getDescription(i);
if (!shouldPrintOption(OptionName, Description, O))
continue;
- size_t FirstLineIndent = OptionName.size() + OptionPrefixesSize;
+ size_t FirstLineIndent = OptionName.size() + getOptionPrefixesSize();
outs() << OptionPrefix << OptionName;
if (OptionName.empty()) {
outs() << EmptyOption;
@@ -2374,105 +2378,6 @@ 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.
@@ -2528,59 +2433,203 @@ public:
#endif
OS << '\n';
}
- void operator=(bool OptionWasSpecified) {
- if (!OptionWasSpecified)
- return;
+ void operator=(bool OptionWasSpecified);
+};
- 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());
- }
+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 VersionPrinter::operator=(bool OptionWasSpecified) {
+ if (!OptionWasSpecified)
+ return;
+
+ if (CommonOptions->OverrideVersionPrinter != nullptr) {
+ CommonOptions->OverrideVersionPrinter(outs());
exit(0);
}
-};
-} // End anonymous namespace
+ 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);
-// Define the --version option that prints out the LLVM version for the tool
-static VersionPrinter VersionPrinterInstance;
+ 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;
-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));
+ 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, CommonOptions->PrintAllOptions);
+}
// Utility function for printing the help message.
void cl::PrintHelpMessage(bool Hidden, bool Categorized) {
if (!Hidden && !Categorized)
- UncategorizedNormalPrinter.printHelp();
+ CommonOptions->UncategorizedNormalPrinter.printHelp();
else if (!Hidden && Categorized)
- CategorizedNormalPrinter.printHelp();
+ CommonOptions->CategorizedNormalPrinter.printHelp();
else if (Hidden && !Categorized)
- UncategorizedHiddenPrinter.printHelp();
+ CommonOptions->UncategorizedHiddenPrinter.printHelp();
else
- CategorizedHiddenPrinter.printHelp();
+ CommonOptions->CategorizedHiddenPrinter.printHelp();
}
/// Utility function for printing version number.
-void cl::PrintVersionMessage() { VersionPrinterInstance.print(); }
+void cl::PrintVersionMessage() {
+ CommonOptions->VersionPrinterInstance.print();
+}
-void cl::SetVersionPrinter(VersionPrinterTy func) { OverrideVersionPrinter = func; }
+void cl::SetVersionPrinter(VersionPrinterTy func) {
+ CommonOptions->OverrideVersionPrinter = func;
+}
void cl::AddExtraVersionPrinter(VersionPrinterTy func) {
- if (!ExtraVersionPrinters)
- ExtraVersionPrinters = new std::vector<VersionPrinterTy>;
-
- ExtraVersionPrinters->push_back(func);
+ CommonOptions->ExtraVersionPrinters.push_back(func);
}
StringMap<Option *> &cl::getRegisteredOptions(SubCommand &Sub) {
@@ -2596,10 +2645,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 != &GenericCategory)
+ if (Cat != &Category && Cat != &CommonOptions->GenericCategory)
I.second->setHiddenFlag(cl::ReallyHidden);
}
}
@@ -2607,9 +2656,11 @@ 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 != &GenericCategory)
+ if (!is_contained(Categories, Cat) &&
+ Cat != &CommonOptions->GenericCategory)
I.second->setHiddenFlag(cl::ReallyHidden);
}
}