Changeset View
Changeset View
Standalone View
Standalone View
llvm/lib/Support/CommandLine.cpp
Show All 31 Lines | |||||
#include "llvm/ADT/Twine.h" | #include "llvm/ADT/Twine.h" | ||||
#include "llvm/Config/config.h" | #include "llvm/Config/config.h" | ||||
#include "llvm/Support/ConvertUTF.h" | #include "llvm/Support/ConvertUTF.h" | ||||
#include "llvm/Support/Debug.h" | #include "llvm/Support/Debug.h" | ||||
#include "llvm/Support/Error.h" | #include "llvm/Support/Error.h" | ||||
#include "llvm/Support/ErrorHandling.h" | #include "llvm/Support/ErrorHandling.h" | ||||
#include "llvm/Support/FileSystem.h" | #include "llvm/Support/FileSystem.h" | ||||
#include "llvm/Support/Host.h" | #include "llvm/Support/Host.h" | ||||
#include "llvm/Support/ManagedStatic.h" | |||||
#include "llvm/Support/MemoryBuffer.h" | #include "llvm/Support/MemoryBuffer.h" | ||||
#include "llvm/Support/Path.h" | #include "llvm/Support/Path.h" | ||||
#include "llvm/Support/Process.h" | #include "llvm/Support/Process.h" | ||||
#include "llvm/Support/StringSaver.h" | #include "llvm/Support/StringSaver.h" | ||||
#include "llvm/Support/VirtualFileSystem.h" | #include "llvm/Support/VirtualFileSystem.h" | ||||
#include "llvm/Support/raw_ostream.h" | #include "llvm/Support/raw_ostream.h" | ||||
#include <cstdlib> | #include <cstdlib> | ||||
#include <string> | #include <string> | ||||
▲ Show 20 Lines • Show All 379 Lines • ▼ Show 20 Lines | if (Opt && LongOptionsUseDoubleDash && !HaveDoubleDash && !isGrouping(Opt)) | ||||
return nullptr; | return nullptr; | ||||
return Opt; | return Opt; | ||||
} | } | ||||
SubCommand *LookupSubCommand(StringRef Name); | SubCommand *LookupSubCommand(StringRef Name); | ||||
}; | }; | ||||
} // namespace | } // namespace | ||||
static ManagedStatic<CommandLineParser> GlobalParser; | static CommandLineParser &getGlobalParser() { | ||||
static CommandLineParser GlobalParser; | |||||
return GlobalParser; | |||||
} | |||||
void cl::AddLiteralOption(Option &O, StringRef Name) { | void cl::AddLiteralOption(Option &O, StringRef Name) { | ||||
GlobalParser->addLiteralOption(O, Name); | getGlobalParser().addLiteralOption(O, Name); | ||||
} | } | ||||
extrahelp::extrahelp(StringRef Help) : morehelp(Help) { | extrahelp::extrahelp(StringRef Help) : morehelp(Help) { | ||||
GlobalParser->MoreHelp.push_back(Help); | getGlobalParser().MoreHelp.push_back(Help); | ||||
} | } | ||||
void Option::addArgument() { | void Option::addArgument() { | ||||
GlobalParser->addOption(this); | getGlobalParser().addOption(this); | ||||
FullyInitialized = true; | FullyInitialized = true; | ||||
} | } | ||||
void Option::removeArgument() { GlobalParser->removeOption(this); } | void Option::removeArgument() { getGlobalParser().removeOption(this); } | ||||
void Option::setArgStr(StringRef S) { | void Option::setArgStr(StringRef S) { | ||||
if (FullyInitialized) | if (FullyInitialized) | ||||
GlobalParser->updateArgStr(this, S); | getGlobalParser().updateArgStr(this, S); | ||||
assert((S.empty() || S[0] != '-') && "Option can't start with '-"); | assert((S.empty() || S[0] != '-') && "Option can't start with '-"); | ||||
ArgStr = S; | ArgStr = S; | ||||
if (ArgStr.size() == 1) | if (ArgStr.size() == 1) | ||||
setMiscFlag(Grouping); | setMiscFlag(Grouping); | ||||
} | } | ||||
void Option::addCategory(OptionCategory &C) { | void Option::addCategory(OptionCategory &C) { | ||||
assert(!Categories.empty() && "Categories cannot be empty."); | assert(!Categories.empty() && "Categories cannot be empty."); | ||||
Show All 9 Lines | |||||
void Option::reset() { | void Option::reset() { | ||||
NumOccurrences = 0; | NumOccurrences = 0; | ||||
setDefault(); | setDefault(); | ||||
if (isDefaultOption()) | if (isDefaultOption()) | ||||
removeArgument(); | removeArgument(); | ||||
} | } | ||||
void OptionCategory::registerCategory() { | void OptionCategory::registerCategory() { | ||||
GlobalParser->registerCategory(this); | getGlobalParser().registerCategory(this); | ||||
} | } | ||||
// A special subcommand representing no subcommand. It is particularly important | SubCommand &SubCommand::getTopLevel() { | ||||
// that this ManagedStatic uses constant initailization and not dynamic | static SubCommand TopLevelSubCommand; | ||||
// initialization because it is referenced from cl::opt constructors, which run | return TopLevelSubCommand; | ||||
// dynamically in an arbitrary order. | } | ||||
LLVM_REQUIRE_CONSTANT_INITIALIZATION | |||||
ManagedStatic<SubCommand> llvm::cl::TopLevelSubCommand; | |||||
// 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; } | SubCommand &SubCommand::getAll() { | ||||
static SubCommand AllSubCommands; | |||||
return AllSubCommands; | |||||
} | |||||
void SubCommand::registerSubCommand() { | void SubCommand::registerSubCommand() { | ||||
GlobalParser->registerSubCommand(this); | getGlobalParser().registerSubCommand(this); | ||||
} | } | ||||
void SubCommand::unregisterSubCommand() { | void SubCommand::unregisterSubCommand() { | ||||
GlobalParser->unregisterSubCommand(this); | getGlobalParser().unregisterSubCommand(this); | ||||
} | } | ||||
void SubCommand::reset() { | void SubCommand::reset() { | ||||
PositionalOpts.clear(); | PositionalOpts.clear(); | ||||
SinkOpts.clear(); | SinkOpts.clear(); | ||||
OptionsMap.clear(); | OptionsMap.clear(); | ||||
ConsumeAfterOpt = nullptr; | ConsumeAfterOpt = nullptr; | ||||
} | } | ||||
SubCommand::operator bool() const { | SubCommand::operator bool() const { | ||||
return (GlobalParser->getActiveSubCommand() == this); | return (getGlobalParser().getActiveSubCommand() == this); | ||||
} | } | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
// Basic, shared command line option processing machinery. | // Basic, shared command line option processing machinery. | ||||
// | // | ||||
/// LookupOption - Lookup the option specified by the specified option on the | /// LookupOption - Lookup the option specified by the specified option on the | ||||
/// command line. If there is a value specified (after an equal sign) return | /// command line. If there is a value specified (after an equal sign) return | ||||
▲ Show 20 Lines • Show All 872 Lines • ▼ Show 20 Lines | bool cl::ParseCommandLineOptions(int argc, const char *const *argv, | ||||
} | } | ||||
// Append options from command line. | // Append options from command line. | ||||
for (int I = 1; I < argc; ++I) | for (int I = 1; I < argc; ++I) | ||||
NewArgv.push_back(argv[I]); | NewArgv.push_back(argv[I]); | ||||
int NewArgc = static_cast<int>(NewArgv.size()); | int NewArgc = static_cast<int>(NewArgv.size()); | ||||
// Parse all options. | // Parse all options. | ||||
return GlobalParser->ParseCommandLineOptions(NewArgc, &NewArgv[0], Overview, | return getGlobalParser().ParseCommandLineOptions( | ||||
Errs, LongOptionsUseDoubleDash); | NewArgc, &NewArgv[0], Overview, Errs, LongOptionsUseDoubleDash); | ||||
} | } | ||||
/// Reset all options at least once, so that we can parse different options. | /// Reset all options at least once, so that we can parse different options. | ||||
void CommandLineParser::ResetAllOptionOccurrences() { | void CommandLineParser::ResetAllOptionOccurrences() { | ||||
// Reset all option values to look like they have never been seen before. | // Reset all option values to look like they have never been seen before. | ||||
// Options might be reset twice (they can be reference in both OptionsMap | // Options might be reset twice (they can be reference in both OptionsMap | ||||
// and one of the other members), but that does not harm. | // and one of the other members), but that does not harm. | ||||
for (auto *SC : RegisteredSubCommands) { | for (auto *SC : RegisteredSubCommands) { | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | bool CommandLineParser::ParseCommandLineOptions(int argc, | ||||
SubCommand *ChosenSubCommand = &SubCommand::getTopLevel(); | SubCommand *ChosenSubCommand = &SubCommand::getTopLevel(); | ||||
if (argc >= 2 && argv[FirstArg][0] != '-') { | if (argc >= 2 && argv[FirstArg][0] != '-') { | ||||
// If the first argument specifies a valid subcommand, start processing | // If the first argument specifies a valid subcommand, start processing | ||||
// options from the second argument. | // options from the second argument. | ||||
ChosenSubCommand = LookupSubCommand(StringRef(argv[FirstArg])); | ChosenSubCommand = LookupSubCommand(StringRef(argv[FirstArg])); | ||||
if (ChosenSubCommand != &SubCommand::getTopLevel()) | if (ChosenSubCommand != &SubCommand::getTopLevel()) | ||||
FirstArg = 2; | FirstArg = 2; | ||||
} | } | ||||
GlobalParser->ActiveSubCommand = ChosenSubCommand; | getGlobalParser().ActiveSubCommand = ChosenSubCommand; | ||||
assert(ChosenSubCommand); | assert(ChosenSubCommand); | ||||
auto &ConsumeAfterOpt = ChosenSubCommand->ConsumeAfterOpt; | auto &ConsumeAfterOpt = ChosenSubCommand->ConsumeAfterOpt; | ||||
auto &PositionalOpts = ChosenSubCommand->PositionalOpts; | auto &PositionalOpts = ChosenSubCommand->PositionalOpts; | ||||
auto &SinkOpts = ChosenSubCommand->SinkOpts; | auto &SinkOpts = ChosenSubCommand->SinkOpts; | ||||
auto &OptionsMap = ChosenSubCommand->OptionsMap; | auto &OptionsMap = ChosenSubCommand->OptionsMap; | ||||
for (auto *O: DefaultOptions) { | for (auto *O: DefaultOptions) { | ||||
▲ Show 20 Lines • Show All 288 Lines • ▼ Show 20 Lines | |||||
// | // | ||||
bool Option::error(const Twine &Message, StringRef ArgName, raw_ostream &Errs) { | bool Option::error(const Twine &Message, StringRef ArgName, raw_ostream &Errs) { | ||||
if (!ArgName.data()) | if (!ArgName.data()) | ||||
ArgName = ArgStr; | ArgName = ArgStr; | ||||
if (ArgName.empty()) | if (ArgName.empty()) | ||||
Errs << HelpStr; // Be nice for positional arguments | Errs << HelpStr; // Be nice for positional arguments | ||||
else | else | ||||
Errs << GlobalParser->ProgramName << ": for the " << PrintArg(ArgName, 0); | Errs << getGlobalParser().ProgramName << ": for the " | ||||
<< PrintArg(ArgName, 0); | |||||
Errs << " option: " << Message << "\n"; | Errs << " option: " << Message << "\n"; | ||||
return true; | return true; | ||||
} | } | ||||
bool Option::addOccurrence(unsigned pos, StringRef ArgName, StringRef Value, | bool Option::addOccurrence(unsigned pos, StringRef ArgName, StringRef Value, | ||||
bool MultiArg) { | bool MultiArg) { | ||||
if (!MultiArg) | if (!MultiArg) | ||||
▲ Show 20 Lines • Show All 493 Lines • ▼ Show 20 Lines | if (!Value) | ||||
return; | return; | ||||
printHelp(); | printHelp(); | ||||
// Halt the program since help information was printed | // Halt the program since help information was printed | ||||
exit(0); | exit(0); | ||||
} | } | ||||
void printHelp() { | void printHelp() { | ||||
SubCommand *Sub = GlobalParser->getActiveSubCommand(); | SubCommand *Sub = getGlobalParser().getActiveSubCommand(); | ||||
auto &OptionsMap = Sub->OptionsMap; | auto &OptionsMap = Sub->OptionsMap; | ||||
auto &PositionalOpts = Sub->PositionalOpts; | auto &PositionalOpts = Sub->PositionalOpts; | ||||
auto &ConsumeAfterOpt = Sub->ConsumeAfterOpt; | auto &ConsumeAfterOpt = Sub->ConsumeAfterOpt; | ||||
StrOptionPairVector Opts; | StrOptionPairVector Opts; | ||||
sortOpts(OptionsMap, Opts, ShowHidden); | sortOpts(OptionsMap, Opts, ShowHidden); | ||||
StrSubCommandPairVector Subs; | StrSubCommandPairVector Subs; | ||||
sortSubCommands(GlobalParser->RegisteredSubCommands, Subs); | sortSubCommands(getGlobalParser().RegisteredSubCommands, Subs); | ||||
if (!GlobalParser->ProgramOverview.empty()) | if (!getGlobalParser().ProgramOverview.empty()) | ||||
outs() << "OVERVIEW: " << GlobalParser->ProgramOverview << "\n"; | outs() << "OVERVIEW: " << getGlobalParser().ProgramOverview << "\n"; | ||||
outs() << "USAGE: " << GlobalParser->ProgramName; | |||||
if (Sub == &SubCommand::getTopLevel()) { | if (Sub == &SubCommand::getTopLevel()) { | ||||
outs() << "USAGE: " << getGlobalParser().ProgramName; | |||||
if (Subs.size() > 2) | if (Subs.size() > 2) | ||||
outs() << " [subcommand]"; | outs() << " [subcommand]"; | ||||
outs() << " [options]"; | outs() << " [options]"; | ||||
} else { | } else { | ||||
if (!Sub->getDescription().empty()) { | if (!Sub->getDescription().empty()) { | ||||
outs() << "SUBCOMMAND '" << Sub->getName() | outs() << "SUBCOMMAND '" << Sub->getName() | ||||
<< "': " << Sub->getDescription() << "\n\n"; | << "': " << Sub->getDescription() << "\n\n"; | ||||
} | } | ||||
outs() << "USAGE: " << GlobalParser->ProgramName << " " | outs() << "USAGE: " << getGlobalParser().ProgramName << " " | ||||
<< Sub->getName() << " [options]"; | << Sub->getName() << " [options]"; | ||||
} | } | ||||
for (auto *Opt : PositionalOpts) { | for (auto *Opt : PositionalOpts) { | ||||
if (Opt->hasArgStr()) | if (Opt->hasArgStr()) | ||||
outs() << " --" << Opt->ArgStr; | outs() << " --" << Opt->ArgStr; | ||||
outs() << " " << Opt->HelpStr; | outs() << " " << Opt->HelpStr; | ||||
} | } | ||||
// Print the consume after option info if it exists... | // Print the consume after option info if it exists... | ||||
if (ConsumeAfterOpt) | if (ConsumeAfterOpt) | ||||
outs() << " " << ConsumeAfterOpt->HelpStr; | outs() << " " << ConsumeAfterOpt->HelpStr; | ||||
if (Sub == &SubCommand::getTopLevel() && !Subs.empty()) { | if (Sub == &SubCommand::getTopLevel() && !Subs.empty()) { | ||||
// Compute the maximum subcommand length... | // Compute the maximum subcommand length... | ||||
size_t MaxSubLen = 0; | size_t MaxSubLen = 0; | ||||
for (size_t i = 0, e = Subs.size(); i != e; ++i) | for (size_t i = 0, e = Subs.size(); i != e; ++i) | ||||
MaxSubLen = std::max(MaxSubLen, strlen(Subs[i].first)); | MaxSubLen = std::max(MaxSubLen, strlen(Subs[i].first)); | ||||
outs() << "\n\n"; | outs() << "\n\n"; | ||||
outs() << "SUBCOMMANDS:\n\n"; | outs() << "SUBCOMMANDS:\n\n"; | ||||
printSubCommands(Subs, MaxSubLen); | printSubCommands(Subs, MaxSubLen); | ||||
outs() << "\n"; | outs() << "\n"; | ||||
outs() << " Type \"" << GlobalParser->ProgramName | outs() << " Type \"" << getGlobalParser().ProgramName | ||||
<< " <subcommand> --help\" to get more help on a specific " | << " <subcommand> --help\" to get more help on a specific " | ||||
"subcommand"; | "subcommand"; | ||||
} | } | ||||
outs() << "\n\n"; | outs() << "\n\n"; | ||||
// Compute the maximum argument length... | // Compute the maximum argument length... | ||||
size_t MaxArgLen = 0; | size_t MaxArgLen = 0; | ||||
for (size_t i = 0, e = Opts.size(); i != e; ++i) | for (size_t i = 0, e = Opts.size(); i != e; ++i) | ||||
MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth()); | MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth()); | ||||
outs() << "OPTIONS:\n"; | outs() << "OPTIONS:\n"; | ||||
printOptions(Opts, MaxArgLen); | printOptions(Opts, MaxArgLen); | ||||
// Print any extra help the user has declared. | // Print any extra help the user has declared. | ||||
for (const auto &I : GlobalParser->MoreHelp) | for (const auto &I : getGlobalParser().MoreHelp) | ||||
outs() << I; | outs() << I; | ||||
GlobalParser->MoreHelp.clear(); | getGlobalParser().MoreHelp.clear(); | ||||
} | } | ||||
}; | }; | ||||
class CategorizedHelpPrinter : public HelpPrinter { | class CategorizedHelpPrinter : public HelpPrinter { | ||||
public: | public: | ||||
explicit CategorizedHelpPrinter(bool showHidden) : HelpPrinter(showHidden) {} | explicit CategorizedHelpPrinter(bool showHidden) : HelpPrinter(showHidden) {} | ||||
// Helper function for printOptions(). | // Helper function for printOptions(). | ||||
Show All 10 Lines | |||||
protected: | protected: | ||||
void printOptions(StrOptionPairVector &Opts, size_t MaxArgLen) override { | void printOptions(StrOptionPairVector &Opts, size_t MaxArgLen) override { | ||||
std::vector<OptionCategory *> SortedCategories; | std::vector<OptionCategory *> SortedCategories; | ||||
DenseMap<OptionCategory *, std::vector<Option *>> CategorizedOptions; | DenseMap<OptionCategory *, std::vector<Option *>> CategorizedOptions; | ||||
// Collect registered option categories into vector in preparation for | // Collect registered option categories into vector in preparation for | ||||
// sorting. | // sorting. | ||||
for (OptionCategory *Category : GlobalParser->RegisteredOptionCategories) | for (OptionCategory *Category : | ||||
getGlobalParser().RegisteredOptionCategories) | |||||
SortedCategories.push_back(Category); | SortedCategories.push_back(Category); | ||||
// Sort the different option categories alphabetically. | // Sort the different option categories alphabetically. | ||||
assert(SortedCategories.size() > 0 && "No option categories registered!"); | assert(SortedCategories.size() > 0 && "No option categories registered!"); | ||||
array_pod_sort(SortedCategories.begin(), SortedCategories.end(), | array_pod_sort(SortedCategories.begin(), SortedCategories.end(), | ||||
OptionCategoryCompare); | OptionCategoryCompare); | ||||
// Walk through pre-sorted options and assign into categories. | // Walk through pre-sorted options and assign into categories. | ||||
▲ Show 20 Lines • Show All 196 Lines • ▼ Show 20 Lines | struct CommandLineCommonOptions { | ||||
// Define the --version option that prints out the LLVM version for the tool | // Define the --version option that prints out the LLVM version for the tool | ||||
VersionPrinter VersionPrinterInstance; | VersionPrinter VersionPrinterInstance; | ||||
cl::opt<VersionPrinter, true, parser<bool>> VersOp{ | cl::opt<VersionPrinter, true, parser<bool>> VersOp{ | ||||
"version", cl::desc("Display the version of this program"), | "version", cl::desc("Display the version of this program"), | ||||
cl::location(VersionPrinterInstance), cl::ValueDisallowed, | cl::location(VersionPrinterInstance), cl::ValueDisallowed, | ||||
cl::cat(GenericCategory)}; | cl::cat(GenericCategory)}; | ||||
}; | }; | ||||
} // End anonymous namespace | |||||
// Lazy-initialized global instance of options controlling the command-line | static CommandLineCommonOptions &getCommonOptions() { | ||||
// parser and general handling. | static CommandLineCommonOptions CommonOptions; | ||||
static ManagedStatic<CommandLineCommonOptions> CommonOptions; | return CommonOptions; | ||||
} | |||||
} // End anonymous namespace | |||||
static void initCommonOptions() { | static void initCommonOptions() { | ||||
*CommonOptions; | getCommonOptions(); | ||||
initDebugCounterOptions(); | initDebugCounterOptions(); | ||||
initGraphWriterOptions(); | initGraphWriterOptions(); | ||||
initSignalsOptions(); | initSignalsOptions(); | ||||
initStatisticOptions(); | initStatisticOptions(); | ||||
initTimerOptions(); | initTimerOptions(); | ||||
initTypeSizeOptions(); | initTypeSizeOptions(); | ||||
initWithColorOptions(); | initWithColorOptions(); | ||||
initDebugOptions(); | initDebugOptions(); | ||||
initRandomSeedOptions(); | initRandomSeedOptions(); | ||||
} | } | ||||
OptionCategory &cl::getGeneralCategory() { | OptionCategory &cl::getGeneralCategory() { | ||||
// Initialise the general option category. | // Initialise the general option category. | ||||
static OptionCategory GeneralCategory{"General options"}; | static OptionCategory GeneralCategory{"General options"}; | ||||
return GeneralCategory; | return GeneralCategory; | ||||
} | } | ||||
void VersionPrinter::operator=(bool OptionWasSpecified) { | void VersionPrinter::operator=(bool OptionWasSpecified) { | ||||
if (!OptionWasSpecified) | if (!OptionWasSpecified) | ||||
return; | return; | ||||
if (CommonOptions->OverrideVersionPrinter != nullptr) { | if (getCommonOptions().OverrideVersionPrinter != nullptr) { | ||||
CommonOptions->OverrideVersionPrinter(outs()); | getCommonOptions().OverrideVersionPrinter(outs()); | ||||
exit(0); | exit(0); | ||||
} | } | ||||
print(); | print(); | ||||
// Iterate over any registered extra printers and call them to add further | // Iterate over any registered extra printers and call them to add further | ||||
// information. | // information. | ||||
if (!CommonOptions->ExtraVersionPrinters.empty()) { | if (!getCommonOptions().ExtraVersionPrinters.empty()) { | ||||
outs() << '\n'; | outs() << '\n'; | ||||
for (const auto &I : CommonOptions->ExtraVersionPrinters) | for (const auto &I : getCommonOptions().ExtraVersionPrinters) | ||||
I(outs()); | I(outs()); | ||||
} | } | ||||
exit(0); | exit(0); | ||||
} | } | ||||
void HelpPrinterWrapper::operator=(bool Value) { | void HelpPrinterWrapper::operator=(bool Value) { | ||||
if (!Value) | if (!Value) | ||||
return; | return; | ||||
// Decide which printer to invoke. If more than one option category is | // Decide which printer to invoke. If more than one option category is | ||||
// registered then it is useful to show the categorized help instead of | // registered then it is useful to show the categorized help instead of | ||||
// uncategorized help. | // uncategorized help. | ||||
if (GlobalParser->RegisteredOptionCategories.size() > 1) { | if (getGlobalParser().RegisteredOptionCategories.size() > 1) { | ||||
// unhide --help-list option so user can have uncategorized output if they | // unhide --help-list option so user can have uncategorized output if they | ||||
// want it. | // want it. | ||||
CommonOptions->HLOp.setHiddenFlag(NotHidden); | getCommonOptions().HLOp.setHiddenFlag(NotHidden); | ||||
CategorizedPrinter = true; // Invoke categorized printer | CategorizedPrinter = true; // Invoke categorized printer | ||||
} else | } else | ||||
UncategorizedPrinter = true; // Invoke uncategorized printer | UncategorizedPrinter = true; // Invoke uncategorized printer | ||||
} | } | ||||
// Print the value of each option. | // Print the value of each option. | ||||
void cl::PrintOptionValues() { GlobalParser->printOptionValues(); } | void cl::PrintOptionValues() { getGlobalParser().printOptionValues(); } | ||||
void CommandLineParser::printOptionValues() { | void CommandLineParser::printOptionValues() { | ||||
if (!CommonOptions->PrintOptions && !CommonOptions->PrintAllOptions) | if (!getCommonOptions().PrintOptions && !getCommonOptions().PrintAllOptions) | ||||
return; | return; | ||||
SmallVector<std::pair<const char *, Option *>, 128> Opts; | SmallVector<std::pair<const char *, Option *>, 128> Opts; | ||||
sortOpts(ActiveSubCommand->OptionsMap, Opts, /*ShowHidden*/ true); | sortOpts(ActiveSubCommand->OptionsMap, Opts, /*ShowHidden*/ true); | ||||
// Compute the maximum argument length... | // Compute the maximum argument length... | ||||
size_t MaxArgLen = 0; | size_t MaxArgLen = 0; | ||||
for (size_t i = 0, e = Opts.size(); i != e; ++i) | for (size_t i = 0, e = Opts.size(); i != e; ++i) | ||||
MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth()); | MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth()); | ||||
for (size_t i = 0, e = Opts.size(); i != e; ++i) | for (size_t i = 0, e = Opts.size(); i != e; ++i) | ||||
Opts[i].second->printOptionValue(MaxArgLen, CommonOptions->PrintAllOptions); | Opts[i].second->printOptionValue(MaxArgLen, | ||||
getCommonOptions().PrintAllOptions); | |||||
} | } | ||||
// Utility function for printing the help message. | // Utility function for printing the help message. | ||||
void cl::PrintHelpMessage(bool Hidden, bool Categorized) { | void cl::PrintHelpMessage(bool Hidden, bool Categorized) { | ||||
if (!Hidden && !Categorized) | if (!Hidden && !Categorized) | ||||
CommonOptions->UncategorizedNormalPrinter.printHelp(); | getCommonOptions().UncategorizedNormalPrinter.printHelp(); | ||||
else if (!Hidden && Categorized) | else if (!Hidden && Categorized) | ||||
CommonOptions->CategorizedNormalPrinter.printHelp(); | getCommonOptions().CategorizedNormalPrinter.printHelp(); | ||||
else if (Hidden && !Categorized) | else if (Hidden && !Categorized) | ||||
CommonOptions->UncategorizedHiddenPrinter.printHelp(); | getCommonOptions().UncategorizedHiddenPrinter.printHelp(); | ||||
else | else | ||||
CommonOptions->CategorizedHiddenPrinter.printHelp(); | getCommonOptions().CategorizedHiddenPrinter.printHelp(); | ||||
} | } | ||||
/// Utility function for printing version number. | /// Utility function for printing version number. | ||||
void cl::PrintVersionMessage() { | void cl::PrintVersionMessage() { | ||||
CommonOptions->VersionPrinterInstance.print(); | getCommonOptions().VersionPrinterInstance.print(); | ||||
} | } | ||||
void cl::SetVersionPrinter(VersionPrinterTy func) { | void cl::SetVersionPrinter(VersionPrinterTy func) { | ||||
CommonOptions->OverrideVersionPrinter = func; | getCommonOptions().OverrideVersionPrinter = func; | ||||
} | } | ||||
void cl::AddExtraVersionPrinter(VersionPrinterTy func) { | void cl::AddExtraVersionPrinter(VersionPrinterTy func) { | ||||
CommonOptions->ExtraVersionPrinters.push_back(func); | getCommonOptions().ExtraVersionPrinters.push_back(func); | ||||
} | } | ||||
StringMap<Option *> &cl::getRegisteredOptions(SubCommand &Sub) { | StringMap<Option *> &cl::getRegisteredOptions(SubCommand &Sub) { | ||||
initCommonOptions(); | initCommonOptions(); | ||||
auto &Subs = GlobalParser->RegisteredSubCommands; | auto &Subs = getGlobalParser().RegisteredSubCommands; | ||||
(void)Subs; | (void)Subs; | ||||
assert(is_contained(Subs, &Sub)); | assert(is_contained(Subs, &Sub)); | ||||
return Sub.OptionsMap; | return Sub.OptionsMap; | ||||
} | } | ||||
iterator_range<typename SmallPtrSet<SubCommand *, 4>::iterator> | iterator_range<typename SmallPtrSet<SubCommand *, 4>::iterator> | ||||
cl::getRegisteredSubcommands() { | cl::getRegisteredSubcommands() { | ||||
return GlobalParser->getRegisteredSubcommands(); | return getGlobalParser().getRegisteredSubcommands(); | ||||
} | } | ||||
void cl::HideUnrelatedOptions(cl::OptionCategory &Category, SubCommand &Sub) { | void cl::HideUnrelatedOptions(cl::OptionCategory &Category, SubCommand &Sub) { | ||||
initCommonOptions(); | initCommonOptions(); | ||||
for (auto &I : Sub.OptionsMap) { | for (auto &I : Sub.OptionsMap) { | ||||
bool Unrelated = true; | bool Unrelated = true; | ||||
for (auto &Cat : I.second->Categories) { | for (auto &Cat : I.second->Categories) { | ||||
if (Cat == &Category || Cat == &CommonOptions->GenericCategory) | if (Cat == &Category || Cat == &getCommonOptions().GenericCategory) | ||||
Unrelated = false; | Unrelated = false; | ||||
} | } | ||||
if (Unrelated) | if (Unrelated) | ||||
I.second->setHiddenFlag(cl::ReallyHidden); | I.second->setHiddenFlag(cl::ReallyHidden); | ||||
} | } | ||||
} | } | ||||
void cl::HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *> Categories, | void cl::HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *> Categories, | ||||
SubCommand &Sub) { | SubCommand &Sub) { | ||||
initCommonOptions(); | initCommonOptions(); | ||||
for (auto &I : Sub.OptionsMap) { | for (auto &I : Sub.OptionsMap) { | ||||
bool Unrelated = true; | bool Unrelated = true; | ||||
for (auto &Cat : I.second->Categories) { | for (auto &Cat : I.second->Categories) { | ||||
if (is_contained(Categories, Cat) || | if (is_contained(Categories, Cat) || | ||||
Cat == &CommonOptions->GenericCategory) | Cat == &getCommonOptions().GenericCategory) | ||||
Unrelated = false; | Unrelated = false; | ||||
} | } | ||||
if (Unrelated) | if (Unrelated) | ||||
I.second->setHiddenFlag(cl::ReallyHidden); | I.second->setHiddenFlag(cl::ReallyHidden); | ||||
} | } | ||||
} | } | ||||
void cl::ResetCommandLineParser() { GlobalParser->reset(); } | void cl::ResetCommandLineParser() { getGlobalParser().reset(); } | ||||
void cl::ResetAllOptionOccurrences() { | void cl::ResetAllOptionOccurrences() { | ||||
GlobalParser->ResetAllOptionOccurrences(); | getGlobalParser().ResetAllOptionOccurrences(); | ||||
} | } | ||||
void LLVMParseCommandLineOptions(int argc, const char *const *argv, | void LLVMParseCommandLineOptions(int argc, const char *const *argv, | ||||
const char *Overview) { | const char *Overview) { | ||||
llvm::cl::ParseCommandLineOptions(argc, argv, StringRef(Overview), | llvm::cl::ParseCommandLineOptions(argc, argv, StringRef(Overview), | ||||
&llvm::nulls()); | &llvm::nulls()); | ||||
} | } |