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"
310b57cec5SDimitry Andric #include "llvm/Support/ManagedStatic.h"
32bdd1243dSDimitry Andric #include "llvm/Support/StringSaver.h"
330b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
340b57cec5SDimitry Andric #include <cassert>
350b57cec5SDimitry Andric #include <climits>
360b57cec5SDimitry Andric #include <cstddef>
370b57cec5SDimitry Andric #include <functional>
380b57cec5SDimitry Andric #include <initializer_list>
390b57cec5SDimitry Andric #include <string>
400b57cec5SDimitry Andric #include <type_traits>
410b57cec5SDimitry Andric #include <vector>
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric namespace llvm {
440b57cec5SDimitry Andric 
45fe6060f1SDimitry Andric namespace vfs {
46fe6060f1SDimitry Andric class FileSystem;
47fe6060f1SDimitry Andric }
48fe6060f1SDimitry Andric 
490b57cec5SDimitry Andric class StringSaver;
500b57cec5SDimitry Andric 
5181ad6265SDimitry Andric /// This namespace contains all of the command line option processing machinery.
5281ad6265SDimitry Andric /// It is intentionally a short name to make qualified usage concise.
530b57cec5SDimitry Andric namespace cl {
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
5681ad6265SDimitry Andric // Command line option processing entry point.
570b57cec5SDimitry Andric //
580b57cec5SDimitry Andric // Returns true on success. Otherwise, this will print the error message to
590b57cec5SDimitry Andric // stderr and exit if \p Errs is not set (nullptr by default), or print the
600b57cec5SDimitry Andric // error message to \p Errs and return false if \p Errs is provided.
610b57cec5SDimitry Andric //
620b57cec5SDimitry Andric // If EnvVar is not nullptr, command-line options are also parsed from the
630b57cec5SDimitry Andric // environment variable named by EnvVar.  Precedence is given to occurrences
640b57cec5SDimitry Andric // from argv.  This precedence is currently implemented by parsing argv after
650b57cec5SDimitry Andric // the environment variable, so it is only implemented correctly for options
660b57cec5SDimitry Andric // that give precedence to later occurrences.  If your program supports options
670b57cec5SDimitry Andric // that give precedence to earlier occurrences, you will need to extend this
680b57cec5SDimitry Andric // function to support it correctly.
690b57cec5SDimitry Andric bool ParseCommandLineOptions(int argc, const char *const *argv,
700b57cec5SDimitry Andric                              StringRef Overview = "",
710b57cec5SDimitry Andric                              raw_ostream *Errs = nullptr,
720b57cec5SDimitry Andric                              const char *EnvVar = nullptr,
730b57cec5SDimitry Andric                              bool LongOptionsUseDoubleDash = false);
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric // Function pointer type for printing version information.
760b57cec5SDimitry Andric using VersionPrinterTy = std::function<void(raw_ostream &)>;
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric ///===---------------------------------------------------------------------===//
7981ad6265SDimitry Andric /// Override the default (LLVM specific) version printer used to print out the
8081ad6265SDimitry Andric /// version when --version is given on the command line. This allows other
8181ad6265SDimitry Andric /// systems using the CommandLine utilities to print their own version string.
820b57cec5SDimitry Andric void SetVersionPrinter(VersionPrinterTy func);
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric ///===---------------------------------------------------------------------===//
8581ad6265SDimitry Andric /// Add an extra printer to use in addition to the default one. This can be
8681ad6265SDimitry Andric /// called multiple times, and each time it adds a new function to the list
8781ad6265SDimitry Andric /// which will be called after the basic LLVM version printing is complete.
8881ad6265SDimitry Andric /// Each can then add additional information specific to the tool.
890b57cec5SDimitry Andric void AddExtraVersionPrinter(VersionPrinterTy func);
900b57cec5SDimitry Andric 
9181ad6265SDimitry Andric // Print option values.
920b57cec5SDimitry Andric // With -print-options print the difference between option values and defaults.
930b57cec5SDimitry Andric // With -print-all-options print all option values.
940b57cec5SDimitry Andric // (Currently not perfect, but best-effort.)
950b57cec5SDimitry Andric void PrintOptionValues();
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric // Forward declaration - AddLiteralOption needs to be up here to make gcc happy.
980b57cec5SDimitry Andric class Option;
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric /// Adds a new option for parsing and provides the option it refers to.
1010b57cec5SDimitry Andric ///
1020b57cec5SDimitry Andric /// \param O pointer to the option
1030b57cec5SDimitry Andric /// \param Name the string name for the option to handle during parsing
1040b57cec5SDimitry Andric ///
1050b57cec5SDimitry Andric /// Literal options are used by some parsers to register special option values.
1060b57cec5SDimitry Andric /// This is how the PassNameParser registers pass names for opt.
1070b57cec5SDimitry Andric void AddLiteralOption(Option &O, StringRef Name);
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1100b57cec5SDimitry Andric // Flags permitted to be passed to command line arguments
1110b57cec5SDimitry Andric //
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric enum NumOccurrencesFlag { // Flags for the number of occurrences allowed
1140b57cec5SDimitry Andric   Optional = 0x00,        // Zero or One occurrence
1150b57cec5SDimitry Andric   ZeroOrMore = 0x01,      // Zero or more occurrences allowed
1160b57cec5SDimitry Andric   Required = 0x02,        // One occurrence required
1170b57cec5SDimitry Andric   OneOrMore = 0x03,       // One or more occurrences required
1180b57cec5SDimitry Andric 
11981ad6265SDimitry Andric   // Indicates that this option is fed anything that follows the last positional
12081ad6265SDimitry Andric   // argument required by the application (it is an error if there are zero
12181ad6265SDimitry Andric   // positional arguments, and a ConsumeAfter option is used).
1220b57cec5SDimitry Andric   // Thus, for example, all arguments to LLI are processed until a filename is
1230b57cec5SDimitry Andric   // found.  Once a filename is found, all of the succeeding arguments are
1240b57cec5SDimitry Andric   // passed, unprocessed, to the ConsumeAfter option.
1250b57cec5SDimitry Andric   //
1260b57cec5SDimitry Andric   ConsumeAfter = 0x04
1270b57cec5SDimitry Andric };
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric enum ValueExpected { // Is a value required for the option?
1300b57cec5SDimitry Andric   // zero reserved for the unspecified value
1310b57cec5SDimitry Andric   ValueOptional = 0x01,  // The value can appear... or not
1320b57cec5SDimitry Andric   ValueRequired = 0x02,  // The value is required to appear!
1330b57cec5SDimitry Andric   ValueDisallowed = 0x03 // A value may not be specified (for flags)
1340b57cec5SDimitry Andric };
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric enum OptionHidden {   // Control whether -help shows this option
1370b57cec5SDimitry Andric   NotHidden = 0x00,   // Option included in -help & -help-hidden
1380b57cec5SDimitry Andric   Hidden = 0x01,      // -help doesn't, but -help-hidden does
1390b57cec5SDimitry Andric   ReallyHidden = 0x02 // Neither -help nor -help-hidden show this arg
1400b57cec5SDimitry Andric };
1410b57cec5SDimitry Andric 
14281ad6265SDimitry Andric // This controls special features that the option might have that cause it to be
14381ad6265SDimitry Andric // parsed differently...
1440b57cec5SDimitry Andric //
1450b57cec5SDimitry Andric // Prefix - This option allows arguments that are otherwise unrecognized to be
1460b57cec5SDimitry Andric // matched by options that are a prefix of the actual value.  This is useful for
1470b57cec5SDimitry Andric // cases like a linker, where options are typically of the form '-lfoo' or
1480b57cec5SDimitry Andric // '-L../../include' where -l or -L are the actual flags.  When prefix is
1490b57cec5SDimitry Andric // enabled, and used, the value for the flag comes from the suffix of the
1500b57cec5SDimitry Andric // argument.
1510b57cec5SDimitry Andric //
1520b57cec5SDimitry Andric // AlwaysPrefix - Only allow the behavior enabled by the Prefix flag and reject
1530b57cec5SDimitry Andric // the Option=Value form.
1540b57cec5SDimitry Andric //
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric enum FormattingFlags {
1570b57cec5SDimitry Andric   NormalFormatting = 0x00, // Nothing special
1580b57cec5SDimitry Andric   Positional = 0x01,       // Is a positional argument, no '-' required
1590b57cec5SDimitry Andric   Prefix = 0x02,           // Can this option directly prefix its value?
1600b57cec5SDimitry Andric   AlwaysPrefix = 0x03      // Can this option only directly prefix its value?
1610b57cec5SDimitry Andric };
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric enum MiscFlags {             // Miscellaneous flags to adjust argument
1640b57cec5SDimitry Andric   CommaSeparated = 0x01,     // Should this cl::list split between commas?
1650b57cec5SDimitry Andric   PositionalEatsArgs = 0x02, // Should this positional cl::list eat -args?
1660b57cec5SDimitry Andric   Sink = 0x04,               // Should this cl::list eat all unknown options?
1670b57cec5SDimitry Andric 
16881ad6265SDimitry Andric   // Can this option group with other options?
1690b57cec5SDimitry Andric   // If this is enabled, multiple letter options are allowed to bunch together
1700b57cec5SDimitry Andric   // with only a single hyphen for the whole group.  This allows emulation
1710b57cec5SDimitry Andric   // of the behavior that ls uses for example: ls -la === ls -l -a
1720b57cec5SDimitry Andric   Grouping = 0x08,
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric   // Default option
1750b57cec5SDimitry Andric   DefaultOption = 0x10
1760b57cec5SDimitry Andric };
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1790b57cec5SDimitry Andric //
1800b57cec5SDimitry Andric class OptionCategory {
1810b57cec5SDimitry Andric private:
1820b57cec5SDimitry Andric   StringRef const Name;
1830b57cec5SDimitry Andric   StringRef const Description;
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric   void registerCategory();
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric public:
1880b57cec5SDimitry Andric   OptionCategory(StringRef const Name,
1890b57cec5SDimitry Andric                  StringRef const Description = "")
Name(Name)1900b57cec5SDimitry Andric       : Name(Name), Description(Description) {
1910b57cec5SDimitry Andric     registerCategory();
1920b57cec5SDimitry Andric   }
1930b57cec5SDimitry Andric 
getName()1940b57cec5SDimitry Andric   StringRef getName() const { return Name; }
getDescription()1950b57cec5SDimitry Andric   StringRef getDescription() const { return Description; }
1960b57cec5SDimitry Andric };
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric // The general Option Category (used as default category).
199fe6060f1SDimitry Andric OptionCategory &getGeneralCategory();
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2020b57cec5SDimitry Andric //
2030b57cec5SDimitry Andric class SubCommand {
2040b57cec5SDimitry Andric private:
2050b57cec5SDimitry Andric   StringRef Name;
2060b57cec5SDimitry Andric   StringRef Description;
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric protected:
2090b57cec5SDimitry Andric   void registerSubCommand();
2100b57cec5SDimitry Andric   void unregisterSubCommand();
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric public:
2130b57cec5SDimitry Andric   SubCommand(StringRef Name, StringRef Description = "")
Name(Name)2140b57cec5SDimitry Andric       : Name(Name), Description(Description) {
2150b57cec5SDimitry Andric         registerSubCommand();
2160b57cec5SDimitry Andric   }
2170b57cec5SDimitry Andric   SubCommand() = default;
2180b57cec5SDimitry Andric 
219bdd1243dSDimitry Andric   // Get the special subcommand representing no subcommand.
220bdd1243dSDimitry Andric   static SubCommand &getTopLevel();
221bdd1243dSDimitry Andric 
222bdd1243dSDimitry Andric   // Get the special subcommand that can be used to put an option into all
22306c3fb27SDimitry Andric   // subcommands.
224bdd1243dSDimitry Andric   static SubCommand &getAll();
225bdd1243dSDimitry Andric 
2260b57cec5SDimitry Andric   void reset();
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric   explicit operator bool() const;
2290b57cec5SDimitry Andric 
getName()2300b57cec5SDimitry Andric   StringRef getName() const { return Name; }
getDescription()2310b57cec5SDimitry Andric   StringRef getDescription() const { return Description; }
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric   SmallVector<Option *, 4> PositionalOpts;
2340b57cec5SDimitry Andric   SmallVector<Option *, 4> SinkOpts;
2350b57cec5SDimitry Andric   StringMap<Option *> OptionsMap;
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric   Option *ConsumeAfterOpt = nullptr; // The ConsumeAfter option if it exists.
2380b57cec5SDimitry Andric };
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric // A special subcommand representing no subcommand
2410b57cec5SDimitry Andric extern ManagedStatic<SubCommand> TopLevelSubCommand;
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric // A special subcommand that can be used to put an option into all subcommands.
2440b57cec5SDimitry Andric extern ManagedStatic<SubCommand> AllSubCommands;
2450b57cec5SDimitry Andric 
246cb14a3feSDimitry Andric class SubCommandGroup {
247cb14a3feSDimitry Andric   SmallVector<SubCommand *, 4> Subs;
248cb14a3feSDimitry Andric 
249cb14a3feSDimitry Andric public:
SubCommandGroup(std::initializer_list<SubCommand * > IL)250cb14a3feSDimitry Andric   SubCommandGroup(std::initializer_list<SubCommand *> IL) : Subs(IL) {}
251cb14a3feSDimitry Andric 
getSubCommands()252cb14a3feSDimitry Andric   ArrayRef<SubCommand *> getSubCommands() const { return Subs; }
253cb14a3feSDimitry Andric };
254cb14a3feSDimitry Andric 
2550b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2560b57cec5SDimitry Andric //
2570b57cec5SDimitry Andric class Option {
2580b57cec5SDimitry Andric   friend class alias;
2590b57cec5SDimitry Andric 
26081ad6265SDimitry Andric   // Overriden by subclasses to handle the value passed into an argument. Should
26181ad6265SDimitry Andric   // return true if there was an error processing the argument and the program
26281ad6265SDimitry Andric   // should exit.
2630b57cec5SDimitry Andric   //
2640b57cec5SDimitry Andric   virtual bool handleOccurrence(unsigned pos, StringRef ArgName,
2650b57cec5SDimitry Andric                                 StringRef Arg) = 0;
2660b57cec5SDimitry Andric 
getValueExpectedFlagDefault()2670b57cec5SDimitry Andric   virtual enum ValueExpected getValueExpectedFlagDefault() const {
2680b57cec5SDimitry Andric     return ValueOptional;
2690b57cec5SDimitry Andric   }
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric   // Out of line virtual function to provide home for the class.
2720b57cec5SDimitry Andric   virtual void anchor();
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric   uint16_t NumOccurrences; // The number of times specified
2750b57cec5SDimitry Andric   // Occurrences, HiddenFlag, and Formatting are all enum types but to avoid
2760b57cec5SDimitry Andric   // problems with signed enums in bitfields.
2770b57cec5SDimitry Andric   uint16_t Occurrences : 3; // enum NumOccurrencesFlag
2780b57cec5SDimitry Andric   // not using the enum type for 'Value' because zero is an implementation
2790b57cec5SDimitry Andric   // detail representing the non-value
2800b57cec5SDimitry Andric   uint16_t Value : 2;
2810b57cec5SDimitry Andric   uint16_t HiddenFlag : 2; // enum OptionHidden
2820b57cec5SDimitry Andric   uint16_t Formatting : 2; // enum FormattingFlags
2830b57cec5SDimitry Andric   uint16_t Misc : 5;
2840b57cec5SDimitry Andric   uint16_t FullyInitialized : 1; // Has addArgument been called?
2850b57cec5SDimitry Andric   uint16_t Position;             // Position of last occurrence of the option
2860b57cec5SDimitry Andric   uint16_t AdditionalVals;       // Greater than 0 for multi-valued option.
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric public:
2890b57cec5SDimitry Andric   StringRef ArgStr;   // The argument string itself (ex: "help", "o")
2900b57cec5SDimitry Andric   StringRef HelpStr;  // The descriptive text message for -help
2910b57cec5SDimitry Andric   StringRef ValueStr; // String describing what the value of this option is
2920b57cec5SDimitry Andric   SmallVector<OptionCategory *, 1>
2930b57cec5SDimitry Andric       Categories;                    // The Categories this option belongs to
2940b57cec5SDimitry Andric   SmallPtrSet<SubCommand *, 1> Subs; // The subcommands this option belongs to.
2950b57cec5SDimitry Andric 
getNumOccurrencesFlag()2960b57cec5SDimitry Andric   inline enum NumOccurrencesFlag getNumOccurrencesFlag() const {
2970b57cec5SDimitry Andric     return (enum NumOccurrencesFlag)Occurrences;
2980b57cec5SDimitry Andric   }
2990b57cec5SDimitry Andric 
getValueExpectedFlag()3000b57cec5SDimitry Andric   inline enum ValueExpected getValueExpectedFlag() const {
3010b57cec5SDimitry Andric     return Value ? ((enum ValueExpected)Value) : getValueExpectedFlagDefault();
3020b57cec5SDimitry Andric   }
3030b57cec5SDimitry Andric 
getOptionHiddenFlag()3040b57cec5SDimitry Andric   inline enum OptionHidden getOptionHiddenFlag() const {
3050b57cec5SDimitry Andric     return (enum OptionHidden)HiddenFlag;
3060b57cec5SDimitry Andric   }
3070b57cec5SDimitry Andric 
getFormattingFlag()3080b57cec5SDimitry Andric   inline enum FormattingFlags getFormattingFlag() const {
3090b57cec5SDimitry Andric     return (enum FormattingFlags)Formatting;
3100b57cec5SDimitry Andric   }
3110b57cec5SDimitry Andric 
getMiscFlags()3120b57cec5SDimitry Andric   inline unsigned getMiscFlags() const { return Misc; }
getPosition()3130b57cec5SDimitry Andric   inline unsigned getPosition() const { return Position; }
getNumAdditionalVals()3140b57cec5SDimitry Andric   inline unsigned getNumAdditionalVals() const { return AdditionalVals; }
3150b57cec5SDimitry Andric 
31681ad6265SDimitry Andric   // Return true if the argstr != ""
hasArgStr()3170b57cec5SDimitry Andric   bool hasArgStr() const { return !ArgStr.empty(); }
isPositional()3180b57cec5SDimitry Andric   bool isPositional() const { return getFormattingFlag() == cl::Positional; }
isSink()3190b57cec5SDimitry Andric   bool isSink() const { return getMiscFlags() & cl::Sink; }
isDefaultOption()3200b57cec5SDimitry Andric   bool isDefaultOption() const { return getMiscFlags() & cl::DefaultOption; }
3210b57cec5SDimitry Andric 
isConsumeAfter()3220b57cec5SDimitry Andric   bool isConsumeAfter() const {
3230b57cec5SDimitry Andric     return getNumOccurrencesFlag() == cl::ConsumeAfter;
3240b57cec5SDimitry Andric   }
3250b57cec5SDimitry Andric 
3260b57cec5SDimitry Andric   //-------------------------------------------------------------------------===
3270b57cec5SDimitry Andric   // Accessor functions set by OptionModifiers
3280b57cec5SDimitry Andric   //
3290b57cec5SDimitry Andric   void setArgStr(StringRef S);
setDescription(StringRef S)3300b57cec5SDimitry Andric   void setDescription(StringRef S) { HelpStr = S; }
setValueStr(StringRef S)3310b57cec5SDimitry Andric   void setValueStr(StringRef S) { ValueStr = S; }
setNumOccurrencesFlag(enum NumOccurrencesFlag Val)3320b57cec5SDimitry Andric   void setNumOccurrencesFlag(enum NumOccurrencesFlag Val) { Occurrences = Val; }
setValueExpectedFlag(enum ValueExpected Val)3330b57cec5SDimitry Andric   void setValueExpectedFlag(enum ValueExpected Val) { Value = Val; }
setHiddenFlag(enum OptionHidden Val)3340b57cec5SDimitry Andric   void setHiddenFlag(enum OptionHidden Val) { HiddenFlag = Val; }
setFormattingFlag(enum FormattingFlags V)3350b57cec5SDimitry Andric   void setFormattingFlag(enum FormattingFlags V) { Formatting = V; }
setMiscFlag(enum MiscFlags M)3360b57cec5SDimitry Andric   void setMiscFlag(enum MiscFlags M) { Misc |= M; }
setPosition(unsigned pos)3370b57cec5SDimitry Andric   void setPosition(unsigned pos) { Position = pos; }
3380b57cec5SDimitry Andric   void addCategory(OptionCategory &C);
addSubCommand(SubCommand & S)3390b57cec5SDimitry Andric   void addSubCommand(SubCommand &S) { Subs.insert(&S); }
3400b57cec5SDimitry Andric 
3410b57cec5SDimitry Andric protected:
Option(enum NumOccurrencesFlag OccurrencesFlag,enum OptionHidden Hidden)3420b57cec5SDimitry Andric   explicit Option(enum NumOccurrencesFlag OccurrencesFlag,
3430b57cec5SDimitry Andric                   enum OptionHidden Hidden)
3440b57cec5SDimitry Andric       : NumOccurrences(0), Occurrences(OccurrencesFlag), Value(0),
3450b57cec5SDimitry Andric         HiddenFlag(Hidden), Formatting(NormalFormatting), Misc(0),
3460b57cec5SDimitry Andric         FullyInitialized(false), Position(0), AdditionalVals(0) {
347fe6060f1SDimitry Andric     Categories.push_back(&getGeneralCategory());
3480b57cec5SDimitry Andric   }
3490b57cec5SDimitry Andric 
setNumAdditionalVals(unsigned n)3500b57cec5SDimitry Andric   inline void setNumAdditionalVals(unsigned n) { AdditionalVals = n; }
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric public:
3530b57cec5SDimitry Andric   virtual ~Option() = default;
3540b57cec5SDimitry Andric 
35581ad6265SDimitry Andric   // Register this argument with the commandline system.
3560b57cec5SDimitry Andric   //
3570b57cec5SDimitry Andric   void addArgument();
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric   /// Unregisters this option from the CommandLine system.
3600b57cec5SDimitry Andric   ///
3610b57cec5SDimitry Andric   /// This option must have been the last option registered.
3620b57cec5SDimitry Andric   /// For testing purposes only.
3630b57cec5SDimitry Andric   void removeArgument();
3640b57cec5SDimitry Andric 
3650b57cec5SDimitry Andric   // Return the width of the option tag for printing...
3660b57cec5SDimitry Andric   virtual size_t getOptionWidth() const = 0;
3670b57cec5SDimitry Andric 
36881ad6265SDimitry Andric   // Print out information about this option. The to-be-maintained width is
36981ad6265SDimitry Andric   // specified.
3700b57cec5SDimitry Andric   //
3710b57cec5SDimitry Andric   virtual void printOptionInfo(size_t GlobalWidth) const = 0;
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric   virtual void printOptionValue(size_t GlobalWidth, bool Force) const = 0;
3740b57cec5SDimitry Andric 
3750b57cec5SDimitry Andric   virtual void setDefault() = 0;
3760b57cec5SDimitry Andric 
377d409305fSDimitry Andric   // Prints the help string for an option.
378d409305fSDimitry Andric   //
379d409305fSDimitry Andric   // This maintains the Indent for multi-line descriptions.
380d409305fSDimitry Andric   // FirstLineIndentedBy is the count of chars of the first line
381d409305fSDimitry Andric   //      i.e. the one containing the --<option name>.
3820b57cec5SDimitry Andric   static void printHelpStr(StringRef HelpStr, size_t Indent,
3830b57cec5SDimitry Andric                            size_t FirstLineIndentedBy);
3840b57cec5SDimitry Andric 
385d409305fSDimitry Andric   // Prints the help string for an enum value.
386d409305fSDimitry Andric   //
387d409305fSDimitry Andric   // This maintains the Indent for multi-line descriptions.
388d409305fSDimitry Andric   // FirstLineIndentedBy is the count of chars of the first line
389d409305fSDimitry Andric   //      i.e. the one containing the =<value>.
390d409305fSDimitry Andric   static void printEnumValHelpStr(StringRef HelpStr, size_t Indent,
391d409305fSDimitry Andric                                   size_t FirstLineIndentedBy);
392d409305fSDimitry Andric 
getExtraOptionNames(SmallVectorImpl<StringRef> &)3930b57cec5SDimitry Andric   virtual void getExtraOptionNames(SmallVectorImpl<StringRef> &) {}
3940b57cec5SDimitry Andric 
39581ad6265SDimitry Andric   // Wrapper around handleOccurrence that enforces Flags.
3960b57cec5SDimitry Andric   //
3970b57cec5SDimitry Andric   virtual bool addOccurrence(unsigned pos, StringRef ArgName, StringRef Value,
3980b57cec5SDimitry Andric                              bool MultiArg = false);
3990b57cec5SDimitry Andric 
4000b57cec5SDimitry Andric   // Prints option name followed by message.  Always returns true.
4010b57cec5SDimitry Andric   bool error(const Twine &Message, StringRef ArgName = StringRef(), raw_ostream &Errs = llvm::errs());
error(const Twine & Message,raw_ostream & Errs)4020b57cec5SDimitry Andric   bool error(const Twine &Message, raw_ostream &Errs) {
4030b57cec5SDimitry Andric     return error(Message, StringRef(), Errs);
4040b57cec5SDimitry Andric   }
4050b57cec5SDimitry Andric 
getNumOccurrences()4060b57cec5SDimitry Andric   inline int getNumOccurrences() const { return NumOccurrences; }
4070b57cec5SDimitry Andric   void reset();
4080b57cec5SDimitry Andric };
4090b57cec5SDimitry Andric 
4100b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
4110b57cec5SDimitry Andric // Command line option modifiers that can be used to modify the behavior of
4120b57cec5SDimitry Andric // command line option parsers...
4130b57cec5SDimitry Andric //
4140b57cec5SDimitry Andric 
41581ad6265SDimitry Andric // Modifier to set the description shown in the -help output...
4160b57cec5SDimitry Andric struct desc {
4170b57cec5SDimitry Andric   StringRef Desc;
4180b57cec5SDimitry Andric 
descdesc4190b57cec5SDimitry Andric   desc(StringRef Str) : Desc(Str) {}
4200b57cec5SDimitry Andric 
applydesc4210b57cec5SDimitry Andric   void apply(Option &O) const { O.setDescription(Desc); }
4220b57cec5SDimitry Andric };
4230b57cec5SDimitry Andric 
42481ad6265SDimitry Andric // Modifier to set the value description shown in the -help output...
4250b57cec5SDimitry Andric struct value_desc {
4260b57cec5SDimitry Andric   StringRef Desc;
4270b57cec5SDimitry Andric 
value_descvalue_desc4280b57cec5SDimitry Andric   value_desc(StringRef Str) : Desc(Str) {}
4290b57cec5SDimitry Andric 
applyvalue_desc4300b57cec5SDimitry Andric   void apply(Option &O) const { O.setValueStr(Desc); }
4310b57cec5SDimitry Andric };
4320b57cec5SDimitry Andric 
43381ad6265SDimitry Andric // Specify a default (initial) value for the command line argument, if the
43481ad6265SDimitry Andric // default constructor for the argument type does not give you what you want.
43581ad6265SDimitry Andric // This is only valid on "opt" arguments, not on "list" arguments.
4360b57cec5SDimitry Andric template <class Ty> struct initializer {
4370b57cec5SDimitry Andric   const Ty &Init;
initializerinitializer4380b57cec5SDimitry Andric   initializer(const Ty &Val) : Init(Val) {}
4390b57cec5SDimitry Andric 
applyinitializer4400b57cec5SDimitry Andric   template <class Opt> void apply(Opt &O) const { O.setInitialValue(Init); }
4410b57cec5SDimitry Andric };
4420b57cec5SDimitry Andric 
443bdd1243dSDimitry Andric template <class Ty> struct list_initializer {
444bdd1243dSDimitry Andric   ArrayRef<Ty> Inits;
list_initializerlist_initializer445bdd1243dSDimitry Andric   list_initializer(ArrayRef<Ty> Vals) : Inits(Vals) {}
446bdd1243dSDimitry Andric 
applylist_initializer447bdd1243dSDimitry Andric   template <class Opt> void apply(Opt &O) const { O.setInitialValues(Inits); }
448bdd1243dSDimitry Andric };
449bdd1243dSDimitry Andric 
init(const Ty & Val)4500b57cec5SDimitry Andric template <class Ty> initializer<Ty> init(const Ty &Val) {
4510b57cec5SDimitry Andric   return initializer<Ty>(Val);
4520b57cec5SDimitry Andric }
4530b57cec5SDimitry Andric 
454bdd1243dSDimitry Andric template <class Ty>
list_init(ArrayRef<Ty> Vals)455bdd1243dSDimitry Andric list_initializer<Ty> list_init(ArrayRef<Ty> Vals) {
456bdd1243dSDimitry Andric   return list_initializer<Ty>(Vals);
457bdd1243dSDimitry Andric }
458bdd1243dSDimitry Andric 
45981ad6265SDimitry Andric // Allow the user to specify which external variable they want to store the
46081ad6265SDimitry Andric // results of the command line argument processing into, if they don't want to
46181ad6265SDimitry Andric // store it in the option itself.
4620b57cec5SDimitry Andric template <class Ty> struct LocationClass {
4630b57cec5SDimitry Andric   Ty &Loc;
4640b57cec5SDimitry Andric 
LocationClassLocationClass4650b57cec5SDimitry Andric   LocationClass(Ty &L) : Loc(L) {}
4660b57cec5SDimitry Andric 
applyLocationClass4670b57cec5SDimitry Andric   template <class Opt> void apply(Opt &O) const { O.setLocation(O, Loc); }
4680b57cec5SDimitry Andric };
4690b57cec5SDimitry Andric 
location(Ty & L)4700b57cec5SDimitry Andric template <class Ty> LocationClass<Ty> location(Ty &L) {
4710b57cec5SDimitry Andric   return LocationClass<Ty>(L);
4720b57cec5SDimitry Andric }
4730b57cec5SDimitry Andric 
47481ad6265SDimitry Andric // Specify the Option category for the command line argument to belong to.
4750b57cec5SDimitry Andric struct cat {
4760b57cec5SDimitry Andric   OptionCategory &Category;
4770b57cec5SDimitry Andric 
catcat4780b57cec5SDimitry Andric   cat(OptionCategory &c) : Category(c) {}
4790b57cec5SDimitry Andric 
applycat4800b57cec5SDimitry Andric   template <class Opt> void apply(Opt &O) const { O.addCategory(Category); }
4810b57cec5SDimitry Andric };
4820b57cec5SDimitry Andric 
48381ad6265SDimitry Andric // Specify the subcommand that this option belongs to.
4840b57cec5SDimitry Andric struct sub {
485cb14a3feSDimitry Andric   SubCommand *Sub = nullptr;
486cb14a3feSDimitry Andric   SubCommandGroup *Group = nullptr;
4870b57cec5SDimitry Andric 
subsub488cb14a3feSDimitry Andric   sub(SubCommand &S) : Sub(&S) {}
subsub489cb14a3feSDimitry Andric   sub(SubCommandGroup &G) : Group(&G) {}
4900b57cec5SDimitry Andric 
applysub491cb14a3feSDimitry Andric   template <class Opt> void apply(Opt &O) const {
492cb14a3feSDimitry Andric     if (Sub)
493cb14a3feSDimitry Andric       O.addSubCommand(*Sub);
494cb14a3feSDimitry Andric     else if (Group)
495cb14a3feSDimitry Andric       for (SubCommand *SC : Group->getSubCommands())
496cb14a3feSDimitry Andric         O.addSubCommand(*SC);
497cb14a3feSDimitry Andric   }
4980b57cec5SDimitry Andric };
4990b57cec5SDimitry Andric 
500480093f4SDimitry Andric // Specify a callback function to be called when an option is seen.
501480093f4SDimitry Andric // Can be used to set other options automatically.
502480093f4SDimitry Andric template <typename R, typename Ty> struct cb {
503480093f4SDimitry Andric   std::function<R(Ty)> CB;
504480093f4SDimitry Andric 
cbcb505480093f4SDimitry Andric   cb(std::function<R(Ty)> CB) : CB(CB) {}
506480093f4SDimitry Andric 
applycb507480093f4SDimitry Andric   template <typename Opt> void apply(Opt &O) const { O.setCallback(CB); }
508480093f4SDimitry Andric };
509480093f4SDimitry Andric 
510480093f4SDimitry Andric namespace detail {
511480093f4SDimitry Andric template <typename F>
512480093f4SDimitry Andric struct callback_traits : public callback_traits<decltype(&F::operator())> {};
513480093f4SDimitry Andric 
514480093f4SDimitry Andric template <typename R, typename C, typename... Args>
515480093f4SDimitry Andric struct callback_traits<R (C::*)(Args...) const> {
516480093f4SDimitry Andric   using result_type = R;
5175ffd83dbSDimitry Andric   using arg_type = std::tuple_element_t<0, std::tuple<Args...>>;
518480093f4SDimitry Andric   static_assert(sizeof...(Args) == 1, "callback function must have one and only one parameter");
51906c3fb27SDimitry Andric   static_assert(std::is_same_v<result_type, void>,
520480093f4SDimitry Andric                 "callback return type must be void");
52106c3fb27SDimitry Andric   static_assert(std::is_lvalue_reference_v<arg_type> &&
52206c3fb27SDimitry Andric                     std::is_const_v<std::remove_reference_t<arg_type>>,
523480093f4SDimitry Andric                 "callback arg_type must be a const lvalue reference");
524480093f4SDimitry Andric };
525480093f4SDimitry Andric } // namespace detail
526480093f4SDimitry Andric 
527480093f4SDimitry Andric template <typename F>
528480093f4SDimitry Andric cb<typename detail::callback_traits<F>::result_type,
529480093f4SDimitry Andric    typename detail::callback_traits<F>::arg_type>
530480093f4SDimitry Andric callback(F CB) {
531480093f4SDimitry Andric   using result_type = typename detail::callback_traits<F>::result_type;
532480093f4SDimitry Andric   using arg_type = typename detail::callback_traits<F>::arg_type;
533480093f4SDimitry Andric   return cb<result_type, arg_type>(CB);
534480093f4SDimitry Andric }
535480093f4SDimitry Andric 
5360b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
5370b57cec5SDimitry Andric 
5380b57cec5SDimitry Andric // Support value comparison outside the template.
5390b57cec5SDimitry Andric struct GenericOptionValue {
5400b57cec5SDimitry Andric   virtual bool compare(const GenericOptionValue &V) const = 0;
5410b57cec5SDimitry Andric 
5420b57cec5SDimitry Andric protected:
5430b57cec5SDimitry Andric   GenericOptionValue() = default;
5440b57cec5SDimitry Andric   GenericOptionValue(const GenericOptionValue&) = default;
5450b57cec5SDimitry Andric   GenericOptionValue &operator=(const GenericOptionValue &) = default;
5460b57cec5SDimitry Andric   ~GenericOptionValue() = default;
5470b57cec5SDimitry Andric 
5480b57cec5SDimitry Andric private:
5490b57cec5SDimitry Andric   virtual void anchor();
5500b57cec5SDimitry Andric };
5510b57cec5SDimitry Andric 
5520b57cec5SDimitry Andric template <class DataType> struct OptionValue;
5530b57cec5SDimitry Andric 
5540b57cec5SDimitry Andric // The default value safely does nothing. Option value printing is only
5550b57cec5SDimitry Andric // best-effort.
5560b57cec5SDimitry Andric template <class DataType, bool isClass>
5570b57cec5SDimitry Andric struct OptionValueBase : public GenericOptionValue {
5580b57cec5SDimitry Andric   // Temporary storage for argument passing.
5590b57cec5SDimitry Andric   using WrapperType = OptionValue<DataType>;
5600b57cec5SDimitry Andric 
5610b57cec5SDimitry Andric   bool hasValue() const { return false; }
5620b57cec5SDimitry Andric 
5630b57cec5SDimitry Andric   const DataType &getValue() const { llvm_unreachable("no default value"); }
5640b57cec5SDimitry Andric 
5650b57cec5SDimitry Andric   // Some options may take their value from a different data type.
5660b57cec5SDimitry Andric   template <class DT> void setValue(const DT & /*V*/) {}
5670b57cec5SDimitry Andric 
5685f757f3fSDimitry Andric   // Returns whether this instance matches the argument.
5690b57cec5SDimitry Andric   bool compare(const DataType & /*V*/) const { return false; }
5700b57cec5SDimitry Andric 
5710b57cec5SDimitry Andric   bool compare(const GenericOptionValue & /*V*/) const override {
5720b57cec5SDimitry Andric     return false;
5730b57cec5SDimitry Andric   }
5740b57cec5SDimitry Andric 
5750b57cec5SDimitry Andric protected:
5760b57cec5SDimitry Andric   ~OptionValueBase() = default;
5770b57cec5SDimitry Andric };
5780b57cec5SDimitry Andric 
5790b57cec5SDimitry Andric // Simple copy of the option value.
5800b57cec5SDimitry Andric template <class DataType> class OptionValueCopy : public GenericOptionValue {
5810b57cec5SDimitry Andric   DataType Value;
5820b57cec5SDimitry Andric   bool Valid = false;
5830b57cec5SDimitry Andric 
5840b57cec5SDimitry Andric protected:
5850b57cec5SDimitry Andric   OptionValueCopy(const OptionValueCopy&) = default;
5860b57cec5SDimitry Andric   OptionValueCopy &operator=(const OptionValueCopy &) = default;
5870b57cec5SDimitry Andric   ~OptionValueCopy() = default;
5880b57cec5SDimitry Andric 
5890b57cec5SDimitry Andric public:
5900b57cec5SDimitry Andric   OptionValueCopy() = default;
5910b57cec5SDimitry Andric 
5920b57cec5SDimitry Andric   bool hasValue() const { return Valid; }
5930b57cec5SDimitry Andric 
5940b57cec5SDimitry Andric   const DataType &getValue() const {
5950b57cec5SDimitry Andric     assert(Valid && "invalid option value");
5960b57cec5SDimitry Andric     return Value;
5970b57cec5SDimitry Andric   }
5980b57cec5SDimitry Andric 
5990b57cec5SDimitry Andric   void setValue(const DataType &V) {
6000b57cec5SDimitry Andric     Valid = true;
6010b57cec5SDimitry Andric     Value = V;
6020b57cec5SDimitry Andric   }
6030b57cec5SDimitry Andric 
6045f757f3fSDimitry Andric   // Returns whether this instance matches V.
6055f757f3fSDimitry Andric   bool compare(const DataType &V) const { return Valid && (Value == V); }
6060b57cec5SDimitry Andric 
6070b57cec5SDimitry Andric   bool compare(const GenericOptionValue &V) const override {
6080b57cec5SDimitry Andric     const OptionValueCopy<DataType> &VC =
6090b57cec5SDimitry Andric         static_cast<const OptionValueCopy<DataType> &>(V);
6100b57cec5SDimitry Andric     if (!VC.hasValue())
6110b57cec5SDimitry Andric       return false;
6120b57cec5SDimitry Andric     return compare(VC.getValue());
6130b57cec5SDimitry Andric   }
6140b57cec5SDimitry Andric };
6150b57cec5SDimitry Andric 
6160b57cec5SDimitry Andric // Non-class option values.
6170b57cec5SDimitry Andric template <class DataType>
6180b57cec5SDimitry Andric struct OptionValueBase<DataType, false> : OptionValueCopy<DataType> {
6190b57cec5SDimitry Andric   using WrapperType = DataType;
6200b57cec5SDimitry Andric 
6210b57cec5SDimitry Andric protected:
6220b57cec5SDimitry Andric   OptionValueBase() = default;
6230b57cec5SDimitry Andric   OptionValueBase(const OptionValueBase&) = default;
6240b57cec5SDimitry Andric   OptionValueBase &operator=(const OptionValueBase &) = default;
6250b57cec5SDimitry Andric   ~OptionValueBase() = default;
6260b57cec5SDimitry Andric };
6270b57cec5SDimitry Andric 
6280b57cec5SDimitry Andric // Top-level option class.
6290b57cec5SDimitry Andric template <class DataType>
6300b57cec5SDimitry Andric struct OptionValue final
63106c3fb27SDimitry Andric     : OptionValueBase<DataType, std::is_class_v<DataType>> {
6320b57cec5SDimitry Andric   OptionValue() = default;
6330b57cec5SDimitry Andric 
6340b57cec5SDimitry Andric   OptionValue(const DataType &V) { this->setValue(V); }
6350b57cec5SDimitry Andric 
6360b57cec5SDimitry Andric   // Some options may take their value from a different data type.
6370b57cec5SDimitry Andric   template <class DT> OptionValue<DataType> &operator=(const DT &V) {
6380b57cec5SDimitry Andric     this->setValue(V);
6390b57cec5SDimitry Andric     return *this;
6400b57cec5SDimitry Andric   }
6410b57cec5SDimitry Andric };
6420b57cec5SDimitry Andric 
6430b57cec5SDimitry Andric // Other safe-to-copy-by-value common option types.
6440b57cec5SDimitry Andric enum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE };
6450b57cec5SDimitry Andric template <>
6460b57cec5SDimitry Andric struct OptionValue<cl::boolOrDefault> final
6470b57cec5SDimitry Andric     : OptionValueCopy<cl::boolOrDefault> {
6480b57cec5SDimitry Andric   using WrapperType = cl::boolOrDefault;
6490b57cec5SDimitry Andric 
6500b57cec5SDimitry Andric   OptionValue() = default;
6510b57cec5SDimitry Andric 
6520b57cec5SDimitry Andric   OptionValue(const cl::boolOrDefault &V) { this->setValue(V); }
6530b57cec5SDimitry Andric 
6540b57cec5SDimitry Andric   OptionValue<cl::boolOrDefault> &operator=(const cl::boolOrDefault &V) {
6550b57cec5SDimitry Andric     setValue(V);
6560b57cec5SDimitry Andric     return *this;
6570b57cec5SDimitry Andric   }
6580b57cec5SDimitry Andric 
6590b57cec5SDimitry Andric private:
6600b57cec5SDimitry Andric   void anchor() override;
6610b57cec5SDimitry Andric };
6620b57cec5SDimitry Andric 
6630b57cec5SDimitry Andric template <>
6640b57cec5SDimitry Andric struct OptionValue<std::string> final : OptionValueCopy<std::string> {
6650b57cec5SDimitry Andric   using WrapperType = StringRef;
6660b57cec5SDimitry Andric 
6670b57cec5SDimitry Andric   OptionValue() = default;
6680b57cec5SDimitry Andric 
6690b57cec5SDimitry Andric   OptionValue(const std::string &V) { this->setValue(V); }
6700b57cec5SDimitry Andric 
6710b57cec5SDimitry Andric   OptionValue<std::string> &operator=(const std::string &V) {
6720b57cec5SDimitry Andric     setValue(V);
6730b57cec5SDimitry Andric     return *this;
6740b57cec5SDimitry Andric   }
6750b57cec5SDimitry Andric 
6760b57cec5SDimitry Andric private:
6770b57cec5SDimitry Andric   void anchor() override;
6780b57cec5SDimitry Andric };
6790b57cec5SDimitry Andric 
6800b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
6810b57cec5SDimitry Andric // Enum valued command line option
6820b57cec5SDimitry Andric //
6830b57cec5SDimitry Andric 
6840b57cec5SDimitry Andric // This represents a single enum value, using "int" as the underlying type.
6850b57cec5SDimitry Andric struct OptionEnumValue {
6860b57cec5SDimitry Andric   StringRef Name;
6870b57cec5SDimitry Andric   int Value;
6880b57cec5SDimitry Andric   StringRef Description;
6890b57cec5SDimitry Andric };
6900b57cec5SDimitry Andric 
6910b57cec5SDimitry Andric #define clEnumVal(ENUMVAL, DESC)                                               \
6920b57cec5SDimitry Andric   llvm::cl::OptionEnumValue { #ENUMVAL, int(ENUMVAL), DESC }
6930b57cec5SDimitry Andric #define clEnumValN(ENUMVAL, FLAGNAME, DESC)                                    \
6940b57cec5SDimitry Andric   llvm::cl::OptionEnumValue { FLAGNAME, int(ENUMVAL), DESC }
6950b57cec5SDimitry Andric 
69681ad6265SDimitry Andric // For custom data types, allow specifying a group of values together as the
69781ad6265SDimitry Andric // values that go into the mapping that the option handler uses.
6980b57cec5SDimitry Andric //
6990b57cec5SDimitry Andric class ValuesClass {
7000b57cec5SDimitry Andric   // Use a vector instead of a map, because the lists should be short,
7010b57cec5SDimitry Andric   // the overhead is less, and most importantly, it keeps them in the order
7020b57cec5SDimitry Andric   // inserted so we can print our option out nicely.
7030b57cec5SDimitry Andric   SmallVector<OptionEnumValue, 4> Values;
7040b57cec5SDimitry Andric 
7050b57cec5SDimitry Andric public:
7060b57cec5SDimitry Andric   ValuesClass(std::initializer_list<OptionEnumValue> Options)
7070b57cec5SDimitry Andric       : Values(Options) {}
7080b57cec5SDimitry Andric 
7090b57cec5SDimitry Andric   template <class Opt> void apply(Opt &O) const {
710e8d8bef9SDimitry Andric     for (const auto &Value : Values)
7110b57cec5SDimitry Andric       O.getParser().addLiteralOption(Value.Name, Value.Value,
7120b57cec5SDimitry Andric                                      Value.Description);
7130b57cec5SDimitry Andric   }
7140b57cec5SDimitry Andric };
7150b57cec5SDimitry Andric 
7160b57cec5SDimitry Andric /// Helper to build a ValuesClass by forwarding a variable number of arguments
7170b57cec5SDimitry Andric /// as an initializer list to the ValuesClass constructor.
7180b57cec5SDimitry Andric template <typename... OptsTy> ValuesClass values(OptsTy... Options) {
7190b57cec5SDimitry Andric   return ValuesClass({Options...});
7200b57cec5SDimitry Andric }
7210b57cec5SDimitry Andric 
7220b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
72381ad6265SDimitry Andric // Parameterizable parser for different data types. By default, known data types
72481ad6265SDimitry Andric // (string, int, bool) have specialized parsers, that do what you would expect.
72581ad6265SDimitry Andric // The default parser, used for data types that are not built-in, uses a mapping
72681ad6265SDimitry Andric // table to map specific options to values, which is used, among other things,
72781ad6265SDimitry Andric // to handle enum types.
7280b57cec5SDimitry Andric 
7290b57cec5SDimitry Andric //--------------------------------------------------
73081ad6265SDimitry Andric // This class holds all the non-generic code that we do not need replicated for
73181ad6265SDimitry Andric // every instance of the generic parser.  This also allows us to put stuff into
73281ad6265SDimitry Andric // CommandLine.cpp
7330b57cec5SDimitry Andric //
7340b57cec5SDimitry Andric class generic_parser_base {
7350b57cec5SDimitry Andric protected:
7360b57cec5SDimitry Andric   class GenericOptionInfo {
7370b57cec5SDimitry Andric   public:
7380b57cec5SDimitry Andric     GenericOptionInfo(StringRef name, StringRef helpStr)
7390b57cec5SDimitry Andric         : Name(name), HelpStr(helpStr) {}
7400b57cec5SDimitry Andric     StringRef Name;
7410b57cec5SDimitry Andric     StringRef HelpStr;
7420b57cec5SDimitry Andric   };
7430b57cec5SDimitry Andric 
7440b57cec5SDimitry Andric public:
7450b57cec5SDimitry Andric   generic_parser_base(Option &O) : Owner(O) {}
7460b57cec5SDimitry Andric 
7470b57cec5SDimitry Andric   virtual ~generic_parser_base() = default;
7480b57cec5SDimitry Andric   // Base class should have virtual-destructor
7490b57cec5SDimitry Andric 
75081ad6265SDimitry Andric   // Virtual function implemented by generic subclass to indicate how many
75181ad6265SDimitry Andric   // entries are in Values.
7520b57cec5SDimitry Andric   //
7530b57cec5SDimitry Andric   virtual unsigned getNumOptions() const = 0;
7540b57cec5SDimitry Andric 
75581ad6265SDimitry Andric   // Return option name N.
7560b57cec5SDimitry Andric   virtual StringRef getOption(unsigned N) const = 0;
7570b57cec5SDimitry Andric 
75881ad6265SDimitry Andric   // Return description N
7590b57cec5SDimitry Andric   virtual StringRef getDescription(unsigned N) const = 0;
7600b57cec5SDimitry Andric 
7610b57cec5SDimitry Andric   // Return the width of the option tag for printing...
7620b57cec5SDimitry Andric   virtual size_t getOptionWidth(const Option &O) const;
7630b57cec5SDimitry Andric 
7640b57cec5SDimitry Andric   virtual const GenericOptionValue &getOptionValue(unsigned N) const = 0;
7650b57cec5SDimitry Andric 
76681ad6265SDimitry Andric   // Print out information about this option. The to-be-maintained width is
76781ad6265SDimitry Andric   // specified.
7680b57cec5SDimitry Andric   //
7690b57cec5SDimitry Andric   virtual void printOptionInfo(const Option &O, size_t GlobalWidth) const;
7700b57cec5SDimitry Andric 
7710b57cec5SDimitry Andric   void printGenericOptionDiff(const Option &O, const GenericOptionValue &V,
7720b57cec5SDimitry Andric                               const GenericOptionValue &Default,
7730b57cec5SDimitry Andric                               size_t GlobalWidth) const;
7740b57cec5SDimitry Andric 
77581ad6265SDimitry Andric   // Print the value of an option and it's default.
7760b57cec5SDimitry Andric   //
7770b57cec5SDimitry Andric   // Template definition ensures that the option and default have the same
7780b57cec5SDimitry Andric   // DataType (via the same AnyOptionValue).
7790b57cec5SDimitry Andric   template <class AnyOptionValue>
7800b57cec5SDimitry Andric   void printOptionDiff(const Option &O, const AnyOptionValue &V,
7810b57cec5SDimitry Andric                        const AnyOptionValue &Default,
7820b57cec5SDimitry Andric                        size_t GlobalWidth) const {
7830b57cec5SDimitry Andric     printGenericOptionDiff(O, V, Default, GlobalWidth);
7840b57cec5SDimitry Andric   }
7850b57cec5SDimitry Andric 
7860b57cec5SDimitry Andric   void initialize() {}
7870b57cec5SDimitry Andric 
7880b57cec5SDimitry Andric   void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) {
7890b57cec5SDimitry Andric     // If there has been no argstr specified, that means that we need to add an
7900b57cec5SDimitry Andric     // argument for every possible option.  This ensures that our options are
7910b57cec5SDimitry Andric     // vectored to us.
7920b57cec5SDimitry Andric     if (!Owner.hasArgStr())
7930b57cec5SDimitry Andric       for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
7940b57cec5SDimitry Andric         OptionNames.push_back(getOption(i));
7950b57cec5SDimitry Andric   }
7960b57cec5SDimitry Andric 
7970b57cec5SDimitry Andric   enum ValueExpected getValueExpectedFlagDefault() const {
7980b57cec5SDimitry Andric     // If there is an ArgStr specified, then we are of the form:
7990b57cec5SDimitry Andric     //
8000b57cec5SDimitry Andric     //    -opt=O2   or   -opt O2  or  -optO2
8010b57cec5SDimitry Andric     //
8020b57cec5SDimitry Andric     // In which case, the value is required.  Otherwise if an arg str has not
8030b57cec5SDimitry Andric     // been specified, we are of the form:
8040b57cec5SDimitry Andric     //
8050b57cec5SDimitry Andric     //    -O2 or O2 or -la (where -l and -a are separate options)
8060b57cec5SDimitry Andric     //
8070b57cec5SDimitry Andric     // If this is the case, we cannot allow a value.
8080b57cec5SDimitry Andric     //
8090b57cec5SDimitry Andric     if (Owner.hasArgStr())
8100b57cec5SDimitry Andric       return ValueRequired;
8110b57cec5SDimitry Andric     else
8120b57cec5SDimitry Andric       return ValueDisallowed;
8130b57cec5SDimitry Andric   }
8140b57cec5SDimitry Andric 
81581ad6265SDimitry Andric   // Return the option number corresponding to the specified
8160b57cec5SDimitry Andric   // argument string.  If the option is not found, getNumOptions() is returned.
8170b57cec5SDimitry Andric   //
8180b57cec5SDimitry Andric   unsigned findOption(StringRef Name);
8190b57cec5SDimitry Andric 
8200b57cec5SDimitry Andric protected:
8210b57cec5SDimitry Andric   Option &Owner;
8220b57cec5SDimitry Andric };
8230b57cec5SDimitry Andric 
8240b57cec5SDimitry Andric // Default parser implementation - This implementation depends on having a
8250b57cec5SDimitry Andric // mapping of recognized options to values of some sort.  In addition to this,
8260b57cec5SDimitry Andric // each entry in the mapping also tracks a help message that is printed with the
8270b57cec5SDimitry Andric // command line option for -help.  Because this is a simple mapping parser, the
8280b57cec5SDimitry Andric // data type can be any unsupported type.
8290b57cec5SDimitry Andric //
8300b57cec5SDimitry Andric template <class DataType> class parser : public generic_parser_base {
8310b57cec5SDimitry Andric protected:
8320b57cec5SDimitry Andric   class OptionInfo : public GenericOptionInfo {
8330b57cec5SDimitry Andric   public:
8340b57cec5SDimitry Andric     OptionInfo(StringRef name, DataType v, StringRef helpStr)
8350b57cec5SDimitry Andric         : GenericOptionInfo(name, helpStr), V(v) {}
8360b57cec5SDimitry Andric 
8370b57cec5SDimitry Andric     OptionValue<DataType> V;
8380b57cec5SDimitry Andric   };
8390b57cec5SDimitry Andric   SmallVector<OptionInfo, 8> Values;
8400b57cec5SDimitry Andric 
8410b57cec5SDimitry Andric public:
8420b57cec5SDimitry Andric   parser(Option &O) : generic_parser_base(O) {}
8430b57cec5SDimitry Andric 
8440b57cec5SDimitry Andric   using parser_data_type = DataType;
8450b57cec5SDimitry Andric 
8460b57cec5SDimitry Andric   // Implement virtual functions needed by generic_parser_base
8470b57cec5SDimitry Andric   unsigned getNumOptions() const override { return unsigned(Values.size()); }
8480b57cec5SDimitry Andric   StringRef getOption(unsigned N) const override { return Values[N].Name; }
8490b57cec5SDimitry Andric   StringRef getDescription(unsigned N) const override {
8500b57cec5SDimitry Andric     return Values[N].HelpStr;
8510b57cec5SDimitry Andric   }
8520b57cec5SDimitry Andric 
85381ad6265SDimitry Andric   // Return the value of option name N.
8540b57cec5SDimitry Andric   const GenericOptionValue &getOptionValue(unsigned N) const override {
8550b57cec5SDimitry Andric     return Values[N].V;
8560b57cec5SDimitry Andric   }
8570b57cec5SDimitry Andric 
85881ad6265SDimitry Andric   // Return true on error.
8590b57cec5SDimitry Andric   bool parse(Option &O, StringRef ArgName, StringRef Arg, DataType &V) {
8600b57cec5SDimitry Andric     StringRef ArgVal;
8610b57cec5SDimitry Andric     if (Owner.hasArgStr())
8620b57cec5SDimitry Andric       ArgVal = Arg;
8630b57cec5SDimitry Andric     else
8640b57cec5SDimitry Andric       ArgVal = ArgName;
8650b57cec5SDimitry Andric 
8660b57cec5SDimitry Andric     for (size_t i = 0, e = Values.size(); i != e; ++i)
8670b57cec5SDimitry Andric       if (Values[i].Name == ArgVal) {
8680b57cec5SDimitry Andric         V = Values[i].V.getValue();
8690b57cec5SDimitry Andric         return false;
8700b57cec5SDimitry Andric       }
8710b57cec5SDimitry Andric 
8720b57cec5SDimitry Andric     return O.error("Cannot find option named '" + ArgVal + "'!");
8730b57cec5SDimitry Andric   }
8740b57cec5SDimitry Andric 
87581ad6265SDimitry Andric   /// Add an entry to the mapping table.
8760b57cec5SDimitry Andric   ///
8770b57cec5SDimitry Andric   template <class DT>
8780b57cec5SDimitry Andric   void addLiteralOption(StringRef Name, const DT &V, StringRef HelpStr) {
8795f757f3fSDimitry Andric #ifndef NDEBUG
8805f757f3fSDimitry Andric     if (findOption(Name) != Values.size())
8815f757f3fSDimitry Andric       report_fatal_error("Option " + Name + " already exists!");
8825f757f3fSDimitry Andric #endif
8830b57cec5SDimitry Andric     OptionInfo X(Name, static_cast<DataType>(V), HelpStr);
8840b57cec5SDimitry Andric     Values.push_back(X);
8850b57cec5SDimitry Andric     AddLiteralOption(Owner, Name);
8860b57cec5SDimitry Andric   }
8870b57cec5SDimitry Andric 
88881ad6265SDimitry Andric   /// Remove the specified option.
8890b57cec5SDimitry Andric   ///
8900b57cec5SDimitry Andric   void removeLiteralOption(StringRef Name) {
8910b57cec5SDimitry Andric     unsigned N = findOption(Name);
8920b57cec5SDimitry Andric     assert(N != Values.size() && "Option not found!");
8930b57cec5SDimitry Andric     Values.erase(Values.begin() + N);
8940b57cec5SDimitry Andric   }
8950b57cec5SDimitry Andric };
8960b57cec5SDimitry Andric 
8970b57cec5SDimitry Andric //--------------------------------------------------
89881ad6265SDimitry Andric // Super class of parsers to provide boilerplate code
8990b57cec5SDimitry Andric //
9000b57cec5SDimitry Andric class basic_parser_impl { // non-template implementation of basic_parser<t>
9010b57cec5SDimitry Andric public:
9020b57cec5SDimitry Andric   basic_parser_impl(Option &) {}
9030b57cec5SDimitry Andric 
9041fd87a68SDimitry Andric   virtual ~basic_parser_impl() = default;
9050b57cec5SDimitry Andric 
9060b57cec5SDimitry Andric   enum ValueExpected getValueExpectedFlagDefault() const {
9070b57cec5SDimitry Andric     return ValueRequired;
9080b57cec5SDimitry Andric   }
9090b57cec5SDimitry Andric 
9100b57cec5SDimitry Andric   void getExtraOptionNames(SmallVectorImpl<StringRef> &) {}
9110b57cec5SDimitry Andric 
9120b57cec5SDimitry Andric   void initialize() {}
9130b57cec5SDimitry Andric 
9140b57cec5SDimitry Andric   // Return the width of the option tag for printing...
9150b57cec5SDimitry Andric   size_t getOptionWidth(const Option &O) const;
9160b57cec5SDimitry Andric 
91781ad6265SDimitry Andric   // Print out information about this option. The to-be-maintained width is
91881ad6265SDimitry Andric   // specified.
9190b57cec5SDimitry Andric   //
9200b57cec5SDimitry Andric   void printOptionInfo(const Option &O, size_t GlobalWidth) const;
9210b57cec5SDimitry Andric 
92281ad6265SDimitry Andric   // Print a placeholder for options that don't yet support printOptionDiff().
9230b57cec5SDimitry Andric   void printOptionNoValue(const Option &O, size_t GlobalWidth) const;
9240b57cec5SDimitry Andric 
92581ad6265SDimitry Andric   // Overload in subclass to provide a better default value.
9260b57cec5SDimitry Andric   virtual StringRef getValueName() const { return "value"; }
9270b57cec5SDimitry Andric 
9280b57cec5SDimitry Andric   // An out-of-line virtual method to provide a 'home' for this class.
9290b57cec5SDimitry Andric   virtual void anchor();
9300b57cec5SDimitry Andric 
9310b57cec5SDimitry Andric protected:
9320b57cec5SDimitry Andric   // A helper for basic_parser::printOptionDiff.
9330b57cec5SDimitry Andric   void printOptionName(const Option &O, size_t GlobalWidth) const;
9340b57cec5SDimitry Andric };
9350b57cec5SDimitry Andric 
93681ad6265SDimitry Andric // The real basic parser is just a template wrapper that provides a typedef for
93781ad6265SDimitry Andric // the provided data type.
9380b57cec5SDimitry Andric //
9390b57cec5SDimitry Andric template <class DataType> class basic_parser : public basic_parser_impl {
9400b57cec5SDimitry Andric public:
9410b57cec5SDimitry Andric   using parser_data_type = DataType;
9420b57cec5SDimitry Andric   using OptVal = OptionValue<DataType>;
9430b57cec5SDimitry Andric 
9440b57cec5SDimitry Andric   basic_parser(Option &O) : basic_parser_impl(O) {}
9450b57cec5SDimitry Andric };
9460b57cec5SDimitry Andric 
9470b57cec5SDimitry Andric //--------------------------------------------------
948349cc55cSDimitry Andric 
949349cc55cSDimitry Andric extern template class basic_parser<bool>;
950349cc55cSDimitry Andric 
9510b57cec5SDimitry Andric template <> class parser<bool> : public basic_parser<bool> {
9520b57cec5SDimitry Andric public:
9530b57cec5SDimitry Andric   parser(Option &O) : basic_parser(O) {}
9540b57cec5SDimitry Andric 
95581ad6265SDimitry Andric   // Return true on error.
9560b57cec5SDimitry Andric   bool parse(Option &O, StringRef ArgName, StringRef Arg, bool &Val);
9570b57cec5SDimitry Andric 
9580b57cec5SDimitry Andric   void initialize() {}
9590b57cec5SDimitry Andric 
9600b57cec5SDimitry Andric   enum ValueExpected getValueExpectedFlagDefault() const {
9610b57cec5SDimitry Andric     return ValueOptional;
9620b57cec5SDimitry Andric   }
9630b57cec5SDimitry Andric 
96481ad6265SDimitry Andric   // Do not print =<value> at all.
9650b57cec5SDimitry Andric   StringRef getValueName() const override { return StringRef(); }
9660b57cec5SDimitry Andric 
9670b57cec5SDimitry Andric   void printOptionDiff(const Option &O, bool V, OptVal Default,
9680b57cec5SDimitry Andric                        size_t GlobalWidth) const;
9690b57cec5SDimitry Andric 
9700b57cec5SDimitry Andric   // An out-of-line virtual method to provide a 'home' for this class.
9710b57cec5SDimitry Andric   void anchor() override;
9720b57cec5SDimitry Andric };
9730b57cec5SDimitry Andric 
9740b57cec5SDimitry Andric //--------------------------------------------------
975349cc55cSDimitry Andric 
976349cc55cSDimitry Andric extern template class basic_parser<boolOrDefault>;
977349cc55cSDimitry Andric 
9780b57cec5SDimitry Andric template <> class parser<boolOrDefault> : public basic_parser<boolOrDefault> {
9790b57cec5SDimitry Andric public:
9800b57cec5SDimitry Andric   parser(Option &O) : basic_parser(O) {}
9810b57cec5SDimitry Andric 
98281ad6265SDimitry Andric   // Return true on error.
9830b57cec5SDimitry Andric   bool parse(Option &O, StringRef ArgName, StringRef Arg, boolOrDefault &Val);
9840b57cec5SDimitry Andric 
9850b57cec5SDimitry Andric   enum ValueExpected getValueExpectedFlagDefault() const {
9860b57cec5SDimitry Andric     return ValueOptional;
9870b57cec5SDimitry Andric   }
9880b57cec5SDimitry Andric 
98981ad6265SDimitry Andric   // Do not print =<value> at all.
9900b57cec5SDimitry Andric   StringRef getValueName() const override { return StringRef(); }
9910b57cec5SDimitry Andric 
9920b57cec5SDimitry Andric   void printOptionDiff(const Option &O, boolOrDefault V, OptVal Default,
9930b57cec5SDimitry Andric                        size_t GlobalWidth) const;
9940b57cec5SDimitry Andric 
9950b57cec5SDimitry Andric   // An out-of-line virtual method to provide a 'home' for this class.
9960b57cec5SDimitry Andric   void anchor() override;
9970b57cec5SDimitry Andric };
9980b57cec5SDimitry Andric 
9990b57cec5SDimitry Andric //--------------------------------------------------
1000349cc55cSDimitry Andric 
1001349cc55cSDimitry Andric extern template class basic_parser<int>;
1002349cc55cSDimitry Andric 
10030b57cec5SDimitry Andric template <> class parser<int> : public basic_parser<int> {
10040b57cec5SDimitry Andric public:
10050b57cec5SDimitry Andric   parser(Option &O) : basic_parser(O) {}
10060b57cec5SDimitry Andric 
100781ad6265SDimitry Andric   // Return true on error.
10080b57cec5SDimitry Andric   bool parse(Option &O, StringRef ArgName, StringRef Arg, int &Val);
10090b57cec5SDimitry Andric 
101081ad6265SDimitry Andric   // Overload in subclass to provide a better default value.
10110b57cec5SDimitry Andric   StringRef getValueName() const override { return "int"; }
10120b57cec5SDimitry Andric 
10130b57cec5SDimitry Andric   void printOptionDiff(const Option &O, int V, OptVal Default,
10140b57cec5SDimitry Andric                        size_t GlobalWidth) const;
10150b57cec5SDimitry Andric 
10160b57cec5SDimitry Andric   // An out-of-line virtual method to provide a 'home' for this class.
10170b57cec5SDimitry Andric   void anchor() override;
10180b57cec5SDimitry Andric };
10190b57cec5SDimitry Andric 
10200b57cec5SDimitry Andric //--------------------------------------------------
1021349cc55cSDimitry Andric 
1022349cc55cSDimitry Andric extern template class basic_parser<long>;
1023349cc55cSDimitry Andric 
1024480093f4SDimitry Andric template <> class parser<long> final : public basic_parser<long> {
1025480093f4SDimitry Andric public:
1026480093f4SDimitry Andric   parser(Option &O) : basic_parser(O) {}
1027480093f4SDimitry Andric 
102881ad6265SDimitry Andric   // Return true on error.
1029480093f4SDimitry Andric   bool parse(Option &O, StringRef ArgName, StringRef Arg, long &Val);
1030480093f4SDimitry Andric 
103181ad6265SDimitry Andric   // Overload in subclass to provide a better default value.
1032480093f4SDimitry Andric   StringRef getValueName() const override { return "long"; }
1033480093f4SDimitry Andric 
1034480093f4SDimitry Andric   void printOptionDiff(const Option &O, long V, OptVal Default,
1035480093f4SDimitry Andric                        size_t GlobalWidth) const;
1036480093f4SDimitry Andric 
1037480093f4SDimitry Andric   // An out-of-line virtual method to provide a 'home' for this class.
1038480093f4SDimitry Andric   void anchor() override;
1039480093f4SDimitry Andric };
1040480093f4SDimitry Andric 
1041480093f4SDimitry Andric //--------------------------------------------------
1042349cc55cSDimitry Andric 
1043349cc55cSDimitry Andric extern template class basic_parser<long long>;
1044349cc55cSDimitry Andric 
1045480093f4SDimitry Andric template <> class parser<long long> : public basic_parser<long long> {
1046480093f4SDimitry Andric public:
1047480093f4SDimitry Andric   parser(Option &O) : basic_parser(O) {}
1048480093f4SDimitry Andric 
104981ad6265SDimitry Andric   // Return true on error.
1050480093f4SDimitry Andric   bool parse(Option &O, StringRef ArgName, StringRef Arg, long long &Val);
1051480093f4SDimitry Andric 
105281ad6265SDimitry Andric   // Overload in subclass to provide a better default value.
1053480093f4SDimitry Andric   StringRef getValueName() const override { return "long"; }
1054480093f4SDimitry Andric 
1055480093f4SDimitry Andric   void printOptionDiff(const Option &O, long long V, OptVal Default,
1056480093f4SDimitry Andric                        size_t GlobalWidth) const;
1057480093f4SDimitry Andric 
1058480093f4SDimitry Andric   // An out-of-line virtual method to provide a 'home' for this class.
1059480093f4SDimitry Andric   void anchor() override;
1060480093f4SDimitry Andric };
1061480093f4SDimitry Andric 
1062480093f4SDimitry Andric //--------------------------------------------------
1063349cc55cSDimitry Andric 
1064349cc55cSDimitry Andric extern template class basic_parser<unsigned>;
1065349cc55cSDimitry Andric 
10660b57cec5SDimitry Andric template <> class parser<unsigned> : public basic_parser<unsigned> {
10670b57cec5SDimitry Andric public:
10680b57cec5SDimitry Andric   parser(Option &O) : basic_parser(O) {}
10690b57cec5SDimitry Andric 
107081ad6265SDimitry Andric   // Return true on error.
10710b57cec5SDimitry Andric   bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned &Val);
10720b57cec5SDimitry Andric 
107381ad6265SDimitry Andric   // Overload in subclass to provide a better default value.
10740b57cec5SDimitry Andric   StringRef getValueName() const override { return "uint"; }
10750b57cec5SDimitry Andric 
10760b57cec5SDimitry Andric   void printOptionDiff(const Option &O, unsigned V, OptVal Default,
10770b57cec5SDimitry Andric                        size_t GlobalWidth) const;
10780b57cec5SDimitry Andric 
10790b57cec5SDimitry Andric   // An out-of-line virtual method to provide a 'home' for this class.
10800b57cec5SDimitry Andric   void anchor() override;
10810b57cec5SDimitry Andric };
10820b57cec5SDimitry Andric 
10830b57cec5SDimitry Andric //--------------------------------------------------
1084349cc55cSDimitry Andric 
1085349cc55cSDimitry Andric extern template class basic_parser<unsigned long>;
1086349cc55cSDimitry Andric 
10870b57cec5SDimitry Andric template <>
10880b57cec5SDimitry Andric class parser<unsigned long> final : public basic_parser<unsigned long> {
10890b57cec5SDimitry Andric public:
10900b57cec5SDimitry Andric   parser(Option &O) : basic_parser(O) {}
10910b57cec5SDimitry Andric 
109281ad6265SDimitry Andric   // Return true on error.
10930b57cec5SDimitry Andric   bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned long &Val);
10940b57cec5SDimitry Andric 
109581ad6265SDimitry Andric   // Overload in subclass to provide a better default value.
10960b57cec5SDimitry Andric   StringRef getValueName() const override { return "ulong"; }
10970b57cec5SDimitry Andric 
10980b57cec5SDimitry Andric   void printOptionDiff(const Option &O, unsigned long V, OptVal Default,
10990b57cec5SDimitry Andric                        size_t GlobalWidth) const;
11000b57cec5SDimitry Andric 
11010b57cec5SDimitry Andric   // An out-of-line virtual method to provide a 'home' for this class.
11020b57cec5SDimitry Andric   void anchor() override;
11030b57cec5SDimitry Andric };
11040b57cec5SDimitry Andric 
11050b57cec5SDimitry Andric //--------------------------------------------------
1106349cc55cSDimitry Andric 
1107349cc55cSDimitry Andric extern template class basic_parser<unsigned long long>;
1108349cc55cSDimitry Andric 
11090b57cec5SDimitry Andric template <>
11100b57cec5SDimitry Andric class parser<unsigned long long> : public basic_parser<unsigned long long> {
11110b57cec5SDimitry Andric public:
11120b57cec5SDimitry Andric   parser(Option &O) : basic_parser(O) {}
11130b57cec5SDimitry Andric 
111481ad6265SDimitry Andric   // Return true on error.
11150b57cec5SDimitry Andric   bool parse(Option &O, StringRef ArgName, StringRef Arg,
11160b57cec5SDimitry Andric              unsigned long long &Val);
11170b57cec5SDimitry Andric 
111881ad6265SDimitry Andric   // Overload in subclass to provide a better default value.
11190b57cec5SDimitry Andric   StringRef getValueName() const override { return "ulong"; }
11200b57cec5SDimitry Andric 
11210b57cec5SDimitry Andric   void printOptionDiff(const Option &O, unsigned long long V, OptVal Default,
11220b57cec5SDimitry Andric                        size_t GlobalWidth) const;
11230b57cec5SDimitry Andric 
11240b57cec5SDimitry Andric   // An out-of-line virtual method to provide a 'home' for this class.
11250b57cec5SDimitry Andric   void anchor() override;
11260b57cec5SDimitry Andric };
11270b57cec5SDimitry Andric 
11280b57cec5SDimitry Andric //--------------------------------------------------
1129349cc55cSDimitry Andric 
1130349cc55cSDimitry Andric extern template class basic_parser<double>;
1131349cc55cSDimitry Andric 
11320b57cec5SDimitry Andric template <> class parser<double> : public basic_parser<double> {
11330b57cec5SDimitry Andric public:
11340b57cec5SDimitry Andric   parser(Option &O) : basic_parser(O) {}
11350b57cec5SDimitry Andric 
113681ad6265SDimitry Andric   // Return true on error.
11370b57cec5SDimitry Andric   bool parse(Option &O, StringRef ArgName, StringRef Arg, double &Val);
11380b57cec5SDimitry Andric 
113981ad6265SDimitry Andric   // Overload in subclass to provide a better default value.
11400b57cec5SDimitry Andric   StringRef getValueName() const override { return "number"; }
11410b57cec5SDimitry Andric 
11420b57cec5SDimitry Andric   void printOptionDiff(const Option &O, double V, OptVal Default,
11430b57cec5SDimitry Andric                        size_t GlobalWidth) const;
11440b57cec5SDimitry Andric 
11450b57cec5SDimitry Andric   // An out-of-line virtual method to provide a 'home' for this class.
11460b57cec5SDimitry Andric   void anchor() override;
11470b57cec5SDimitry Andric };
11480b57cec5SDimitry Andric 
11490b57cec5SDimitry Andric //--------------------------------------------------
1150349cc55cSDimitry Andric 
1151349cc55cSDimitry Andric extern template class basic_parser<float>;
1152349cc55cSDimitry Andric 
11530b57cec5SDimitry Andric template <> class parser<float> : public basic_parser<float> {
11540b57cec5SDimitry Andric public:
11550b57cec5SDimitry Andric   parser(Option &O) : basic_parser(O) {}
11560b57cec5SDimitry Andric 
115781ad6265SDimitry Andric   // Return true on error.
11580b57cec5SDimitry Andric   bool parse(Option &O, StringRef ArgName, StringRef Arg, float &Val);
11590b57cec5SDimitry Andric 
116081ad6265SDimitry Andric   // Overload in subclass to provide a better default value.
11610b57cec5SDimitry Andric   StringRef getValueName() const override { return "number"; }
11620b57cec5SDimitry Andric 
11630b57cec5SDimitry Andric   void printOptionDiff(const Option &O, float V, OptVal Default,
11640b57cec5SDimitry Andric                        size_t GlobalWidth) const;
11650b57cec5SDimitry Andric 
11660b57cec5SDimitry Andric   // An out-of-line virtual method to provide a 'home' for this class.
11670b57cec5SDimitry Andric   void anchor() override;
11680b57cec5SDimitry Andric };
11690b57cec5SDimitry Andric 
11700b57cec5SDimitry Andric //--------------------------------------------------
1171349cc55cSDimitry Andric 
1172349cc55cSDimitry Andric extern template class basic_parser<std::string>;
1173349cc55cSDimitry Andric 
11740b57cec5SDimitry Andric template <> class parser<std::string> : public basic_parser<std::string> {
11750b57cec5SDimitry Andric public:
11760b57cec5SDimitry Andric   parser(Option &O) : basic_parser(O) {}
11770b57cec5SDimitry Andric 
117881ad6265SDimitry Andric   // Return true on error.
11790b57cec5SDimitry Andric   bool parse(Option &, StringRef, StringRef Arg, std::string &Value) {
11800b57cec5SDimitry Andric     Value = Arg.str();
11810b57cec5SDimitry Andric     return false;
11820b57cec5SDimitry Andric   }
11830b57cec5SDimitry Andric 
118481ad6265SDimitry Andric   // Overload in subclass to provide a better default value.
11850b57cec5SDimitry Andric   StringRef getValueName() const override { return "string"; }
11860b57cec5SDimitry Andric 
11870b57cec5SDimitry Andric   void printOptionDiff(const Option &O, StringRef V, const OptVal &Default,
11880b57cec5SDimitry Andric                        size_t GlobalWidth) const;
11890b57cec5SDimitry Andric 
11900b57cec5SDimitry Andric   // An out-of-line virtual method to provide a 'home' for this class.
11910b57cec5SDimitry Andric   void anchor() override;
11920b57cec5SDimitry Andric };
11930b57cec5SDimitry Andric 
11940b57cec5SDimitry Andric //--------------------------------------------------
1195349cc55cSDimitry Andric 
1196349cc55cSDimitry Andric extern template class basic_parser<char>;
1197349cc55cSDimitry Andric 
11980b57cec5SDimitry Andric template <> class parser<char> : public basic_parser<char> {
11990b57cec5SDimitry Andric public:
12000b57cec5SDimitry Andric   parser(Option &O) : basic_parser(O) {}
12010b57cec5SDimitry Andric 
120281ad6265SDimitry Andric   // Return true on error.
12030b57cec5SDimitry Andric   bool parse(Option &, StringRef, StringRef Arg, char &Value) {
12040b57cec5SDimitry Andric     Value = Arg[0];
12050b57cec5SDimitry Andric     return false;
12060b57cec5SDimitry Andric   }
12070b57cec5SDimitry Andric 
120881ad6265SDimitry Andric   // Overload in subclass to provide a better default value.
12090b57cec5SDimitry Andric   StringRef getValueName() const override { return "char"; }
12100b57cec5SDimitry Andric 
12110b57cec5SDimitry Andric   void printOptionDiff(const Option &O, char V, OptVal Default,
12120b57cec5SDimitry Andric                        size_t GlobalWidth) const;
12130b57cec5SDimitry Andric 
12140b57cec5SDimitry Andric   // An out-of-line virtual method to provide a 'home' for this class.
12150b57cec5SDimitry Andric   void anchor() override;
12160b57cec5SDimitry Andric };
12170b57cec5SDimitry Andric 
12180b57cec5SDimitry Andric //--------------------------------------------------
12190b57cec5SDimitry Andric // This collection of wrappers is the intermediary between class opt and class
12200b57cec5SDimitry Andric // parser to handle all the template nastiness.
12210b57cec5SDimitry Andric 
12220b57cec5SDimitry Andric // This overloaded function is selected by the generic parser.
12230b57cec5SDimitry Andric template <class ParserClass, class DT>
12240b57cec5SDimitry Andric void printOptionDiff(const Option &O, const generic_parser_base &P, const DT &V,
12250b57cec5SDimitry Andric                      const OptionValue<DT> &Default, size_t GlobalWidth) {
12260b57cec5SDimitry Andric   OptionValue<DT> OV = V;
12270b57cec5SDimitry Andric   P.printOptionDiff(O, OV, Default, GlobalWidth);
12280b57cec5SDimitry Andric }
12290b57cec5SDimitry Andric 
12300b57cec5SDimitry Andric // This is instantiated for basic parsers when the parsed value has a different
12310b57cec5SDimitry Andric // type than the option value. e.g. HelpPrinter.
12320b57cec5SDimitry Andric template <class ParserDT, class ValDT> struct OptionDiffPrinter {
12330b57cec5SDimitry Andric   void print(const Option &O, const parser<ParserDT> &P, const ValDT & /*V*/,
12340b57cec5SDimitry Andric              const OptionValue<ValDT> & /*Default*/, size_t GlobalWidth) {
12350b57cec5SDimitry Andric     P.printOptionNoValue(O, GlobalWidth);
12360b57cec5SDimitry Andric   }
12370b57cec5SDimitry Andric };
12380b57cec5SDimitry Andric 
12390b57cec5SDimitry Andric // This is instantiated for basic parsers when the parsed value has the same
12400b57cec5SDimitry Andric // type as the option value.
12410b57cec5SDimitry Andric template <class DT> struct OptionDiffPrinter<DT, DT> {
12420b57cec5SDimitry Andric   void print(const Option &O, const parser<DT> &P, const DT &V,
12430b57cec5SDimitry Andric              const OptionValue<DT> &Default, size_t GlobalWidth) {
12440b57cec5SDimitry Andric     P.printOptionDiff(O, V, Default, GlobalWidth);
12450b57cec5SDimitry Andric   }
12460b57cec5SDimitry Andric };
12470b57cec5SDimitry Andric 
12480b57cec5SDimitry Andric // This overloaded function is selected by the basic parser, which may parse a
12490b57cec5SDimitry Andric // different type than the option type.
12500b57cec5SDimitry Andric template <class ParserClass, class ValDT>
12510b57cec5SDimitry Andric void printOptionDiff(
12520b57cec5SDimitry Andric     const Option &O,
12530b57cec5SDimitry Andric     const basic_parser<typename ParserClass::parser_data_type> &P,
12540b57cec5SDimitry Andric     const ValDT &V, const OptionValue<ValDT> &Default, size_t GlobalWidth) {
12550b57cec5SDimitry Andric 
12560b57cec5SDimitry Andric   OptionDiffPrinter<typename ParserClass::parser_data_type, ValDT> printer;
12570b57cec5SDimitry Andric   printer.print(O, static_cast<const ParserClass &>(P), V, Default,
12580b57cec5SDimitry Andric                 GlobalWidth);
12590b57cec5SDimitry Andric }
12600b57cec5SDimitry Andric 
12610b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
126281ad6265SDimitry Andric // This class is used because we must use partial specialization to handle
126381ad6265SDimitry Andric // literal string arguments specially (const char* does not correctly respond to
126481ad6265SDimitry Andric // the apply method). Because the syntax to use this is a pain, we have the
126581ad6265SDimitry Andric // 'apply' method below to handle the nastiness...
12660b57cec5SDimitry Andric //
12670b57cec5SDimitry Andric template <class Mod> struct applicator {
12680b57cec5SDimitry Andric   template <class Opt> static void opt(const Mod &M, Opt &O) { M.apply(O); }
12690b57cec5SDimitry Andric };
12700b57cec5SDimitry Andric 
12710b57cec5SDimitry Andric // Handle const char* as a special case...
12720b57cec5SDimitry Andric template <unsigned n> struct applicator<char[n]> {
12730b57cec5SDimitry Andric   template <class Opt> static void opt(StringRef Str, Opt &O) {
12740b57cec5SDimitry Andric     O.setArgStr(Str);
12750b57cec5SDimitry Andric   }
12760b57cec5SDimitry Andric };
12770b57cec5SDimitry Andric template <unsigned n> struct applicator<const char[n]> {
12780b57cec5SDimitry Andric   template <class Opt> static void opt(StringRef Str, Opt &O) {
12790b57cec5SDimitry Andric     O.setArgStr(Str);
12800b57cec5SDimitry Andric   }
12810b57cec5SDimitry Andric };
12820b57cec5SDimitry Andric template <> struct applicator<StringRef > {
12830b57cec5SDimitry Andric   template <class Opt> static void opt(StringRef Str, Opt &O) {
12840b57cec5SDimitry Andric     O.setArgStr(Str);
12850b57cec5SDimitry Andric   }
12860b57cec5SDimitry Andric };
12870b57cec5SDimitry Andric 
12880b57cec5SDimitry Andric template <> struct applicator<NumOccurrencesFlag> {
12890b57cec5SDimitry Andric   static void opt(NumOccurrencesFlag N, Option &O) {
12900b57cec5SDimitry Andric     O.setNumOccurrencesFlag(N);
12910b57cec5SDimitry Andric   }
12920b57cec5SDimitry Andric };
12930b57cec5SDimitry Andric 
12940b57cec5SDimitry Andric template <> struct applicator<ValueExpected> {
12950b57cec5SDimitry Andric   static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
12960b57cec5SDimitry Andric };
12970b57cec5SDimitry Andric 
12980b57cec5SDimitry Andric template <> struct applicator<OptionHidden> {
12990b57cec5SDimitry Andric   static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
13000b57cec5SDimitry Andric };
13010b57cec5SDimitry Andric 
13020b57cec5SDimitry Andric template <> struct applicator<FormattingFlags> {
13030b57cec5SDimitry Andric   static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
13040b57cec5SDimitry Andric };
13050b57cec5SDimitry Andric 
13060b57cec5SDimitry Andric template <> struct applicator<MiscFlags> {
13070b57cec5SDimitry Andric   static void opt(MiscFlags MF, Option &O) {
13080b57cec5SDimitry Andric     assert((MF != Grouping || O.ArgStr.size() == 1) &&
1309bdd1243dSDimitry Andric            "cl::Grouping can only apply to single character Options.");
13100b57cec5SDimitry Andric     O.setMiscFlag(MF);
13110b57cec5SDimitry Andric   }
13120b57cec5SDimitry Andric };
13130b57cec5SDimitry Andric 
131481ad6265SDimitry Andric // Apply modifiers to an option in a type safe way.
13150b57cec5SDimitry Andric template <class Opt, class Mod, class... Mods>
13160b57cec5SDimitry Andric void apply(Opt *O, const Mod &M, const Mods &... Ms) {
13170b57cec5SDimitry Andric   applicator<Mod>::opt(M, *O);
13180b57cec5SDimitry Andric   apply(O, Ms...);
13190b57cec5SDimitry Andric }
13200b57cec5SDimitry Andric 
13210b57cec5SDimitry Andric template <class Opt, class Mod> void apply(Opt *O, const Mod &M) {
13220b57cec5SDimitry Andric   applicator<Mod>::opt(M, *O);
13230b57cec5SDimitry Andric }
13240b57cec5SDimitry Andric 
13250b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
13260b57cec5SDimitry Andric // Default storage class definition: external storage.  This implementation
13270b57cec5SDimitry Andric // assumes the user will specify a variable to store the data into with the
13280b57cec5SDimitry Andric // cl::location(x) modifier.
13290b57cec5SDimitry Andric //
13300b57cec5SDimitry Andric template <class DataType, bool ExternalStorage, bool isClass>
13310b57cec5SDimitry Andric class opt_storage {
13320b57cec5SDimitry Andric   DataType *Location = nullptr; // Where to store the object...
13330b57cec5SDimitry Andric   OptionValue<DataType> Default;
13340b57cec5SDimitry Andric 
13350b57cec5SDimitry Andric   void check_location() const {
13360b57cec5SDimitry Andric     assert(Location && "cl::location(...) not specified for a command "
13370b57cec5SDimitry Andric                        "line option with external storage, "
13380b57cec5SDimitry Andric                        "or cl::init specified before cl::location()!!");
13390b57cec5SDimitry Andric   }
13400b57cec5SDimitry Andric 
13410b57cec5SDimitry Andric public:
13420b57cec5SDimitry Andric   opt_storage() = default;
13430b57cec5SDimitry Andric 
13440b57cec5SDimitry Andric   bool setLocation(Option &O, DataType &L) {
13450b57cec5SDimitry Andric     if (Location)
13460b57cec5SDimitry Andric       return O.error("cl::location(x) specified more than once!");
13470b57cec5SDimitry Andric     Location = &L;
13480b57cec5SDimitry Andric     Default = L;
13490b57cec5SDimitry Andric     return false;
13500b57cec5SDimitry Andric   }
13510b57cec5SDimitry Andric 
13520b57cec5SDimitry Andric   template <class T> void setValue(const T &V, bool initial = false) {
13530b57cec5SDimitry Andric     check_location();
13540b57cec5SDimitry Andric     *Location = V;
13550b57cec5SDimitry Andric     if (initial)
13560b57cec5SDimitry Andric       Default = V;
13570b57cec5SDimitry Andric   }
13580b57cec5SDimitry Andric 
13590b57cec5SDimitry Andric   DataType &getValue() {
13600b57cec5SDimitry Andric     check_location();
13610b57cec5SDimitry Andric     return *Location;
13620b57cec5SDimitry Andric   }
13630b57cec5SDimitry Andric   const DataType &getValue() const {
13640b57cec5SDimitry Andric     check_location();
13650b57cec5SDimitry Andric     return *Location;
13660b57cec5SDimitry Andric   }
13670b57cec5SDimitry Andric 
13680b57cec5SDimitry Andric   operator DataType() const { return this->getValue(); }
13690b57cec5SDimitry Andric 
13700b57cec5SDimitry Andric   const OptionValue<DataType> &getDefault() const { return Default; }
13710b57cec5SDimitry Andric };
13720b57cec5SDimitry Andric 
13730b57cec5SDimitry Andric // Define how to hold a class type object, such as a string.  Since we can
13740b57cec5SDimitry Andric // inherit from a class, we do so.  This makes us exactly compatible with the
13750b57cec5SDimitry Andric // object in all cases that it is used.
13760b57cec5SDimitry Andric //
13770b57cec5SDimitry Andric template <class DataType>
13780b57cec5SDimitry Andric class opt_storage<DataType, false, true> : public DataType {
13790b57cec5SDimitry Andric public:
13800b57cec5SDimitry Andric   OptionValue<DataType> Default;
13810b57cec5SDimitry Andric 
13820b57cec5SDimitry Andric   template <class T> void setValue(const T &V, bool initial = false) {
13830b57cec5SDimitry Andric     DataType::operator=(V);
13840b57cec5SDimitry Andric     if (initial)
13850b57cec5SDimitry Andric       Default = V;
13860b57cec5SDimitry Andric   }
13870b57cec5SDimitry Andric 
13880b57cec5SDimitry Andric   DataType &getValue() { return *this; }
13890b57cec5SDimitry Andric   const DataType &getValue() const { return *this; }
13900b57cec5SDimitry Andric 
13910b57cec5SDimitry Andric   const OptionValue<DataType> &getDefault() const { return Default; }
13920b57cec5SDimitry Andric };
13930b57cec5SDimitry Andric 
13940b57cec5SDimitry Andric // Define a partial specialization to handle things we cannot inherit from.  In
13950b57cec5SDimitry Andric // this case, we store an instance through containment, and overload operators
13960b57cec5SDimitry Andric // to get at the value.
13970b57cec5SDimitry Andric //
13980b57cec5SDimitry Andric template <class DataType> class opt_storage<DataType, false, false> {
13990b57cec5SDimitry Andric public:
14000b57cec5SDimitry Andric   DataType Value;
14010b57cec5SDimitry Andric   OptionValue<DataType> Default;
14020b57cec5SDimitry Andric 
14030b57cec5SDimitry Andric   // Make sure we initialize the value with the default constructor for the
14040b57cec5SDimitry Andric   // type.
140581ad6265SDimitry Andric   opt_storage() : Value(DataType()), Default() {}
14060b57cec5SDimitry Andric 
14070b57cec5SDimitry Andric   template <class T> void setValue(const T &V, bool initial = false) {
14080b57cec5SDimitry Andric     Value = V;
14090b57cec5SDimitry Andric     if (initial)
14100b57cec5SDimitry Andric       Default = V;
14110b57cec5SDimitry Andric   }
14120b57cec5SDimitry Andric   DataType &getValue() { return Value; }
14130b57cec5SDimitry Andric   DataType getValue() const { return Value; }
14140b57cec5SDimitry Andric 
14150b57cec5SDimitry Andric   const OptionValue<DataType> &getDefault() const { return Default; }
14160b57cec5SDimitry Andric 
14170b57cec5SDimitry Andric   operator DataType() const { return getValue(); }
14180b57cec5SDimitry Andric 
14190b57cec5SDimitry Andric   // If the datatype is a pointer, support -> on it.
14200b57cec5SDimitry Andric   DataType operator->() const { return Value; }
14210b57cec5SDimitry Andric };
14220b57cec5SDimitry Andric 
14230b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
142481ad6265SDimitry Andric // A scalar command line option.
14250b57cec5SDimitry Andric //
14260b57cec5SDimitry Andric template <class DataType, bool ExternalStorage = false,
14270b57cec5SDimitry Andric           class ParserClass = parser<DataType>>
142806c3fb27SDimitry Andric class opt
142906c3fb27SDimitry Andric     : public Option,
143006c3fb27SDimitry Andric       public opt_storage<DataType, ExternalStorage, std::is_class_v<DataType>> {
14310b57cec5SDimitry Andric   ParserClass Parser;
14320b57cec5SDimitry Andric 
14330b57cec5SDimitry Andric   bool handleOccurrence(unsigned pos, StringRef ArgName,
14340b57cec5SDimitry Andric                         StringRef Arg) override {
14350b57cec5SDimitry Andric     typename ParserClass::parser_data_type Val =
14360b57cec5SDimitry Andric         typename ParserClass::parser_data_type();
14370b57cec5SDimitry Andric     if (Parser.parse(*this, ArgName, Arg, Val))
14380b57cec5SDimitry Andric       return true; // Parse error!
14390b57cec5SDimitry Andric     this->setValue(Val);
14400b57cec5SDimitry Andric     this->setPosition(pos);
1441480093f4SDimitry Andric     Callback(Val);
14420b57cec5SDimitry Andric     return false;
14430b57cec5SDimitry Andric   }
14440b57cec5SDimitry Andric 
14450b57cec5SDimitry Andric   enum ValueExpected getValueExpectedFlagDefault() const override {
14460b57cec5SDimitry Andric     return Parser.getValueExpectedFlagDefault();
14470b57cec5SDimitry Andric   }
14480b57cec5SDimitry Andric 
14490b57cec5SDimitry Andric   void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) override {
14500b57cec5SDimitry Andric     return Parser.getExtraOptionNames(OptionNames);
14510b57cec5SDimitry Andric   }
14520b57cec5SDimitry Andric 
14530b57cec5SDimitry Andric   // Forward printing stuff to the parser...
14540b57cec5SDimitry Andric   size_t getOptionWidth() const override {
14550b57cec5SDimitry Andric     return Parser.getOptionWidth(*this);
14560b57cec5SDimitry Andric   }
14570b57cec5SDimitry Andric 
14580b57cec5SDimitry Andric   void printOptionInfo(size_t GlobalWidth) const override {
14590b57cec5SDimitry Andric     Parser.printOptionInfo(*this, GlobalWidth);
14600b57cec5SDimitry Andric   }
14610b57cec5SDimitry Andric 
14620b57cec5SDimitry Andric   void printOptionValue(size_t GlobalWidth, bool Force) const override {
14635f757f3fSDimitry Andric     if (Force || !this->getDefault().compare(this->getValue())) {
14640b57cec5SDimitry Andric       cl::printOptionDiff<ParserClass>(*this, Parser, this->getValue(),
14650b57cec5SDimitry Andric                                        this->getDefault(), GlobalWidth);
14660b57cec5SDimitry Andric     }
14670b57cec5SDimitry Andric   }
14680b57cec5SDimitry Andric 
146906c3fb27SDimitry Andric   template <class T, class = std::enable_if_t<std::is_assignable_v<T &, T>>>
14700b57cec5SDimitry Andric   void setDefaultImpl() {
14710b57cec5SDimitry Andric     const OptionValue<DataType> &V = this->getDefault();
14720b57cec5SDimitry Andric     if (V.hasValue())
14730b57cec5SDimitry Andric       this->setValue(V.getValue());
147481ad6265SDimitry Andric     else
147581ad6265SDimitry Andric       this->setValue(T());
14760b57cec5SDimitry Andric   }
14770b57cec5SDimitry Andric 
147806c3fb27SDimitry Andric   template <class T, class = std::enable_if_t<!std::is_assignable_v<T &, T>>>
14790b57cec5SDimitry Andric   void setDefaultImpl(...) {}
14800b57cec5SDimitry Andric 
14810b57cec5SDimitry Andric   void setDefault() override { setDefaultImpl<DataType>(); }
14820b57cec5SDimitry Andric 
14830b57cec5SDimitry Andric   void done() {
14840b57cec5SDimitry Andric     addArgument();
14850b57cec5SDimitry Andric     Parser.initialize();
14860b57cec5SDimitry Andric   }
14870b57cec5SDimitry Andric 
14880b57cec5SDimitry Andric public:
14890b57cec5SDimitry Andric   // Command line options should not be copyable
14900b57cec5SDimitry Andric   opt(const opt &) = delete;
14910b57cec5SDimitry Andric   opt &operator=(const opt &) = delete;
14920b57cec5SDimitry Andric 
14930b57cec5SDimitry Andric   // setInitialValue - Used by the cl::init modifier...
14940b57cec5SDimitry Andric   void setInitialValue(const DataType &V) { this->setValue(V, true); }
14950b57cec5SDimitry Andric 
14960b57cec5SDimitry Andric   ParserClass &getParser() { return Parser; }
14970b57cec5SDimitry Andric 
14980b57cec5SDimitry Andric   template <class T> DataType &operator=(const T &Val) {
14990b57cec5SDimitry Andric     this->setValue(Val);
1500480093f4SDimitry Andric     Callback(Val);
15010b57cec5SDimitry Andric     return this->getValue();
15020b57cec5SDimitry Andric   }
15030b57cec5SDimitry Andric 
15040b57cec5SDimitry Andric   template <class... Mods>
15050b57cec5SDimitry Andric   explicit opt(const Mods &... Ms)
1506e8d8bef9SDimitry Andric       : Option(llvm::cl::Optional, NotHidden), Parser(*this) {
15070b57cec5SDimitry Andric     apply(this, Ms...);
15080b57cec5SDimitry Andric     done();
15090b57cec5SDimitry Andric   }
1510480093f4SDimitry Andric 
1511480093f4SDimitry Andric   void setCallback(
1512480093f4SDimitry Andric       std::function<void(const typename ParserClass::parser_data_type &)> CB) {
1513480093f4SDimitry Andric     Callback = CB;
1514480093f4SDimitry Andric   }
1515480093f4SDimitry Andric 
1516480093f4SDimitry Andric   std::function<void(const typename ParserClass::parser_data_type &)> Callback =
1517480093f4SDimitry Andric       [](const typename ParserClass::parser_data_type &) {};
15180b57cec5SDimitry Andric };
15190b57cec5SDimitry Andric 
15200b57cec5SDimitry Andric extern template class opt<unsigned>;
15210b57cec5SDimitry Andric extern template class opt<int>;
15220b57cec5SDimitry Andric extern template class opt<std::string>;
15230b57cec5SDimitry Andric extern template class opt<char>;
15240b57cec5SDimitry Andric extern template class opt<bool>;
15250b57cec5SDimitry Andric 
15260b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
15270b57cec5SDimitry Andric // Default storage class definition: external storage.  This implementation
15280b57cec5SDimitry Andric // assumes the user will specify a variable to store the data into with the
15290b57cec5SDimitry Andric // cl::location(x) modifier.
15300b57cec5SDimitry Andric //
15310b57cec5SDimitry Andric template <class DataType, class StorageClass> class list_storage {
15320b57cec5SDimitry Andric   StorageClass *Location = nullptr; // Where to store the object...
1533bdd1243dSDimitry Andric   std::vector<OptionValue<DataType>> Default =
1534bdd1243dSDimitry Andric       std::vector<OptionValue<DataType>>();
1535bdd1243dSDimitry Andric   bool DefaultAssigned = false;
15360b57cec5SDimitry Andric 
15370b57cec5SDimitry Andric public:
15380b57cec5SDimitry Andric   list_storage() = default;
15390b57cec5SDimitry Andric 
15400b57cec5SDimitry Andric   void clear() {}
15410b57cec5SDimitry Andric 
15420b57cec5SDimitry Andric   bool setLocation(Option &O, StorageClass &L) {
15430b57cec5SDimitry Andric     if (Location)
15440b57cec5SDimitry Andric       return O.error("cl::location(x) specified more than once!");
15450b57cec5SDimitry Andric     Location = &L;
15460b57cec5SDimitry Andric     return false;
15470b57cec5SDimitry Andric   }
15480b57cec5SDimitry Andric 
1549bdd1243dSDimitry Andric   template <class T> void addValue(const T &V, bool initial = false) {
155004eeddc0SDimitry Andric     assert(Location != nullptr &&
155104eeddc0SDimitry Andric            "cl::location(...) not specified for a command "
15520b57cec5SDimitry Andric            "line option with external storage!");
15530b57cec5SDimitry Andric     Location->push_back(V);
1554bdd1243dSDimitry Andric     if (initial)
1555bdd1243dSDimitry Andric       Default.push_back(V);
15560b57cec5SDimitry Andric   }
1557bdd1243dSDimitry Andric 
1558bdd1243dSDimitry Andric   const std::vector<OptionValue<DataType>> &getDefault() const {
1559bdd1243dSDimitry Andric     return Default;
1560bdd1243dSDimitry Andric   }
1561bdd1243dSDimitry Andric 
1562bdd1243dSDimitry Andric   void assignDefault() { DefaultAssigned = true; }
1563bdd1243dSDimitry Andric   void overwriteDefault() { DefaultAssigned = false; }
1564bdd1243dSDimitry Andric   bool isDefaultAssigned() { return DefaultAssigned; }
15650b57cec5SDimitry Andric };
15660b57cec5SDimitry Andric 
15670b57cec5SDimitry Andric // Define how to hold a class type object, such as a string.
15680b57cec5SDimitry Andric // Originally this code inherited from std::vector. In transitioning to a new
15690b57cec5SDimitry Andric // API for command line options we should change this. The new implementation
15700b57cec5SDimitry Andric // of this list_storage specialization implements the minimum subset of the
15710b57cec5SDimitry Andric // std::vector API required for all the current clients.
15720b57cec5SDimitry Andric //
15730b57cec5SDimitry Andric // FIXME: Reduce this API to a more narrow subset of std::vector
15740b57cec5SDimitry Andric //
15750b57cec5SDimitry Andric template <class DataType> class list_storage<DataType, bool> {
15760b57cec5SDimitry Andric   std::vector<DataType> Storage;
1577bdd1243dSDimitry Andric   std::vector<OptionValue<DataType>> Default;
1578bdd1243dSDimitry Andric   bool DefaultAssigned = false;
15790b57cec5SDimitry Andric 
15800b57cec5SDimitry Andric public:
15810b57cec5SDimitry Andric   using iterator = typename std::vector<DataType>::iterator;
15820b57cec5SDimitry Andric 
15830b57cec5SDimitry Andric   iterator begin() { return Storage.begin(); }
15840b57cec5SDimitry Andric   iterator end() { return Storage.end(); }
15850b57cec5SDimitry Andric 
15860b57cec5SDimitry Andric   using const_iterator = typename std::vector<DataType>::const_iterator;
15870b57cec5SDimitry Andric 
15880b57cec5SDimitry Andric   const_iterator begin() const { return Storage.begin(); }
15890b57cec5SDimitry Andric   const_iterator end() const { return Storage.end(); }
15900b57cec5SDimitry Andric 
15910b57cec5SDimitry Andric   using size_type = typename std::vector<DataType>::size_type;
15920b57cec5SDimitry Andric 
15930b57cec5SDimitry Andric   size_type size() const { return Storage.size(); }
15940b57cec5SDimitry Andric 
15950b57cec5SDimitry Andric   bool empty() const { return Storage.empty(); }
15960b57cec5SDimitry Andric 
15970b57cec5SDimitry Andric   void push_back(const DataType &value) { Storage.push_back(value); }
15980b57cec5SDimitry Andric   void push_back(DataType &&value) { Storage.push_back(value); }
15990b57cec5SDimitry Andric 
16000b57cec5SDimitry Andric   using reference = typename std::vector<DataType>::reference;
16010b57cec5SDimitry Andric   using const_reference = typename std::vector<DataType>::const_reference;
16020b57cec5SDimitry Andric 
16030b57cec5SDimitry Andric   reference operator[](size_type pos) { return Storage[pos]; }
16040b57cec5SDimitry Andric   const_reference operator[](size_type pos) const { return Storage[pos]; }
16050b57cec5SDimitry Andric 
16060b57cec5SDimitry Andric   void clear() {
16070b57cec5SDimitry Andric     Storage.clear();
16080b57cec5SDimitry Andric   }
16090b57cec5SDimitry Andric 
16100b57cec5SDimitry Andric   iterator erase(const_iterator pos) { return Storage.erase(pos); }
16110b57cec5SDimitry Andric   iterator erase(const_iterator first, const_iterator last) {
16120b57cec5SDimitry Andric     return Storage.erase(first, last);
16130b57cec5SDimitry Andric   }
16140b57cec5SDimitry Andric 
16150b57cec5SDimitry Andric   iterator erase(iterator pos) { return Storage.erase(pos); }
16160b57cec5SDimitry Andric   iterator erase(iterator first, iterator last) {
16170b57cec5SDimitry Andric     return Storage.erase(first, last);
16180b57cec5SDimitry Andric   }
16190b57cec5SDimitry Andric 
16200b57cec5SDimitry Andric   iterator insert(const_iterator pos, const DataType &value) {
16210b57cec5SDimitry Andric     return Storage.insert(pos, value);
16220b57cec5SDimitry Andric   }
16230b57cec5SDimitry Andric   iterator insert(const_iterator pos, DataType &&value) {
16240b57cec5SDimitry Andric     return Storage.insert(pos, value);
16250b57cec5SDimitry Andric   }
16260b57cec5SDimitry Andric 
16270b57cec5SDimitry Andric   iterator insert(iterator pos, const DataType &value) {
16280b57cec5SDimitry Andric     return Storage.insert(pos, value);
16290b57cec5SDimitry Andric   }
16300b57cec5SDimitry Andric   iterator insert(iterator pos, DataType &&value) {
16310b57cec5SDimitry Andric     return Storage.insert(pos, value);
16320b57cec5SDimitry Andric   }
16330b57cec5SDimitry Andric 
16340b57cec5SDimitry Andric   reference front() { return Storage.front(); }
16350b57cec5SDimitry Andric   const_reference front() const { return Storage.front(); }
16360b57cec5SDimitry Andric 
16370b57cec5SDimitry Andric   operator std::vector<DataType> &() { return Storage; }
16385ffd83dbSDimitry Andric   operator ArrayRef<DataType>() const { return Storage; }
16390b57cec5SDimitry Andric   std::vector<DataType> *operator&() { return &Storage; }
16400b57cec5SDimitry Andric   const std::vector<DataType> *operator&() const { return &Storage; }
16410b57cec5SDimitry Andric 
1642bdd1243dSDimitry Andric   template <class T> void addValue(const T &V, bool initial = false) {
1643bdd1243dSDimitry Andric     Storage.push_back(V);
1644bdd1243dSDimitry Andric     if (initial)
1645bdd1243dSDimitry Andric       Default.push_back(OptionValue<DataType>(V));
1646bdd1243dSDimitry Andric   }
1647bdd1243dSDimitry Andric 
1648bdd1243dSDimitry Andric   const std::vector<OptionValue<DataType>> &getDefault() const {
1649bdd1243dSDimitry Andric     return Default;
1650bdd1243dSDimitry Andric   }
1651bdd1243dSDimitry Andric 
1652bdd1243dSDimitry Andric   void assignDefault() { DefaultAssigned = true; }
1653bdd1243dSDimitry Andric   void overwriteDefault() { DefaultAssigned = false; }
1654bdd1243dSDimitry Andric   bool isDefaultAssigned() { return DefaultAssigned; }
16550b57cec5SDimitry Andric };
16560b57cec5SDimitry Andric 
16570b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
165881ad6265SDimitry Andric // A list of command line options.
16590b57cec5SDimitry Andric //
16600b57cec5SDimitry Andric template <class DataType, class StorageClass = bool,
16610b57cec5SDimitry Andric           class ParserClass = parser<DataType>>
16620b57cec5SDimitry Andric class list : public Option, public list_storage<DataType, StorageClass> {
16630b57cec5SDimitry Andric   std::vector<unsigned> Positions;
16640b57cec5SDimitry Andric   ParserClass Parser;
16650b57cec5SDimitry Andric 
16660b57cec5SDimitry Andric   enum ValueExpected getValueExpectedFlagDefault() const override {
16670b57cec5SDimitry Andric     return Parser.getValueExpectedFlagDefault();
16680b57cec5SDimitry Andric   }
16690b57cec5SDimitry Andric 
16700b57cec5SDimitry Andric   void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) override {
16710b57cec5SDimitry Andric     return Parser.getExtraOptionNames(OptionNames);
16720b57cec5SDimitry Andric   }
16730b57cec5SDimitry Andric 
16740b57cec5SDimitry Andric   bool handleOccurrence(unsigned pos, StringRef ArgName,
16750b57cec5SDimitry Andric                         StringRef Arg) override {
16760b57cec5SDimitry Andric     typename ParserClass::parser_data_type Val =
16770b57cec5SDimitry Andric         typename ParserClass::parser_data_type();
1678bdd1243dSDimitry Andric     if (list_storage<DataType, StorageClass>::isDefaultAssigned()) {
1679bdd1243dSDimitry Andric       clear();
1680bdd1243dSDimitry Andric       list_storage<DataType, StorageClass>::overwriteDefault();
1681bdd1243dSDimitry Andric     }
16820b57cec5SDimitry Andric     if (Parser.parse(*this, ArgName, Arg, Val))
16830b57cec5SDimitry Andric       return true; // Parse Error!
16840b57cec5SDimitry Andric     list_storage<DataType, StorageClass>::addValue(Val);
16850b57cec5SDimitry Andric     setPosition(pos);
16860b57cec5SDimitry Andric     Positions.push_back(pos);
1687480093f4SDimitry Andric     Callback(Val);
16880b57cec5SDimitry Andric     return false;
16890b57cec5SDimitry Andric   }
16900b57cec5SDimitry Andric 
16910b57cec5SDimitry Andric   // Forward printing stuff to the parser...
16920b57cec5SDimitry Andric   size_t getOptionWidth() const override {
16930b57cec5SDimitry Andric     return Parser.getOptionWidth(*this);
16940b57cec5SDimitry Andric   }
16950b57cec5SDimitry Andric 
16960b57cec5SDimitry Andric   void printOptionInfo(size_t GlobalWidth) const override {
16970b57cec5SDimitry Andric     Parser.printOptionInfo(*this, GlobalWidth);
16980b57cec5SDimitry Andric   }
16990b57cec5SDimitry Andric 
17000b57cec5SDimitry Andric   // Unimplemented: list options don't currently store their default value.
17010b57cec5SDimitry Andric   void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
17020b57cec5SDimitry Andric   }
17030b57cec5SDimitry Andric 
17040b57cec5SDimitry Andric   void setDefault() override {
17050b57cec5SDimitry Andric     Positions.clear();
17060b57cec5SDimitry Andric     list_storage<DataType, StorageClass>::clear();
1707bdd1243dSDimitry Andric     for (auto &Val : list_storage<DataType, StorageClass>::getDefault())
1708bdd1243dSDimitry Andric       list_storage<DataType, StorageClass>::addValue(Val.getValue());
17090b57cec5SDimitry Andric   }
17100b57cec5SDimitry Andric 
17110b57cec5SDimitry Andric   void done() {
17120b57cec5SDimitry Andric     addArgument();
17130b57cec5SDimitry Andric     Parser.initialize();
17140b57cec5SDimitry Andric   }
17150b57cec5SDimitry Andric 
17160b57cec5SDimitry Andric public:
17170b57cec5SDimitry Andric   // Command line options should not be copyable
17180b57cec5SDimitry Andric   list(const list &) = delete;
17190b57cec5SDimitry Andric   list &operator=(const list &) = delete;
17200b57cec5SDimitry Andric 
17210b57cec5SDimitry Andric   ParserClass &getParser() { return Parser; }
17220b57cec5SDimitry Andric 
17230b57cec5SDimitry Andric   unsigned getPosition(unsigned optnum) const {
17240b57cec5SDimitry Andric     assert(optnum < this->size() && "Invalid option index");
17250b57cec5SDimitry Andric     return Positions[optnum];
17260b57cec5SDimitry Andric   }
17270b57cec5SDimitry Andric 
1728bdd1243dSDimitry Andric   void clear() {
1729bdd1243dSDimitry Andric     Positions.clear();
1730bdd1243dSDimitry Andric     list_storage<DataType, StorageClass>::clear();
1731bdd1243dSDimitry Andric   }
1732bdd1243dSDimitry Andric 
1733bdd1243dSDimitry Andric   // setInitialValues - Used by the cl::list_init modifier...
1734bdd1243dSDimitry Andric   void setInitialValues(ArrayRef<DataType> Vs) {
1735bdd1243dSDimitry Andric     assert(!(list_storage<DataType, StorageClass>::isDefaultAssigned()) &&
1736bdd1243dSDimitry Andric            "Cannot have two default values");
1737bdd1243dSDimitry Andric     list_storage<DataType, StorageClass>::assignDefault();
1738bdd1243dSDimitry Andric     for (auto &Val : Vs)
1739bdd1243dSDimitry Andric       list_storage<DataType, StorageClass>::addValue(Val, true);
1740bdd1243dSDimitry Andric   }
1741bdd1243dSDimitry Andric 
17420b57cec5SDimitry Andric   void setNumAdditionalVals(unsigned n) { Option::setNumAdditionalVals(n); }
17430b57cec5SDimitry Andric 
17440b57cec5SDimitry Andric   template <class... Mods>
17450b57cec5SDimitry Andric   explicit list(const Mods &... Ms)
17460b57cec5SDimitry Andric       : Option(ZeroOrMore, NotHidden), Parser(*this) {
17470b57cec5SDimitry Andric     apply(this, Ms...);
17480b57cec5SDimitry Andric     done();
17490b57cec5SDimitry Andric   }
1750480093f4SDimitry Andric 
1751480093f4SDimitry Andric   void setCallback(
1752480093f4SDimitry Andric       std::function<void(const typename ParserClass::parser_data_type &)> CB) {
1753480093f4SDimitry Andric     Callback = CB;
1754480093f4SDimitry Andric   }
1755480093f4SDimitry Andric 
1756480093f4SDimitry Andric   std::function<void(const typename ParserClass::parser_data_type &)> Callback =
1757480093f4SDimitry Andric       [](const typename ParserClass::parser_data_type &) {};
17580b57cec5SDimitry Andric };
17590b57cec5SDimitry Andric 
176081ad6265SDimitry Andric // Modifier to set the number of additional values.
17610b57cec5SDimitry Andric struct multi_val {
17620b57cec5SDimitry Andric   unsigned AdditionalVals;
17630b57cec5SDimitry Andric   explicit multi_val(unsigned N) : AdditionalVals(N) {}
17640b57cec5SDimitry Andric 
17650b57cec5SDimitry Andric   template <typename D, typename S, typename P>
17660b57cec5SDimitry Andric   void apply(list<D, S, P> &L) const {
17670b57cec5SDimitry Andric     L.setNumAdditionalVals(AdditionalVals);
17680b57cec5SDimitry Andric   }
17690b57cec5SDimitry Andric };
17700b57cec5SDimitry Andric 
17710b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
17720b57cec5SDimitry Andric // Default storage class definition: external storage.  This implementation
17730b57cec5SDimitry Andric // assumes the user will specify a variable to store the data into with the
17740b57cec5SDimitry Andric // cl::location(x) modifier.
17750b57cec5SDimitry Andric //
17760b57cec5SDimitry Andric template <class DataType, class StorageClass> class bits_storage {
17770b57cec5SDimitry Andric   unsigned *Location = nullptr; // Where to store the bits...
17780b57cec5SDimitry Andric 
17790b57cec5SDimitry Andric   template <class T> static unsigned Bit(const T &V) {
178081ad6265SDimitry Andric     unsigned BitPos = static_cast<unsigned>(V);
17810b57cec5SDimitry Andric     assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
17820b57cec5SDimitry Andric            "enum exceeds width of bit vector!");
17830b57cec5SDimitry Andric     return 1 << BitPos;
17840b57cec5SDimitry Andric   }
17850b57cec5SDimitry Andric 
17860b57cec5SDimitry Andric public:
17870b57cec5SDimitry Andric   bits_storage() = default;
17880b57cec5SDimitry Andric 
17890b57cec5SDimitry Andric   bool setLocation(Option &O, unsigned &L) {
17900b57cec5SDimitry Andric     if (Location)
17910b57cec5SDimitry Andric       return O.error("cl::location(x) specified more than once!");
17920b57cec5SDimitry Andric     Location = &L;
17930b57cec5SDimitry Andric     return false;
17940b57cec5SDimitry Andric   }
17950b57cec5SDimitry Andric 
17960b57cec5SDimitry Andric   template <class T> void addValue(const T &V) {
179704eeddc0SDimitry Andric     assert(Location != nullptr &&
179804eeddc0SDimitry Andric            "cl::location(...) not specified for a command "
17990b57cec5SDimitry Andric            "line option with external storage!");
18000b57cec5SDimitry Andric     *Location |= Bit(V);
18010b57cec5SDimitry Andric   }
18020b57cec5SDimitry Andric 
18030b57cec5SDimitry Andric   unsigned getBits() { return *Location; }
18040b57cec5SDimitry Andric 
180581ad6265SDimitry Andric   void clear() {
180681ad6265SDimitry Andric     if (Location)
180781ad6265SDimitry Andric       *Location = 0;
180881ad6265SDimitry Andric   }
180981ad6265SDimitry Andric 
18100b57cec5SDimitry Andric   template <class T> bool isSet(const T &V) {
18110b57cec5SDimitry Andric     return (*Location & Bit(V)) != 0;
18120b57cec5SDimitry Andric   }
18130b57cec5SDimitry Andric };
18140b57cec5SDimitry Andric 
18150b57cec5SDimitry Andric // Define how to hold bits.  Since we can inherit from a class, we do so.
18160b57cec5SDimitry Andric // This makes us exactly compatible with the bits in all cases that it is used.
18170b57cec5SDimitry Andric //
18180b57cec5SDimitry Andric template <class DataType> class bits_storage<DataType, bool> {
181981ad6265SDimitry Andric   unsigned Bits{0}; // Where to store the bits...
18200b57cec5SDimitry Andric 
18210b57cec5SDimitry Andric   template <class T> static unsigned Bit(const T &V) {
182281ad6265SDimitry Andric     unsigned BitPos = static_cast<unsigned>(V);
18230b57cec5SDimitry Andric     assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
18240b57cec5SDimitry Andric            "enum exceeds width of bit vector!");
18250b57cec5SDimitry Andric     return 1 << BitPos;
18260b57cec5SDimitry Andric   }
18270b57cec5SDimitry Andric 
18280b57cec5SDimitry Andric public:
18290b57cec5SDimitry Andric   template <class T> void addValue(const T &V) { Bits |= Bit(V); }
18300b57cec5SDimitry Andric 
18310b57cec5SDimitry Andric   unsigned getBits() { return Bits; }
18320b57cec5SDimitry Andric 
183381ad6265SDimitry Andric   void clear() { Bits = 0; }
183481ad6265SDimitry Andric 
18350b57cec5SDimitry Andric   template <class T> bool isSet(const T &V) { return (Bits & Bit(V)) != 0; }
18360b57cec5SDimitry Andric };
18370b57cec5SDimitry Andric 
18380b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
183981ad6265SDimitry Andric // A bit vector of command options.
18400b57cec5SDimitry Andric //
18410b57cec5SDimitry Andric template <class DataType, class Storage = bool,
18420b57cec5SDimitry Andric           class ParserClass = parser<DataType>>
18430b57cec5SDimitry Andric class bits : public Option, public bits_storage<DataType, Storage> {
18440b57cec5SDimitry Andric   std::vector<unsigned> Positions;
18450b57cec5SDimitry Andric   ParserClass Parser;
18460b57cec5SDimitry Andric 
18470b57cec5SDimitry Andric   enum ValueExpected getValueExpectedFlagDefault() const override {
18480b57cec5SDimitry Andric     return Parser.getValueExpectedFlagDefault();
18490b57cec5SDimitry Andric   }
18500b57cec5SDimitry Andric 
18510b57cec5SDimitry Andric   void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) override {
18520b57cec5SDimitry Andric     return Parser.getExtraOptionNames(OptionNames);
18530b57cec5SDimitry Andric   }
18540b57cec5SDimitry Andric 
18550b57cec5SDimitry Andric   bool handleOccurrence(unsigned pos, StringRef ArgName,
18560b57cec5SDimitry Andric                         StringRef Arg) override {
18570b57cec5SDimitry Andric     typename ParserClass::parser_data_type Val =
18580b57cec5SDimitry Andric         typename ParserClass::parser_data_type();
18590b57cec5SDimitry Andric     if (Parser.parse(*this, ArgName, Arg, Val))
18600b57cec5SDimitry Andric       return true; // Parse Error!
18610b57cec5SDimitry Andric     this->addValue(Val);
18620b57cec5SDimitry Andric     setPosition(pos);
18630b57cec5SDimitry Andric     Positions.push_back(pos);
1864480093f4SDimitry Andric     Callback(Val);
18650b57cec5SDimitry Andric     return false;
18660b57cec5SDimitry Andric   }
18670b57cec5SDimitry Andric 
18680b57cec5SDimitry Andric   // Forward printing stuff to the parser...
18690b57cec5SDimitry Andric   size_t getOptionWidth() const override {
18700b57cec5SDimitry Andric     return Parser.getOptionWidth(*this);
18710b57cec5SDimitry Andric   }
18720b57cec5SDimitry Andric 
18730b57cec5SDimitry Andric   void printOptionInfo(size_t GlobalWidth) const override {
18740b57cec5SDimitry Andric     Parser.printOptionInfo(*this, GlobalWidth);
18750b57cec5SDimitry Andric   }
18760b57cec5SDimitry Andric 
18770b57cec5SDimitry Andric   // Unimplemented: bits options don't currently store their default values.
18780b57cec5SDimitry Andric   void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
18790b57cec5SDimitry Andric   }
18800b57cec5SDimitry Andric 
188181ad6265SDimitry Andric   void setDefault() override { bits_storage<DataType, Storage>::clear(); }
18820b57cec5SDimitry Andric 
18830b57cec5SDimitry Andric   void done() {
18840b57cec5SDimitry Andric     addArgument();
18850b57cec5SDimitry Andric     Parser.initialize();
18860b57cec5SDimitry Andric   }
18870b57cec5SDimitry Andric 
18880b57cec5SDimitry Andric public:
18890b57cec5SDimitry Andric   // Command line options should not be copyable
18900b57cec5SDimitry Andric   bits(const bits &) = delete;
18910b57cec5SDimitry Andric   bits &operator=(const bits &) = delete;
18920b57cec5SDimitry Andric 
18930b57cec5SDimitry Andric   ParserClass &getParser() { return Parser; }
18940b57cec5SDimitry Andric 
18950b57cec5SDimitry Andric   unsigned getPosition(unsigned optnum) const {
18960b57cec5SDimitry Andric     assert(optnum < this->size() && "Invalid option index");
18970b57cec5SDimitry Andric     return Positions[optnum];
18980b57cec5SDimitry Andric   }
18990b57cec5SDimitry Andric 
19000b57cec5SDimitry Andric   template <class... Mods>
19010b57cec5SDimitry Andric   explicit bits(const Mods &... Ms)
19020b57cec5SDimitry Andric       : Option(ZeroOrMore, NotHidden), Parser(*this) {
19030b57cec5SDimitry Andric     apply(this, Ms...);
19040b57cec5SDimitry Andric     done();
19050b57cec5SDimitry Andric   }
1906480093f4SDimitry Andric 
1907480093f4SDimitry Andric   void setCallback(
1908480093f4SDimitry Andric       std::function<void(const typename ParserClass::parser_data_type &)> CB) {
1909480093f4SDimitry Andric     Callback = CB;
1910480093f4SDimitry Andric   }
1911480093f4SDimitry Andric 
1912480093f4SDimitry Andric   std::function<void(const typename ParserClass::parser_data_type &)> Callback =
1913480093f4SDimitry Andric       [](const typename ParserClass::parser_data_type &) {};
19140b57cec5SDimitry Andric };
19150b57cec5SDimitry Andric 
19160b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
19170b57cec5SDimitry Andric // Aliased command line option (alias this name to a preexisting name)
19180b57cec5SDimitry Andric //
19190b57cec5SDimitry Andric 
19200b57cec5SDimitry Andric class alias : public Option {
19210b57cec5SDimitry Andric   Option *AliasFor;
19220b57cec5SDimitry Andric 
19230b57cec5SDimitry Andric   bool handleOccurrence(unsigned pos, StringRef /*ArgName*/,
19240b57cec5SDimitry Andric                         StringRef Arg) override {
19250b57cec5SDimitry Andric     return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
19260b57cec5SDimitry Andric   }
19270b57cec5SDimitry Andric 
19280b57cec5SDimitry Andric   bool addOccurrence(unsigned pos, StringRef /*ArgName*/, StringRef Value,
19290b57cec5SDimitry Andric                      bool MultiArg = false) override {
19300b57cec5SDimitry Andric     return AliasFor->addOccurrence(pos, AliasFor->ArgStr, Value, MultiArg);
19310b57cec5SDimitry Andric   }
19320b57cec5SDimitry Andric 
19330b57cec5SDimitry Andric   // Handle printing stuff...
19340b57cec5SDimitry Andric   size_t getOptionWidth() const override;
19350b57cec5SDimitry Andric   void printOptionInfo(size_t GlobalWidth) const override;
19360b57cec5SDimitry Andric 
19370b57cec5SDimitry Andric   // Aliases do not need to print their values.
19380b57cec5SDimitry Andric   void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
19390b57cec5SDimitry Andric   }
19400b57cec5SDimitry Andric 
19410b57cec5SDimitry Andric   void setDefault() override { AliasFor->setDefault(); }
19420b57cec5SDimitry Andric 
19430b57cec5SDimitry Andric   ValueExpected getValueExpectedFlagDefault() const override {
19440b57cec5SDimitry Andric     return AliasFor->getValueExpectedFlag();
19450b57cec5SDimitry Andric   }
19460b57cec5SDimitry Andric 
19470b57cec5SDimitry Andric   void done() {
19480b57cec5SDimitry Andric     if (!hasArgStr())
19490b57cec5SDimitry Andric       error("cl::alias must have argument name specified!");
19500b57cec5SDimitry Andric     if (!AliasFor)
19510b57cec5SDimitry Andric       error("cl::alias must have an cl::aliasopt(option) specified!");
19520b57cec5SDimitry Andric     if (!Subs.empty())
19530b57cec5SDimitry Andric       error("cl::alias must not have cl::sub(), aliased option's cl::sub() will be used!");
19540b57cec5SDimitry Andric     Subs = AliasFor->Subs;
19550b57cec5SDimitry Andric     Categories = AliasFor->Categories;
19560b57cec5SDimitry Andric     addArgument();
19570b57cec5SDimitry Andric   }
19580b57cec5SDimitry Andric 
19590b57cec5SDimitry Andric public:
19600b57cec5SDimitry Andric   // Command line options should not be copyable
19610b57cec5SDimitry Andric   alias(const alias &) = delete;
19620b57cec5SDimitry Andric   alias &operator=(const alias &) = delete;
19630b57cec5SDimitry Andric 
19640b57cec5SDimitry Andric   void setAliasFor(Option &O) {
19650b57cec5SDimitry Andric     if (AliasFor)
19660b57cec5SDimitry Andric       error("cl::alias must only have one cl::aliasopt(...) specified!");
19670b57cec5SDimitry Andric     AliasFor = &O;
19680b57cec5SDimitry Andric   }
19690b57cec5SDimitry Andric 
19700b57cec5SDimitry Andric   template <class... Mods>
19710b57cec5SDimitry Andric   explicit alias(const Mods &... Ms)
19720b57cec5SDimitry Andric       : Option(Optional, Hidden), AliasFor(nullptr) {
19730b57cec5SDimitry Andric     apply(this, Ms...);
19740b57cec5SDimitry Andric     done();
19750b57cec5SDimitry Andric   }
19760b57cec5SDimitry Andric };
19770b57cec5SDimitry Andric 
197881ad6265SDimitry Andric // Modifier to set the option an alias aliases.
19790b57cec5SDimitry Andric struct aliasopt {
19800b57cec5SDimitry Andric   Option &Opt;
19810b57cec5SDimitry Andric 
19820b57cec5SDimitry Andric   explicit aliasopt(Option &O) : Opt(O) {}
19830b57cec5SDimitry Andric 
19840b57cec5SDimitry Andric   void apply(alias &A) const { A.setAliasFor(Opt); }
19850b57cec5SDimitry Andric };
19860b57cec5SDimitry Andric 
198781ad6265SDimitry Andric // Provide additional help at the end of the normal help output. All occurrences
198881ad6265SDimitry Andric // of cl::extrahelp will be accumulated and printed to stderr at the end of the
198981ad6265SDimitry Andric // regular help, just before exit is called.
19900b57cec5SDimitry Andric struct extrahelp {
19910b57cec5SDimitry Andric   StringRef morehelp;
19920b57cec5SDimitry Andric 
19930b57cec5SDimitry Andric   explicit extrahelp(StringRef help);
19940b57cec5SDimitry Andric };
19950b57cec5SDimitry Andric 
19960b57cec5SDimitry Andric void PrintVersionMessage();
19970b57cec5SDimitry Andric 
19980b57cec5SDimitry Andric /// This function just prints the help message, exactly the same way as if the
19990b57cec5SDimitry Andric /// -help or -help-hidden option had been given on the command line.
20000b57cec5SDimitry Andric ///
20010b57cec5SDimitry Andric /// \param Hidden if true will print hidden options
20020b57cec5SDimitry Andric /// \param Categorized if true print options in categories
20030b57cec5SDimitry Andric void PrintHelpMessage(bool Hidden = false, bool Categorized = false);
20040b57cec5SDimitry Andric 
20050b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
20060b57cec5SDimitry Andric // Public interface for accessing registered options.
20070b57cec5SDimitry Andric //
20080b57cec5SDimitry Andric 
20090b57cec5SDimitry Andric /// Use this to get a StringMap to all registered named options
2010480093f4SDimitry Andric /// (e.g. -help).
20110b57cec5SDimitry Andric ///
20120b57cec5SDimitry Andric /// \return A reference to the StringMap used by the cl APIs to parse options.
20130b57cec5SDimitry Andric ///
20140b57cec5SDimitry Andric /// Access to unnamed arguments (i.e. positional) are not provided because
20150b57cec5SDimitry Andric /// it is expected that the client already has access to these.
20160b57cec5SDimitry Andric ///
20170b57cec5SDimitry Andric /// Typical usage:
20180b57cec5SDimitry Andric /// \code
20190b57cec5SDimitry Andric /// main(int argc,char* argv[]) {
20200b57cec5SDimitry Andric /// StringMap<llvm::cl::Option*> &opts = llvm::cl::getRegisteredOptions();
20210b57cec5SDimitry Andric /// assert(opts.count("help") == 1)
20220b57cec5SDimitry Andric /// opts["help"]->setDescription("Show alphabetical help information")
20230b57cec5SDimitry Andric /// // More code
20240b57cec5SDimitry Andric /// llvm::cl::ParseCommandLineOptions(argc,argv);
20250b57cec5SDimitry Andric /// //More code
20260b57cec5SDimitry Andric /// }
20270b57cec5SDimitry Andric /// \endcode
20280b57cec5SDimitry Andric ///
20290b57cec5SDimitry Andric /// This interface is useful for modifying options in libraries that are out of
20300b57cec5SDimitry Andric /// the control of the client. The options should be modified before calling
20310b57cec5SDimitry Andric /// llvm::cl::ParseCommandLineOptions().
20320b57cec5SDimitry Andric ///
20330b57cec5SDimitry Andric /// Hopefully this API can be deprecated soon. Any situation where options need
20340b57cec5SDimitry Andric /// to be modified by tools or libraries should be handled by sane APIs rather
20350b57cec5SDimitry Andric /// than just handing around a global list.
2036bdd1243dSDimitry Andric StringMap<Option *> &
2037bdd1243dSDimitry Andric getRegisteredOptions(SubCommand &Sub = SubCommand::getTopLevel());
20380b57cec5SDimitry Andric 
20390b57cec5SDimitry Andric /// Use this to get all registered SubCommands from the provided parser.
20400b57cec5SDimitry Andric ///
20410b57cec5SDimitry Andric /// \return A range of all SubCommand pointers registered with the parser.
20420b57cec5SDimitry Andric ///
20430b57cec5SDimitry Andric /// Typical usage:
20440b57cec5SDimitry Andric /// \code
20450b57cec5SDimitry Andric /// main(int argc, char* argv[]) {
20460b57cec5SDimitry Andric ///   llvm::cl::ParseCommandLineOptions(argc, argv);
20470b57cec5SDimitry Andric ///   for (auto* S : llvm::cl::getRegisteredSubcommands()) {
20480b57cec5SDimitry Andric ///     if (*S) {
20490b57cec5SDimitry Andric ///       std::cout << "Executing subcommand: " << S->getName() << std::endl;
20500b57cec5SDimitry Andric ///       // Execute some function based on the name...
20510b57cec5SDimitry Andric ///     }
20520b57cec5SDimitry Andric ///   }
20530b57cec5SDimitry Andric /// }
20540b57cec5SDimitry Andric /// \endcode
20550b57cec5SDimitry Andric ///
20560b57cec5SDimitry Andric /// This interface is useful for defining subcommands in libraries and
20570b57cec5SDimitry Andric /// the dispatch from a single point (like in the main function).
20580b57cec5SDimitry Andric iterator_range<typename SmallPtrSet<SubCommand *, 4>::iterator>
20590b57cec5SDimitry Andric getRegisteredSubcommands();
20600b57cec5SDimitry Andric 
20610b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
20620b57cec5SDimitry Andric // Standalone command line processing utilities.
20630b57cec5SDimitry Andric //
20640b57cec5SDimitry Andric 
20650b57cec5SDimitry Andric /// Tokenizes a command line that can contain escapes and quotes.
20660b57cec5SDimitry Andric //
20670b57cec5SDimitry Andric /// The quoting rules match those used by GCC and other tools that use
20680b57cec5SDimitry Andric /// libiberty's buildargv() or expandargv() utilities, and do not match bash.
20690b57cec5SDimitry Andric /// They differ from buildargv() on treatment of backslashes that do not escape
20700b57cec5SDimitry Andric /// a special character to make it possible to accept most Windows file paths.
20710b57cec5SDimitry Andric ///
20720b57cec5SDimitry Andric /// \param [in] Source The string to be split on whitespace with quotes.
20730b57cec5SDimitry Andric /// \param [in] Saver Delegates back to the caller for saving parsed strings.
20740b57cec5SDimitry Andric /// \param [in] MarkEOLs true if tokenizing a response file and you want end of
20750b57cec5SDimitry Andric /// lines and end of the response file to be marked with a nullptr string.
20760b57cec5SDimitry Andric /// \param [out] NewArgv All parsed strings are appended to NewArgv.
20770b57cec5SDimitry Andric void TokenizeGNUCommandLine(StringRef Source, StringSaver &Saver,
20780b57cec5SDimitry Andric                             SmallVectorImpl<const char *> &NewArgv,
20790b57cec5SDimitry Andric                             bool MarkEOLs = false);
20800b57cec5SDimitry Andric 
208181ad6265SDimitry Andric /// Tokenizes a string of Windows command line arguments, which may contain
208281ad6265SDimitry Andric /// quotes and escaped quotes.
20830b57cec5SDimitry Andric ///
20840b57cec5SDimitry Andric /// See MSDN docs for CommandLineToArgvW for information on the quoting rules.
20850b57cec5SDimitry Andric /// http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx
20860b57cec5SDimitry Andric ///
208781ad6265SDimitry Andric /// For handling a full Windows command line including the executable name at
208881ad6265SDimitry Andric /// the start, see TokenizeWindowsCommandLineFull below.
208981ad6265SDimitry Andric ///
20900b57cec5SDimitry Andric /// \param [in] Source The string to be split on whitespace with quotes.
20910b57cec5SDimitry Andric /// \param [in] Saver Delegates back to the caller for saving parsed strings.
20920b57cec5SDimitry Andric /// \param [in] MarkEOLs true if tokenizing a response file and you want end of
20930b57cec5SDimitry Andric /// lines and end of the response file to be marked with a nullptr string.
20940b57cec5SDimitry Andric /// \param [out] NewArgv All parsed strings are appended to NewArgv.
20950b57cec5SDimitry Andric void TokenizeWindowsCommandLine(StringRef Source, StringSaver &Saver,
20960b57cec5SDimitry Andric                                 SmallVectorImpl<const char *> &NewArgv,
20970b57cec5SDimitry Andric                                 bool MarkEOLs = false);
20980b57cec5SDimitry Andric 
20995ffd83dbSDimitry Andric /// Tokenizes a Windows command line while attempting to avoid copies. If no
21005ffd83dbSDimitry Andric /// quoting or escaping was used, this produces substrings of the original
21015ffd83dbSDimitry Andric /// string. If a token requires unquoting, it will be allocated with the
21025ffd83dbSDimitry Andric /// StringSaver.
21035ffd83dbSDimitry Andric void TokenizeWindowsCommandLineNoCopy(StringRef Source, StringSaver &Saver,
21045ffd83dbSDimitry Andric                                       SmallVectorImpl<StringRef> &NewArgv);
21055ffd83dbSDimitry Andric 
210681ad6265SDimitry Andric /// Tokenizes a Windows full command line, including command name at the start.
210781ad6265SDimitry Andric ///
210881ad6265SDimitry Andric /// This uses the same syntax rules as TokenizeWindowsCommandLine for all but
210981ad6265SDimitry Andric /// the first token. But the first token is expected to be parsed as the
211081ad6265SDimitry Andric /// executable file name in the way CreateProcess would do it, rather than the
211181ad6265SDimitry Andric /// way the C library startup code would do it: CreateProcess does not consider
211281ad6265SDimitry Andric /// that \ is ever an escape character (because " is not a valid filename char,
211381ad6265SDimitry Andric /// hence there's never a need to escape it to be used literally).
211481ad6265SDimitry Andric ///
211581ad6265SDimitry Andric /// Parameters are the same as for TokenizeWindowsCommandLine. In particular,
211681ad6265SDimitry Andric /// if you set MarkEOLs = true, then the first word of every line will be
211781ad6265SDimitry Andric /// parsed using the special rules for command names, making this function
211881ad6265SDimitry Andric /// suitable for parsing a file full of commands to execute.
211981ad6265SDimitry Andric void TokenizeWindowsCommandLineFull(StringRef Source, StringSaver &Saver,
212081ad6265SDimitry Andric                                     SmallVectorImpl<const char *> &NewArgv,
212181ad6265SDimitry Andric                                     bool MarkEOLs = false);
212281ad6265SDimitry Andric 
21230b57cec5SDimitry Andric /// String tokenization function type.  Should be compatible with either
21240b57cec5SDimitry Andric /// Windows or Unix command line tokenizers.
21250b57cec5SDimitry Andric using TokenizerCallback = void (*)(StringRef Source, StringSaver &Saver,
21260b57cec5SDimitry Andric                                    SmallVectorImpl<const char *> &NewArgv,
21270b57cec5SDimitry Andric                                    bool MarkEOLs);
21280b57cec5SDimitry Andric 
21290b57cec5SDimitry Andric /// Tokenizes content of configuration file.
21300b57cec5SDimitry Andric ///
21310b57cec5SDimitry Andric /// \param [in] Source The string representing content of config file.
21320b57cec5SDimitry Andric /// \param [in] Saver Delegates back to the caller for saving parsed strings.
21330b57cec5SDimitry Andric /// \param [out] NewArgv All parsed strings are appended to NewArgv.
21340b57cec5SDimitry Andric /// \param [in] MarkEOLs Added for compatibility with TokenizerCallback.
21350b57cec5SDimitry Andric ///
21360b57cec5SDimitry Andric /// It works like TokenizeGNUCommandLine with ability to skip comment lines.
21370b57cec5SDimitry Andric ///
21380b57cec5SDimitry Andric void tokenizeConfigFile(StringRef Source, StringSaver &Saver,
21390b57cec5SDimitry Andric                         SmallVectorImpl<const char *> &NewArgv,
21400b57cec5SDimitry Andric                         bool MarkEOLs = false);
21410b57cec5SDimitry Andric 
2142bdd1243dSDimitry Andric /// Contains options that control response file expansion.
2143bdd1243dSDimitry Andric class ExpansionContext {
2144bdd1243dSDimitry Andric   /// Provides persistent storage for parsed strings.
2145bdd1243dSDimitry Andric   StringSaver Saver;
2146bdd1243dSDimitry Andric 
2147bdd1243dSDimitry Andric   /// Tokenization strategy. Typically Unix or Windows.
2148bdd1243dSDimitry Andric   TokenizerCallback Tokenizer;
2149bdd1243dSDimitry Andric 
2150bdd1243dSDimitry Andric   /// File system used for all file access when running the expansion.
2151bdd1243dSDimitry Andric   vfs::FileSystem *FS;
2152bdd1243dSDimitry Andric 
2153bdd1243dSDimitry Andric   /// Path used to resolve relative rsp files. If empty, the file system
2154bdd1243dSDimitry Andric   /// current directory is used instead.
2155bdd1243dSDimitry Andric   StringRef CurrentDir;
2156bdd1243dSDimitry Andric 
2157bdd1243dSDimitry Andric   /// Directories used for search of config files.
2158bdd1243dSDimitry Andric   ArrayRef<StringRef> SearchDirs;
2159bdd1243dSDimitry Andric 
2160bdd1243dSDimitry Andric   /// True if names of nested response files must be resolved relative to
2161bdd1243dSDimitry Andric   /// including file.
2162bdd1243dSDimitry Andric   bool RelativeNames = false;
2163bdd1243dSDimitry Andric 
2164bdd1243dSDimitry Andric   /// If true, mark end of lines and the end of the response file with nullptrs
2165bdd1243dSDimitry Andric   /// in the Argv vector.
2166bdd1243dSDimitry Andric   bool MarkEOLs = false;
2167bdd1243dSDimitry Andric 
2168bdd1243dSDimitry Andric   /// If true, body of config file is expanded.
2169bdd1243dSDimitry Andric   bool InConfigFile = false;
2170bdd1243dSDimitry Andric 
2171bdd1243dSDimitry Andric   llvm::Error expandResponseFile(StringRef FName,
2172bdd1243dSDimitry Andric                                  SmallVectorImpl<const char *> &NewArgv);
2173bdd1243dSDimitry Andric 
2174bdd1243dSDimitry Andric public:
2175bdd1243dSDimitry Andric   ExpansionContext(BumpPtrAllocator &A, TokenizerCallback T);
2176bdd1243dSDimitry Andric 
2177bdd1243dSDimitry Andric   ExpansionContext &setMarkEOLs(bool X) {
2178bdd1243dSDimitry Andric     MarkEOLs = X;
2179bdd1243dSDimitry Andric     return *this;
2180bdd1243dSDimitry Andric   }
2181bdd1243dSDimitry Andric 
2182bdd1243dSDimitry Andric   ExpansionContext &setRelativeNames(bool X) {
2183bdd1243dSDimitry Andric     RelativeNames = X;
2184bdd1243dSDimitry Andric     return *this;
2185bdd1243dSDimitry Andric   }
2186bdd1243dSDimitry Andric 
2187bdd1243dSDimitry Andric   ExpansionContext &setCurrentDir(StringRef X) {
2188bdd1243dSDimitry Andric     CurrentDir = X;
2189bdd1243dSDimitry Andric     return *this;
2190bdd1243dSDimitry Andric   }
2191bdd1243dSDimitry Andric 
2192bdd1243dSDimitry Andric   ExpansionContext &setSearchDirs(ArrayRef<StringRef> X) {
2193bdd1243dSDimitry Andric     SearchDirs = X;
2194bdd1243dSDimitry Andric     return *this;
2195bdd1243dSDimitry Andric   }
2196bdd1243dSDimitry Andric 
2197bdd1243dSDimitry Andric   ExpansionContext &setVFS(vfs::FileSystem *X) {
2198bdd1243dSDimitry Andric     FS = X;
2199bdd1243dSDimitry Andric     return *this;
2200bdd1243dSDimitry Andric   }
2201bdd1243dSDimitry Andric 
2202bdd1243dSDimitry Andric   /// Looks for the specified configuration file.
2203bdd1243dSDimitry Andric   ///
2204bdd1243dSDimitry Andric   /// \param[in]  FileName Name of the file to search for.
2205bdd1243dSDimitry Andric   /// \param[out] FilePath File absolute path, if it was found.
2206bdd1243dSDimitry Andric   /// \return True if file was found.
2207bdd1243dSDimitry Andric   ///
2208bdd1243dSDimitry Andric   /// If the specified file name contains a directory separator, it is searched
2209bdd1243dSDimitry Andric   /// for by its absolute path. Otherwise looks for file sequentially in
2210bdd1243dSDimitry Andric   /// directories specified by SearchDirs field.
2211bdd1243dSDimitry Andric   bool findConfigFile(StringRef FileName, SmallVectorImpl<char> &FilePath);
2212bdd1243dSDimitry Andric 
22130b57cec5SDimitry Andric   /// Reads command line options from the given configuration file.
22140b57cec5SDimitry Andric   ///
2215bdd1243dSDimitry Andric   /// \param [in] CfgFile Path to configuration file.
22160b57cec5SDimitry Andric   /// \param [out] Argv Array to which the read options are added.
22170b57cec5SDimitry Andric   /// \return true if the file was successfully read.
22180b57cec5SDimitry Andric   ///
22190b57cec5SDimitry Andric   /// It reads content of the specified file, tokenizes it and expands "@file"
22200b57cec5SDimitry Andric   /// commands resolving file names in them relative to the directory where
222104eeddc0SDimitry Andric   /// CfgFilename resides. It also expands "<CFGDIR>" to the base path of the
222204eeddc0SDimitry Andric   /// current config file.
2223bdd1243dSDimitry Andric   Error readConfigFile(StringRef CfgFile, SmallVectorImpl<const char *> &Argv);
22240b57cec5SDimitry Andric 
2225bdd1243dSDimitry Andric   /// Expands constructs "@file" in the provided array of arguments recursively.
2226bdd1243dSDimitry Andric   Error expandResponseFiles(SmallVectorImpl<const char *> &Argv);
2227bdd1243dSDimitry Andric };
2228bdd1243dSDimitry Andric 
2229bdd1243dSDimitry Andric /// A convenience helper which concatenates the options specified by the
2230bdd1243dSDimitry Andric /// environment variable EnvVar and command line options, then expands
2231bdd1243dSDimitry Andric /// response files recursively.
22320b57cec5SDimitry Andric /// \return true if all @files were expanded successfully or there were none.
2233bdd1243dSDimitry Andric bool expandResponseFiles(int Argc, const char *const *Argv, const char *EnvVar,
2234bdd1243dSDimitry Andric                          SmallVectorImpl<const char *> &NewArgv);
2235fe6060f1SDimitry Andric 
2236bdd1243dSDimitry Andric /// A convenience helper which supports the typical use case of expansion
2237bdd1243dSDimitry Andric /// function call.
2238bdd1243dSDimitry Andric bool ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer,
2239bdd1243dSDimitry Andric                          SmallVectorImpl<const char *> &Argv);
22400b57cec5SDimitry Andric 
2241e8d8bef9SDimitry Andric /// A convenience helper which concatenates the options specified by the
2242e8d8bef9SDimitry Andric /// environment variable EnvVar and command line options, then expands response
2243e8d8bef9SDimitry Andric /// files recursively. The tokenizer is a predefined GNU or Windows one.
2244e8d8bef9SDimitry Andric /// \return true if all @files were expanded successfully or there were none.
2245e8d8bef9SDimitry Andric bool expandResponseFiles(int Argc, const char *const *Argv, const char *EnvVar,
2246e8d8bef9SDimitry Andric                          StringSaver &Saver,
2247e8d8bef9SDimitry Andric                          SmallVectorImpl<const char *> &NewArgv);
2248e8d8bef9SDimitry Andric 
22490b57cec5SDimitry Andric /// Mark all options not part of this category as cl::ReallyHidden.
22500b57cec5SDimitry Andric ///
22510b57cec5SDimitry Andric /// \param Category the category of options to keep displaying
22520b57cec5SDimitry Andric ///
22530b57cec5SDimitry Andric /// Some tools (like clang-format) like to be able to hide all options that are
22540b57cec5SDimitry Andric /// not specific to the tool. This function allows a tool to specify a single
22550b57cec5SDimitry Andric /// option category to display in the -help output.
22560b57cec5SDimitry Andric void HideUnrelatedOptions(cl::OptionCategory &Category,
2257bdd1243dSDimitry Andric                           SubCommand &Sub = SubCommand::getTopLevel());
22580b57cec5SDimitry Andric 
22590b57cec5SDimitry Andric /// Mark all options not part of the categories as cl::ReallyHidden.
22600b57cec5SDimitry Andric ///
22610b57cec5SDimitry Andric /// \param Categories the categories of options to keep displaying.
22620b57cec5SDimitry Andric ///
22630b57cec5SDimitry Andric /// Some tools (like clang-format) like to be able to hide all options that are
22640b57cec5SDimitry Andric /// not specific to the tool. This function allows a tool to specify a single
22650b57cec5SDimitry Andric /// option category to display in the -help output.
22660b57cec5SDimitry Andric void HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *> Categories,
2267bdd1243dSDimitry Andric                           SubCommand &Sub = SubCommand::getTopLevel());
22680b57cec5SDimitry Andric 
22690b57cec5SDimitry Andric /// Reset all command line options to a state that looks as if they have
22700b57cec5SDimitry Andric /// never appeared on the command line.  This is useful for being able to parse
22710b57cec5SDimitry Andric /// a command line multiple times (especially useful for writing tests).
22720b57cec5SDimitry Andric void ResetAllOptionOccurrences();
22730b57cec5SDimitry Andric 
22740b57cec5SDimitry Andric /// Reset the command line parser back to its initial state.  This
22750b57cec5SDimitry Andric /// removes
22760b57cec5SDimitry Andric /// all options, categories, and subcommands and returns the parser to a state
22770b57cec5SDimitry Andric /// where no options are supported.
22780b57cec5SDimitry Andric void ResetCommandLineParser();
22790b57cec5SDimitry Andric 
22808bcb0991SDimitry Andric /// Parses `Arg` into the option handler `Handler`.
22818bcb0991SDimitry Andric bool ProvidePositionalOption(Option *Handler, StringRef Arg, int i);
22828bcb0991SDimitry Andric 
22830b57cec5SDimitry Andric } // end namespace cl
22840b57cec5SDimitry Andric 
22850b57cec5SDimitry Andric } // end namespace llvm
22860b57cec5SDimitry Andric 
22870b57cec5SDimitry Andric #endif // LLVM_SUPPORT_COMMANDLINE_H
2288