1 //===- llvm/Support/CommandLine.h - Command line handler --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements a command line argument processor that is useful when
11 // creating a tool. It provides a simple, minimalistic interface that is easily
12 // extensible and supports nonlocal (library) command line options.
14 // Note that rather than trying to figure out what this code does, you should
15 // read the library documentation located in docs/CommandLine.html or looks at
16 // the many example usages in tools/*/*.cpp
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_SUPPORT_COMMANDLINE_H
21 #define LLVM_SUPPORT_COMMANDLINE_H
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/StringMap.h"
26 #include "llvm/ADT/Twine.h"
27 #include "llvm/Support/Compiler.h"
36 /// cl Namespace - This namespace contains all of the command line option
37 /// processing machinery. It is intentionally a short name to make qualified
41 //===----------------------------------------------------------------------===//
42 // ParseCommandLineOptions - Command line option processing entry point.
44 void ParseCommandLineOptions(int argc, const char *const *argv,
45 const char *Overview = nullptr);
47 //===----------------------------------------------------------------------===//
48 // ParseEnvironmentOptions - Environment variable option processing alternate
51 void ParseEnvironmentOptions(const char *progName, const char *envvar,
52 const char *Overview = nullptr);
54 ///===---------------------------------------------------------------------===//
55 /// SetVersionPrinter - Override the default (LLVM specific) version printer
56 /// used to print out the version when --version is given
57 /// on the command line. This allows other systems using the
58 /// CommandLine utilities to print their own version string.
59 void SetVersionPrinter(void (*func)());
61 ///===---------------------------------------------------------------------===//
62 /// AddExtraVersionPrinter - Add an extra printer to use in addition to the
63 /// default one. This can be called multiple times,
64 /// and each time it adds a new function to the list
65 /// which will be called after the basic LLVM version
66 /// printing is complete. Each can then add additional
67 /// information specific to the tool.
68 void AddExtraVersionPrinter(void (*func)());
70 // PrintOptionValues - Print option values.
71 // With -print-options print the difference between option values and defaults.
72 // With -print-all-options print all option values.
73 // (Currently not perfect, but best-effort.)
74 void PrintOptionValues();
76 // Forward declaration - AddLiteralOption needs to be up here to make gcc happy.
79 /// \brief Adds a new option for parsing and provides the option it refers to.
81 /// \param O pointer to the option
82 /// \param Name the string name for the option to handle during parsing
84 /// Literal options are used by some parsers to register special option values.
85 /// This is how the PassNameParser registers pass names for opt.
86 void AddLiteralOption(Option &O, const char *Name);
88 //===----------------------------------------------------------------------===//
89 // Flags permitted to be passed to command line arguments
92 enum NumOccurrencesFlag { // Flags for the number of occurrences allowed
93 Optional = 0x00, // Zero or One occurrence
94 ZeroOrMore = 0x01, // Zero or more occurrences allowed
95 Required = 0x02, // One occurrence required
96 OneOrMore = 0x03, // One or more occurrences required
98 // ConsumeAfter - Indicates that this option is fed anything that follows the
99 // last positional argument required by the application (it is an error if
100 // there are zero positional arguments, and a ConsumeAfter option is used).
101 // Thus, for example, all arguments to LLI are processed until a filename is
102 // found. Once a filename is found, all of the succeeding arguments are
103 // passed, unprocessed, to the ConsumeAfter option.
108 enum ValueExpected { // Is a value required for the option?
109 // zero reserved for the unspecified value
110 ValueOptional = 0x01, // The value can appear... or not
111 ValueRequired = 0x02, // The value is required to appear!
112 ValueDisallowed = 0x03 // A value may not be specified (for flags)
115 enum OptionHidden { // Control whether -help shows this option
116 NotHidden = 0x00, // Option included in -help & -help-hidden
117 Hidden = 0x01, // -help doesn't, but -help-hidden does
118 ReallyHidden = 0x02 // Neither -help nor -help-hidden show this arg
121 // Formatting flags - This controls special features that the option might have
122 // that cause it to be parsed differently...
124 // Prefix - This option allows arguments that are otherwise unrecognized to be
125 // matched by options that are a prefix of the actual value. This is useful for
126 // cases like a linker, where options are typically of the form '-lfoo' or
127 // '-L../../include' where -l or -L are the actual flags. When prefix is
128 // enabled, and used, the value for the flag comes from the suffix of the
131 // Grouping - With this option enabled, multiple letter options are allowed to
132 // bunch together with only a single hyphen for the whole group. This allows
133 // emulation of the behavior that ls uses for example: ls -la === ls -l -a
136 enum FormattingFlags {
137 NormalFormatting = 0x00, // Nothing special
138 Positional = 0x01, // Is a positional argument, no '-' required
139 Prefix = 0x02, // Can this option directly prefix its value?
140 Grouping = 0x03 // Can this option group with other options?
143 enum MiscFlags { // Miscellaneous flags to adjust argument
144 CommaSeparated = 0x01, // Should this cl::list split between commas?
145 PositionalEatsArgs = 0x02, // Should this positional cl::list eat -args?
146 Sink = 0x04 // Should this cl::list eat all unknown options?
149 //===----------------------------------------------------------------------===//
150 // Option Category class
152 class OptionCategory {
154 const char *const Name;
155 const char *const Description;
156 void registerCategory();
159 OptionCategory(const char *const Name,
160 const char *const Description = nullptr)
161 : Name(Name), Description(Description) {
164 const char *getName() const { return Name; }
165 const char *getDescription() const { return Description; }
168 // The general Option Category (used as default category).
169 extern OptionCategory GeneralCategory;
171 //===----------------------------------------------------------------------===//
178 // handleOccurrences - Overriden by subclasses to handle the value passed into
179 // an argument. Should return true if there was an error processing the
180 // argument and the program should exit.
182 virtual bool handleOccurrence(unsigned pos, StringRef ArgName,
185 virtual enum ValueExpected getValueExpectedFlagDefault() const {
186 return ValueOptional;
189 // Out of line virtual function to provide home for the class.
190 virtual void anchor();
192 int NumOccurrences; // The number of times specified
193 // Occurrences, HiddenFlag, and Formatting are all enum types but to avoid
194 // problems with signed enums in bitfields.
195 unsigned Occurrences : 3; // enum NumOccurrencesFlag
196 // not using the enum type for 'Value' because zero is an implementation
197 // detail representing the non-value
199 unsigned HiddenFlag : 2; // enum OptionHidden
200 unsigned Formatting : 2; // enum FormattingFlags
202 unsigned Position; // Position of last occurrence of the option
203 unsigned AdditionalVals; // Greater than 0 for multi-valued option.
206 const char *ArgStr; // The argument string itself (ex: "help", "o")
207 const char *HelpStr; // The descriptive text message for -help
208 const char *ValueStr; // String describing what the value of this option is
209 OptionCategory *Category; // The Category this option belongs to
210 bool FullyInitialized; // Has addArguemnt been called?
212 inline enum NumOccurrencesFlag getNumOccurrencesFlag() const {
213 return (enum NumOccurrencesFlag)Occurrences;
215 inline enum ValueExpected getValueExpectedFlag() const {
216 return Value ? ((enum ValueExpected)Value) : getValueExpectedFlagDefault();
218 inline enum OptionHidden getOptionHiddenFlag() const {
219 return (enum OptionHidden)HiddenFlag;
221 inline enum FormattingFlags getFormattingFlag() const {
222 return (enum FormattingFlags)Formatting;
224 inline unsigned getMiscFlags() const { return Misc; }
225 inline unsigned getPosition() const { return Position; }
226 inline unsigned getNumAdditionalVals() const { return AdditionalVals; }
228 // hasArgStr - Return true if the argstr != ""
229 bool hasArgStr() const { return ArgStr[0] != 0; }
231 //-------------------------------------------------------------------------===
232 // Accessor functions set by OptionModifiers
234 void setArgStr(const char *S);
235 void setDescription(const char *S) { HelpStr = S; }
236 void setValueStr(const char *S) { ValueStr = S; }
237 void setNumOccurrencesFlag(enum NumOccurrencesFlag Val) { Occurrences = Val; }
238 void setValueExpectedFlag(enum ValueExpected Val) { Value = Val; }
239 void setHiddenFlag(enum OptionHidden Val) { HiddenFlag = Val; }
240 void setFormattingFlag(enum FormattingFlags V) { Formatting = V; }
241 void setMiscFlag(enum MiscFlags M) { Misc |= M; }
242 void setPosition(unsigned pos) { Position = pos; }
243 void setCategory(OptionCategory &C) { Category = &C; }
246 explicit Option(enum NumOccurrencesFlag OccurrencesFlag,
247 enum OptionHidden Hidden)
248 : NumOccurrences(0), Occurrences(OccurrencesFlag), Value(0),
249 HiddenFlag(Hidden), Formatting(NormalFormatting), Misc(0), Position(0),
250 AdditionalVals(0), ArgStr(""), HelpStr(""), ValueStr(""),
251 Category(&GeneralCategory), FullyInitialized(false) {}
253 inline void setNumAdditionalVals(unsigned n) { AdditionalVals = n; }
256 // addArgument - Register this argument with the commandline system.
260 /// Unregisters this option from the CommandLine system.
262 /// This option must have been the last option registered.
263 /// For testing purposes only.
264 void removeArgument();
266 // Return the width of the option tag for printing...
267 virtual size_t getOptionWidth() const = 0;
269 // printOptionInfo - Print out information about this option. The
270 // to-be-maintained width is specified.
272 virtual void printOptionInfo(size_t GlobalWidth) const = 0;
274 virtual void printOptionValue(size_t GlobalWidth, bool Force) const = 0;
276 virtual void getExtraOptionNames(SmallVectorImpl<const char *> &) {}
278 // addOccurrence - Wrapper around handleOccurrence that enforces Flags.
280 virtual bool addOccurrence(unsigned pos, StringRef ArgName, StringRef Value,
281 bool MultiArg = false);
283 // Prints option name followed by message. Always returns true.
284 bool error(const Twine &Message, StringRef ArgName = StringRef());
287 inline int getNumOccurrences() const { return NumOccurrences; }
291 //===----------------------------------------------------------------------===//
292 // Command line option modifiers that can be used to modify the behavior of
293 // command line option parsers...
296 // desc - Modifier to set the description shown in the -help output...
299 desc(const char *Str) : Desc(Str) {}
300 void apply(Option &O) const { O.setDescription(Desc); }
303 // value_desc - Modifier to set the value description shown in the -help
307 value_desc(const char *Str) : Desc(Str) {}
308 void apply(Option &O) const { O.setValueStr(Desc); }
311 // init - Specify a default (initial) value for the command line argument, if
312 // the default constructor for the argument type does not give you what you
313 // want. This is only valid on "opt" arguments, not on "list" arguments.
315 template <class Ty> struct initializer {
317 initializer(const Ty &Val) : Init(Val) {}
319 template <class Opt> void apply(Opt &O) const { O.setInitialValue(Init); }
322 template <class Ty> initializer<Ty> init(const Ty &Val) {
323 return initializer<Ty>(Val);
326 // location - Allow the user to specify which external variable they want to
327 // store the results of the command line argument processing into, if they don't
328 // want to store it in the option itself.
330 template <class Ty> struct LocationClass {
332 LocationClass(Ty &L) : Loc(L) {}
334 template <class Opt> void apply(Opt &O) const { O.setLocation(O, Loc); }
337 template <class Ty> LocationClass<Ty> location(Ty &L) {
338 return LocationClass<Ty>(L);
341 // cat - Specifiy the Option category for the command line argument to belong
344 OptionCategory &Category;
345 cat(OptionCategory &c) : Category(c) {}
347 template <class Opt> void apply(Opt &O) const { O.setCategory(Category); }
350 //===----------------------------------------------------------------------===//
353 // Support value comparison outside the template.
354 struct GenericOptionValue {
355 virtual ~GenericOptionValue() {}
356 virtual bool compare(const GenericOptionValue &V) const = 0;
359 virtual void anchor();
362 template <class DataType> struct OptionValue;
364 // The default value safely does nothing. Option value printing is only
366 template <class DataType, bool isClass>
367 struct OptionValueBase : public GenericOptionValue {
368 // Temporary storage for argument passing.
369 typedef OptionValue<DataType> WrapperType;
371 bool hasValue() const { return false; }
373 const DataType &getValue() const { llvm_unreachable("no default value"); }
375 // Some options may take their value from a different data type.
376 template <class DT> void setValue(const DT & /*V*/) {}
378 bool compare(const DataType & /*V*/) const { return false; }
380 bool compare(const GenericOptionValue & /*V*/) const override {
385 // Simple copy of the option value.
386 template <class DataType> class OptionValueCopy : public GenericOptionValue {
391 OptionValueCopy() : Valid(false) {}
393 bool hasValue() const { return Valid; }
395 const DataType &getValue() const {
396 assert(Valid && "invalid option value");
400 void setValue(const DataType &V) {
405 bool compare(const DataType &V) const { return Valid && (Value != V); }
407 bool compare(const GenericOptionValue &V) const override {
408 const OptionValueCopy<DataType> &VC =
409 static_cast<const OptionValueCopy<DataType> &>(V);
412 return compare(VC.getValue());
416 // Non-class option values.
417 template <class DataType>
418 struct OptionValueBase<DataType, false> : OptionValueCopy<DataType> {
419 typedef DataType WrapperType;
422 // Top-level option class.
423 template <class DataType>
424 struct OptionValue : OptionValueBase<DataType, std::is_class<DataType>::value> {
427 OptionValue(const DataType &V) { this->setValue(V); }
428 // Some options may take their value from a different data type.
429 template <class DT> OptionValue<DataType> &operator=(const DT &V) {
435 // Other safe-to-copy-by-value common option types.
436 enum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE };
438 struct OptionValue<cl::boolOrDefault> : OptionValueCopy<cl::boolOrDefault> {
439 typedef cl::boolOrDefault WrapperType;
443 OptionValue(const cl::boolOrDefault &V) { this->setValue(V); }
444 OptionValue<cl::boolOrDefault> &operator=(const cl::boolOrDefault &V) {
450 void anchor() override;
453 template <> struct OptionValue<std::string> : OptionValueCopy<std::string> {
454 typedef StringRef WrapperType;
458 OptionValue(const std::string &V) { this->setValue(V); }
459 OptionValue<std::string> &operator=(const std::string &V) {
465 void anchor() override;
468 //===----------------------------------------------------------------------===//
469 // Enum valued command line option
471 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, int(ENUMVAL), DESC
472 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, int(ENUMVAL), DESC
473 #define clEnumValEnd (reinterpret_cast<void *>(0))
475 // values - For custom data types, allow specifying a group of values together
476 // as the values that go into the mapping that the option handler uses. Note
477 // that the values list must always have a 0 at the end of the list to indicate
478 // that the list has ended.
480 template <class DataType> class ValuesClass {
481 // Use a vector instead of a map, because the lists should be short,
482 // the overhead is less, and most importantly, it keeps them in the order
483 // inserted so we can print our option out nicely.
484 SmallVector<std::pair<const char *, std::pair<int, const char *>>, 4> Values;
485 void processValues(va_list Vals);
488 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
490 // Insert the first value, which is required.
491 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
493 // Process the varargs portion of the values...
494 while (const char *enumName = va_arg(ValueArgs, const char *)) {
495 DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int));
496 const char *EnumDesc = va_arg(ValueArgs, const char *);
497 Values.push_back(std::make_pair(enumName, // Add value to value map
498 std::make_pair(EnumVal, EnumDesc)));
502 template <class Opt> void apply(Opt &O) const {
503 for (size_t i = 0, e = Values.size(); i != e; ++i)
504 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
505 Values[i].second.second);
509 template <class DataType>
510 ValuesClass<DataType> LLVM_END_WITH_NULL
511 values(const char *Arg, DataType Val, const char *Desc, ...) {
513 va_start(ValueArgs, Desc);
514 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
519 //===----------------------------------------------------------------------===//
520 // parser class - Parameterizable parser for different data types. By default,
521 // known data types (string, int, bool) have specialized parsers, that do what
522 // you would expect. The default parser, used for data types that are not
523 // built-in, uses a mapping table to map specific options to values, which is
524 // used, among other things, to handle enum types.
526 //--------------------------------------------------
527 // generic_parser_base - This class holds all the non-generic code that we do
528 // not need replicated for every instance of the generic parser. This also
529 // allows us to put stuff into CommandLine.cpp
531 class generic_parser_base {
533 class GenericOptionInfo {
535 GenericOptionInfo(const char *name, const char *helpStr)
536 : Name(name), HelpStr(helpStr) {}
542 generic_parser_base(Option &O) : Owner(O) {}
544 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
546 // getNumOptions - Virtual function implemented by generic subclass to
547 // indicate how many entries are in Values.
549 virtual unsigned getNumOptions() const = 0;
551 // getOption - Return option name N.
552 virtual const char *getOption(unsigned N) const = 0;
554 // getDescription - Return description N
555 virtual const char *getDescription(unsigned N) const = 0;
557 // Return the width of the option tag for printing...
558 virtual size_t getOptionWidth(const Option &O) const;
560 virtual const GenericOptionValue &getOptionValue(unsigned N) const = 0;
562 // printOptionInfo - Print out information about this option. The
563 // to-be-maintained width is specified.
565 virtual void printOptionInfo(const Option &O, size_t GlobalWidth) const;
567 void printGenericOptionDiff(const Option &O, const GenericOptionValue &V,
568 const GenericOptionValue &Default,
569 size_t GlobalWidth) const;
571 // printOptionDiff - print the value of an option and it's default.
573 // Template definition ensures that the option and default have the same
574 // DataType (via the same AnyOptionValue).
575 template <class AnyOptionValue>
576 void printOptionDiff(const Option &O, const AnyOptionValue &V,
577 const AnyOptionValue &Default,
578 size_t GlobalWidth) const {
579 printGenericOptionDiff(O, V, Default, GlobalWidth);
584 void getExtraOptionNames(SmallVectorImpl<const char *> &OptionNames) {
585 // If there has been no argstr specified, that means that we need to add an
586 // argument for every possible option. This ensures that our options are
588 if (!Owner.hasArgStr())
589 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
590 OptionNames.push_back(getOption(i));
593 enum ValueExpected getValueExpectedFlagDefault() const {
594 // If there is an ArgStr specified, then we are of the form:
596 // -opt=O2 or -opt O2 or -optO2
598 // In which case, the value is required. Otherwise if an arg str has not
599 // been specified, we are of the form:
601 // -O2 or O2 or -la (where -l and -a are separate options)
603 // If this is the case, we cannot allow a value.
605 if (Owner.hasArgStr())
606 return ValueRequired;
608 return ValueDisallowed;
611 // findOption - Return the option number corresponding to the specified
612 // argument string. If the option is not found, getNumOptions() is returned.
614 unsigned findOption(const char *Name);
620 // Default parser implementation - This implementation depends on having a
621 // mapping of recognized options to values of some sort. In addition to this,
622 // each entry in the mapping also tracks a help message that is printed with the
623 // command line option for -help. Because this is a simple mapping parser, the
624 // data type can be any unsupported type.
626 template <class DataType> class parser : public generic_parser_base {
628 class OptionInfo : public GenericOptionInfo {
630 OptionInfo(const char *name, DataType v, const char *helpStr)
631 : GenericOptionInfo(name, helpStr), V(v) {}
632 OptionValue<DataType> V;
634 SmallVector<OptionInfo, 8> Values;
637 parser(Option &O) : generic_parser_base(O) {}
638 typedef DataType parser_data_type;
640 // Implement virtual functions needed by generic_parser_base
641 unsigned getNumOptions() const override { return unsigned(Values.size()); }
642 const char *getOption(unsigned N) const override { return Values[N].Name; }
643 const char *getDescription(unsigned N) const override {
644 return Values[N].HelpStr;
647 // getOptionValue - Return the value of option name N.
648 const GenericOptionValue &getOptionValue(unsigned N) const override {
652 // parse - Return true on error.
653 bool parse(Option &O, StringRef ArgName, StringRef Arg, DataType &V) {
655 if (Owner.hasArgStr())
660 for (size_t i = 0, e = Values.size(); i != e; ++i)
661 if (Values[i].Name == ArgVal) {
662 V = Values[i].V.getValue();
666 return O.error("Cannot find option named '" + ArgVal + "'!");
669 /// addLiteralOption - Add an entry to the mapping table.
672 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
673 assert(findOption(Name) == Values.size() && "Option already exists!");
674 OptionInfo X(Name, static_cast<DataType>(V), HelpStr);
676 AddLiteralOption(Owner, Name);
679 /// removeLiteralOption - Remove the specified option.
681 void removeLiteralOption(const char *Name) {
682 unsigned N = findOption(Name);
683 assert(N != Values.size() && "Option not found!");
684 Values.erase(Values.begin() + N);
688 //--------------------------------------------------
689 // basic_parser - Super class of parsers to provide boilerplate code
691 class basic_parser_impl { // non-template implementation of basic_parser<t>
693 basic_parser_impl(Option &O) {}
695 virtual ~basic_parser_impl() {}
697 enum ValueExpected getValueExpectedFlagDefault() const {
698 return ValueRequired;
701 void getExtraOptionNames(SmallVectorImpl<const char *> &) {}
705 // Return the width of the option tag for printing...
706 size_t getOptionWidth(const Option &O) const;
708 // printOptionInfo - Print out information about this option. The
709 // to-be-maintained width is specified.
711 void printOptionInfo(const Option &O, size_t GlobalWidth) const;
713 // printOptionNoValue - Print a placeholder for options that don't yet support
714 // printOptionDiff().
715 void printOptionNoValue(const Option &O, size_t GlobalWidth) const;
717 // getValueName - Overload in subclass to provide a better default value.
718 virtual const char *getValueName() const { return "value"; }
720 // An out-of-line virtual method to provide a 'home' for this class.
721 virtual void anchor();
724 // A helper for basic_parser::printOptionDiff.
725 void printOptionName(const Option &O, size_t GlobalWidth) const;
728 // basic_parser - The real basic parser is just a template wrapper that provides
729 // a typedef for the provided data type.
731 template <class DataType> class basic_parser : public basic_parser_impl {
733 basic_parser(Option &O) : basic_parser_impl(O) {}
734 typedef DataType parser_data_type;
735 typedef OptionValue<DataType> OptVal;
738 //--------------------------------------------------
741 template <> class parser<bool> : public basic_parser<bool> {
743 parser(Option &O) : basic_parser(O) {}
745 // parse - Return true on error.
746 bool parse(Option &O, StringRef ArgName, StringRef Arg, bool &Val);
750 enum ValueExpected getValueExpectedFlagDefault() const {
751 return ValueOptional;
754 // getValueName - Do not print =<value> at all.
755 const char *getValueName() const override { return nullptr; }
757 void printOptionDiff(const Option &O, bool V, OptVal Default,
758 size_t GlobalWidth) const;
760 // An out-of-line virtual method to provide a 'home' for this class.
761 void anchor() override;
764 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<bool>);
766 //--------------------------------------------------
767 // parser<boolOrDefault>
768 template <> class parser<boolOrDefault> : public basic_parser<boolOrDefault> {
770 parser(Option &O) : basic_parser(O) {}
772 // parse - Return true on error.
773 bool parse(Option &O, StringRef ArgName, StringRef Arg, boolOrDefault &Val);
775 enum ValueExpected getValueExpectedFlagDefault() const {
776 return ValueOptional;
779 // getValueName - Do not print =<value> at all.
780 const char *getValueName() const override { return nullptr; }
782 void printOptionDiff(const Option &O, boolOrDefault V, OptVal Default,
783 size_t GlobalWidth) const;
785 // An out-of-line virtual method to provide a 'home' for this class.
786 void anchor() override;
789 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<boolOrDefault>);
791 //--------------------------------------------------
794 template <> class parser<int> : public basic_parser<int> {
796 parser(Option &O) : basic_parser(O) {}
798 // parse - Return true on error.
799 bool parse(Option &O, StringRef ArgName, StringRef Arg, int &Val);
801 // getValueName - Overload in subclass to provide a better default value.
802 const char *getValueName() const override { return "int"; }
804 void printOptionDiff(const Option &O, int V, OptVal Default,
805 size_t GlobalWidth) const;
807 // An out-of-line virtual method to provide a 'home' for this class.
808 void anchor() override;
811 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<int>);
813 //--------------------------------------------------
816 template <> class parser<unsigned> : public basic_parser<unsigned> {
818 parser(Option &O) : basic_parser(O) {}
820 // parse - Return true on error.
821 bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned &Val);
823 // getValueName - Overload in subclass to provide a better default value.
824 const char *getValueName() const override { return "uint"; }
826 void printOptionDiff(const Option &O, unsigned V, OptVal Default,
827 size_t GlobalWidth) const;
829 // An out-of-line virtual method to provide a 'home' for this class.
830 void anchor() override;
833 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<unsigned>);
835 //--------------------------------------------------
836 // parser<unsigned long long>
839 class parser<unsigned long long> : public basic_parser<unsigned long long> {
841 parser(Option &O) : basic_parser(O) {}
843 // parse - Return true on error.
844 bool parse(Option &O, StringRef ArgName, StringRef Arg,
845 unsigned long long &Val);
847 // getValueName - Overload in subclass to provide a better default value.
848 const char *getValueName() const override { return "uint"; }
850 void printOptionDiff(const Option &O, unsigned long long V, OptVal Default,
851 size_t GlobalWidth) const;
853 // An out-of-line virtual method to provide a 'home' for this class.
854 void anchor() override;
857 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<unsigned long long>);
859 //--------------------------------------------------
862 template <> class parser<double> : public basic_parser<double> {
864 parser(Option &O) : basic_parser(O) {}
866 // parse - Return true on error.
867 bool parse(Option &O, StringRef ArgName, StringRef Arg, double &Val);
869 // getValueName - Overload in subclass to provide a better default value.
870 const char *getValueName() const override { return "number"; }
872 void printOptionDiff(const Option &O, double V, OptVal Default,
873 size_t GlobalWidth) const;
875 // An out-of-line virtual method to provide a 'home' for this class.
876 void anchor() override;
879 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<double>);
881 //--------------------------------------------------
884 template <> class parser<float> : public basic_parser<float> {
886 parser(Option &O) : basic_parser(O) {}
888 // parse - Return true on error.
889 bool parse(Option &O, StringRef ArgName, StringRef Arg, float &Val);
891 // getValueName - Overload in subclass to provide a better default value.
892 const char *getValueName() const override { return "number"; }
894 void printOptionDiff(const Option &O, float V, OptVal Default,
895 size_t GlobalWidth) const;
897 // An out-of-line virtual method to provide a 'home' for this class.
898 void anchor() override;
901 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<float>);
903 //--------------------------------------------------
904 // parser<std::string>
906 template <> class parser<std::string> : public basic_parser<std::string> {
908 parser(Option &O) : basic_parser(O) {}
910 // parse - Return true on error.
911 bool parse(Option &, StringRef, StringRef Arg, std::string &Value) {
916 // getValueName - Overload in subclass to provide a better default value.
917 const char *getValueName() const override { return "string"; }
919 void printOptionDiff(const Option &O, StringRef V, OptVal Default,
920 size_t GlobalWidth) const;
922 // An out-of-line virtual method to provide a 'home' for this class.
923 void anchor() override;
926 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<std::string>);
928 //--------------------------------------------------
931 template <> class parser<char> : public basic_parser<char> {
933 parser(Option &O) : basic_parser(O) {}
935 // parse - Return true on error.
936 bool parse(Option &, StringRef, StringRef Arg, char &Value) {
941 // getValueName - Overload in subclass to provide a better default value.
942 const char *getValueName() const override { return "char"; }
944 void printOptionDiff(const Option &O, char V, OptVal Default,
945 size_t GlobalWidth) const;
947 // An out-of-line virtual method to provide a 'home' for this class.
948 void anchor() override;
951 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<char>);
953 //--------------------------------------------------
956 // This collection of wrappers is the intermediary between class opt and class
957 // parser to handle all the template nastiness.
959 // This overloaded function is selected by the generic parser.
960 template <class ParserClass, class DT>
961 void printOptionDiff(const Option &O, const generic_parser_base &P, const DT &V,
962 const OptionValue<DT> &Default, size_t GlobalWidth) {
963 OptionValue<DT> OV = V;
964 P.printOptionDiff(O, OV, Default, GlobalWidth);
967 // This is instantiated for basic parsers when the parsed value has a different
968 // type than the option value. e.g. HelpPrinter.
969 template <class ParserDT, class ValDT> struct OptionDiffPrinter {
970 void print(const Option &O, const parser<ParserDT> P, const ValDT & /*V*/,
971 const OptionValue<ValDT> & /*Default*/, size_t GlobalWidth) {
972 P.printOptionNoValue(O, GlobalWidth);
976 // This is instantiated for basic parsers when the parsed value has the same
977 // type as the option value.
978 template <class DT> struct OptionDiffPrinter<DT, DT> {
979 void print(const Option &O, const parser<DT> P, const DT &V,
980 const OptionValue<DT> &Default, size_t GlobalWidth) {
981 P.printOptionDiff(O, V, Default, GlobalWidth);
985 // This overloaded function is selected by the basic parser, which may parse a
986 // different type than the option type.
987 template <class ParserClass, class ValDT>
988 void printOptionDiff(
990 const basic_parser<typename ParserClass::parser_data_type> &P,
991 const ValDT &V, const OptionValue<ValDT> &Default, size_t GlobalWidth) {
993 OptionDiffPrinter<typename ParserClass::parser_data_type, ValDT> printer;
994 printer.print(O, static_cast<const ParserClass &>(P), V, Default,
998 //===----------------------------------------------------------------------===//
999 // applicator class - This class is used because we must use partial
1000 // specialization to handle literal string arguments specially (const char* does
1001 // not correctly respond to the apply method). Because the syntax to use this
1002 // is a pain, we have the 'apply' method below to handle the nastiness...
1004 template <class Mod> struct applicator {
1005 template <class Opt> static void opt(const Mod &M, Opt &O) { M.apply(O); }
1008 // Handle const char* as a special case...
1009 template <unsigned n> struct applicator<char[n]> {
1010 template <class Opt> static void opt(const char *Str, Opt &O) {
1014 template <unsigned n> struct applicator<const char[n]> {
1015 template <class Opt> static void opt(const char *Str, Opt &O) {
1019 template <> struct applicator<const char *> {
1020 template <class Opt> static void opt(const char *Str, Opt &O) {
1025 template <> struct applicator<NumOccurrencesFlag> {
1026 static void opt(NumOccurrencesFlag N, Option &O) {
1027 O.setNumOccurrencesFlag(N);
1030 template <> struct applicator<ValueExpected> {
1031 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
1033 template <> struct applicator<OptionHidden> {
1034 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
1036 template <> struct applicator<FormattingFlags> {
1037 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
1039 template <> struct applicator<MiscFlags> {
1040 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
1043 // apply method - Apply modifiers to an option in a type safe way.
1044 template <class Opt, class Mod, class... Mods>
1045 void apply(Opt *O, const Mod &M, const Mods &... Ms) {
1046 applicator<Mod>::opt(M, *O);
1050 template <class Opt, class Mod> void apply(Opt *O, const Mod &M) {
1051 applicator<Mod>::opt(M, *O);
1054 //===----------------------------------------------------------------------===//
1055 // opt_storage class
1057 // Default storage class definition: external storage. This implementation
1058 // assumes the user will specify a variable to store the data into with the
1059 // cl::location(x) modifier.
1061 template <class DataType, bool ExternalStorage, bool isClass>
1063 DataType *Location; // Where to store the object...
1064 OptionValue<DataType> Default;
1066 void check_location() const {
1067 assert(Location && "cl::location(...) not specified for a command "
1068 "line option with external storage, "
1069 "or cl::init specified before cl::location()!!");
1073 opt_storage() : Location(nullptr) {}
1075 bool setLocation(Option &O, DataType &L) {
1077 return O.error("cl::location(x) specified more than once!");
1083 template <class T> void setValue(const T &V, bool initial = false) {
1090 DataType &getValue() {
1094 const DataType &getValue() const {
1099 operator DataType() const { return this->getValue(); }
1101 const OptionValue<DataType> &getDefault() const { return Default; }
1104 // Define how to hold a class type object, such as a string. Since we can
1105 // inherit from a class, we do so. This makes us exactly compatible with the
1106 // object in all cases that it is used.
1108 template <class DataType>
1109 class opt_storage<DataType, false, true> : public DataType {
1111 OptionValue<DataType> Default;
1113 template <class T> void setValue(const T &V, bool initial = false) {
1114 DataType::operator=(V);
1119 DataType &getValue() { return *this; }
1120 const DataType &getValue() const { return *this; }
1122 const OptionValue<DataType> &getDefault() const { return Default; }
1125 // Define a partial specialization to handle things we cannot inherit from. In
1126 // this case, we store an instance through containment, and overload operators
1127 // to get at the value.
1129 template <class DataType> class opt_storage<DataType, false, false> {
1132 OptionValue<DataType> Default;
1134 // Make sure we initialize the value with the default constructor for the
1136 opt_storage() : Value(DataType()), Default(DataType()) {}
1138 template <class T> void setValue(const T &V, bool initial = false) {
1143 DataType &getValue() { return Value; }
1144 DataType getValue() const { return Value; }
1146 const OptionValue<DataType> &getDefault() const { return Default; }
1148 operator DataType() const { return getValue(); }
1150 // If the datatype is a pointer, support -> on it.
1151 DataType operator->() const { return Value; }
1154 //===----------------------------------------------------------------------===//
1155 // opt - A scalar command line option.
1157 template <class DataType, bool ExternalStorage = false,
1158 class ParserClass = parser<DataType>>
1159 class opt : public Option,
1160 public opt_storage<DataType, ExternalStorage,
1161 std::is_class<DataType>::value> {
1164 bool handleOccurrence(unsigned pos, StringRef ArgName,
1165 StringRef Arg) override {
1166 typename ParserClass::parser_data_type Val =
1167 typename ParserClass::parser_data_type();
1168 if (Parser.parse(*this, ArgName, Arg, Val))
1169 return true; // Parse error!
1170 this->setValue(Val);
1171 this->setPosition(pos);
1175 enum ValueExpected getValueExpectedFlagDefault() const override {
1176 return Parser.getValueExpectedFlagDefault();
1179 getExtraOptionNames(SmallVectorImpl<const char *> &OptionNames) override {
1180 return Parser.getExtraOptionNames(OptionNames);
1183 // Forward printing stuff to the parser...
1184 size_t getOptionWidth() const override {
1185 return Parser.getOptionWidth(*this);
1187 void printOptionInfo(size_t GlobalWidth) const override {
1188 Parser.printOptionInfo(*this, GlobalWidth);
1191 void printOptionValue(size_t GlobalWidth, bool Force) const override {
1192 if (Force || this->getDefault().compare(this->getValue())) {
1193 cl::printOptionDiff<ParserClass>(*this, Parser, this->getValue(),
1194 this->getDefault(), GlobalWidth);
1200 Parser.initialize();
1203 // Command line options should not be copyable
1204 opt(const opt &) = delete;
1205 opt &operator=(const opt &) = delete;
1208 // setInitialValue - Used by the cl::init modifier...
1209 void setInitialValue(const DataType &V) { this->setValue(V, true); }
1211 ParserClass &getParser() { return Parser; }
1213 template <class T> DataType &operator=(const T &Val) {
1214 this->setValue(Val);
1215 return this->getValue();
1218 template <class... Mods>
1219 explicit opt(const Mods &... Ms)
1220 : Option(Optional, NotHidden), Parser(*this) {
1226 EXTERN_TEMPLATE_INSTANTIATION(class opt<unsigned>);
1227 EXTERN_TEMPLATE_INSTANTIATION(class opt<int>);
1228 EXTERN_TEMPLATE_INSTANTIATION(class opt<std::string>);
1229 EXTERN_TEMPLATE_INSTANTIATION(class opt<char>);
1230 EXTERN_TEMPLATE_INSTANTIATION(class opt<bool>);
1232 //===----------------------------------------------------------------------===//
1233 // list_storage class
1235 // Default storage class definition: external storage. This implementation
1236 // assumes the user will specify a variable to store the data into with the
1237 // cl::location(x) modifier.
1239 template <class DataType, class StorageClass> class list_storage {
1240 StorageClass *Location; // Where to store the object...
1243 list_storage() : Location(0) {}
1245 bool setLocation(Option &O, StorageClass &L) {
1247 return O.error("cl::location(x) specified more than once!");
1252 template <class T> void addValue(const T &V) {
1253 assert(Location != 0 && "cl::location(...) not specified for a command "
1254 "line option with external storage!");
1255 Location->push_back(V);
1259 // Define how to hold a class type object, such as a string. Since we can
1260 // inherit from a class, we do so. This makes us exactly compatible with the
1261 // object in all cases that it is used.
1263 template <class DataType>
1264 class list_storage<DataType, bool> : public std::vector<DataType> {
1266 template <class T> void addValue(const T &V) {
1267 std::vector<DataType>::push_back(V);
1271 //===----------------------------------------------------------------------===//
1272 // list - A list of command line options.
1274 template <class DataType, class Storage = bool,
1275 class ParserClass = parser<DataType>>
1276 class list : public Option, public list_storage<DataType, Storage> {
1277 std::vector<unsigned> Positions;
1280 enum ValueExpected getValueExpectedFlagDefault() const override {
1281 return Parser.getValueExpectedFlagDefault();
1284 getExtraOptionNames(SmallVectorImpl<const char *> &OptionNames) override {
1285 return Parser.getExtraOptionNames(OptionNames);
1288 bool handleOccurrence(unsigned pos, StringRef ArgName,
1289 StringRef Arg) override {
1290 typename ParserClass::parser_data_type Val =
1291 typename ParserClass::parser_data_type();
1292 if (Parser.parse(*this, ArgName, Arg, Val))
1293 return true; // Parse Error!
1294 list_storage<DataType, Storage>::addValue(Val);
1296 Positions.push_back(pos);
1300 // Forward printing stuff to the parser...
1301 size_t getOptionWidth() const override {
1302 return Parser.getOptionWidth(*this);
1304 void printOptionInfo(size_t GlobalWidth) const override {
1305 Parser.printOptionInfo(*this, GlobalWidth);
1308 // Unimplemented: list options don't currently store their default value.
1309 void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
1314 Parser.initialize();
1317 // Command line options should not be copyable
1318 list(const list &) = delete;
1319 list &operator=(const list &) = delete;
1322 ParserClass &getParser() { return Parser; }
1324 unsigned getPosition(unsigned optnum) const {
1325 assert(optnum < this->size() && "Invalid option index");
1326 return Positions[optnum];
1329 void setNumAdditionalVals(unsigned n) { Option::setNumAdditionalVals(n); }
1331 template <class... Mods>
1332 explicit list(const Mods &... Ms)
1333 : Option(ZeroOrMore, NotHidden), Parser(*this) {
1339 // multi_val - Modifier to set the number of additional values.
1341 unsigned AdditionalVals;
1342 explicit multi_val(unsigned N) : AdditionalVals(N) {}
1344 template <typename D, typename S, typename P>
1345 void apply(list<D, S, P> &L) const {
1346 L.setNumAdditionalVals(AdditionalVals);
1350 //===----------------------------------------------------------------------===//
1351 // bits_storage class
1353 // Default storage class definition: external storage. This implementation
1354 // assumes the user will specify a variable to store the data into with the
1355 // cl::location(x) modifier.
1357 template <class DataType, class StorageClass> class bits_storage {
1358 unsigned *Location; // Where to store the bits...
1360 template <class T> static unsigned Bit(const T &V) {
1361 unsigned BitPos = reinterpret_cast<unsigned>(V);
1362 assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
1363 "enum exceeds width of bit vector!");
1368 bits_storage() : Location(nullptr) {}
1370 bool setLocation(Option &O, unsigned &L) {
1372 return O.error("cl::location(x) specified more than once!");
1377 template <class T> void addValue(const T &V) {
1378 assert(Location != 0 && "cl::location(...) not specified for a command "
1379 "line option with external storage!");
1380 *Location |= Bit(V);
1383 unsigned getBits() { return *Location; }
1385 template <class T> bool isSet(const T &V) {
1386 return (*Location & Bit(V)) != 0;
1390 // Define how to hold bits. Since we can inherit from a class, we do so.
1391 // This makes us exactly compatible with the bits in all cases that it is used.
1393 template <class DataType> class bits_storage<DataType, bool> {
1394 unsigned Bits; // Where to store the bits...
1396 template <class T> static unsigned Bit(const T &V) {
1397 unsigned BitPos = (unsigned)V;
1398 assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
1399 "enum exceeds width of bit vector!");
1404 template <class T> void addValue(const T &V) { Bits |= Bit(V); }
1406 unsigned getBits() { return Bits; }
1408 template <class T> bool isSet(const T &V) { return (Bits & Bit(V)) != 0; }
1411 //===----------------------------------------------------------------------===//
1412 // bits - A bit vector of command options.
1414 template <class DataType, class Storage = bool,
1415 class ParserClass = parser<DataType>>
1416 class bits : public Option, public bits_storage<DataType, Storage> {
1417 std::vector<unsigned> Positions;
1420 enum ValueExpected getValueExpectedFlagDefault() const override {
1421 return Parser.getValueExpectedFlagDefault();
1424 getExtraOptionNames(SmallVectorImpl<const char *> &OptionNames) override {
1425 return Parser.getExtraOptionNames(OptionNames);
1428 bool handleOccurrence(unsigned pos, StringRef ArgName,
1429 StringRef Arg) override {
1430 typename ParserClass::parser_data_type Val =
1431 typename ParserClass::parser_data_type();
1432 if (Parser.parse(*this, ArgName, Arg, Val))
1433 return true; // Parse Error!
1434 this->addValue(Val);
1436 Positions.push_back(pos);
1440 // Forward printing stuff to the parser...
1441 size_t getOptionWidth() const override {
1442 return Parser.getOptionWidth(*this);
1444 void printOptionInfo(size_t GlobalWidth) const override {
1445 Parser.printOptionInfo(*this, GlobalWidth);
1448 // Unimplemented: bits options don't currently store their default values.
1449 void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
1454 Parser.initialize();
1457 // Command line options should not be copyable
1458 bits(const bits &) = delete;
1459 bits &operator=(const bits &) = delete;
1462 ParserClass &getParser() { return Parser; }
1464 unsigned getPosition(unsigned optnum) const {
1465 assert(optnum < this->size() && "Invalid option index");
1466 return Positions[optnum];
1469 template <class... Mods>
1470 explicit bits(const Mods &... Ms)
1471 : Option(ZeroOrMore, NotHidden), Parser(*this) {
1477 //===----------------------------------------------------------------------===//
1478 // Aliased command line option (alias this name to a preexisting name)
1481 class alias : public Option {
1483 bool handleOccurrence(unsigned pos, StringRef /*ArgName*/,
1484 StringRef Arg) override {
1485 return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
1487 bool addOccurrence(unsigned pos, StringRef /*ArgName*/, StringRef Value,
1488 bool MultiArg = false) override {
1489 return AliasFor->addOccurrence(pos, AliasFor->ArgStr, Value, MultiArg);
1491 // Handle printing stuff...
1492 size_t getOptionWidth() const override;
1493 void printOptionInfo(size_t GlobalWidth) const override;
1495 // Aliases do not need to print their values.
1496 void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
1499 ValueExpected getValueExpectedFlagDefault() const override {
1500 return AliasFor->getValueExpectedFlag();
1505 error("cl::alias must have argument name specified!");
1507 error("cl::alias must have an cl::aliasopt(option) specified!");
1511 // Command line options should not be copyable
1512 alias(const alias &) = delete;
1513 alias &operator=(const alias &) = delete;
1516 void setAliasFor(Option &O) {
1518 error("cl::alias must only have one cl::aliasopt(...) specified!");
1522 template <class... Mods>
1523 explicit alias(const Mods &... Ms)
1524 : Option(Optional, Hidden), AliasFor(nullptr) {
1530 // aliasfor - Modifier to set the option an alias aliases.
1533 explicit aliasopt(Option &O) : Opt(O) {}
1534 void apply(alias &A) const { A.setAliasFor(Opt); }
1537 // extrahelp - provide additional help at the end of the normal help
1538 // output. All occurrences of cl::extrahelp will be accumulated and
1539 // printed to stderr at the end of the regular help, just before
1542 const char *morehelp;
1543 explicit extrahelp(const char *help);
1546 void PrintVersionMessage();
1548 /// This function just prints the help message, exactly the same way as if the
1549 /// -help or -help-hidden option had been given on the command line.
1551 /// NOTE: THIS FUNCTION TERMINATES THE PROGRAM!
1553 /// \param Hidden if true will print hidden options
1554 /// \param Categorized if true print options in categories
1555 void PrintHelpMessage(bool Hidden = false, bool Categorized = false);
1557 //===----------------------------------------------------------------------===//
1558 // Public interface for accessing registered options.
1561 /// \brief Use this to get a StringMap to all registered named options
1562 /// (e.g. -help). Note \p Map Should be an empty StringMap.
1564 /// \return A reference to the StringMap used by the cl APIs to parse options.
1566 /// Access to unnamed arguments (i.e. positional) are not provided because
1567 /// it is expected that the client already has access to these.
1571 /// main(int argc,char* argv[]) {
1572 /// StringMap<llvm::cl::Option*> &opts = llvm::cl::getRegisteredOptions();
1573 /// assert(opts.count("help") == 1)
1574 /// opts["help"]->setDescription("Show alphabetical help information")
1576 /// llvm::cl::ParseCommandLineOptions(argc,argv);
1581 /// This interface is useful for modifying options in libraries that are out of
1582 /// the control of the client. The options should be modified before calling
1583 /// llvm::cl::ParseCommandLineOptions().
1585 /// Hopefully this API can be depricated soon. Any situation where options need
1586 /// to be modified by tools or libraries should be handled by sane APIs rather
1587 /// than just handing around a global list.
1588 StringMap<Option *> &getRegisteredOptions();
1590 //===----------------------------------------------------------------------===//
1591 // Standalone command line processing utilities.
1594 /// \brief Saves strings in the inheritor's stable storage and returns a stable
1595 /// raw character pointer.
1597 virtual void anchor();
1600 virtual const char *SaveString(const char *Str) = 0;
1601 virtual ~StringSaver(){}; // Pacify -Wnon-virtual-dtor.
1604 /// \brief Tokenizes a command line that can contain escapes and quotes.
1606 /// The quoting rules match those used by GCC and other tools that use
1607 /// libiberty's buildargv() or expandargv() utilities, and do not match bash.
1608 /// They differ from buildargv() on treatment of backslashes that do not escape
1609 /// a special character to make it possible to accept most Windows file paths.
1611 /// \param [in] Source The string to be split on whitespace with quotes.
1612 /// \param [in] Saver Delegates back to the caller for saving parsed strings.
1613 /// \param [in] MarkEOLs true if tokenizing a response file and you want end of
1614 /// lines and end of the response file to be marked with a nullptr string.
1615 /// \param [out] NewArgv All parsed strings are appended to NewArgv.
1616 void TokenizeGNUCommandLine(StringRef Source, StringSaver &Saver,
1617 SmallVectorImpl<const char *> &NewArgv,
1618 bool MarkEOLs = false);
1620 /// \brief Tokenizes a Windows command line which may contain quotes and escaped
1623 /// See MSDN docs for CommandLineToArgvW for information on the quoting rules.
1624 /// http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx
1626 /// \param [in] Source The string to be split on whitespace with quotes.
1627 /// \param [in] Saver Delegates back to the caller for saving parsed strings.
1628 /// \param [in] MarkEOLs true if tokenizing a response file and you want end of
1629 /// lines and end of the response file to be marked with a nullptr string.
1630 /// \param [out] NewArgv All parsed strings are appended to NewArgv.
1631 void TokenizeWindowsCommandLine(StringRef Source, StringSaver &Saver,
1632 SmallVectorImpl<const char *> &NewArgv,
1633 bool MarkEOLs = false);
1635 /// \brief String tokenization function type. Should be compatible with either
1636 /// Windows or Unix command line tokenizers.
1637 typedef void (*TokenizerCallback)(StringRef Source, StringSaver &Saver,
1638 SmallVectorImpl<const char *> &NewArgv,
1641 /// \brief Expand response files on a command line recursively using the given
1642 /// StringSaver and tokenization strategy. Argv should contain the command line
1643 /// before expansion and will be modified in place. If requested, Argv will
1644 /// also be populated with nullptrs indicating where each response file line
1645 /// ends, which is useful for the "/link" argument that needs to consume all
1646 /// remaining arguments only until the next end of line, when in a response
1649 /// \param [in] Saver Delegates back to the caller for saving parsed strings.
1650 /// \param [in] Tokenizer Tokenization strategy. Typically Unix or Windows.
1651 /// \param [in,out] Argv Command line into which to expand response files.
1652 /// \param [in] MarkEOLs Mark end of lines and the end of the response file
1653 /// with nullptrs in the Argv vector.
1654 /// \return true if all @files were expanded successfully or there were none.
1655 bool ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer,
1656 SmallVectorImpl<const char *> &Argv,
1657 bool MarkEOLs = false);
1659 /// \brief Mark all options not part of this category as cl::ReallyHidden.
1661 /// \param Category the category of options to keep displaying
1663 /// Some tools (like clang-format) like to be able to hide all options that are
1664 /// not specific to the tool. This function allows a tool to specify a single
1665 /// option category to display in the -help output.
1666 void HideUnrelatedOptions(cl::OptionCategory &Category);
1668 /// \brief Mark all options not part of the categories as cl::ReallyHidden.
1670 /// \param Categories the categories of options to keep displaying.
1672 /// Some tools (like clang-format) like to be able to hide all options that are
1673 /// not specific to the tool. This function allows a tool to specify a single
1674 /// option category to display in the -help output.
1675 void HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *> Categories);
1677 } // End namespace cl
1679 } // End namespace llvm