10b57cec5SDimitry Andric //===- llvm/Support/CommandLine.h - Command line handler --------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This class implements a command line argument processor that is useful when
100b57cec5SDimitry Andric // creating a tool. It provides a simple, minimalistic interface that is easily
110b57cec5SDimitry Andric // extensible and supports nonlocal (library) command line options.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric // Note that rather than trying to figure out what this code does, you should
140b57cec5SDimitry Andric // read the library documentation located in docs/CommandLine.html or looks at
150b57cec5SDimitry Andric // the many example usages in tools/*/*.cpp
160b57cec5SDimitry Andric //
170b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
180b57cec5SDimitry Andric
190b57cec5SDimitry Andric #ifndef LLVM_SUPPORT_COMMANDLINE_H
200b57cec5SDimitry Andric #define LLVM_SUPPORT_COMMANDLINE_H
210b57cec5SDimitry Andric
220b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
230b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
240b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
250b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
260b57cec5SDimitry Andric #include "llvm/ADT/StringMap.h"
270b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
280b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
290b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h"
300b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
31bdd1243dSDimitry Andric #include "llvm/Support/StringSaver.h"
320b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
330b57cec5SDimitry Andric #include <cassert>
340b57cec5SDimitry Andric #include <climits>
350b57cec5SDimitry Andric #include <cstddef>
360b57cec5SDimitry Andric #include <functional>
370b57cec5SDimitry Andric #include <initializer_list>
380b57cec5SDimitry Andric #include <string>
390b57cec5SDimitry Andric #include <type_traits>
400b57cec5SDimitry Andric #include <vector>
410b57cec5SDimitry Andric
420b57cec5SDimitry Andric namespace llvm {
430b57cec5SDimitry Andric
44fe6060f1SDimitry Andric namespace vfs {
45fe6060f1SDimitry Andric class FileSystem;
46fe6060f1SDimitry Andric }
47fe6060f1SDimitry Andric
480b57cec5SDimitry Andric class StringSaver;
490b57cec5SDimitry Andric
5081ad6265SDimitry Andric /// This namespace contains all of the command line option processing machinery.
5181ad6265SDimitry Andric /// It is intentionally a short name to make qualified usage concise.
520b57cec5SDimitry Andric namespace cl {
530b57cec5SDimitry Andric
540b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
5581ad6265SDimitry Andric // Command line option processing entry point.
560b57cec5SDimitry Andric //
570b57cec5SDimitry Andric // Returns true on success. Otherwise, this will print the error message to
580b57cec5SDimitry Andric // stderr and exit if \p Errs is not set (nullptr by default), or print the
590b57cec5SDimitry Andric // error message to \p Errs and return false if \p Errs is provided.
600b57cec5SDimitry Andric //
610b57cec5SDimitry Andric // If EnvVar is not nullptr, command-line options are also parsed from the
620b57cec5SDimitry Andric // environment variable named by EnvVar. Precedence is given to occurrences
630b57cec5SDimitry Andric // from argv. This precedence is currently implemented by parsing argv after
640b57cec5SDimitry Andric // the environment variable, so it is only implemented correctly for options
650b57cec5SDimitry Andric // that give precedence to later occurrences. If your program supports options
660b57cec5SDimitry Andric // that give precedence to earlier occurrences, you will need to extend this
670b57cec5SDimitry Andric // function to support it correctly.
680b57cec5SDimitry Andric bool ParseCommandLineOptions(int argc, const char *const *argv,
690b57cec5SDimitry Andric StringRef Overview = "",
700b57cec5SDimitry Andric raw_ostream *Errs = nullptr,
710b57cec5SDimitry Andric const char *EnvVar = nullptr,
720b57cec5SDimitry Andric bool LongOptionsUseDoubleDash = false);
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric // Function pointer type for printing version information.
750b57cec5SDimitry Andric using VersionPrinterTy = std::function<void(raw_ostream &)>;
760b57cec5SDimitry Andric
770b57cec5SDimitry Andric ///===---------------------------------------------------------------------===//
7881ad6265SDimitry Andric /// Override the default (LLVM specific) version printer used to print out the
7981ad6265SDimitry Andric /// version when --version is given on the command line. This allows other
8081ad6265SDimitry Andric /// systems using the CommandLine utilities to print their own version string.
810b57cec5SDimitry Andric void SetVersionPrinter(VersionPrinterTy func);
820b57cec5SDimitry Andric
830b57cec5SDimitry Andric ///===---------------------------------------------------------------------===//
8481ad6265SDimitry Andric /// Add an extra printer to use in addition to the default one. This can be
8581ad6265SDimitry Andric /// called multiple times, and each time it adds a new function to the list
8681ad6265SDimitry Andric /// which will be called after the basic LLVM version printing is complete.
8781ad6265SDimitry Andric /// Each can then add additional information specific to the tool.
880b57cec5SDimitry Andric void AddExtraVersionPrinter(VersionPrinterTy func);
890b57cec5SDimitry Andric
9081ad6265SDimitry Andric // Print option values.
910b57cec5SDimitry Andric // With -print-options print the difference between option values and defaults.
920b57cec5SDimitry Andric // With -print-all-options print all option values.
930b57cec5SDimitry Andric // (Currently not perfect, but best-effort.)
940b57cec5SDimitry Andric void PrintOptionValues();
950b57cec5SDimitry Andric
960b57cec5SDimitry Andric // Forward declaration - AddLiteralOption needs to be up here to make gcc happy.
970b57cec5SDimitry Andric class Option;
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric /// Adds a new option for parsing and provides the option it refers to.
1000b57cec5SDimitry Andric ///
1010b57cec5SDimitry Andric /// \param O pointer to the option
1020b57cec5SDimitry Andric /// \param Name the string name for the option to handle during parsing
1030b57cec5SDimitry Andric ///
1040b57cec5SDimitry Andric /// Literal options are used by some parsers to register special option values.
1050b57cec5SDimitry Andric /// This is how the PassNameParser registers pass names for opt.
1060b57cec5SDimitry Andric void AddLiteralOption(Option &O, StringRef Name);
1070b57cec5SDimitry Andric
1080b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1090b57cec5SDimitry Andric // Flags permitted to be passed to command line arguments
1100b57cec5SDimitry Andric //
1110b57cec5SDimitry Andric
1120b57cec5SDimitry Andric enum NumOccurrencesFlag { // Flags for the number of occurrences allowed
1130b57cec5SDimitry Andric Optional = 0x00, // Zero or One occurrence
1140b57cec5SDimitry Andric ZeroOrMore = 0x01, // Zero or more occurrences allowed
1150b57cec5SDimitry Andric Required = 0x02, // One occurrence required
1160b57cec5SDimitry Andric OneOrMore = 0x03, // One or more occurrences required
1170b57cec5SDimitry Andric
11881ad6265SDimitry Andric // Indicates that this option is fed anything that follows the last positional
11981ad6265SDimitry Andric // argument required by the application (it is an error if there are zero
12081ad6265SDimitry Andric // positional arguments, and a ConsumeAfter option is used).
1210b57cec5SDimitry Andric // Thus, for example, all arguments to LLI are processed until a filename is
1220b57cec5SDimitry Andric // found. Once a filename is found, all of the succeeding arguments are
1230b57cec5SDimitry Andric // passed, unprocessed, to the ConsumeAfter option.
1240b57cec5SDimitry Andric //
1250b57cec5SDimitry Andric ConsumeAfter = 0x04
1260b57cec5SDimitry Andric };
1270b57cec5SDimitry Andric
1280b57cec5SDimitry Andric enum ValueExpected { // Is a value required for the option?
1290b57cec5SDimitry Andric // zero reserved for the unspecified value
1300b57cec5SDimitry Andric ValueOptional = 0x01, // The value can appear... or not
1310b57cec5SDimitry Andric ValueRequired = 0x02, // The value is required to appear!
1320b57cec5SDimitry Andric ValueDisallowed = 0x03 // A value may not be specified (for flags)
1330b57cec5SDimitry Andric };
1340b57cec5SDimitry Andric
1350b57cec5SDimitry Andric enum OptionHidden { // Control whether -help shows this option
1360b57cec5SDimitry Andric NotHidden = 0x00, // Option included in -help & -help-hidden
1370b57cec5SDimitry Andric Hidden = 0x01, // -help doesn't, but -help-hidden does
1380b57cec5SDimitry Andric ReallyHidden = 0x02 // Neither -help nor -help-hidden show this arg
1390b57cec5SDimitry Andric };
1400b57cec5SDimitry Andric
14181ad6265SDimitry Andric // This controls special features that the option might have that cause it to be
14281ad6265SDimitry Andric // parsed differently...
1430b57cec5SDimitry Andric //
1440b57cec5SDimitry Andric // Prefix - This option allows arguments that are otherwise unrecognized to be
1450b57cec5SDimitry Andric // matched by options that are a prefix of the actual value. This is useful for
1460b57cec5SDimitry Andric // cases like a linker, where options are typically of the form '-lfoo' or
1470b57cec5SDimitry Andric // '-L../../include' where -l or -L are the actual flags. When prefix is
1480b57cec5SDimitry Andric // enabled, and used, the value for the flag comes from the suffix of the
1490b57cec5SDimitry Andric // argument.
1500b57cec5SDimitry Andric //
1510b57cec5SDimitry Andric // AlwaysPrefix - Only allow the behavior enabled by the Prefix flag and reject
1520b57cec5SDimitry Andric // the Option=Value form.
1530b57cec5SDimitry Andric //
1540b57cec5SDimitry Andric
1550b57cec5SDimitry Andric enum FormattingFlags {
1560b57cec5SDimitry Andric NormalFormatting = 0x00, // Nothing special
1570b57cec5SDimitry Andric Positional = 0x01, // Is a positional argument, no '-' required
1580b57cec5SDimitry Andric Prefix = 0x02, // Can this option directly prefix its value?
1590b57cec5SDimitry Andric AlwaysPrefix = 0x03 // Can this option only directly prefix its value?
1600b57cec5SDimitry Andric };
1610b57cec5SDimitry Andric
1620b57cec5SDimitry Andric enum MiscFlags { // Miscellaneous flags to adjust argument
1630b57cec5SDimitry Andric CommaSeparated = 0x01, // Should this cl::list split between commas?
1640b57cec5SDimitry Andric PositionalEatsArgs = 0x02, // Should this positional cl::list eat -args?
1650b57cec5SDimitry Andric Sink = 0x04, // Should this cl::list eat all unknown options?
1660b57cec5SDimitry Andric
16781ad6265SDimitry Andric // Can this option group with other options?
1680b57cec5SDimitry Andric // If this is enabled, multiple letter options are allowed to bunch together
1690b57cec5SDimitry Andric // with only a single hyphen for the whole group. This allows emulation
1700b57cec5SDimitry Andric // of the behavior that ls uses for example: ls -la === ls -l -a
1710b57cec5SDimitry Andric Grouping = 0x08,
1720b57cec5SDimitry Andric
1730b57cec5SDimitry Andric // Default option
1740b57cec5SDimitry Andric DefaultOption = 0x10
1750b57cec5SDimitry Andric };
1760b57cec5SDimitry Andric
1770b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1780b57cec5SDimitry Andric //
1790b57cec5SDimitry Andric class OptionCategory {
1800b57cec5SDimitry Andric private:
1810b57cec5SDimitry Andric StringRef const Name;
1820b57cec5SDimitry Andric StringRef const Description;
1830b57cec5SDimitry Andric
1840b57cec5SDimitry Andric void registerCategory();
1850b57cec5SDimitry Andric
1860b57cec5SDimitry Andric public:
1870b57cec5SDimitry Andric OptionCategory(StringRef const Name,
1880b57cec5SDimitry Andric StringRef const Description = "")
Name(Name)1890b57cec5SDimitry Andric : Name(Name), Description(Description) {
1900b57cec5SDimitry Andric registerCategory();
1910b57cec5SDimitry Andric }
1920b57cec5SDimitry Andric
getName()1930b57cec5SDimitry Andric StringRef getName() const { return Name; }
getDescription()1940b57cec5SDimitry Andric StringRef getDescription() const { return Description; }
1950b57cec5SDimitry Andric };
1960b57cec5SDimitry Andric
1970b57cec5SDimitry Andric // The general Option Category (used as default category).
198fe6060f1SDimitry Andric OptionCategory &getGeneralCategory();
1990b57cec5SDimitry Andric
2000b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2010b57cec5SDimitry Andric //
2020b57cec5SDimitry Andric class SubCommand {
2030b57cec5SDimitry Andric private:
2040b57cec5SDimitry Andric StringRef Name;
2050b57cec5SDimitry Andric StringRef Description;
2060b57cec5SDimitry Andric
2070b57cec5SDimitry Andric protected:
2080b57cec5SDimitry Andric void registerSubCommand();
2090b57cec5SDimitry Andric void unregisterSubCommand();
2100b57cec5SDimitry Andric
2110b57cec5SDimitry Andric public:
2120b57cec5SDimitry Andric SubCommand(StringRef Name, StringRef Description = "")
Name(Name)2130b57cec5SDimitry Andric : Name(Name), Description(Description) {
2140b57cec5SDimitry Andric registerSubCommand();
2150b57cec5SDimitry Andric }
2160b57cec5SDimitry Andric SubCommand() = default;
2170b57cec5SDimitry Andric
218bdd1243dSDimitry Andric // Get the special subcommand representing no subcommand.
219bdd1243dSDimitry Andric static SubCommand &getTopLevel();
220bdd1243dSDimitry Andric
221bdd1243dSDimitry Andric // Get the special subcommand that can be used to put an option into all
22206c3fb27SDimitry Andric // subcommands.
223bdd1243dSDimitry Andric static SubCommand &getAll();
224bdd1243dSDimitry Andric
2250b57cec5SDimitry Andric void reset();
2260b57cec5SDimitry Andric
2270b57cec5SDimitry Andric explicit operator bool() const;
2280b57cec5SDimitry Andric
getName()2290b57cec5SDimitry Andric StringRef getName() const { return Name; }
getDescription()2300b57cec5SDimitry Andric StringRef getDescription() const { return Description; }
2310b57cec5SDimitry Andric
2320b57cec5SDimitry Andric SmallVector<Option *, 4> PositionalOpts;
2330b57cec5SDimitry Andric SmallVector<Option *, 4> SinkOpts;
2340b57cec5SDimitry Andric StringMap<Option *> OptionsMap;
2350b57cec5SDimitry Andric
2360b57cec5SDimitry Andric Option *ConsumeAfterOpt = nullptr; // The ConsumeAfter option if it exists.
2370b57cec5SDimitry Andric };
2380b57cec5SDimitry Andric
239cb14a3feSDimitry Andric class SubCommandGroup {
240cb14a3feSDimitry Andric SmallVector<SubCommand *, 4> Subs;
241cb14a3feSDimitry Andric
242cb14a3feSDimitry Andric public:
SubCommandGroup(std::initializer_list<SubCommand * > IL)243cb14a3feSDimitry Andric SubCommandGroup(std::initializer_list<SubCommand *> IL) : Subs(IL) {}
244cb14a3feSDimitry Andric
getSubCommands()245cb14a3feSDimitry Andric ArrayRef<SubCommand *> getSubCommands() const { return Subs; }
246cb14a3feSDimitry Andric };
247cb14a3feSDimitry Andric
2480b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2490b57cec5SDimitry Andric //
2500b57cec5SDimitry Andric class Option {
2510b57cec5SDimitry Andric friend class alias;
2520b57cec5SDimitry Andric
25381ad6265SDimitry Andric // Overriden by subclasses to handle the value passed into an argument. Should
25481ad6265SDimitry Andric // return true if there was an error processing the argument and the program
25581ad6265SDimitry Andric // should exit.
2560b57cec5SDimitry Andric //
2570b57cec5SDimitry Andric virtual bool handleOccurrence(unsigned pos, StringRef ArgName,
2580b57cec5SDimitry Andric StringRef Arg) = 0;
2590b57cec5SDimitry Andric
getValueExpectedFlagDefault()2600b57cec5SDimitry Andric virtual enum ValueExpected getValueExpectedFlagDefault() const {
2610b57cec5SDimitry Andric return ValueOptional;
2620b57cec5SDimitry Andric }
2630b57cec5SDimitry Andric
2640b57cec5SDimitry Andric // Out of line virtual function to provide home for the class.
2650b57cec5SDimitry Andric virtual void anchor();
2660b57cec5SDimitry Andric
2670b57cec5SDimitry Andric uint16_t NumOccurrences; // The number of times specified
2680b57cec5SDimitry Andric // Occurrences, HiddenFlag, and Formatting are all enum types but to avoid
2690b57cec5SDimitry Andric // problems with signed enums in bitfields.
2700b57cec5SDimitry Andric uint16_t Occurrences : 3; // enum NumOccurrencesFlag
2710b57cec5SDimitry Andric // not using the enum type for 'Value' because zero is an implementation
2720b57cec5SDimitry Andric // detail representing the non-value
2730b57cec5SDimitry Andric uint16_t Value : 2;
2740b57cec5SDimitry Andric uint16_t HiddenFlag : 2; // enum OptionHidden
2750b57cec5SDimitry Andric uint16_t Formatting : 2; // enum FormattingFlags
2760b57cec5SDimitry Andric uint16_t Misc : 5;
2770b57cec5SDimitry Andric uint16_t FullyInitialized : 1; // Has addArgument been called?
2780b57cec5SDimitry Andric uint16_t Position; // Position of last occurrence of the option
2790b57cec5SDimitry Andric uint16_t AdditionalVals; // Greater than 0 for multi-valued option.
2800b57cec5SDimitry Andric
2810b57cec5SDimitry Andric public:
2820b57cec5SDimitry Andric StringRef ArgStr; // The argument string itself (ex: "help", "o")
2830b57cec5SDimitry Andric StringRef HelpStr; // The descriptive text message for -help
2840b57cec5SDimitry Andric StringRef ValueStr; // String describing what the value of this option is
2850b57cec5SDimitry Andric SmallVector<OptionCategory *, 1>
2860b57cec5SDimitry Andric Categories; // The Categories this option belongs to
2870b57cec5SDimitry Andric SmallPtrSet<SubCommand *, 1> Subs; // The subcommands this option belongs to.
2880b57cec5SDimitry Andric
getNumOccurrencesFlag()2890b57cec5SDimitry Andric inline enum NumOccurrencesFlag getNumOccurrencesFlag() const {
2900b57cec5SDimitry Andric return (enum NumOccurrencesFlag)Occurrences;
2910b57cec5SDimitry Andric }
2920b57cec5SDimitry Andric
getValueExpectedFlag()2930b57cec5SDimitry Andric inline enum ValueExpected getValueExpectedFlag() const {
2940b57cec5SDimitry Andric return Value ? ((enum ValueExpected)Value) : getValueExpectedFlagDefault();
2950b57cec5SDimitry Andric }
2960b57cec5SDimitry Andric
getOptionHiddenFlag()2970b57cec5SDimitry Andric inline enum OptionHidden getOptionHiddenFlag() const {
2980b57cec5SDimitry Andric return (enum OptionHidden)HiddenFlag;
2990b57cec5SDimitry Andric }
3000b57cec5SDimitry Andric
getFormattingFlag()3010b57cec5SDimitry Andric inline enum FormattingFlags getFormattingFlag() const {
3020b57cec5SDimitry Andric return (enum FormattingFlags)Formatting;
3030b57cec5SDimitry Andric }
3040b57cec5SDimitry Andric
getMiscFlags()3050b57cec5SDimitry Andric inline unsigned getMiscFlags() const { return Misc; }
getPosition()3060b57cec5SDimitry Andric inline unsigned getPosition() const { return Position; }
getNumAdditionalVals()3070b57cec5SDimitry Andric inline unsigned getNumAdditionalVals() const { return AdditionalVals; }
3080b57cec5SDimitry Andric
30981ad6265SDimitry Andric // Return true if the argstr != ""
hasArgStr()3100b57cec5SDimitry Andric bool hasArgStr() const { return !ArgStr.empty(); }
isPositional()3110b57cec5SDimitry Andric bool isPositional() const { return getFormattingFlag() == cl::Positional; }
isSink()3120b57cec5SDimitry Andric bool isSink() const { return getMiscFlags() & cl::Sink; }
isDefaultOption()3130b57cec5SDimitry Andric bool isDefaultOption() const { return getMiscFlags() & cl::DefaultOption; }
3140b57cec5SDimitry Andric
isConsumeAfter()3150b57cec5SDimitry Andric bool isConsumeAfter() const {
3160b57cec5SDimitry Andric return getNumOccurrencesFlag() == cl::ConsumeAfter;
3170b57cec5SDimitry Andric }
3180b57cec5SDimitry Andric
3190b57cec5SDimitry Andric //-------------------------------------------------------------------------===
3200b57cec5SDimitry Andric // Accessor functions set by OptionModifiers
3210b57cec5SDimitry Andric //
3220b57cec5SDimitry Andric void setArgStr(StringRef S);
setDescription(StringRef S)3230b57cec5SDimitry Andric void setDescription(StringRef S) { HelpStr = S; }
setValueStr(StringRef S)3240b57cec5SDimitry Andric void setValueStr(StringRef S) { ValueStr = S; }
setNumOccurrencesFlag(enum NumOccurrencesFlag Val)3250b57cec5SDimitry Andric void setNumOccurrencesFlag(enum NumOccurrencesFlag Val) { Occurrences = Val; }
setValueExpectedFlag(enum ValueExpected Val)3260b57cec5SDimitry Andric void setValueExpectedFlag(enum ValueExpected Val) { Value = Val; }
setHiddenFlag(enum OptionHidden Val)3270b57cec5SDimitry Andric void setHiddenFlag(enum OptionHidden Val) { HiddenFlag = Val; }
setFormattingFlag(enum FormattingFlags V)3280b57cec5SDimitry Andric void setFormattingFlag(enum FormattingFlags V) { Formatting = V; }
setMiscFlag(enum MiscFlags M)3290b57cec5SDimitry Andric void setMiscFlag(enum MiscFlags M) { Misc |= M; }
setPosition(unsigned pos)3300b57cec5SDimitry Andric void setPosition(unsigned pos) { Position = pos; }
3310b57cec5SDimitry Andric void addCategory(OptionCategory &C);
addSubCommand(SubCommand & S)3320b57cec5SDimitry Andric void addSubCommand(SubCommand &S) { Subs.insert(&S); }
3330b57cec5SDimitry Andric
3340b57cec5SDimitry Andric protected:
Option(enum NumOccurrencesFlag OccurrencesFlag,enum OptionHidden Hidden)3350b57cec5SDimitry Andric explicit Option(enum NumOccurrencesFlag OccurrencesFlag,
3360b57cec5SDimitry Andric enum OptionHidden Hidden)
3370b57cec5SDimitry Andric : NumOccurrences(0), Occurrences(OccurrencesFlag), Value(0),
3380b57cec5SDimitry Andric HiddenFlag(Hidden), Formatting(NormalFormatting), Misc(0),
3390b57cec5SDimitry Andric FullyInitialized(false), Position(0), AdditionalVals(0) {
340fe6060f1SDimitry Andric Categories.push_back(&getGeneralCategory());
3410b57cec5SDimitry Andric }
3420b57cec5SDimitry Andric
setNumAdditionalVals(unsigned n)3430b57cec5SDimitry Andric inline void setNumAdditionalVals(unsigned n) { AdditionalVals = n; }
3440b57cec5SDimitry Andric
3450b57cec5SDimitry Andric public:
3460b57cec5SDimitry Andric virtual ~Option() = default;
3470b57cec5SDimitry Andric
34881ad6265SDimitry Andric // Register this argument with the commandline system.
3490b57cec5SDimitry Andric //
3500b57cec5SDimitry Andric void addArgument();
3510b57cec5SDimitry Andric
3520b57cec5SDimitry Andric /// Unregisters this option from the CommandLine system.
3530b57cec5SDimitry Andric ///
3540b57cec5SDimitry Andric /// This option must have been the last option registered.
3550b57cec5SDimitry Andric /// For testing purposes only.
3560b57cec5SDimitry Andric void removeArgument();
3570b57cec5SDimitry Andric
3580b57cec5SDimitry Andric // Return the width of the option tag for printing...
3590b57cec5SDimitry Andric virtual size_t getOptionWidth() const = 0;
3600b57cec5SDimitry Andric
36181ad6265SDimitry Andric // Print out information about this option. The to-be-maintained width is
36281ad6265SDimitry Andric // specified.
3630b57cec5SDimitry Andric //
3640b57cec5SDimitry Andric virtual void printOptionInfo(size_t GlobalWidth) const = 0;
3650b57cec5SDimitry Andric
3660b57cec5SDimitry Andric virtual void printOptionValue(size_t GlobalWidth, bool Force) const = 0;
3670b57cec5SDimitry Andric
3680b57cec5SDimitry Andric virtual void setDefault() = 0;
3690b57cec5SDimitry Andric
370d409305fSDimitry Andric // Prints the help string for an option.
371d409305fSDimitry Andric //
372d409305fSDimitry Andric // This maintains the Indent for multi-line descriptions.
373d409305fSDimitry Andric // FirstLineIndentedBy is the count of chars of the first line
374d409305fSDimitry Andric // i.e. the one containing the --<option name>.
3750b57cec5SDimitry Andric static void printHelpStr(StringRef HelpStr, size_t Indent,
3760b57cec5SDimitry Andric size_t FirstLineIndentedBy);
3770b57cec5SDimitry Andric
378d409305fSDimitry Andric // Prints the help string for an enum value.
379d409305fSDimitry Andric //
380d409305fSDimitry Andric // This maintains the Indent for multi-line descriptions.
381d409305fSDimitry Andric // FirstLineIndentedBy is the count of chars of the first line
382d409305fSDimitry Andric // i.e. the one containing the =<value>.
383d409305fSDimitry Andric static void printEnumValHelpStr(StringRef HelpStr, size_t Indent,
384d409305fSDimitry Andric size_t FirstLineIndentedBy);
385d409305fSDimitry Andric
getExtraOptionNames(SmallVectorImpl<StringRef> &)3860b57cec5SDimitry Andric virtual void getExtraOptionNames(SmallVectorImpl<StringRef> &) {}
3870b57cec5SDimitry Andric
38881ad6265SDimitry Andric // Wrapper around handleOccurrence that enforces Flags.
3890b57cec5SDimitry Andric //
3900b57cec5SDimitry Andric virtual bool addOccurrence(unsigned pos, StringRef ArgName, StringRef Value,
3910b57cec5SDimitry Andric bool MultiArg = false);
3920b57cec5SDimitry Andric
3930b57cec5SDimitry Andric // Prints option name followed by message. Always returns true.
3940b57cec5SDimitry Andric bool error(const Twine &Message, StringRef ArgName = StringRef(), raw_ostream &Errs = llvm::errs());
error(const Twine & Message,raw_ostream & Errs)3950b57cec5SDimitry Andric bool error(const Twine &Message, raw_ostream &Errs) {
3960b57cec5SDimitry Andric return error(Message, StringRef(), Errs);
3970b57cec5SDimitry Andric }
3980b57cec5SDimitry Andric
getNumOccurrences()3990b57cec5SDimitry Andric inline int getNumOccurrences() const { return NumOccurrences; }
4000b57cec5SDimitry Andric void reset();
4010b57cec5SDimitry Andric };
4020b57cec5SDimitry Andric
4030b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
4040b57cec5SDimitry Andric // Command line option modifiers that can be used to modify the behavior of
4050b57cec5SDimitry Andric // command line option parsers...
4060b57cec5SDimitry Andric //
4070b57cec5SDimitry Andric
40881ad6265SDimitry Andric // Modifier to set the description shown in the -help output...
4090b57cec5SDimitry Andric struct desc {
4100b57cec5SDimitry Andric StringRef Desc;
4110b57cec5SDimitry Andric
descdesc4120b57cec5SDimitry Andric desc(StringRef Str) : Desc(Str) {}
4130b57cec5SDimitry Andric
applydesc4140b57cec5SDimitry Andric void apply(Option &O) const { O.setDescription(Desc); }
4150b57cec5SDimitry Andric };
4160b57cec5SDimitry Andric
41781ad6265SDimitry Andric // Modifier to set the value description shown in the -help output...
4180b57cec5SDimitry Andric struct value_desc {
4190b57cec5SDimitry Andric StringRef Desc;
4200b57cec5SDimitry Andric
value_descvalue_desc4210b57cec5SDimitry Andric value_desc(StringRef Str) : Desc(Str) {}
4220b57cec5SDimitry Andric
applyvalue_desc4230b57cec5SDimitry Andric void apply(Option &O) const { O.setValueStr(Desc); }
4240b57cec5SDimitry Andric };
4250b57cec5SDimitry Andric
42681ad6265SDimitry Andric // Specify a default (initial) value for the command line argument, if the
42781ad6265SDimitry Andric // default constructor for the argument type does not give you what you want.
42881ad6265SDimitry Andric // This is only valid on "opt" arguments, not on "list" arguments.
4290b57cec5SDimitry Andric template <class Ty> struct initializer {
4300b57cec5SDimitry Andric const Ty &Init;
initializerinitializer4310b57cec5SDimitry Andric initializer(const Ty &Val) : Init(Val) {}
4320b57cec5SDimitry Andric
applyinitializer4330b57cec5SDimitry Andric template <class Opt> void apply(Opt &O) const { O.setInitialValue(Init); }
4340b57cec5SDimitry Andric };
4350b57cec5SDimitry Andric
436bdd1243dSDimitry Andric template <class Ty> struct list_initializer {
437bdd1243dSDimitry Andric ArrayRef<Ty> Inits;
list_initializerlist_initializer438bdd1243dSDimitry Andric list_initializer(ArrayRef<Ty> Vals) : Inits(Vals) {}
439bdd1243dSDimitry Andric
applylist_initializer440bdd1243dSDimitry Andric template <class Opt> void apply(Opt &O) const { O.setInitialValues(Inits); }
441bdd1243dSDimitry Andric };
442bdd1243dSDimitry Andric
init(const Ty & Val)4430b57cec5SDimitry Andric template <class Ty> initializer<Ty> init(const Ty &Val) {
4440b57cec5SDimitry Andric return initializer<Ty>(Val);
4450b57cec5SDimitry Andric }
4460b57cec5SDimitry Andric
447bdd1243dSDimitry Andric template <class Ty>
list_init(ArrayRef<Ty> Vals)448bdd1243dSDimitry Andric list_initializer<Ty> list_init(ArrayRef<Ty> Vals) {
449bdd1243dSDimitry Andric return list_initializer<Ty>(Vals);
450bdd1243dSDimitry Andric }
451bdd1243dSDimitry Andric
45281ad6265SDimitry Andric // Allow the user to specify which external variable they want to store the
45381ad6265SDimitry Andric // results of the command line argument processing into, if they don't want to
45481ad6265SDimitry Andric // store it in the option itself.
4550b57cec5SDimitry Andric template <class Ty> struct LocationClass {
4560b57cec5SDimitry Andric Ty &Loc;
4570b57cec5SDimitry Andric
LocationClassLocationClass4580b57cec5SDimitry Andric LocationClass(Ty &L) : Loc(L) {}
4590b57cec5SDimitry Andric
applyLocationClass4600b57cec5SDimitry Andric template <class Opt> void apply(Opt &O) const { O.setLocation(O, Loc); }
4610b57cec5SDimitry Andric };
4620b57cec5SDimitry Andric
location(Ty & L)4630b57cec5SDimitry Andric template <class Ty> LocationClass<Ty> location(Ty &L) {
4640b57cec5SDimitry Andric return LocationClass<Ty>(L);
4650b57cec5SDimitry Andric }
4660b57cec5SDimitry Andric
46781ad6265SDimitry Andric // Specify the Option category for the command line argument to belong to.
4680b57cec5SDimitry Andric struct cat {
4690b57cec5SDimitry Andric OptionCategory &Category;
4700b57cec5SDimitry Andric
catcat4710b57cec5SDimitry Andric cat(OptionCategory &c) : Category(c) {}
4720b57cec5SDimitry Andric
applycat4730b57cec5SDimitry Andric template <class Opt> void apply(Opt &O) const { O.addCategory(Category); }
4740b57cec5SDimitry Andric };
4750b57cec5SDimitry Andric
47681ad6265SDimitry Andric // Specify the subcommand that this option belongs to.
4770b57cec5SDimitry Andric struct sub {
478cb14a3feSDimitry Andric SubCommand *Sub = nullptr;
479cb14a3feSDimitry Andric SubCommandGroup *Group = nullptr;
4800b57cec5SDimitry Andric
subsub481cb14a3feSDimitry Andric sub(SubCommand &S) : Sub(&S) {}
subsub482cb14a3feSDimitry Andric sub(SubCommandGroup &G) : Group(&G) {}
4830b57cec5SDimitry Andric
applysub484cb14a3feSDimitry Andric template <class Opt> void apply(Opt &O) const {
485cb14a3feSDimitry Andric if (Sub)
486cb14a3feSDimitry Andric O.addSubCommand(*Sub);
487cb14a3feSDimitry Andric else if (Group)
488cb14a3feSDimitry Andric for (SubCommand *SC : Group->getSubCommands())
489cb14a3feSDimitry Andric O.addSubCommand(*SC);
490cb14a3feSDimitry Andric }
4910b57cec5SDimitry Andric };
4920b57cec5SDimitry Andric
493480093f4SDimitry Andric // Specify a callback function to be called when an option is seen.
494480093f4SDimitry Andric // Can be used to set other options automatically.
495480093f4SDimitry Andric template <typename R, typename Ty> struct cb {
496480093f4SDimitry Andric std::function<R(Ty)> CB;
497480093f4SDimitry Andric
cbcb498480093f4SDimitry Andric cb(std::function<R(Ty)> CB) : CB(CB) {}
499480093f4SDimitry Andric
applycb500480093f4SDimitry Andric template <typename Opt> void apply(Opt &O) const { O.setCallback(CB); }
501480093f4SDimitry Andric };
502480093f4SDimitry Andric
503480093f4SDimitry Andric namespace detail {
504480093f4SDimitry Andric template <typename F>
505480093f4SDimitry Andric struct callback_traits : public callback_traits<decltype(&F::operator())> {};
506480093f4SDimitry Andric
507480093f4SDimitry Andric template <typename R, typename C, typename... Args>
508480093f4SDimitry Andric struct callback_traits<R (C::*)(Args...) const> {
509480093f4SDimitry Andric using result_type = R;
5105ffd83dbSDimitry Andric using arg_type = std::tuple_element_t<0, std::tuple<Args...>>;
511480093f4SDimitry Andric static_assert(sizeof...(Args) == 1, "callback function must have one and only one parameter");
51206c3fb27SDimitry Andric static_assert(std::is_same_v<result_type, void>,
513480093f4SDimitry Andric "callback return type must be void");
51406c3fb27SDimitry Andric static_assert(std::is_lvalue_reference_v<arg_type> &&
51506c3fb27SDimitry Andric std::is_const_v<std::remove_reference_t<arg_type>>,
516480093f4SDimitry Andric "callback arg_type must be a const lvalue reference");
517480093f4SDimitry Andric };
518480093f4SDimitry Andric } // namespace detail
519480093f4SDimitry Andric
520480093f4SDimitry Andric template <typename F>
521480093f4SDimitry Andric cb<typename detail::callback_traits<F>::result_type,
522480093f4SDimitry Andric typename detail::callback_traits<F>::arg_type>
523480093f4SDimitry Andric callback(F CB) {
524480093f4SDimitry Andric using result_type = typename detail::callback_traits<F>::result_type;
525480093f4SDimitry Andric using arg_type = typename detail::callback_traits<F>::arg_type;
526480093f4SDimitry Andric return cb<result_type, arg_type>(CB);
527480093f4SDimitry Andric }
528480093f4SDimitry Andric
5290b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
5300b57cec5SDimitry Andric
5310b57cec5SDimitry Andric // Support value comparison outside the template.
5320b57cec5SDimitry Andric struct GenericOptionValue {
5330b57cec5SDimitry Andric virtual bool compare(const GenericOptionValue &V) const = 0;
5340b57cec5SDimitry Andric
5350b57cec5SDimitry Andric protected:
5360b57cec5SDimitry Andric GenericOptionValue() = default;
5370b57cec5SDimitry Andric GenericOptionValue(const GenericOptionValue&) = default;
5380b57cec5SDimitry Andric GenericOptionValue &operator=(const GenericOptionValue &) = default;
5390b57cec5SDimitry Andric ~GenericOptionValue() = default;
5400b57cec5SDimitry Andric
5410b57cec5SDimitry Andric private:
5420b57cec5SDimitry Andric virtual void anchor();
5430b57cec5SDimitry Andric };
5440b57cec5SDimitry Andric
5450b57cec5SDimitry Andric template <class DataType> struct OptionValue;
5460b57cec5SDimitry Andric
5470b57cec5SDimitry Andric // The default value safely does nothing. Option value printing is only
5480b57cec5SDimitry Andric // best-effort.
5490b57cec5SDimitry Andric template <class DataType, bool isClass>
5500b57cec5SDimitry Andric struct OptionValueBase : public GenericOptionValue {
5510b57cec5SDimitry Andric // Temporary storage for argument passing.
5520b57cec5SDimitry Andric using WrapperType = OptionValue<DataType>;
5530b57cec5SDimitry Andric
5540b57cec5SDimitry Andric bool hasValue() const { return false; }
5550b57cec5SDimitry Andric
5560b57cec5SDimitry Andric const DataType &getValue() const { llvm_unreachable("no default value"); }
5570b57cec5SDimitry Andric
5580b57cec5SDimitry Andric // Some options may take their value from a different data type.
5590b57cec5SDimitry Andric template <class DT> void setValue(const DT & /*V*/) {}
5600b57cec5SDimitry Andric
5615f757f3fSDimitry Andric // Returns whether this instance matches the argument.
5620b57cec5SDimitry Andric bool compare(const DataType & /*V*/) const { return false; }
5630b57cec5SDimitry Andric
5640b57cec5SDimitry Andric bool compare(const GenericOptionValue & /*V*/) const override {
5650b57cec5SDimitry Andric return false;
5660b57cec5SDimitry Andric }
5670b57cec5SDimitry Andric
5680b57cec5SDimitry Andric protected:
5690b57cec5SDimitry Andric ~OptionValueBase() = default;
5700b57cec5SDimitry Andric };
5710b57cec5SDimitry Andric
5720b57cec5SDimitry Andric // Simple copy of the option value.
5730b57cec5SDimitry Andric template <class DataType> class OptionValueCopy : public GenericOptionValue {
5740b57cec5SDimitry Andric DataType Value;
5750b57cec5SDimitry Andric bool Valid = false;
5760b57cec5SDimitry Andric
5770b57cec5SDimitry Andric protected:
5780b57cec5SDimitry Andric OptionValueCopy(const OptionValueCopy&) = default;
5790b57cec5SDimitry Andric OptionValueCopy &operator=(const OptionValueCopy &) = default;
5800b57cec5SDimitry Andric ~OptionValueCopy() = default;
5810b57cec5SDimitry Andric
5820b57cec5SDimitry Andric public:
5830b57cec5SDimitry Andric OptionValueCopy() = default;
5840b57cec5SDimitry Andric
5850b57cec5SDimitry Andric bool hasValue() const { return Valid; }
5860b57cec5SDimitry Andric
5870b57cec5SDimitry Andric const DataType &getValue() const {
5880b57cec5SDimitry Andric assert(Valid && "invalid option value");
5890b57cec5SDimitry Andric return Value;
5900b57cec5SDimitry Andric }
5910b57cec5SDimitry Andric
5920b57cec5SDimitry Andric void setValue(const DataType &V) {
5930b57cec5SDimitry Andric Valid = true;
5940b57cec5SDimitry Andric Value = V;
5950b57cec5SDimitry Andric }
5960b57cec5SDimitry Andric
5975f757f3fSDimitry Andric // Returns whether this instance matches V.
5985f757f3fSDimitry Andric bool compare(const DataType &V) const { return Valid && (Value == V); }
5990b57cec5SDimitry Andric
6000b57cec5SDimitry Andric bool compare(const GenericOptionValue &V) const override {
6010b57cec5SDimitry Andric const OptionValueCopy<DataType> &VC =
6020b57cec5SDimitry Andric static_cast<const OptionValueCopy<DataType> &>(V);
6030b57cec5SDimitry Andric if (!VC.hasValue())
6040b57cec5SDimitry Andric return false;
6050b57cec5SDimitry Andric return compare(VC.getValue());
6060b57cec5SDimitry Andric }
6070b57cec5SDimitry Andric };
6080b57cec5SDimitry Andric
6090b57cec5SDimitry Andric // Non-class option values.
6100b57cec5SDimitry Andric template <class DataType>
6110b57cec5SDimitry Andric struct OptionValueBase<DataType, false> : OptionValueCopy<DataType> {
6120b57cec5SDimitry Andric using WrapperType = DataType;
6130b57cec5SDimitry Andric
6140b57cec5SDimitry Andric protected:
6150b57cec5SDimitry Andric OptionValueBase() = default;
6160b57cec5SDimitry Andric OptionValueBase(const OptionValueBase&) = default;
6170b57cec5SDimitry Andric OptionValueBase &operator=(const OptionValueBase &) = default;
6180b57cec5SDimitry Andric ~OptionValueBase() = default;
6190b57cec5SDimitry Andric };
6200b57cec5SDimitry Andric
6210b57cec5SDimitry Andric // Top-level option class.
6220b57cec5SDimitry Andric template <class DataType>
6230b57cec5SDimitry Andric struct OptionValue final
62406c3fb27SDimitry Andric : OptionValueBase<DataType, std::is_class_v<DataType>> {
6250b57cec5SDimitry Andric OptionValue() = default;
6260b57cec5SDimitry Andric
6270b57cec5SDimitry Andric OptionValue(const DataType &V) { this->setValue(V); }
6280b57cec5SDimitry Andric
6290b57cec5SDimitry Andric // Some options may take their value from a different data type.
6300b57cec5SDimitry Andric template <class DT> OptionValue<DataType> &operator=(const DT &V) {
6310b57cec5SDimitry Andric this->setValue(V);
6320b57cec5SDimitry Andric return *this;
6330b57cec5SDimitry Andric }
6340b57cec5SDimitry Andric };
6350b57cec5SDimitry Andric
6360b57cec5SDimitry Andric // Other safe-to-copy-by-value common option types.
6370b57cec5SDimitry Andric enum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE };
6380b57cec5SDimitry Andric template <>
6390b57cec5SDimitry Andric struct OptionValue<cl::boolOrDefault> final
6400b57cec5SDimitry Andric : OptionValueCopy<cl::boolOrDefault> {
6410b57cec5SDimitry Andric using WrapperType = cl::boolOrDefault;
6420b57cec5SDimitry Andric
6430b57cec5SDimitry Andric OptionValue() = default;
6440b57cec5SDimitry Andric
6450b57cec5SDimitry Andric OptionValue(const cl::boolOrDefault &V) { this->setValue(V); }
6460b57cec5SDimitry Andric
6470b57cec5SDimitry Andric OptionValue<cl::boolOrDefault> &operator=(const cl::boolOrDefault &V) {
6480b57cec5SDimitry Andric setValue(V);
6490b57cec5SDimitry Andric return *this;
6500b57cec5SDimitry Andric }
6510b57cec5SDimitry Andric
6520b57cec5SDimitry Andric private:
6530b57cec5SDimitry Andric void anchor() override;
6540b57cec5SDimitry Andric };
6550b57cec5SDimitry Andric
6560b57cec5SDimitry Andric template <>
6570b57cec5SDimitry Andric struct OptionValue<std::string> final : OptionValueCopy<std::string> {
6580b57cec5SDimitry Andric using WrapperType = StringRef;
6590b57cec5SDimitry Andric
6600b57cec5SDimitry Andric OptionValue() = default;
6610b57cec5SDimitry Andric
6620b57cec5SDimitry Andric OptionValue(const std::string &V) { this->setValue(V); }
6630b57cec5SDimitry Andric
6640b57cec5SDimitry Andric OptionValue<std::string> &operator=(const std::string &V) {
6650b57cec5SDimitry Andric setValue(V);
6660b57cec5SDimitry Andric return *this;
6670b57cec5SDimitry Andric }
6680b57cec5SDimitry Andric
6690b57cec5SDimitry Andric private:
6700b57cec5SDimitry Andric void anchor() override;
6710b57cec5SDimitry Andric };
6720b57cec5SDimitry Andric
6730b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
6740b57cec5SDimitry Andric // Enum valued command line option
6750b57cec5SDimitry Andric //
6760b57cec5SDimitry Andric
6770b57cec5SDimitry Andric // This represents a single enum value, using "int" as the underlying type.
6780b57cec5SDimitry Andric struct OptionEnumValue {
6790b57cec5SDimitry Andric StringRef Name;
6800b57cec5SDimitry Andric int Value;
6810b57cec5SDimitry Andric StringRef Description;
6820b57cec5SDimitry Andric };
6830b57cec5SDimitry Andric
6840b57cec5SDimitry Andric #define clEnumVal(ENUMVAL, DESC) \
6850b57cec5SDimitry Andric llvm::cl::OptionEnumValue { #ENUMVAL, int(ENUMVAL), DESC }
6860b57cec5SDimitry Andric #define clEnumValN(ENUMVAL, FLAGNAME, DESC) \
6870b57cec5SDimitry Andric llvm::cl::OptionEnumValue { FLAGNAME, int(ENUMVAL), DESC }
6880b57cec5SDimitry Andric
68981ad6265SDimitry Andric // For custom data types, allow specifying a group of values together as the
69081ad6265SDimitry Andric // values that go into the mapping that the option handler uses.
6910b57cec5SDimitry Andric //
6920b57cec5SDimitry Andric class ValuesClass {
6930b57cec5SDimitry Andric // Use a vector instead of a map, because the lists should be short,
6940b57cec5SDimitry Andric // the overhead is less, and most importantly, it keeps them in the order
6950b57cec5SDimitry Andric // inserted so we can print our option out nicely.
6960b57cec5SDimitry Andric SmallVector<OptionEnumValue, 4> Values;
6970b57cec5SDimitry Andric
6980b57cec5SDimitry Andric public:
6990b57cec5SDimitry Andric ValuesClass(std::initializer_list<OptionEnumValue> Options)
7000b57cec5SDimitry Andric : Values(Options) {}
7010b57cec5SDimitry Andric
7020b57cec5SDimitry Andric template <class Opt> void apply(Opt &O) const {
703e8d8bef9SDimitry Andric for (const auto &Value : Values)
7040b57cec5SDimitry Andric O.getParser().addLiteralOption(Value.Name, Value.Value,
7050b57cec5SDimitry Andric Value.Description);
7060b57cec5SDimitry Andric }
7070b57cec5SDimitry Andric };
7080b57cec5SDimitry Andric
7090b57cec5SDimitry Andric /// Helper to build a ValuesClass by forwarding a variable number of arguments
7100b57cec5SDimitry Andric /// as an initializer list to the ValuesClass constructor.
7110b57cec5SDimitry Andric template <typename... OptsTy> ValuesClass values(OptsTy... Options) {
7120b57cec5SDimitry Andric return ValuesClass({Options...});
7130b57cec5SDimitry Andric }
7140b57cec5SDimitry Andric
7150b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
71681ad6265SDimitry Andric // Parameterizable parser for different data types. By default, known data types
71781ad6265SDimitry Andric // (string, int, bool) have specialized parsers, that do what you would expect.
71881ad6265SDimitry Andric // The default parser, used for data types that are not built-in, uses a mapping
71981ad6265SDimitry Andric // table to map specific options to values, which is used, among other things,
72081ad6265SDimitry Andric // to handle enum types.
7210b57cec5SDimitry Andric
7220b57cec5SDimitry Andric //--------------------------------------------------
72381ad6265SDimitry Andric // This class holds all the non-generic code that we do not need replicated for
72481ad6265SDimitry Andric // every instance of the generic parser. This also allows us to put stuff into
72581ad6265SDimitry Andric // CommandLine.cpp
7260b57cec5SDimitry Andric //
7270b57cec5SDimitry Andric class generic_parser_base {
7280b57cec5SDimitry Andric protected:
7290b57cec5SDimitry Andric class GenericOptionInfo {
7300b57cec5SDimitry Andric public:
7310b57cec5SDimitry Andric GenericOptionInfo(StringRef name, StringRef helpStr)
7320b57cec5SDimitry Andric : Name(name), HelpStr(helpStr) {}
7330b57cec5SDimitry Andric StringRef Name;
7340b57cec5SDimitry Andric StringRef HelpStr;
7350b57cec5SDimitry Andric };
7360b57cec5SDimitry Andric
7370b57cec5SDimitry Andric public:
7380b57cec5SDimitry Andric generic_parser_base(Option &O) : Owner(O) {}
7390b57cec5SDimitry Andric
7400b57cec5SDimitry Andric virtual ~generic_parser_base() = default;
7410b57cec5SDimitry Andric // Base class should have virtual-destructor
7420b57cec5SDimitry Andric
74381ad6265SDimitry Andric // Virtual function implemented by generic subclass to indicate how many
74481ad6265SDimitry Andric // entries are in Values.
7450b57cec5SDimitry Andric //
7460b57cec5SDimitry Andric virtual unsigned getNumOptions() const = 0;
7470b57cec5SDimitry Andric
74881ad6265SDimitry Andric // Return option name N.
7490b57cec5SDimitry Andric virtual StringRef getOption(unsigned N) const = 0;
7500b57cec5SDimitry Andric
75181ad6265SDimitry Andric // Return description N
7520b57cec5SDimitry Andric virtual StringRef getDescription(unsigned N) const = 0;
7530b57cec5SDimitry Andric
7540b57cec5SDimitry Andric // Return the width of the option tag for printing...
7550b57cec5SDimitry Andric virtual size_t getOptionWidth(const Option &O) const;
7560b57cec5SDimitry Andric
7570b57cec5SDimitry Andric virtual const GenericOptionValue &getOptionValue(unsigned N) const = 0;
7580b57cec5SDimitry Andric
75981ad6265SDimitry Andric // Print out information about this option. The to-be-maintained width is
76081ad6265SDimitry Andric // specified.
7610b57cec5SDimitry Andric //
7620b57cec5SDimitry Andric virtual void printOptionInfo(const Option &O, size_t GlobalWidth) const;
7630b57cec5SDimitry Andric
7640b57cec5SDimitry Andric void printGenericOptionDiff(const Option &O, const GenericOptionValue &V,
7650b57cec5SDimitry Andric const GenericOptionValue &Default,
7660b57cec5SDimitry Andric size_t GlobalWidth) const;
7670b57cec5SDimitry Andric
76881ad6265SDimitry Andric // Print the value of an option and it's default.
7690b57cec5SDimitry Andric //
7700b57cec5SDimitry Andric // Template definition ensures that the option and default have the same
7710b57cec5SDimitry Andric // DataType (via the same AnyOptionValue).
7720b57cec5SDimitry Andric template <class AnyOptionValue>
7730b57cec5SDimitry Andric void printOptionDiff(const Option &O, const AnyOptionValue &V,
7740b57cec5SDimitry Andric const AnyOptionValue &Default,
7750b57cec5SDimitry Andric size_t GlobalWidth) const {
7760b57cec5SDimitry Andric printGenericOptionDiff(O, V, Default, GlobalWidth);
7770b57cec5SDimitry Andric }
7780b57cec5SDimitry Andric
7790b57cec5SDimitry Andric void initialize() {}
7800b57cec5SDimitry Andric
7810b57cec5SDimitry Andric void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) {
7820b57cec5SDimitry Andric // If there has been no argstr specified, that means that we need to add an
7830b57cec5SDimitry Andric // argument for every possible option. This ensures that our options are
7840b57cec5SDimitry Andric // vectored to us.
7850b57cec5SDimitry Andric if (!Owner.hasArgStr())
7860b57cec5SDimitry Andric for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
7870b57cec5SDimitry Andric OptionNames.push_back(getOption(i));
7880b57cec5SDimitry Andric }
7890b57cec5SDimitry Andric
7900b57cec5SDimitry Andric enum ValueExpected getValueExpectedFlagDefault() const {
7910b57cec5SDimitry Andric // If there is an ArgStr specified, then we are of the form:
7920b57cec5SDimitry Andric //
7930b57cec5SDimitry Andric // -opt=O2 or -opt O2 or -optO2
7940b57cec5SDimitry Andric //
7950b57cec5SDimitry Andric // In which case, the value is required. Otherwise if an arg str has not
7960b57cec5SDimitry Andric // been specified, we are of the form:
7970b57cec5SDimitry Andric //
7980b57cec5SDimitry Andric // -O2 or O2 or -la (where -l and -a are separate options)
7990b57cec5SDimitry Andric //
8000b57cec5SDimitry Andric // If this is the case, we cannot allow a value.
8010b57cec5SDimitry Andric //
8020b57cec5SDimitry Andric if (Owner.hasArgStr())
8030b57cec5SDimitry Andric return ValueRequired;
8040b57cec5SDimitry Andric else
8050b57cec5SDimitry Andric return ValueDisallowed;
8060b57cec5SDimitry Andric }
8070b57cec5SDimitry Andric
80881ad6265SDimitry Andric // Return the option number corresponding to the specified
8090b57cec5SDimitry Andric // argument string. If the option is not found, getNumOptions() is returned.
8100b57cec5SDimitry Andric //
8110b57cec5SDimitry Andric unsigned findOption(StringRef Name);
8120b57cec5SDimitry Andric
8130b57cec5SDimitry Andric protected:
8140b57cec5SDimitry Andric Option &Owner;
8150b57cec5SDimitry Andric };
8160b57cec5SDimitry Andric
8170b57cec5SDimitry Andric // Default parser implementation - This implementation depends on having a
8180b57cec5SDimitry Andric // mapping of recognized options to values of some sort. In addition to this,
8190b57cec5SDimitry Andric // each entry in the mapping also tracks a help message that is printed with the
8200b57cec5SDimitry Andric // command line option for -help. Because this is a simple mapping parser, the
8210b57cec5SDimitry Andric // data type can be any unsupported type.
8220b57cec5SDimitry Andric //
8230b57cec5SDimitry Andric template <class DataType> class parser : public generic_parser_base {
8240b57cec5SDimitry Andric protected:
8250b57cec5SDimitry Andric class OptionInfo : public GenericOptionInfo {
8260b57cec5SDimitry Andric public:
8270b57cec5SDimitry Andric OptionInfo(StringRef name, DataType v, StringRef helpStr)
8280b57cec5SDimitry Andric : GenericOptionInfo(name, helpStr), V(v) {}
8290b57cec5SDimitry Andric
8300b57cec5SDimitry Andric OptionValue<DataType> V;
8310b57cec5SDimitry Andric };
8320b57cec5SDimitry Andric SmallVector<OptionInfo, 8> Values;
8330b57cec5SDimitry Andric
8340b57cec5SDimitry Andric public:
8350b57cec5SDimitry Andric parser(Option &O) : generic_parser_base(O) {}
8360b57cec5SDimitry Andric
8370b57cec5SDimitry Andric using parser_data_type = DataType;
8380b57cec5SDimitry Andric
8390b57cec5SDimitry Andric // Implement virtual functions needed by generic_parser_base
8400b57cec5SDimitry Andric unsigned getNumOptions() const override { return unsigned(Values.size()); }
8410b57cec5SDimitry Andric StringRef getOption(unsigned N) const override { return Values[N].Name; }
8420b57cec5SDimitry Andric StringRef getDescription(unsigned N) const override {
8430b57cec5SDimitry Andric return Values[N].HelpStr;
8440b57cec5SDimitry Andric }
8450b57cec5SDimitry Andric
84681ad6265SDimitry Andric // Return the value of option name N.
8470b57cec5SDimitry Andric const GenericOptionValue &getOptionValue(unsigned N) const override {
8480b57cec5SDimitry Andric return Values[N].V;
8490b57cec5SDimitry Andric }
8500b57cec5SDimitry Andric
85181ad6265SDimitry Andric // Return true on error.
8520b57cec5SDimitry Andric bool parse(Option &O, StringRef ArgName, StringRef Arg, DataType &V) {
8530b57cec5SDimitry Andric StringRef ArgVal;
8540b57cec5SDimitry Andric if (Owner.hasArgStr())
8550b57cec5SDimitry Andric ArgVal = Arg;
8560b57cec5SDimitry Andric else
8570b57cec5SDimitry Andric ArgVal = ArgName;
8580b57cec5SDimitry Andric
8590b57cec5SDimitry Andric for (size_t i = 0, e = Values.size(); i != e; ++i)
8600b57cec5SDimitry Andric if (Values[i].Name == ArgVal) {
8610b57cec5SDimitry Andric V = Values[i].V.getValue();
8620b57cec5SDimitry Andric return false;
8630b57cec5SDimitry Andric }
8640b57cec5SDimitry Andric
8650b57cec5SDimitry Andric return O.error("Cannot find option named '" + ArgVal + "'!");
8660b57cec5SDimitry Andric }
8670b57cec5SDimitry Andric
86881ad6265SDimitry Andric /// Add an entry to the mapping table.
8690b57cec5SDimitry Andric ///
8700b57cec5SDimitry Andric template <class DT>
8710b57cec5SDimitry Andric void addLiteralOption(StringRef Name, const DT &V, StringRef HelpStr) {
8725f757f3fSDimitry Andric #ifndef NDEBUG
8735f757f3fSDimitry Andric if (findOption(Name) != Values.size())
874*0fca6ea1SDimitry Andric report_fatal_error("Option '" + Name + "' already exists!");
8755f757f3fSDimitry Andric #endif
8760b57cec5SDimitry Andric OptionInfo X(Name, static_cast<DataType>(V), HelpStr);
8770b57cec5SDimitry Andric Values.push_back(X);
8780b57cec5SDimitry Andric AddLiteralOption(Owner, Name);
8790b57cec5SDimitry Andric }
8800b57cec5SDimitry Andric
88181ad6265SDimitry Andric /// Remove the specified option.
8820b57cec5SDimitry Andric ///
8830b57cec5SDimitry Andric void removeLiteralOption(StringRef Name) {
8840b57cec5SDimitry Andric unsigned N = findOption(Name);
8850b57cec5SDimitry Andric assert(N != Values.size() && "Option not found!");
8860b57cec5SDimitry Andric Values.erase(Values.begin() + N);
8870b57cec5SDimitry Andric }
8880b57cec5SDimitry Andric };
8890b57cec5SDimitry Andric
8900b57cec5SDimitry Andric //--------------------------------------------------
89181ad6265SDimitry Andric // Super class of parsers to provide boilerplate code
8920b57cec5SDimitry Andric //
8930b57cec5SDimitry Andric class basic_parser_impl { // non-template implementation of basic_parser<t>
8940b57cec5SDimitry Andric public:
8950b57cec5SDimitry Andric basic_parser_impl(Option &) {}
8960b57cec5SDimitry Andric
8971fd87a68SDimitry Andric virtual ~basic_parser_impl() = default;
8980b57cec5SDimitry Andric
8990b57cec5SDimitry Andric enum ValueExpected getValueExpectedFlagDefault() const {
9000b57cec5SDimitry Andric return ValueRequired;
9010b57cec5SDimitry Andric }
9020b57cec5SDimitry Andric
9030b57cec5SDimitry Andric void getExtraOptionNames(SmallVectorImpl<StringRef> &) {}
9040b57cec5SDimitry Andric
9050b57cec5SDimitry Andric void initialize() {}
9060b57cec5SDimitry Andric
9070b57cec5SDimitry Andric // Return the width of the option tag for printing...
9080b57cec5SDimitry Andric size_t getOptionWidth(const Option &O) const;
9090b57cec5SDimitry Andric
91081ad6265SDimitry Andric // Print out information about this option. The to-be-maintained width is
91181ad6265SDimitry Andric // specified.
9120b57cec5SDimitry Andric //
9130b57cec5SDimitry Andric void printOptionInfo(const Option &O, size_t GlobalWidth) const;
9140b57cec5SDimitry Andric
91581ad6265SDimitry Andric // Print a placeholder for options that don't yet support printOptionDiff().
9160b57cec5SDimitry Andric void printOptionNoValue(const Option &O, size_t GlobalWidth) const;
9170b57cec5SDimitry Andric
91881ad6265SDimitry Andric // Overload in subclass to provide a better default value.
9190b57cec5SDimitry Andric virtual StringRef getValueName() const { return "value"; }
9200b57cec5SDimitry Andric
9210b57cec5SDimitry Andric // An out-of-line virtual method to provide a 'home' for this class.
9220b57cec5SDimitry Andric virtual void anchor();
9230b57cec5SDimitry Andric
9240b57cec5SDimitry Andric protected:
9250b57cec5SDimitry Andric // A helper for basic_parser::printOptionDiff.
9260b57cec5SDimitry Andric void printOptionName(const Option &O, size_t GlobalWidth) const;
9270b57cec5SDimitry Andric };
9280b57cec5SDimitry Andric
92981ad6265SDimitry Andric // The real basic parser is just a template wrapper that provides a typedef for
93081ad6265SDimitry Andric // the provided data type.
9310b57cec5SDimitry Andric //
9320b57cec5SDimitry Andric template <class DataType> class basic_parser : public basic_parser_impl {
9330b57cec5SDimitry Andric public:
9340b57cec5SDimitry Andric using parser_data_type = DataType;
9350b57cec5SDimitry Andric using OptVal = OptionValue<DataType>;
9360b57cec5SDimitry Andric
9370b57cec5SDimitry Andric basic_parser(Option &O) : basic_parser_impl(O) {}
9380b57cec5SDimitry Andric };
9390b57cec5SDimitry Andric
9400b57cec5SDimitry Andric //--------------------------------------------------
941349cc55cSDimitry Andric
942349cc55cSDimitry Andric extern template class basic_parser<bool>;
943349cc55cSDimitry Andric
9440b57cec5SDimitry Andric template <> class parser<bool> : public basic_parser<bool> {
9450b57cec5SDimitry Andric public:
9460b57cec5SDimitry Andric parser(Option &O) : basic_parser(O) {}
9470b57cec5SDimitry Andric
94881ad6265SDimitry Andric // Return true on error.
9490b57cec5SDimitry Andric bool parse(Option &O, StringRef ArgName, StringRef Arg, bool &Val);
9500b57cec5SDimitry Andric
9510b57cec5SDimitry Andric void initialize() {}
9520b57cec5SDimitry Andric
9530b57cec5SDimitry Andric enum ValueExpected getValueExpectedFlagDefault() const {
9540b57cec5SDimitry Andric return ValueOptional;
9550b57cec5SDimitry Andric }
9560b57cec5SDimitry Andric
95781ad6265SDimitry Andric // Do not print =<value> at all.
9580b57cec5SDimitry Andric StringRef getValueName() const override { return StringRef(); }
9590b57cec5SDimitry Andric
9600b57cec5SDimitry Andric void printOptionDiff(const Option &O, bool V, OptVal Default,
9610b57cec5SDimitry Andric size_t GlobalWidth) const;
9620b57cec5SDimitry Andric
9630b57cec5SDimitry Andric // An out-of-line virtual method to provide a 'home' for this class.
9640b57cec5SDimitry Andric void anchor() override;
9650b57cec5SDimitry Andric };
9660b57cec5SDimitry Andric
9670b57cec5SDimitry Andric //--------------------------------------------------
968349cc55cSDimitry Andric
969349cc55cSDimitry Andric extern template class basic_parser<boolOrDefault>;
970349cc55cSDimitry Andric
9710b57cec5SDimitry Andric template <> class parser<boolOrDefault> : public basic_parser<boolOrDefault> {
9720b57cec5SDimitry Andric public:
9730b57cec5SDimitry Andric parser(Option &O) : basic_parser(O) {}
9740b57cec5SDimitry Andric
97581ad6265SDimitry Andric // Return true on error.
9760b57cec5SDimitry Andric bool parse(Option &O, StringRef ArgName, StringRef Arg, boolOrDefault &Val);
9770b57cec5SDimitry Andric
9780b57cec5SDimitry Andric enum ValueExpected getValueExpectedFlagDefault() const {
9790b57cec5SDimitry Andric return ValueOptional;
9800b57cec5SDimitry Andric }
9810b57cec5SDimitry Andric
98281ad6265SDimitry Andric // Do not print =<value> at all.
9830b57cec5SDimitry Andric StringRef getValueName() const override { return StringRef(); }
9840b57cec5SDimitry Andric
9850b57cec5SDimitry Andric void printOptionDiff(const Option &O, boolOrDefault V, OptVal Default,
9860b57cec5SDimitry Andric size_t GlobalWidth) const;
9870b57cec5SDimitry Andric
9880b57cec5SDimitry Andric // An out-of-line virtual method to provide a 'home' for this class.
9890b57cec5SDimitry Andric void anchor() override;
9900b57cec5SDimitry Andric };
9910b57cec5SDimitry Andric
9920b57cec5SDimitry Andric //--------------------------------------------------
993349cc55cSDimitry Andric
994349cc55cSDimitry Andric extern template class basic_parser<int>;
995349cc55cSDimitry Andric
9960b57cec5SDimitry Andric template <> class parser<int> : public basic_parser<int> {
9970b57cec5SDimitry Andric public:
9980b57cec5SDimitry Andric parser(Option &O) : basic_parser(O) {}
9990b57cec5SDimitry Andric
100081ad6265SDimitry Andric // Return true on error.
10010b57cec5SDimitry Andric bool parse(Option &O, StringRef ArgName, StringRef Arg, int &Val);
10020b57cec5SDimitry Andric
100381ad6265SDimitry Andric // Overload in subclass to provide a better default value.
10040b57cec5SDimitry Andric StringRef getValueName() const override { return "int"; }
10050b57cec5SDimitry Andric
10060b57cec5SDimitry Andric void printOptionDiff(const Option &O, int V, OptVal Default,
10070b57cec5SDimitry Andric size_t GlobalWidth) const;
10080b57cec5SDimitry Andric
10090b57cec5SDimitry Andric // An out-of-line virtual method to provide a 'home' for this class.
10100b57cec5SDimitry Andric void anchor() override;
10110b57cec5SDimitry Andric };
10120b57cec5SDimitry Andric
10130b57cec5SDimitry Andric //--------------------------------------------------
1014349cc55cSDimitry Andric
1015349cc55cSDimitry Andric extern template class basic_parser<long>;
1016349cc55cSDimitry Andric
1017480093f4SDimitry Andric template <> class parser<long> final : public basic_parser<long> {
1018480093f4SDimitry Andric public:
1019480093f4SDimitry Andric parser(Option &O) : basic_parser(O) {}
1020480093f4SDimitry Andric
102181ad6265SDimitry Andric // Return true on error.
1022480093f4SDimitry Andric bool parse(Option &O, StringRef ArgName, StringRef Arg, long &Val);
1023480093f4SDimitry Andric
102481ad6265SDimitry Andric // Overload in subclass to provide a better default value.
1025480093f4SDimitry Andric StringRef getValueName() const override { return "long"; }
1026480093f4SDimitry Andric
1027480093f4SDimitry Andric void printOptionDiff(const Option &O, long V, OptVal Default,
1028480093f4SDimitry Andric size_t GlobalWidth) const;
1029480093f4SDimitry Andric
1030480093f4SDimitry Andric // An out-of-line virtual method to provide a 'home' for this class.
1031480093f4SDimitry Andric void anchor() override;
1032480093f4SDimitry Andric };
1033480093f4SDimitry Andric
1034480093f4SDimitry Andric //--------------------------------------------------
1035349cc55cSDimitry Andric
1036349cc55cSDimitry Andric extern template class basic_parser<long long>;
1037349cc55cSDimitry Andric
1038480093f4SDimitry Andric template <> class parser<long long> : public basic_parser<long long> {
1039480093f4SDimitry Andric public:
1040480093f4SDimitry Andric parser(Option &O) : basic_parser(O) {}
1041480093f4SDimitry Andric
104281ad6265SDimitry Andric // Return true on error.
1043480093f4SDimitry Andric bool parse(Option &O, StringRef ArgName, StringRef Arg, long long &Val);
1044480093f4SDimitry Andric
104581ad6265SDimitry Andric // Overload in subclass to provide a better default value.
1046480093f4SDimitry Andric StringRef getValueName() const override { return "long"; }
1047480093f4SDimitry Andric
1048480093f4SDimitry Andric void printOptionDiff(const Option &O, long long V, OptVal Default,
1049480093f4SDimitry Andric size_t GlobalWidth) const;
1050480093f4SDimitry Andric
1051480093f4SDimitry Andric // An out-of-line virtual method to provide a 'home' for this class.
1052480093f4SDimitry Andric void anchor() override;
1053480093f4SDimitry Andric };
1054480093f4SDimitry Andric
1055480093f4SDimitry Andric //--------------------------------------------------
1056349cc55cSDimitry Andric
1057349cc55cSDimitry Andric extern template class basic_parser<unsigned>;
1058349cc55cSDimitry Andric
10590b57cec5SDimitry Andric template <> class parser<unsigned> : public basic_parser<unsigned> {
10600b57cec5SDimitry Andric public:
10610b57cec5SDimitry Andric parser(Option &O) : basic_parser(O) {}
10620b57cec5SDimitry Andric
106381ad6265SDimitry Andric // Return true on error.
10640b57cec5SDimitry Andric bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned &Val);
10650b57cec5SDimitry Andric
106681ad6265SDimitry Andric // Overload in subclass to provide a better default value.
10670b57cec5SDimitry Andric StringRef getValueName() const override { return "uint"; }
10680b57cec5SDimitry Andric
10690b57cec5SDimitry Andric void printOptionDiff(const Option &O, unsigned V, OptVal Default,
10700b57cec5SDimitry Andric size_t GlobalWidth) const;
10710b57cec5SDimitry Andric
10720b57cec5SDimitry Andric // An out-of-line virtual method to provide a 'home' for this class.
10730b57cec5SDimitry Andric void anchor() override;
10740b57cec5SDimitry Andric };
10750b57cec5SDimitry Andric
10760b57cec5SDimitry Andric //--------------------------------------------------
1077349cc55cSDimitry Andric
1078349cc55cSDimitry Andric extern template class basic_parser<unsigned long>;
1079349cc55cSDimitry Andric
10800b57cec5SDimitry Andric template <>
10810b57cec5SDimitry Andric class parser<unsigned long> final : public basic_parser<unsigned long> {
10820b57cec5SDimitry Andric public:
10830b57cec5SDimitry Andric parser(Option &O) : basic_parser(O) {}
10840b57cec5SDimitry Andric
108581ad6265SDimitry Andric // Return true on error.
10860b57cec5SDimitry Andric bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned long &Val);
10870b57cec5SDimitry Andric
108881ad6265SDimitry Andric // Overload in subclass to provide a better default value.
10890b57cec5SDimitry Andric StringRef getValueName() const override { return "ulong"; }
10900b57cec5SDimitry Andric
10910b57cec5SDimitry Andric void printOptionDiff(const Option &O, unsigned long V, OptVal Default,
10920b57cec5SDimitry Andric size_t GlobalWidth) const;
10930b57cec5SDimitry Andric
10940b57cec5SDimitry Andric // An out-of-line virtual method to provide a 'home' for this class.
10950b57cec5SDimitry Andric void anchor() override;
10960b57cec5SDimitry Andric };
10970b57cec5SDimitry Andric
10980b57cec5SDimitry Andric //--------------------------------------------------
1099349cc55cSDimitry Andric
1100349cc55cSDimitry Andric extern template class basic_parser<unsigned long long>;
1101349cc55cSDimitry Andric
11020b57cec5SDimitry Andric template <>
11030b57cec5SDimitry Andric class parser<unsigned long long> : public basic_parser<unsigned long long> {
11040b57cec5SDimitry Andric public:
11050b57cec5SDimitry Andric parser(Option &O) : basic_parser(O) {}
11060b57cec5SDimitry Andric
110781ad6265SDimitry Andric // Return true on error.
11080b57cec5SDimitry Andric bool parse(Option &O, StringRef ArgName, StringRef Arg,
11090b57cec5SDimitry Andric unsigned long long &Val);
11100b57cec5SDimitry Andric
111181ad6265SDimitry Andric // Overload in subclass to provide a better default value.
11120b57cec5SDimitry Andric StringRef getValueName() const override { return "ulong"; }
11130b57cec5SDimitry Andric
11140b57cec5SDimitry Andric void printOptionDiff(const Option &O, unsigned long long V, OptVal Default,
11150b57cec5SDimitry Andric size_t GlobalWidth) const;
11160b57cec5SDimitry Andric
11170b57cec5SDimitry Andric // An out-of-line virtual method to provide a 'home' for this class.
11180b57cec5SDimitry Andric void anchor() override;
11190b57cec5SDimitry Andric };
11200b57cec5SDimitry Andric
11210b57cec5SDimitry Andric //--------------------------------------------------
1122349cc55cSDimitry Andric
1123349cc55cSDimitry Andric extern template class basic_parser<double>;
1124349cc55cSDimitry Andric
11250b57cec5SDimitry Andric template <> class parser<double> : public basic_parser<double> {
11260b57cec5SDimitry Andric public:
11270b57cec5SDimitry Andric parser(Option &O) : basic_parser(O) {}
11280b57cec5SDimitry Andric
112981ad6265SDimitry Andric // Return true on error.
11300b57cec5SDimitry Andric bool parse(Option &O, StringRef ArgName, StringRef Arg, double &Val);
11310b57cec5SDimitry Andric
113281ad6265SDimitry Andric // Overload in subclass to provide a better default value.
11330b57cec5SDimitry Andric StringRef getValueName() const override { return "number"; }
11340b57cec5SDimitry Andric
11350b57cec5SDimitry Andric void printOptionDiff(const Option &O, double V, OptVal Default,
11360b57cec5SDimitry Andric size_t GlobalWidth) const;
11370b57cec5SDimitry Andric
11380b57cec5SDimitry Andric // An out-of-line virtual method to provide a 'home' for this class.
11390b57cec5SDimitry Andric void anchor() override;
11400b57cec5SDimitry Andric };
11410b57cec5SDimitry Andric
11420b57cec5SDimitry Andric //--------------------------------------------------
1143349cc55cSDimitry Andric
1144349cc55cSDimitry Andric extern template class basic_parser<float>;
1145349cc55cSDimitry Andric
11460b57cec5SDimitry Andric template <> class parser<float> : public basic_parser<float> {
11470b57cec5SDimitry Andric public:
11480b57cec5SDimitry Andric parser(Option &O) : basic_parser(O) {}
11490b57cec5SDimitry Andric
115081ad6265SDimitry Andric // Return true on error.
11510b57cec5SDimitry Andric bool parse(Option &O, StringRef ArgName, StringRef Arg, float &Val);
11520b57cec5SDimitry Andric
115381ad6265SDimitry Andric // Overload in subclass to provide a better default value.
11540b57cec5SDimitry Andric StringRef getValueName() const override { return "number"; }
11550b57cec5SDimitry Andric
11560b57cec5SDimitry Andric void printOptionDiff(const Option &O, float V, OptVal Default,
11570b57cec5SDimitry Andric size_t GlobalWidth) const;
11580b57cec5SDimitry Andric
11590b57cec5SDimitry Andric // An out-of-line virtual method to provide a 'home' for this class.
11600b57cec5SDimitry Andric void anchor() override;
11610b57cec5SDimitry Andric };
11620b57cec5SDimitry Andric
11630b57cec5SDimitry Andric //--------------------------------------------------
1164349cc55cSDimitry Andric
1165349cc55cSDimitry Andric extern template class basic_parser<std::string>;
1166349cc55cSDimitry Andric
11670b57cec5SDimitry Andric template <> class parser<std::string> : public basic_parser<std::string> {
11680b57cec5SDimitry Andric public:
11690b57cec5SDimitry Andric parser(Option &O) : basic_parser(O) {}
11700b57cec5SDimitry Andric
117181ad6265SDimitry Andric // Return true on error.
11720b57cec5SDimitry Andric bool parse(Option &, StringRef, StringRef Arg, std::string &Value) {
11730b57cec5SDimitry Andric Value = Arg.str();
11740b57cec5SDimitry Andric return false;
11750b57cec5SDimitry Andric }
11760b57cec5SDimitry Andric
117781ad6265SDimitry Andric // Overload in subclass to provide a better default value.
11780b57cec5SDimitry Andric StringRef getValueName() const override { return "string"; }
11790b57cec5SDimitry Andric
11800b57cec5SDimitry Andric void printOptionDiff(const Option &O, StringRef V, const OptVal &Default,
11810b57cec5SDimitry Andric size_t GlobalWidth) const;
11820b57cec5SDimitry Andric
11830b57cec5SDimitry Andric // An out-of-line virtual method to provide a 'home' for this class.
11840b57cec5SDimitry Andric void anchor() override;
11850b57cec5SDimitry Andric };
11860b57cec5SDimitry Andric
11870b57cec5SDimitry Andric //--------------------------------------------------
1188349cc55cSDimitry Andric
1189349cc55cSDimitry Andric extern template class basic_parser<char>;
1190349cc55cSDimitry Andric
11910b57cec5SDimitry Andric template <> class parser<char> : public basic_parser<char> {
11920b57cec5SDimitry Andric public:
11930b57cec5SDimitry Andric parser(Option &O) : basic_parser(O) {}
11940b57cec5SDimitry Andric
119581ad6265SDimitry Andric // Return true on error.
11960b57cec5SDimitry Andric bool parse(Option &, StringRef, StringRef Arg, char &Value) {
11970b57cec5SDimitry Andric Value = Arg[0];
11980b57cec5SDimitry Andric return false;
11990b57cec5SDimitry Andric }
12000b57cec5SDimitry Andric
120181ad6265SDimitry Andric // Overload in subclass to provide a better default value.
12020b57cec5SDimitry Andric StringRef getValueName() const override { return "char"; }
12030b57cec5SDimitry Andric
12040b57cec5SDimitry Andric void printOptionDiff(const Option &O, char V, OptVal Default,
12050b57cec5SDimitry Andric size_t GlobalWidth) const;
12060b57cec5SDimitry Andric
12070b57cec5SDimitry Andric // An out-of-line virtual method to provide a 'home' for this class.
12080b57cec5SDimitry Andric void anchor() override;
12090b57cec5SDimitry Andric };
12100b57cec5SDimitry Andric
12110b57cec5SDimitry Andric //--------------------------------------------------
12120b57cec5SDimitry Andric // This collection of wrappers is the intermediary between class opt and class
12130b57cec5SDimitry Andric // parser to handle all the template nastiness.
12140b57cec5SDimitry Andric
12150b57cec5SDimitry Andric // This overloaded function is selected by the generic parser.
12160b57cec5SDimitry Andric template <class ParserClass, class DT>
12170b57cec5SDimitry Andric void printOptionDiff(const Option &O, const generic_parser_base &P, const DT &V,
12180b57cec5SDimitry Andric const OptionValue<DT> &Default, size_t GlobalWidth) {
12190b57cec5SDimitry Andric OptionValue<DT> OV = V;
12200b57cec5SDimitry Andric P.printOptionDiff(O, OV, Default, GlobalWidth);
12210b57cec5SDimitry Andric }
12220b57cec5SDimitry Andric
12230b57cec5SDimitry Andric // This is instantiated for basic parsers when the parsed value has a different
12240b57cec5SDimitry Andric // type than the option value. e.g. HelpPrinter.
12250b57cec5SDimitry Andric template <class ParserDT, class ValDT> struct OptionDiffPrinter {
12260b57cec5SDimitry Andric void print(const Option &O, const parser<ParserDT> &P, const ValDT & /*V*/,
12270b57cec5SDimitry Andric const OptionValue<ValDT> & /*Default*/, size_t GlobalWidth) {
12280b57cec5SDimitry Andric P.printOptionNoValue(O, GlobalWidth);
12290b57cec5SDimitry Andric }
12300b57cec5SDimitry Andric };
12310b57cec5SDimitry Andric
12320b57cec5SDimitry Andric // This is instantiated for basic parsers when the parsed value has the same
12330b57cec5SDimitry Andric // type as the option value.
12340b57cec5SDimitry Andric template <class DT> struct OptionDiffPrinter<DT, DT> {
12350b57cec5SDimitry Andric void print(const Option &O, const parser<DT> &P, const DT &V,
12360b57cec5SDimitry Andric const OptionValue<DT> &Default, size_t GlobalWidth) {
12370b57cec5SDimitry Andric P.printOptionDiff(O, V, Default, GlobalWidth);
12380b57cec5SDimitry Andric }
12390b57cec5SDimitry Andric };
12400b57cec5SDimitry Andric
12410b57cec5SDimitry Andric // This overloaded function is selected by the basic parser, which may parse a
12420b57cec5SDimitry Andric // different type than the option type.
12430b57cec5SDimitry Andric template <class ParserClass, class ValDT>
12440b57cec5SDimitry Andric void printOptionDiff(
12450b57cec5SDimitry Andric const Option &O,
12460b57cec5SDimitry Andric const basic_parser<typename ParserClass::parser_data_type> &P,
12470b57cec5SDimitry Andric const ValDT &V, const OptionValue<ValDT> &Default, size_t GlobalWidth) {
12480b57cec5SDimitry Andric
12490b57cec5SDimitry Andric OptionDiffPrinter<typename ParserClass::parser_data_type, ValDT> printer;
12500b57cec5SDimitry Andric printer.print(O, static_cast<const ParserClass &>(P), V, Default,
12510b57cec5SDimitry Andric GlobalWidth);
12520b57cec5SDimitry Andric }
12530b57cec5SDimitry Andric
12540b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
125581ad6265SDimitry Andric // This class is used because we must use partial specialization to handle
125681ad6265SDimitry Andric // literal string arguments specially (const char* does not correctly respond to
125781ad6265SDimitry Andric // the apply method). Because the syntax to use this is a pain, we have the
125881ad6265SDimitry Andric // 'apply' method below to handle the nastiness...
12590b57cec5SDimitry Andric //
12600b57cec5SDimitry Andric template <class Mod> struct applicator {
12610b57cec5SDimitry Andric template <class Opt> static void opt(const Mod &M, Opt &O) { M.apply(O); }
12620b57cec5SDimitry Andric };
12630b57cec5SDimitry Andric
12640b57cec5SDimitry Andric // Handle const char* as a special case...
12650b57cec5SDimitry Andric template <unsigned n> struct applicator<char[n]> {
12660b57cec5SDimitry Andric template <class Opt> static void opt(StringRef Str, Opt &O) {
12670b57cec5SDimitry Andric O.setArgStr(Str);
12680b57cec5SDimitry Andric }
12690b57cec5SDimitry Andric };
12700b57cec5SDimitry Andric template <unsigned n> struct applicator<const char[n]> {
12710b57cec5SDimitry Andric template <class Opt> static void opt(StringRef Str, Opt &O) {
12720b57cec5SDimitry Andric O.setArgStr(Str);
12730b57cec5SDimitry Andric }
12740b57cec5SDimitry Andric };
12750b57cec5SDimitry Andric template <> struct applicator<StringRef > {
12760b57cec5SDimitry Andric template <class Opt> static void opt(StringRef Str, Opt &O) {
12770b57cec5SDimitry Andric O.setArgStr(Str);
12780b57cec5SDimitry Andric }
12790b57cec5SDimitry Andric };
12800b57cec5SDimitry Andric
12810b57cec5SDimitry Andric template <> struct applicator<NumOccurrencesFlag> {
12820b57cec5SDimitry Andric static void opt(NumOccurrencesFlag N, Option &O) {
12830b57cec5SDimitry Andric O.setNumOccurrencesFlag(N);
12840b57cec5SDimitry Andric }
12850b57cec5SDimitry Andric };
12860b57cec5SDimitry Andric
12870b57cec5SDimitry Andric template <> struct applicator<ValueExpected> {
12880b57cec5SDimitry Andric static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
12890b57cec5SDimitry Andric };
12900b57cec5SDimitry Andric
12910b57cec5SDimitry Andric template <> struct applicator<OptionHidden> {
12920b57cec5SDimitry Andric static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
12930b57cec5SDimitry Andric };
12940b57cec5SDimitry Andric
12950b57cec5SDimitry Andric template <> struct applicator<FormattingFlags> {
12960b57cec5SDimitry Andric static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
12970b57cec5SDimitry Andric };
12980b57cec5SDimitry Andric
12990b57cec5SDimitry Andric template <> struct applicator<MiscFlags> {
13000b57cec5SDimitry Andric static void opt(MiscFlags MF, Option &O) {
13010b57cec5SDimitry Andric assert((MF != Grouping || O.ArgStr.size() == 1) &&
1302bdd1243dSDimitry Andric "cl::Grouping can only apply to single character Options.");
13030b57cec5SDimitry Andric O.setMiscFlag(MF);
13040b57cec5SDimitry Andric }
13050b57cec5SDimitry Andric };
13060b57cec5SDimitry Andric
130781ad6265SDimitry Andric // Apply modifiers to an option in a type safe way.
13080b57cec5SDimitry Andric template <class Opt, class Mod, class... Mods>
13090b57cec5SDimitry Andric void apply(Opt *O, const Mod &M, const Mods &... Ms) {
13100b57cec5SDimitry Andric applicator<Mod>::opt(M, *O);
13110b57cec5SDimitry Andric apply(O, Ms...);
13120b57cec5SDimitry Andric }
13130b57cec5SDimitry Andric
13140b57cec5SDimitry Andric template <class Opt, class Mod> void apply(Opt *O, const Mod &M) {
13150b57cec5SDimitry Andric applicator<Mod>::opt(M, *O);
13160b57cec5SDimitry Andric }
13170b57cec5SDimitry Andric
13180b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
13190b57cec5SDimitry Andric // Default storage class definition: external storage. This implementation
13200b57cec5SDimitry Andric // assumes the user will specify a variable to store the data into with the
13210b57cec5SDimitry Andric // cl::location(x) modifier.
13220b57cec5SDimitry Andric //
13230b57cec5SDimitry Andric template <class DataType, bool ExternalStorage, bool isClass>
13240b57cec5SDimitry Andric class opt_storage {
13250b57cec5SDimitry Andric DataType *Location = nullptr; // Where to store the object...
13260b57cec5SDimitry Andric OptionValue<DataType> Default;
13270b57cec5SDimitry Andric
13280b57cec5SDimitry Andric void check_location() const {
13290b57cec5SDimitry Andric assert(Location && "cl::location(...) not specified for a command "
13300b57cec5SDimitry Andric "line option with external storage, "
13310b57cec5SDimitry Andric "or cl::init specified before cl::location()!!");
13320b57cec5SDimitry Andric }
13330b57cec5SDimitry Andric
13340b57cec5SDimitry Andric public:
13350b57cec5SDimitry Andric opt_storage() = default;
13360b57cec5SDimitry Andric
13370b57cec5SDimitry Andric bool setLocation(Option &O, DataType &L) {
13380b57cec5SDimitry Andric if (Location)
13390b57cec5SDimitry Andric return O.error("cl::location(x) specified more than once!");
13400b57cec5SDimitry Andric Location = &L;
13410b57cec5SDimitry Andric Default = L;
13420b57cec5SDimitry Andric return false;
13430b57cec5SDimitry Andric }
13440b57cec5SDimitry Andric
13450b57cec5SDimitry Andric template <class T> void setValue(const T &V, bool initial = false) {
13460b57cec5SDimitry Andric check_location();
13470b57cec5SDimitry Andric *Location = V;
13480b57cec5SDimitry Andric if (initial)
13490b57cec5SDimitry Andric Default = V;
13500b57cec5SDimitry Andric }
13510b57cec5SDimitry Andric
13520b57cec5SDimitry Andric DataType &getValue() {
13530b57cec5SDimitry Andric check_location();
13540b57cec5SDimitry Andric return *Location;
13550b57cec5SDimitry Andric }
13560b57cec5SDimitry Andric const DataType &getValue() const {
13570b57cec5SDimitry Andric check_location();
13580b57cec5SDimitry Andric return *Location;
13590b57cec5SDimitry Andric }
13600b57cec5SDimitry Andric
13610b57cec5SDimitry Andric operator DataType() const { return this->getValue(); }
13620b57cec5SDimitry Andric
13630b57cec5SDimitry Andric const OptionValue<DataType> &getDefault() const { return Default; }
13640b57cec5SDimitry Andric };
13650b57cec5SDimitry Andric
13660b57cec5SDimitry Andric // Define how to hold a class type object, such as a string. Since we can
13670b57cec5SDimitry Andric // inherit from a class, we do so. This makes us exactly compatible with the
13680b57cec5SDimitry Andric // object in all cases that it is used.
13690b57cec5SDimitry Andric //
13700b57cec5SDimitry Andric template <class DataType>
13710b57cec5SDimitry Andric class opt_storage<DataType, false, true> : public DataType {
13720b57cec5SDimitry Andric public:
13730b57cec5SDimitry Andric OptionValue<DataType> Default;
13740b57cec5SDimitry Andric
13750b57cec5SDimitry Andric template <class T> void setValue(const T &V, bool initial = false) {
13760b57cec5SDimitry Andric DataType::operator=(V);
13770b57cec5SDimitry Andric if (initial)
13780b57cec5SDimitry Andric Default = V;
13790b57cec5SDimitry Andric }
13800b57cec5SDimitry Andric
13810b57cec5SDimitry Andric DataType &getValue() { return *this; }
13820b57cec5SDimitry Andric const DataType &getValue() const { return *this; }
13830b57cec5SDimitry Andric
13840b57cec5SDimitry Andric const OptionValue<DataType> &getDefault() const { return Default; }
13850b57cec5SDimitry Andric };
13860b57cec5SDimitry Andric
13870b57cec5SDimitry Andric // Define a partial specialization to handle things we cannot inherit from. In
13880b57cec5SDimitry Andric // this case, we store an instance through containment, and overload operators
13890b57cec5SDimitry Andric // to get at the value.
13900b57cec5SDimitry Andric //
13910b57cec5SDimitry Andric template <class DataType> class opt_storage<DataType, false, false> {
13920b57cec5SDimitry Andric public:
13930b57cec5SDimitry Andric DataType Value;
13940b57cec5SDimitry Andric OptionValue<DataType> Default;
13950b57cec5SDimitry Andric
13960b57cec5SDimitry Andric // Make sure we initialize the value with the default constructor for the
13970b57cec5SDimitry Andric // type.
139881ad6265SDimitry Andric opt_storage() : Value(DataType()), Default() {}
13990b57cec5SDimitry Andric
14000b57cec5SDimitry Andric template <class T> void setValue(const T &V, bool initial = false) {
14010b57cec5SDimitry Andric Value = V;
14020b57cec5SDimitry Andric if (initial)
14030b57cec5SDimitry Andric Default = V;
14040b57cec5SDimitry Andric }
14050b57cec5SDimitry Andric DataType &getValue() { return Value; }
14060b57cec5SDimitry Andric DataType getValue() const { return Value; }
14070b57cec5SDimitry Andric
14080b57cec5SDimitry Andric const OptionValue<DataType> &getDefault() const { return Default; }
14090b57cec5SDimitry Andric
14100b57cec5SDimitry Andric operator DataType() const { return getValue(); }
14110b57cec5SDimitry Andric
14120b57cec5SDimitry Andric // If the datatype is a pointer, support -> on it.
14130b57cec5SDimitry Andric DataType operator->() const { return Value; }
14140b57cec5SDimitry Andric };
14150b57cec5SDimitry Andric
14160b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
141781ad6265SDimitry Andric // A scalar command line option.
14180b57cec5SDimitry Andric //
14190b57cec5SDimitry Andric template <class DataType, bool ExternalStorage = false,
14200b57cec5SDimitry Andric class ParserClass = parser<DataType>>
142106c3fb27SDimitry Andric class opt
142206c3fb27SDimitry Andric : public Option,
142306c3fb27SDimitry Andric public opt_storage<DataType, ExternalStorage, std::is_class_v<DataType>> {
14240b57cec5SDimitry Andric ParserClass Parser;
14250b57cec5SDimitry Andric
14260b57cec5SDimitry Andric bool handleOccurrence(unsigned pos, StringRef ArgName,
14270b57cec5SDimitry Andric StringRef Arg) override {
14280b57cec5SDimitry Andric typename ParserClass::parser_data_type Val =
14290b57cec5SDimitry Andric typename ParserClass::parser_data_type();
14300b57cec5SDimitry Andric if (Parser.parse(*this, ArgName, Arg, Val))
14310b57cec5SDimitry Andric return true; // Parse error!
14320b57cec5SDimitry Andric this->setValue(Val);
14330b57cec5SDimitry Andric this->setPosition(pos);
1434480093f4SDimitry Andric Callback(Val);
14350b57cec5SDimitry Andric return false;
14360b57cec5SDimitry Andric }
14370b57cec5SDimitry Andric
14380b57cec5SDimitry Andric enum ValueExpected getValueExpectedFlagDefault() const override {
14390b57cec5SDimitry Andric return Parser.getValueExpectedFlagDefault();
14400b57cec5SDimitry Andric }
14410b57cec5SDimitry Andric
14420b57cec5SDimitry Andric void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) override {
14430b57cec5SDimitry Andric return Parser.getExtraOptionNames(OptionNames);
14440b57cec5SDimitry Andric }
14450b57cec5SDimitry Andric
14460b57cec5SDimitry Andric // Forward printing stuff to the parser...
14470b57cec5SDimitry Andric size_t getOptionWidth() const override {
14480b57cec5SDimitry Andric return Parser.getOptionWidth(*this);
14490b57cec5SDimitry Andric }
14500b57cec5SDimitry Andric
14510b57cec5SDimitry Andric void printOptionInfo(size_t GlobalWidth) const override {
14520b57cec5SDimitry Andric Parser.printOptionInfo(*this, GlobalWidth);
14530b57cec5SDimitry Andric }
14540b57cec5SDimitry Andric
14550b57cec5SDimitry Andric void printOptionValue(size_t GlobalWidth, bool Force) const override {
14565f757f3fSDimitry Andric if (Force || !this->getDefault().compare(this->getValue())) {
14570b57cec5SDimitry Andric cl::printOptionDiff<ParserClass>(*this, Parser, this->getValue(),
14580b57cec5SDimitry Andric this->getDefault(), GlobalWidth);
14590b57cec5SDimitry Andric }
14600b57cec5SDimitry Andric }
14610b57cec5SDimitry Andric
146206c3fb27SDimitry Andric template <class T, class = std::enable_if_t<std::is_assignable_v<T &, T>>>
14630b57cec5SDimitry Andric void setDefaultImpl() {
14640b57cec5SDimitry Andric const OptionValue<DataType> &V = this->getDefault();
14650b57cec5SDimitry Andric if (V.hasValue())
14660b57cec5SDimitry Andric this->setValue(V.getValue());
146781ad6265SDimitry Andric else
146881ad6265SDimitry Andric this->setValue(T());
14690b57cec5SDimitry Andric }
14700b57cec5SDimitry Andric
147106c3fb27SDimitry Andric template <class T, class = std::enable_if_t<!std::is_assignable_v<T &, T>>>
14720b57cec5SDimitry Andric void setDefaultImpl(...) {}
14730b57cec5SDimitry Andric
14740b57cec5SDimitry Andric void setDefault() override { setDefaultImpl<DataType>(); }
14750b57cec5SDimitry Andric
14760b57cec5SDimitry Andric void done() {
14770b57cec5SDimitry Andric addArgument();
14780b57cec5SDimitry Andric Parser.initialize();
14790b57cec5SDimitry Andric }
14800b57cec5SDimitry Andric
14810b57cec5SDimitry Andric public:
14820b57cec5SDimitry Andric // Command line options should not be copyable
14830b57cec5SDimitry Andric opt(const opt &) = delete;
14840b57cec5SDimitry Andric opt &operator=(const opt &) = delete;
14850b57cec5SDimitry Andric
14860b57cec5SDimitry Andric // setInitialValue - Used by the cl::init modifier...
14870b57cec5SDimitry Andric void setInitialValue(const DataType &V) { this->setValue(V, true); }
14880b57cec5SDimitry Andric
14890b57cec5SDimitry Andric ParserClass &getParser() { return Parser; }
14900b57cec5SDimitry Andric
14910b57cec5SDimitry Andric template <class T> DataType &operator=(const T &Val) {
14920b57cec5SDimitry Andric this->setValue(Val);
1493480093f4SDimitry Andric Callback(Val);
14940b57cec5SDimitry Andric return this->getValue();
14950b57cec5SDimitry Andric }
14960b57cec5SDimitry Andric
14970b57cec5SDimitry Andric template <class... Mods>
14980b57cec5SDimitry Andric explicit opt(const Mods &... Ms)
1499e8d8bef9SDimitry Andric : Option(llvm::cl::Optional, NotHidden), Parser(*this) {
15000b57cec5SDimitry Andric apply(this, Ms...);
15010b57cec5SDimitry Andric done();
15020b57cec5SDimitry Andric }
1503480093f4SDimitry Andric
1504480093f4SDimitry Andric void setCallback(
1505480093f4SDimitry Andric std::function<void(const typename ParserClass::parser_data_type &)> CB) {
1506480093f4SDimitry Andric Callback = CB;
1507480093f4SDimitry Andric }
1508480093f4SDimitry Andric
1509480093f4SDimitry Andric std::function<void(const typename ParserClass::parser_data_type &)> Callback =
1510480093f4SDimitry Andric [](const typename ParserClass::parser_data_type &) {};
15110b57cec5SDimitry Andric };
15120b57cec5SDimitry Andric
15130b57cec5SDimitry Andric extern template class opt<unsigned>;
15140b57cec5SDimitry Andric extern template class opt<int>;
15150b57cec5SDimitry Andric extern template class opt<std::string>;
15160b57cec5SDimitry Andric extern template class opt<char>;
15170b57cec5SDimitry Andric extern template class opt<bool>;
15180b57cec5SDimitry Andric
15190b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
15200b57cec5SDimitry Andric // Default storage class definition: external storage. This implementation
15210b57cec5SDimitry Andric // assumes the user will specify a variable to store the data into with the
15220b57cec5SDimitry Andric // cl::location(x) modifier.
15230b57cec5SDimitry Andric //
15240b57cec5SDimitry Andric template <class DataType, class StorageClass> class list_storage {
15250b57cec5SDimitry Andric StorageClass *Location = nullptr; // Where to store the object...
1526bdd1243dSDimitry Andric std::vector<OptionValue<DataType>> Default =
1527bdd1243dSDimitry Andric std::vector<OptionValue<DataType>>();
1528bdd1243dSDimitry Andric bool DefaultAssigned = false;
15290b57cec5SDimitry Andric
15300b57cec5SDimitry Andric public:
15310b57cec5SDimitry Andric list_storage() = default;
15320b57cec5SDimitry Andric
15330b57cec5SDimitry Andric void clear() {}
15340b57cec5SDimitry Andric
15350b57cec5SDimitry Andric bool setLocation(Option &O, StorageClass &L) {
15360b57cec5SDimitry Andric if (Location)
15370b57cec5SDimitry Andric return O.error("cl::location(x) specified more than once!");
15380b57cec5SDimitry Andric Location = &L;
15390b57cec5SDimitry Andric return false;
15400b57cec5SDimitry Andric }
15410b57cec5SDimitry Andric
1542bdd1243dSDimitry Andric template <class T> void addValue(const T &V, bool initial = false) {
154304eeddc0SDimitry Andric assert(Location != nullptr &&
154404eeddc0SDimitry Andric "cl::location(...) not specified for a command "
15450b57cec5SDimitry Andric "line option with external storage!");
15460b57cec5SDimitry Andric Location->push_back(V);
1547bdd1243dSDimitry Andric if (initial)
1548bdd1243dSDimitry Andric Default.push_back(V);
15490b57cec5SDimitry Andric }
1550bdd1243dSDimitry Andric
1551bdd1243dSDimitry Andric const std::vector<OptionValue<DataType>> &getDefault() const {
1552bdd1243dSDimitry Andric return Default;
1553bdd1243dSDimitry Andric }
1554bdd1243dSDimitry Andric
1555bdd1243dSDimitry Andric void assignDefault() { DefaultAssigned = true; }
1556bdd1243dSDimitry Andric void overwriteDefault() { DefaultAssigned = false; }
1557bdd1243dSDimitry Andric bool isDefaultAssigned() { return DefaultAssigned; }
15580b57cec5SDimitry Andric };
15590b57cec5SDimitry Andric
15600b57cec5SDimitry Andric // Define how to hold a class type object, such as a string.
15610b57cec5SDimitry Andric // Originally this code inherited from std::vector. In transitioning to a new
15620b57cec5SDimitry Andric // API for command line options we should change this. The new implementation
15630b57cec5SDimitry Andric // of this list_storage specialization implements the minimum subset of the
15640b57cec5SDimitry Andric // std::vector API required for all the current clients.
15650b57cec5SDimitry Andric //
15660b57cec5SDimitry Andric // FIXME: Reduce this API to a more narrow subset of std::vector
15670b57cec5SDimitry Andric //
15680b57cec5SDimitry Andric template <class DataType> class list_storage<DataType, bool> {
15690b57cec5SDimitry Andric std::vector<DataType> Storage;
1570bdd1243dSDimitry Andric std::vector<OptionValue<DataType>> Default;
1571bdd1243dSDimitry Andric bool DefaultAssigned = false;
15720b57cec5SDimitry Andric
15730b57cec5SDimitry Andric public:
15740b57cec5SDimitry Andric using iterator = typename std::vector<DataType>::iterator;
15750b57cec5SDimitry Andric
15760b57cec5SDimitry Andric iterator begin() { return Storage.begin(); }
15770b57cec5SDimitry Andric iterator end() { return Storage.end(); }
15780b57cec5SDimitry Andric
15790b57cec5SDimitry Andric using const_iterator = typename std::vector<DataType>::const_iterator;
15800b57cec5SDimitry Andric
15810b57cec5SDimitry Andric const_iterator begin() const { return Storage.begin(); }
15820b57cec5SDimitry Andric const_iterator end() const { return Storage.end(); }
15830b57cec5SDimitry Andric
15840b57cec5SDimitry Andric using size_type = typename std::vector<DataType>::size_type;
15850b57cec5SDimitry Andric
15860b57cec5SDimitry Andric size_type size() const { return Storage.size(); }
15870b57cec5SDimitry Andric
15880b57cec5SDimitry Andric bool empty() const { return Storage.empty(); }
15890b57cec5SDimitry Andric
15900b57cec5SDimitry Andric void push_back(const DataType &value) { Storage.push_back(value); }
15910b57cec5SDimitry Andric void push_back(DataType &&value) { Storage.push_back(value); }
15920b57cec5SDimitry Andric
15930b57cec5SDimitry Andric using reference = typename std::vector<DataType>::reference;
15940b57cec5SDimitry Andric using const_reference = typename std::vector<DataType>::const_reference;
15950b57cec5SDimitry Andric
15960b57cec5SDimitry Andric reference operator[](size_type pos) { return Storage[pos]; }
15970b57cec5SDimitry Andric const_reference operator[](size_type pos) const { return Storage[pos]; }
15980b57cec5SDimitry Andric
15990b57cec5SDimitry Andric void clear() {
16000b57cec5SDimitry Andric Storage.clear();
16010b57cec5SDimitry Andric }
16020b57cec5SDimitry Andric
16030b57cec5SDimitry Andric iterator erase(const_iterator pos) { return Storage.erase(pos); }
16040b57cec5SDimitry Andric iterator erase(const_iterator first, const_iterator last) {
16050b57cec5SDimitry Andric return Storage.erase(first, last);
16060b57cec5SDimitry Andric }
16070b57cec5SDimitry Andric
16080b57cec5SDimitry Andric iterator erase(iterator pos) { return Storage.erase(pos); }
16090b57cec5SDimitry Andric iterator erase(iterator first, iterator last) {
16100b57cec5SDimitry Andric return Storage.erase(first, last);
16110b57cec5SDimitry Andric }
16120b57cec5SDimitry Andric
16130b57cec5SDimitry Andric iterator insert(const_iterator pos, const DataType &value) {
16140b57cec5SDimitry Andric return Storage.insert(pos, value);
16150b57cec5SDimitry Andric }
16160b57cec5SDimitry Andric iterator insert(const_iterator pos, DataType &&value) {
16170b57cec5SDimitry Andric return Storage.insert(pos, value);
16180b57cec5SDimitry Andric }
16190b57cec5SDimitry Andric
16200b57cec5SDimitry Andric iterator insert(iterator pos, const DataType &value) {
16210b57cec5SDimitry Andric return Storage.insert(pos, value);
16220b57cec5SDimitry Andric }
16230b57cec5SDimitry Andric iterator insert(iterator pos, DataType &&value) {
16240b57cec5SDimitry Andric return Storage.insert(pos, value);
16250b57cec5SDimitry Andric }
16260b57cec5SDimitry Andric
16270b57cec5SDimitry Andric reference front() { return Storage.front(); }
16280b57cec5SDimitry Andric const_reference front() const { return Storage.front(); }
16290b57cec5SDimitry Andric
16300b57cec5SDimitry Andric operator std::vector<DataType> &() { return Storage; }
16315ffd83dbSDimitry Andric operator ArrayRef<DataType>() const { return Storage; }
16320b57cec5SDimitry Andric std::vector<DataType> *operator&() { return &Storage; }
16330b57cec5SDimitry Andric const std::vector<DataType> *operator&() const { return &Storage; }
16340b57cec5SDimitry Andric
1635bdd1243dSDimitry Andric template <class T> void addValue(const T &V, bool initial = false) {
1636bdd1243dSDimitry Andric Storage.push_back(V);
1637bdd1243dSDimitry Andric if (initial)
1638bdd1243dSDimitry Andric Default.push_back(OptionValue<DataType>(V));
1639bdd1243dSDimitry Andric }
1640bdd1243dSDimitry Andric
1641bdd1243dSDimitry Andric const std::vector<OptionValue<DataType>> &getDefault() const {
1642bdd1243dSDimitry Andric return Default;
1643bdd1243dSDimitry Andric }
1644bdd1243dSDimitry Andric
1645bdd1243dSDimitry Andric void assignDefault() { DefaultAssigned = true; }
1646bdd1243dSDimitry Andric void overwriteDefault() { DefaultAssigned = false; }
1647bdd1243dSDimitry Andric bool isDefaultAssigned() { return DefaultAssigned; }
16480b57cec5SDimitry Andric };
16490b57cec5SDimitry Andric
16500b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
165181ad6265SDimitry Andric // A list of command line options.
16520b57cec5SDimitry Andric //
16530b57cec5SDimitry Andric template <class DataType, class StorageClass = bool,
16540b57cec5SDimitry Andric class ParserClass = parser<DataType>>
16550b57cec5SDimitry Andric class list : public Option, public list_storage<DataType, StorageClass> {
16560b57cec5SDimitry Andric std::vector<unsigned> Positions;
16570b57cec5SDimitry Andric ParserClass Parser;
16580b57cec5SDimitry Andric
16590b57cec5SDimitry Andric enum ValueExpected getValueExpectedFlagDefault() const override {
16600b57cec5SDimitry Andric return Parser.getValueExpectedFlagDefault();
16610b57cec5SDimitry Andric }
16620b57cec5SDimitry Andric
16630b57cec5SDimitry Andric void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) override {
16640b57cec5SDimitry Andric return Parser.getExtraOptionNames(OptionNames);
16650b57cec5SDimitry Andric }
16660b57cec5SDimitry Andric
16670b57cec5SDimitry Andric bool handleOccurrence(unsigned pos, StringRef ArgName,
16680b57cec5SDimitry Andric StringRef Arg) override {
16690b57cec5SDimitry Andric typename ParserClass::parser_data_type Val =
16700b57cec5SDimitry Andric typename ParserClass::parser_data_type();
1671bdd1243dSDimitry Andric if (list_storage<DataType, StorageClass>::isDefaultAssigned()) {
1672bdd1243dSDimitry Andric clear();
1673bdd1243dSDimitry Andric list_storage<DataType, StorageClass>::overwriteDefault();
1674bdd1243dSDimitry Andric }
16750b57cec5SDimitry Andric if (Parser.parse(*this, ArgName, Arg, Val))
16760b57cec5SDimitry Andric return true; // Parse Error!
16770b57cec5SDimitry Andric list_storage<DataType, StorageClass>::addValue(Val);
16780b57cec5SDimitry Andric setPosition(pos);
16790b57cec5SDimitry Andric Positions.push_back(pos);
1680480093f4SDimitry Andric Callback(Val);
16810b57cec5SDimitry Andric return false;
16820b57cec5SDimitry Andric }
16830b57cec5SDimitry Andric
16840b57cec5SDimitry Andric // Forward printing stuff to the parser...
16850b57cec5SDimitry Andric size_t getOptionWidth() const override {
16860b57cec5SDimitry Andric return Parser.getOptionWidth(*this);
16870b57cec5SDimitry Andric }
16880b57cec5SDimitry Andric
16890b57cec5SDimitry Andric void printOptionInfo(size_t GlobalWidth) const override {
16900b57cec5SDimitry Andric Parser.printOptionInfo(*this, GlobalWidth);
16910b57cec5SDimitry Andric }
16920b57cec5SDimitry Andric
16930b57cec5SDimitry Andric // Unimplemented: list options don't currently store their default value.
16940b57cec5SDimitry Andric void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
16950b57cec5SDimitry Andric }
16960b57cec5SDimitry Andric
16970b57cec5SDimitry Andric void setDefault() override {
16980b57cec5SDimitry Andric Positions.clear();
16990b57cec5SDimitry Andric list_storage<DataType, StorageClass>::clear();
1700bdd1243dSDimitry Andric for (auto &Val : list_storage<DataType, StorageClass>::getDefault())
1701bdd1243dSDimitry Andric list_storage<DataType, StorageClass>::addValue(Val.getValue());
17020b57cec5SDimitry Andric }
17030b57cec5SDimitry Andric
17040b57cec5SDimitry Andric void done() {
17050b57cec5SDimitry Andric addArgument();
17060b57cec5SDimitry Andric Parser.initialize();
17070b57cec5SDimitry Andric }
17080b57cec5SDimitry Andric
17090b57cec5SDimitry Andric public:
17100b57cec5SDimitry Andric // Command line options should not be copyable
17110b57cec5SDimitry Andric list(const list &) = delete;
17120b57cec5SDimitry Andric list &operator=(const list &) = delete;
17130b57cec5SDimitry Andric
17140b57cec5SDimitry Andric ParserClass &getParser() { return Parser; }
17150b57cec5SDimitry Andric
17160b57cec5SDimitry Andric unsigned getPosition(unsigned optnum) const {
17170b57cec5SDimitry Andric assert(optnum < this->size() && "Invalid option index");
17180b57cec5SDimitry Andric return Positions[optnum];
17190b57cec5SDimitry Andric }
17200b57cec5SDimitry Andric
1721bdd1243dSDimitry Andric void clear() {
1722bdd1243dSDimitry Andric Positions.clear();
1723bdd1243dSDimitry Andric list_storage<DataType, StorageClass>::clear();
1724bdd1243dSDimitry Andric }
1725bdd1243dSDimitry Andric
1726bdd1243dSDimitry Andric // setInitialValues - Used by the cl::list_init modifier...
1727bdd1243dSDimitry Andric void setInitialValues(ArrayRef<DataType> Vs) {
1728bdd1243dSDimitry Andric assert(!(list_storage<DataType, StorageClass>::isDefaultAssigned()) &&
1729bdd1243dSDimitry Andric "Cannot have two default values");
1730bdd1243dSDimitry Andric list_storage<DataType, StorageClass>::assignDefault();
1731bdd1243dSDimitry Andric for (auto &Val : Vs)
1732bdd1243dSDimitry Andric list_storage<DataType, StorageClass>::addValue(Val, true);
1733bdd1243dSDimitry Andric }
1734bdd1243dSDimitry Andric
17350b57cec5SDimitry Andric void setNumAdditionalVals(unsigned n) { Option::setNumAdditionalVals(n); }
17360b57cec5SDimitry Andric
17370b57cec5SDimitry Andric template <class... Mods>
17380b57cec5SDimitry Andric explicit list(const Mods &... Ms)
17390b57cec5SDimitry Andric : Option(ZeroOrMore, NotHidden), Parser(*this) {
17400b57cec5SDimitry Andric apply(this, Ms...);
17410b57cec5SDimitry Andric done();
17420b57cec5SDimitry Andric }
1743480093f4SDimitry Andric
1744480093f4SDimitry Andric void setCallback(
1745480093f4SDimitry Andric std::function<void(const typename ParserClass::parser_data_type &)> CB) {
1746480093f4SDimitry Andric Callback = CB;
1747480093f4SDimitry Andric }
1748480093f4SDimitry Andric
1749480093f4SDimitry Andric std::function<void(const typename ParserClass::parser_data_type &)> Callback =
1750480093f4SDimitry Andric [](const typename ParserClass::parser_data_type &) {};
17510b57cec5SDimitry Andric };
17520b57cec5SDimitry Andric
175381ad6265SDimitry Andric // Modifier to set the number of additional values.
17540b57cec5SDimitry Andric struct multi_val {
17550b57cec5SDimitry Andric unsigned AdditionalVals;
17560b57cec5SDimitry Andric explicit multi_val(unsigned N) : AdditionalVals(N) {}
17570b57cec5SDimitry Andric
17580b57cec5SDimitry Andric template <typename D, typename S, typename P>
17590b57cec5SDimitry Andric void apply(list<D, S, P> &L) const {
17600b57cec5SDimitry Andric L.setNumAdditionalVals(AdditionalVals);
17610b57cec5SDimitry Andric }
17620b57cec5SDimitry Andric };
17630b57cec5SDimitry Andric
17640b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
17650b57cec5SDimitry Andric // Default storage class definition: external storage. This implementation
17660b57cec5SDimitry Andric // assumes the user will specify a variable to store the data into with the
17670b57cec5SDimitry Andric // cl::location(x) modifier.
17680b57cec5SDimitry Andric //
17690b57cec5SDimitry Andric template <class DataType, class StorageClass> class bits_storage {
17700b57cec5SDimitry Andric unsigned *Location = nullptr; // Where to store the bits...
17710b57cec5SDimitry Andric
17720b57cec5SDimitry Andric template <class T> static unsigned Bit(const T &V) {
177381ad6265SDimitry Andric unsigned BitPos = static_cast<unsigned>(V);
17740b57cec5SDimitry Andric assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
17750b57cec5SDimitry Andric "enum exceeds width of bit vector!");
17760b57cec5SDimitry Andric return 1 << BitPos;
17770b57cec5SDimitry Andric }
17780b57cec5SDimitry Andric
17790b57cec5SDimitry Andric public:
17800b57cec5SDimitry Andric bits_storage() = default;
17810b57cec5SDimitry Andric
17820b57cec5SDimitry Andric bool setLocation(Option &O, unsigned &L) {
17830b57cec5SDimitry Andric if (Location)
17840b57cec5SDimitry Andric return O.error("cl::location(x) specified more than once!");
17850b57cec5SDimitry Andric Location = &L;
17860b57cec5SDimitry Andric return false;
17870b57cec5SDimitry Andric }
17880b57cec5SDimitry Andric
17890b57cec5SDimitry Andric template <class T> void addValue(const T &V) {
179004eeddc0SDimitry Andric assert(Location != nullptr &&
179104eeddc0SDimitry Andric "cl::location(...) not specified for a command "
17920b57cec5SDimitry Andric "line option with external storage!");
17930b57cec5SDimitry Andric *Location |= Bit(V);
17940b57cec5SDimitry Andric }
17950b57cec5SDimitry Andric
17960b57cec5SDimitry Andric unsigned getBits() { return *Location; }
17970b57cec5SDimitry Andric
179881ad6265SDimitry Andric void clear() {
179981ad6265SDimitry Andric if (Location)
180081ad6265SDimitry Andric *Location = 0;
180181ad6265SDimitry Andric }
180281ad6265SDimitry Andric
18030b57cec5SDimitry Andric template <class T> bool isSet(const T &V) {
18040b57cec5SDimitry Andric return (*Location & Bit(V)) != 0;
18050b57cec5SDimitry Andric }
18060b57cec5SDimitry Andric };
18070b57cec5SDimitry Andric
18080b57cec5SDimitry Andric // Define how to hold bits. Since we can inherit from a class, we do so.
18090b57cec5SDimitry Andric // This makes us exactly compatible with the bits in all cases that it is used.
18100b57cec5SDimitry Andric //
18110b57cec5SDimitry Andric template <class DataType> class bits_storage<DataType, bool> {
181281ad6265SDimitry Andric unsigned Bits{0}; // Where to store the bits...
18130b57cec5SDimitry Andric
18140b57cec5SDimitry Andric template <class T> static unsigned Bit(const T &V) {
181581ad6265SDimitry Andric unsigned BitPos = static_cast<unsigned>(V);
18160b57cec5SDimitry Andric assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
18170b57cec5SDimitry Andric "enum exceeds width of bit vector!");
18180b57cec5SDimitry Andric return 1 << BitPos;
18190b57cec5SDimitry Andric }
18200b57cec5SDimitry Andric
18210b57cec5SDimitry Andric public:
18220b57cec5SDimitry Andric template <class T> void addValue(const T &V) { Bits |= Bit(V); }
18230b57cec5SDimitry Andric
18240b57cec5SDimitry Andric unsigned getBits() { return Bits; }
18250b57cec5SDimitry Andric
182681ad6265SDimitry Andric void clear() { Bits = 0; }
182781ad6265SDimitry Andric
18280b57cec5SDimitry Andric template <class T> bool isSet(const T &V) { return (Bits & Bit(V)) != 0; }
18290b57cec5SDimitry Andric };
18300b57cec5SDimitry Andric
18310b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
183281ad6265SDimitry Andric // A bit vector of command options.
18330b57cec5SDimitry Andric //
18340b57cec5SDimitry Andric template <class DataType, class Storage = bool,
18350b57cec5SDimitry Andric class ParserClass = parser<DataType>>
18360b57cec5SDimitry Andric class bits : public Option, public bits_storage<DataType, Storage> {
18370b57cec5SDimitry Andric std::vector<unsigned> Positions;
18380b57cec5SDimitry Andric ParserClass Parser;
18390b57cec5SDimitry Andric
18400b57cec5SDimitry Andric enum ValueExpected getValueExpectedFlagDefault() const override {
18410b57cec5SDimitry Andric return Parser.getValueExpectedFlagDefault();
18420b57cec5SDimitry Andric }
18430b57cec5SDimitry Andric
18440b57cec5SDimitry Andric void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) override {
18450b57cec5SDimitry Andric return Parser.getExtraOptionNames(OptionNames);
18460b57cec5SDimitry Andric }
18470b57cec5SDimitry Andric
18480b57cec5SDimitry Andric bool handleOccurrence(unsigned pos, StringRef ArgName,
18490b57cec5SDimitry Andric StringRef Arg) override {
18500b57cec5SDimitry Andric typename ParserClass::parser_data_type Val =
18510b57cec5SDimitry Andric typename ParserClass::parser_data_type();
18520b57cec5SDimitry Andric if (Parser.parse(*this, ArgName, Arg, Val))
18530b57cec5SDimitry Andric return true; // Parse Error!
18540b57cec5SDimitry Andric this->addValue(Val);
18550b57cec5SDimitry Andric setPosition(pos);
18560b57cec5SDimitry Andric Positions.push_back(pos);
1857480093f4SDimitry Andric Callback(Val);
18580b57cec5SDimitry Andric return false;
18590b57cec5SDimitry Andric }
18600b57cec5SDimitry Andric
18610b57cec5SDimitry Andric // Forward printing stuff to the parser...
18620b57cec5SDimitry Andric size_t getOptionWidth() const override {
18630b57cec5SDimitry Andric return Parser.getOptionWidth(*this);
18640b57cec5SDimitry Andric }
18650b57cec5SDimitry Andric
18660b57cec5SDimitry Andric void printOptionInfo(size_t GlobalWidth) const override {
18670b57cec5SDimitry Andric Parser.printOptionInfo(*this, GlobalWidth);
18680b57cec5SDimitry Andric }
18690b57cec5SDimitry Andric
18700b57cec5SDimitry Andric // Unimplemented: bits options don't currently store their default values.
18710b57cec5SDimitry Andric void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
18720b57cec5SDimitry Andric }
18730b57cec5SDimitry Andric
187481ad6265SDimitry Andric void setDefault() override { bits_storage<DataType, Storage>::clear(); }
18750b57cec5SDimitry Andric
18760b57cec5SDimitry Andric void done() {
18770b57cec5SDimitry Andric addArgument();
18780b57cec5SDimitry Andric Parser.initialize();
18790b57cec5SDimitry Andric }
18800b57cec5SDimitry Andric
18810b57cec5SDimitry Andric public:
18820b57cec5SDimitry Andric // Command line options should not be copyable
18830b57cec5SDimitry Andric bits(const bits &) = delete;
18840b57cec5SDimitry Andric bits &operator=(const bits &) = delete;
18850b57cec5SDimitry Andric
18860b57cec5SDimitry Andric ParserClass &getParser() { return Parser; }
18870b57cec5SDimitry Andric
18880b57cec5SDimitry Andric unsigned getPosition(unsigned optnum) const {
18890b57cec5SDimitry Andric assert(optnum < this->size() && "Invalid option index");
18900b57cec5SDimitry Andric return Positions[optnum];
18910b57cec5SDimitry Andric }
18920b57cec5SDimitry Andric
18930b57cec5SDimitry Andric template <class... Mods>
18940b57cec5SDimitry Andric explicit bits(const Mods &... Ms)
18950b57cec5SDimitry Andric : Option(ZeroOrMore, NotHidden), Parser(*this) {
18960b57cec5SDimitry Andric apply(this, Ms...);
18970b57cec5SDimitry Andric done();
18980b57cec5SDimitry Andric }
1899480093f4SDimitry Andric
1900480093f4SDimitry Andric void setCallback(
1901480093f4SDimitry Andric std::function<void(const typename ParserClass::parser_data_type &)> CB) {
1902480093f4SDimitry Andric Callback = CB;
1903480093f4SDimitry Andric }
1904480093f4SDimitry Andric
1905480093f4SDimitry Andric std::function<void(const typename ParserClass::parser_data_type &)> Callback =
1906480093f4SDimitry Andric [](const typename ParserClass::parser_data_type &) {};
19070b57cec5SDimitry Andric };
19080b57cec5SDimitry Andric
19090b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
19100b57cec5SDimitry Andric // Aliased command line option (alias this name to a preexisting name)
19110b57cec5SDimitry Andric //
19120b57cec5SDimitry Andric
19130b57cec5SDimitry Andric class alias : public Option {
19140b57cec5SDimitry Andric Option *AliasFor;
19150b57cec5SDimitry Andric
19160b57cec5SDimitry Andric bool handleOccurrence(unsigned pos, StringRef /*ArgName*/,
19170b57cec5SDimitry Andric StringRef Arg) override {
19180b57cec5SDimitry Andric return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
19190b57cec5SDimitry Andric }
19200b57cec5SDimitry Andric
19210b57cec5SDimitry Andric bool addOccurrence(unsigned pos, StringRef /*ArgName*/, StringRef Value,
19220b57cec5SDimitry Andric bool MultiArg = false) override {
19230b57cec5SDimitry Andric return AliasFor->addOccurrence(pos, AliasFor->ArgStr, Value, MultiArg);
19240b57cec5SDimitry Andric }
19250b57cec5SDimitry Andric
19260b57cec5SDimitry Andric // Handle printing stuff...
19270b57cec5SDimitry Andric size_t getOptionWidth() const override;
19280b57cec5SDimitry Andric void printOptionInfo(size_t GlobalWidth) const override;
19290b57cec5SDimitry Andric
19300b57cec5SDimitry Andric // Aliases do not need to print their values.
19310b57cec5SDimitry Andric void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
19320b57cec5SDimitry Andric }
19330b57cec5SDimitry Andric
19340b57cec5SDimitry Andric void setDefault() override { AliasFor->setDefault(); }
19350b57cec5SDimitry Andric
19360b57cec5SDimitry Andric ValueExpected getValueExpectedFlagDefault() const override {
19370b57cec5SDimitry Andric return AliasFor->getValueExpectedFlag();
19380b57cec5SDimitry Andric }
19390b57cec5SDimitry Andric
19400b57cec5SDimitry Andric void done() {
19410b57cec5SDimitry Andric if (!hasArgStr())
19420b57cec5SDimitry Andric error("cl::alias must have argument name specified!");
19430b57cec5SDimitry Andric if (!AliasFor)
19440b57cec5SDimitry Andric error("cl::alias must have an cl::aliasopt(option) specified!");
19450b57cec5SDimitry Andric if (!Subs.empty())
19460b57cec5SDimitry Andric error("cl::alias must not have cl::sub(), aliased option's cl::sub() will be used!");
19470b57cec5SDimitry Andric Subs = AliasFor->Subs;
19480b57cec5SDimitry Andric Categories = AliasFor->Categories;
19490b57cec5SDimitry Andric addArgument();
19500b57cec5SDimitry Andric }
19510b57cec5SDimitry Andric
19520b57cec5SDimitry Andric public:
19530b57cec5SDimitry Andric // Command line options should not be copyable
19540b57cec5SDimitry Andric alias(const alias &) = delete;
19550b57cec5SDimitry Andric alias &operator=(const alias &) = delete;
19560b57cec5SDimitry Andric
19570b57cec5SDimitry Andric void setAliasFor(Option &O) {
19580b57cec5SDimitry Andric if (AliasFor)
19590b57cec5SDimitry Andric error("cl::alias must only have one cl::aliasopt(...) specified!");
19600b57cec5SDimitry Andric AliasFor = &O;
19610b57cec5SDimitry Andric }
19620b57cec5SDimitry Andric
19630b57cec5SDimitry Andric template <class... Mods>
19640b57cec5SDimitry Andric explicit alias(const Mods &... Ms)
19650b57cec5SDimitry Andric : Option(Optional, Hidden), AliasFor(nullptr) {
19660b57cec5SDimitry Andric apply(this, Ms...);
19670b57cec5SDimitry Andric done();
19680b57cec5SDimitry Andric }
19690b57cec5SDimitry Andric };
19700b57cec5SDimitry Andric
197181ad6265SDimitry Andric // Modifier to set the option an alias aliases.
19720b57cec5SDimitry Andric struct aliasopt {
19730b57cec5SDimitry Andric Option &Opt;
19740b57cec5SDimitry Andric
19750b57cec5SDimitry Andric explicit aliasopt(Option &O) : Opt(O) {}
19760b57cec5SDimitry Andric
19770b57cec5SDimitry Andric void apply(alias &A) const { A.setAliasFor(Opt); }
19780b57cec5SDimitry Andric };
19790b57cec5SDimitry Andric
198081ad6265SDimitry Andric // Provide additional help at the end of the normal help output. All occurrences
198181ad6265SDimitry Andric // of cl::extrahelp will be accumulated and printed to stderr at the end of the
198281ad6265SDimitry Andric // regular help, just before exit is called.
19830b57cec5SDimitry Andric struct extrahelp {
19840b57cec5SDimitry Andric StringRef morehelp;
19850b57cec5SDimitry Andric
19860b57cec5SDimitry Andric explicit extrahelp(StringRef help);
19870b57cec5SDimitry Andric };
19880b57cec5SDimitry Andric
19890b57cec5SDimitry Andric void PrintVersionMessage();
19900b57cec5SDimitry Andric
19910b57cec5SDimitry Andric /// This function just prints the help message, exactly the same way as if the
19920b57cec5SDimitry Andric /// -help or -help-hidden option had been given on the command line.
19930b57cec5SDimitry Andric ///
19940b57cec5SDimitry Andric /// \param Hidden if true will print hidden options
19950b57cec5SDimitry Andric /// \param Categorized if true print options in categories
19960b57cec5SDimitry Andric void PrintHelpMessage(bool Hidden = false, bool Categorized = false);
19970b57cec5SDimitry Andric
1998*0fca6ea1SDimitry Andric /// An array of optional enabled settings in the LLVM build configuration,
1999*0fca6ea1SDimitry Andric /// which may be of interest to compiler developers. For example, includes
2000*0fca6ea1SDimitry Andric /// "+assertions" if assertions are enabled. Used by printBuildConfig.
2001*0fca6ea1SDimitry Andric ArrayRef<StringRef> getCompilerBuildConfig();
2002*0fca6ea1SDimitry Andric
2003*0fca6ea1SDimitry Andric /// Prints the compiler build configuration.
2004*0fca6ea1SDimitry Andric /// Designed for compiler developers, not compiler end-users.
2005*0fca6ea1SDimitry Andric /// Intended to be used in --version output when enabled.
2006*0fca6ea1SDimitry Andric void printBuildConfig(raw_ostream &OS);
2007*0fca6ea1SDimitry Andric
20080b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
20090b57cec5SDimitry Andric // Public interface for accessing registered options.
20100b57cec5SDimitry Andric //
20110b57cec5SDimitry Andric
20120b57cec5SDimitry Andric /// Use this to get a StringMap to all registered named options
2013480093f4SDimitry Andric /// (e.g. -help).
20140b57cec5SDimitry Andric ///
20150b57cec5SDimitry Andric /// \return A reference to the StringMap used by the cl APIs to parse options.
20160b57cec5SDimitry Andric ///
20170b57cec5SDimitry Andric /// Access to unnamed arguments (i.e. positional) are not provided because
20180b57cec5SDimitry Andric /// it is expected that the client already has access to these.
20190b57cec5SDimitry Andric ///
20200b57cec5SDimitry Andric /// Typical usage:
20210b57cec5SDimitry Andric /// \code
20220b57cec5SDimitry Andric /// main(int argc,char* argv[]) {
20230b57cec5SDimitry Andric /// StringMap<llvm::cl::Option*> &opts = llvm::cl::getRegisteredOptions();
20240b57cec5SDimitry Andric /// assert(opts.count("help") == 1)
20250b57cec5SDimitry Andric /// opts["help"]->setDescription("Show alphabetical help information")
20260b57cec5SDimitry Andric /// // More code
20270b57cec5SDimitry Andric /// llvm::cl::ParseCommandLineOptions(argc,argv);
20280b57cec5SDimitry Andric /// //More code
20290b57cec5SDimitry Andric /// }
20300b57cec5SDimitry Andric /// \endcode
20310b57cec5SDimitry Andric ///
20320b57cec5SDimitry Andric /// This interface is useful for modifying options in libraries that are out of
20330b57cec5SDimitry Andric /// the control of the client. The options should be modified before calling
20340b57cec5SDimitry Andric /// llvm::cl::ParseCommandLineOptions().
20350b57cec5SDimitry Andric ///
20360b57cec5SDimitry Andric /// Hopefully this API can be deprecated soon. Any situation where options need
20370b57cec5SDimitry Andric /// to be modified by tools or libraries should be handled by sane APIs rather
20380b57cec5SDimitry Andric /// than just handing around a global list.
2039bdd1243dSDimitry Andric StringMap<Option *> &
2040bdd1243dSDimitry Andric getRegisteredOptions(SubCommand &Sub = SubCommand::getTopLevel());
20410b57cec5SDimitry Andric
20420b57cec5SDimitry Andric /// Use this to get all registered SubCommands from the provided parser.
20430b57cec5SDimitry Andric ///
20440b57cec5SDimitry Andric /// \return A range of all SubCommand pointers registered with the parser.
20450b57cec5SDimitry Andric ///
20460b57cec5SDimitry Andric /// Typical usage:
20470b57cec5SDimitry Andric /// \code
20480b57cec5SDimitry Andric /// main(int argc, char* argv[]) {
20490b57cec5SDimitry Andric /// llvm::cl::ParseCommandLineOptions(argc, argv);
20500b57cec5SDimitry Andric /// for (auto* S : llvm::cl::getRegisteredSubcommands()) {
20510b57cec5SDimitry Andric /// if (*S) {
20520b57cec5SDimitry Andric /// std::cout << "Executing subcommand: " << S->getName() << std::endl;
20530b57cec5SDimitry Andric /// // Execute some function based on the name...
20540b57cec5SDimitry Andric /// }
20550b57cec5SDimitry Andric /// }
20560b57cec5SDimitry Andric /// }
20570b57cec5SDimitry Andric /// \endcode
20580b57cec5SDimitry Andric ///
20590b57cec5SDimitry Andric /// This interface is useful for defining subcommands in libraries and
20600b57cec5SDimitry Andric /// the dispatch from a single point (like in the main function).
20610b57cec5SDimitry Andric iterator_range<typename SmallPtrSet<SubCommand *, 4>::iterator>
20620b57cec5SDimitry Andric getRegisteredSubcommands();
20630b57cec5SDimitry Andric
20640b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
20650b57cec5SDimitry Andric // Standalone command line processing utilities.
20660b57cec5SDimitry Andric //
20670b57cec5SDimitry Andric
20680b57cec5SDimitry Andric /// Tokenizes a command line that can contain escapes and quotes.
20690b57cec5SDimitry Andric //
20700b57cec5SDimitry Andric /// The quoting rules match those used by GCC and other tools that use
20710b57cec5SDimitry Andric /// libiberty's buildargv() or expandargv() utilities, and do not match bash.
20720b57cec5SDimitry Andric /// They differ from buildargv() on treatment of backslashes that do not escape
20730b57cec5SDimitry Andric /// a special character to make it possible to accept most Windows file paths.
20740b57cec5SDimitry Andric ///
20750b57cec5SDimitry Andric /// \param [in] Source The string to be split on whitespace with quotes.
20760b57cec5SDimitry Andric /// \param [in] Saver Delegates back to the caller for saving parsed strings.
20770b57cec5SDimitry Andric /// \param [in] MarkEOLs true if tokenizing a response file and you want end of
20780b57cec5SDimitry Andric /// lines and end of the response file to be marked with a nullptr string.
20790b57cec5SDimitry Andric /// \param [out] NewArgv All parsed strings are appended to NewArgv.
20800b57cec5SDimitry Andric void TokenizeGNUCommandLine(StringRef Source, StringSaver &Saver,
20810b57cec5SDimitry Andric SmallVectorImpl<const char *> &NewArgv,
20820b57cec5SDimitry Andric bool MarkEOLs = false);
20830b57cec5SDimitry Andric
208481ad6265SDimitry Andric /// Tokenizes a string of Windows command line arguments, which may contain
208581ad6265SDimitry Andric /// quotes and escaped quotes.
20860b57cec5SDimitry Andric ///
20870b57cec5SDimitry Andric /// See MSDN docs for CommandLineToArgvW for information on the quoting rules.
20880b57cec5SDimitry Andric /// http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx
20890b57cec5SDimitry Andric ///
209081ad6265SDimitry Andric /// For handling a full Windows command line including the executable name at
209181ad6265SDimitry Andric /// the start, see TokenizeWindowsCommandLineFull below.
209281ad6265SDimitry Andric ///
20930b57cec5SDimitry Andric /// \param [in] Source The string to be split on whitespace with quotes.
20940b57cec5SDimitry Andric /// \param [in] Saver Delegates back to the caller for saving parsed strings.
20950b57cec5SDimitry Andric /// \param [in] MarkEOLs true if tokenizing a response file and you want end of
20960b57cec5SDimitry Andric /// lines and end of the response file to be marked with a nullptr string.
20970b57cec5SDimitry Andric /// \param [out] NewArgv All parsed strings are appended to NewArgv.
20980b57cec5SDimitry Andric void TokenizeWindowsCommandLine(StringRef Source, StringSaver &Saver,
20990b57cec5SDimitry Andric SmallVectorImpl<const char *> &NewArgv,
21000b57cec5SDimitry Andric bool MarkEOLs = false);
21010b57cec5SDimitry Andric
21025ffd83dbSDimitry Andric /// Tokenizes a Windows command line while attempting to avoid copies. If no
21035ffd83dbSDimitry Andric /// quoting or escaping was used, this produces substrings of the original
21045ffd83dbSDimitry Andric /// string. If a token requires unquoting, it will be allocated with the
21055ffd83dbSDimitry Andric /// StringSaver.
21065ffd83dbSDimitry Andric void TokenizeWindowsCommandLineNoCopy(StringRef Source, StringSaver &Saver,
21075ffd83dbSDimitry Andric SmallVectorImpl<StringRef> &NewArgv);
21085ffd83dbSDimitry Andric
210981ad6265SDimitry Andric /// Tokenizes a Windows full command line, including command name at the start.
211081ad6265SDimitry Andric ///
211181ad6265SDimitry Andric /// This uses the same syntax rules as TokenizeWindowsCommandLine for all but
211281ad6265SDimitry Andric /// the first token. But the first token is expected to be parsed as the
211381ad6265SDimitry Andric /// executable file name in the way CreateProcess would do it, rather than the
211481ad6265SDimitry Andric /// way the C library startup code would do it: CreateProcess does not consider
211581ad6265SDimitry Andric /// that \ is ever an escape character (because " is not a valid filename char,
211681ad6265SDimitry Andric /// hence there's never a need to escape it to be used literally).
211781ad6265SDimitry Andric ///
211881ad6265SDimitry Andric /// Parameters are the same as for TokenizeWindowsCommandLine. In particular,
211981ad6265SDimitry Andric /// if you set MarkEOLs = true, then the first word of every line will be
212081ad6265SDimitry Andric /// parsed using the special rules for command names, making this function
212181ad6265SDimitry Andric /// suitable for parsing a file full of commands to execute.
212281ad6265SDimitry Andric void TokenizeWindowsCommandLineFull(StringRef Source, StringSaver &Saver,
212381ad6265SDimitry Andric SmallVectorImpl<const char *> &NewArgv,
212481ad6265SDimitry Andric bool MarkEOLs = false);
212581ad6265SDimitry Andric
21260b57cec5SDimitry Andric /// String tokenization function type. Should be compatible with either
21270b57cec5SDimitry Andric /// Windows or Unix command line tokenizers.
21280b57cec5SDimitry Andric using TokenizerCallback = void (*)(StringRef Source, StringSaver &Saver,
21290b57cec5SDimitry Andric SmallVectorImpl<const char *> &NewArgv,
21300b57cec5SDimitry Andric bool MarkEOLs);
21310b57cec5SDimitry Andric
21320b57cec5SDimitry Andric /// Tokenizes content of configuration file.
21330b57cec5SDimitry Andric ///
21340b57cec5SDimitry Andric /// \param [in] Source The string representing content of config file.
21350b57cec5SDimitry Andric /// \param [in] Saver Delegates back to the caller for saving parsed strings.
21360b57cec5SDimitry Andric /// \param [out] NewArgv All parsed strings are appended to NewArgv.
21370b57cec5SDimitry Andric /// \param [in] MarkEOLs Added for compatibility with TokenizerCallback.
21380b57cec5SDimitry Andric ///
21390b57cec5SDimitry Andric /// It works like TokenizeGNUCommandLine with ability to skip comment lines.
21400b57cec5SDimitry Andric ///
21410b57cec5SDimitry Andric void tokenizeConfigFile(StringRef Source, StringSaver &Saver,
21420b57cec5SDimitry Andric SmallVectorImpl<const char *> &NewArgv,
21430b57cec5SDimitry Andric bool MarkEOLs = false);
21440b57cec5SDimitry Andric
2145bdd1243dSDimitry Andric /// Contains options that control response file expansion.
2146bdd1243dSDimitry Andric class ExpansionContext {
2147bdd1243dSDimitry Andric /// Provides persistent storage for parsed strings.
2148bdd1243dSDimitry Andric StringSaver Saver;
2149bdd1243dSDimitry Andric
2150bdd1243dSDimitry Andric /// Tokenization strategy. Typically Unix or Windows.
2151bdd1243dSDimitry Andric TokenizerCallback Tokenizer;
2152bdd1243dSDimitry Andric
2153bdd1243dSDimitry Andric /// File system used for all file access when running the expansion.
2154bdd1243dSDimitry Andric vfs::FileSystem *FS;
2155bdd1243dSDimitry Andric
2156bdd1243dSDimitry Andric /// Path used to resolve relative rsp files. If empty, the file system
2157bdd1243dSDimitry Andric /// current directory is used instead.
2158bdd1243dSDimitry Andric StringRef CurrentDir;
2159bdd1243dSDimitry Andric
2160bdd1243dSDimitry Andric /// Directories used for search of config files.
2161bdd1243dSDimitry Andric ArrayRef<StringRef> SearchDirs;
2162bdd1243dSDimitry Andric
2163bdd1243dSDimitry Andric /// True if names of nested response files must be resolved relative to
2164bdd1243dSDimitry Andric /// including file.
2165bdd1243dSDimitry Andric bool RelativeNames = false;
2166bdd1243dSDimitry Andric
2167bdd1243dSDimitry Andric /// If true, mark end of lines and the end of the response file with nullptrs
2168bdd1243dSDimitry Andric /// in the Argv vector.
2169bdd1243dSDimitry Andric bool MarkEOLs = false;
2170bdd1243dSDimitry Andric
2171bdd1243dSDimitry Andric /// If true, body of config file is expanded.
2172bdd1243dSDimitry Andric bool InConfigFile = false;
2173bdd1243dSDimitry Andric
2174bdd1243dSDimitry Andric llvm::Error expandResponseFile(StringRef FName,
2175bdd1243dSDimitry Andric SmallVectorImpl<const char *> &NewArgv);
2176bdd1243dSDimitry Andric
2177bdd1243dSDimitry Andric public:
2178bdd1243dSDimitry Andric ExpansionContext(BumpPtrAllocator &A, TokenizerCallback T);
2179bdd1243dSDimitry Andric
2180bdd1243dSDimitry Andric ExpansionContext &setMarkEOLs(bool X) {
2181bdd1243dSDimitry Andric MarkEOLs = X;
2182bdd1243dSDimitry Andric return *this;
2183bdd1243dSDimitry Andric }
2184bdd1243dSDimitry Andric
2185bdd1243dSDimitry Andric ExpansionContext &setRelativeNames(bool X) {
2186bdd1243dSDimitry Andric RelativeNames = X;
2187bdd1243dSDimitry Andric return *this;
2188bdd1243dSDimitry Andric }
2189bdd1243dSDimitry Andric
2190bdd1243dSDimitry Andric ExpansionContext &setCurrentDir(StringRef X) {
2191bdd1243dSDimitry Andric CurrentDir = X;
2192bdd1243dSDimitry Andric return *this;
2193bdd1243dSDimitry Andric }
2194bdd1243dSDimitry Andric
2195bdd1243dSDimitry Andric ExpansionContext &setSearchDirs(ArrayRef<StringRef> X) {
2196bdd1243dSDimitry Andric SearchDirs = X;
2197bdd1243dSDimitry Andric return *this;
2198bdd1243dSDimitry Andric }
2199bdd1243dSDimitry Andric
2200bdd1243dSDimitry Andric ExpansionContext &setVFS(vfs::FileSystem *X) {
2201bdd1243dSDimitry Andric FS = X;
2202bdd1243dSDimitry Andric return *this;
2203bdd1243dSDimitry Andric }
2204bdd1243dSDimitry Andric
2205bdd1243dSDimitry Andric /// Looks for the specified configuration file.
2206bdd1243dSDimitry Andric ///
2207bdd1243dSDimitry Andric /// \param[in] FileName Name of the file to search for.
2208bdd1243dSDimitry Andric /// \param[out] FilePath File absolute path, if it was found.
2209bdd1243dSDimitry Andric /// \return True if file was found.
2210bdd1243dSDimitry Andric ///
2211bdd1243dSDimitry Andric /// If the specified file name contains a directory separator, it is searched
2212bdd1243dSDimitry Andric /// for by its absolute path. Otherwise looks for file sequentially in
2213bdd1243dSDimitry Andric /// directories specified by SearchDirs field.
2214bdd1243dSDimitry Andric bool findConfigFile(StringRef FileName, SmallVectorImpl<char> &FilePath);
2215bdd1243dSDimitry Andric
22160b57cec5SDimitry Andric /// Reads command line options from the given configuration file.
22170b57cec5SDimitry Andric ///
2218bdd1243dSDimitry Andric /// \param [in] CfgFile Path to configuration file.
22190b57cec5SDimitry Andric /// \param [out] Argv Array to which the read options are added.
22200b57cec5SDimitry Andric /// \return true if the file was successfully read.
22210b57cec5SDimitry Andric ///
22220b57cec5SDimitry Andric /// It reads content of the specified file, tokenizes it and expands "@file"
22230b57cec5SDimitry Andric /// commands resolving file names in them relative to the directory where
222404eeddc0SDimitry Andric /// CfgFilename resides. It also expands "<CFGDIR>" to the base path of the
222504eeddc0SDimitry Andric /// current config file.
2226bdd1243dSDimitry Andric Error readConfigFile(StringRef CfgFile, SmallVectorImpl<const char *> &Argv);
22270b57cec5SDimitry Andric
2228bdd1243dSDimitry Andric /// Expands constructs "@file" in the provided array of arguments recursively.
2229bdd1243dSDimitry Andric Error expandResponseFiles(SmallVectorImpl<const char *> &Argv);
2230bdd1243dSDimitry Andric };
2231bdd1243dSDimitry Andric
2232bdd1243dSDimitry Andric /// A convenience helper which concatenates the options specified by the
2233bdd1243dSDimitry Andric /// environment variable EnvVar and command line options, then expands
2234bdd1243dSDimitry Andric /// response files recursively.
22350b57cec5SDimitry Andric /// \return true if all @files were expanded successfully or there were none.
2236bdd1243dSDimitry Andric bool expandResponseFiles(int Argc, const char *const *Argv, const char *EnvVar,
2237bdd1243dSDimitry Andric SmallVectorImpl<const char *> &NewArgv);
2238fe6060f1SDimitry Andric
2239bdd1243dSDimitry Andric /// A convenience helper which supports the typical use case of expansion
2240bdd1243dSDimitry Andric /// function call.
2241bdd1243dSDimitry Andric bool ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer,
2242bdd1243dSDimitry Andric SmallVectorImpl<const char *> &Argv);
22430b57cec5SDimitry Andric
2244e8d8bef9SDimitry Andric /// A convenience helper which concatenates the options specified by the
2245e8d8bef9SDimitry Andric /// environment variable EnvVar and command line options, then expands response
2246e8d8bef9SDimitry Andric /// files recursively. The tokenizer is a predefined GNU or Windows one.
2247e8d8bef9SDimitry Andric /// \return true if all @files were expanded successfully or there were none.
2248e8d8bef9SDimitry Andric bool expandResponseFiles(int Argc, const char *const *Argv, const char *EnvVar,
2249e8d8bef9SDimitry Andric StringSaver &Saver,
2250e8d8bef9SDimitry Andric SmallVectorImpl<const char *> &NewArgv);
2251e8d8bef9SDimitry Andric
22520b57cec5SDimitry Andric /// Mark all options not part of this category as cl::ReallyHidden.
22530b57cec5SDimitry Andric ///
22540b57cec5SDimitry Andric /// \param Category the category of options to keep displaying
22550b57cec5SDimitry Andric ///
22560b57cec5SDimitry Andric /// Some tools (like clang-format) like to be able to hide all options that are
22570b57cec5SDimitry Andric /// not specific to the tool. This function allows a tool to specify a single
22580b57cec5SDimitry Andric /// option category to display in the -help output.
22590b57cec5SDimitry Andric void HideUnrelatedOptions(cl::OptionCategory &Category,
2260bdd1243dSDimitry Andric SubCommand &Sub = SubCommand::getTopLevel());
22610b57cec5SDimitry Andric
22620b57cec5SDimitry Andric /// Mark all options not part of the categories as cl::ReallyHidden.
22630b57cec5SDimitry Andric ///
22640b57cec5SDimitry Andric /// \param Categories the categories of options to keep displaying.
22650b57cec5SDimitry Andric ///
22660b57cec5SDimitry Andric /// Some tools (like clang-format) like to be able to hide all options that are
22670b57cec5SDimitry Andric /// not specific to the tool. This function allows a tool to specify a single
22680b57cec5SDimitry Andric /// option category to display in the -help output.
22690b57cec5SDimitry Andric void HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *> Categories,
2270bdd1243dSDimitry Andric SubCommand &Sub = SubCommand::getTopLevel());
22710b57cec5SDimitry Andric
22720b57cec5SDimitry Andric /// Reset all command line options to a state that looks as if they have
22730b57cec5SDimitry Andric /// never appeared on the command line. This is useful for being able to parse
22740b57cec5SDimitry Andric /// a command line multiple times (especially useful for writing tests).
22750b57cec5SDimitry Andric void ResetAllOptionOccurrences();
22760b57cec5SDimitry Andric
22770b57cec5SDimitry Andric /// Reset the command line parser back to its initial state. This
22780b57cec5SDimitry Andric /// removes
22790b57cec5SDimitry Andric /// all options, categories, and subcommands and returns the parser to a state
22800b57cec5SDimitry Andric /// where no options are supported.
22810b57cec5SDimitry Andric void ResetCommandLineParser();
22820b57cec5SDimitry Andric
22838bcb0991SDimitry Andric /// Parses `Arg` into the option handler `Handler`.
22848bcb0991SDimitry Andric bool ProvidePositionalOption(Option *Handler, StringRef Arg, int i);
22858bcb0991SDimitry Andric
22860b57cec5SDimitry Andric } // end namespace cl
22870b57cec5SDimitry Andric
22880b57cec5SDimitry Andric } // end namespace llvm
22890b57cec5SDimitry Andric
22900b57cec5SDimitry Andric #endif // LLVM_SUPPORT_COMMANDLINE_H
2291