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/Support/type_traits.h"
24 #include "llvm/Support/Compiler.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/Twine.h"
35 /// cl Namespace - This namespace contains all of the command line option
36 /// processing machinery. It is intentionally a short name to make qualified
40 //===----------------------------------------------------------------------===//
41 // ParseCommandLineOptions - Command line option processing entry point.
43 void ParseCommandLineOptions(int argc, char **argv,
44 const char *Overview = 0,
45 bool ReadResponseFiles = false);
47 //===----------------------------------------------------------------------===//
48 // ParseEnvironmentOptions - Environment variable option processing alternate
51 void ParseEnvironmentOptions(const char *progName, const char *envvar,
52 const char *Overview = 0,
53 bool ReadResponseFiles = false);
55 ///===---------------------------------------------------------------------===//
56 /// SetVersionPrinter - Override the default (LLVM specific) version printer
57 /// used to print out the version when --version is given
58 /// on the command line. This allows other systems using the
59 /// CommandLine utilities to print their own version string.
60 void SetVersionPrinter(void (*func)());
63 // MarkOptionsChanged - Internal helper function.
64 void MarkOptionsChanged();
66 //===----------------------------------------------------------------------===//
67 // Flags permitted to be passed to command line arguments
70 enum NumOccurrencesFlag { // Flags for the number of occurrences allowed
71 Optional = 0x01, // Zero or One occurrence
72 ZeroOrMore = 0x02, // Zero or more occurrences allowed
73 Required = 0x03, // One occurrence required
74 OneOrMore = 0x04, // One or more occurrences required
76 // ConsumeAfter - Indicates that this option is fed anything that follows the
77 // last positional argument required by the application (it is an error if
78 // there are zero positional arguments, and a ConsumeAfter option is used).
79 // Thus, for example, all arguments to LLI are processed until a filename is
80 // found. Once a filename is found, all of the succeeding arguments are
81 // passed, unprocessed, to the ConsumeAfter option.
85 OccurrencesMask = 0x07
88 enum ValueExpected { // Is a value required for the option?
89 ValueOptional = 0x08, // The value can appear... or not
90 ValueRequired = 0x10, // The value is required to appear!
91 ValueDisallowed = 0x18, // A value may not be specified (for flags)
95 enum OptionHidden { // Control whether -help shows this option
96 NotHidden = 0x20, // Option included in --help & --help-hidden
97 Hidden = 0x40, // -help doesn't, but --help-hidden does
98 ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg
102 // Formatting flags - This controls special features that the option might have
103 // that cause it to be parsed differently...
105 // Prefix - This option allows arguments that are otherwise unrecognized to be
106 // matched by options that are a prefix of the actual value. This is useful for
107 // cases like a linker, where options are typically of the form '-lfoo' or
108 // '-L../../include' where -l or -L are the actual flags. When prefix is
109 // enabled, and used, the value for the flag comes from the suffix of the
112 // Grouping - With this option enabled, multiple letter options are allowed to
113 // bunch together with only a single hyphen for the whole group. This allows
114 // emulation of the behavior that ls uses for example: ls -la === ls -l -a
117 enum FormattingFlags {
118 NormalFormatting = 0x000, // Nothing special
119 Positional = 0x080, // Is a positional argument, no '-' required
120 Prefix = 0x100, // Can this option directly prefix its value?
121 Grouping = 0x180, // Can this option group with other options?
122 FormattingMask = 0x180 // Union of the above flags.
125 enum MiscFlags { // Miscellaneous flags to adjust argument
126 CommaSeparated = 0x200, // Should this cl::list split between commas?
127 PositionalEatsArgs = 0x400, // Should this positional cl::list eat -args?
128 Sink = 0x800, // Should this cl::list eat all unknown options?
129 MiscMask = 0xE00 // Union of the above flags.
134 //===----------------------------------------------------------------------===//
141 // handleOccurrences - Overriden by subclasses to handle the value passed into
142 // an argument. Should return true if there was an error processing the
143 // argument and the program should exit.
145 virtual bool handleOccurrence(unsigned pos, StringRef ArgName,
148 virtual enum ValueExpected getValueExpectedFlagDefault() const {
149 return ValueOptional;
152 // Out of line virtual function to provide home for the class.
153 virtual void anchor();
155 int NumOccurrences; // The number of times specified
156 int Flags; // Flags for the argument
157 unsigned Position; // Position of last occurrence of the option
158 unsigned AdditionalVals;// Greater than 0 for multi-valued option.
159 Option *NextRegistered; // Singly linked list of registered options.
161 const char *ArgStr; // The argument string itself (ex: "help", "o")
162 const char *HelpStr; // The descriptive text message for --help
163 const char *ValueStr; // String describing what the value of this option is
165 inline enum NumOccurrencesFlag getNumOccurrencesFlag() const {
166 return static_cast<enum NumOccurrencesFlag>(Flags & OccurrencesMask);
168 inline enum ValueExpected getValueExpectedFlag() const {
169 int VE = Flags & ValueMask;
170 return VE ? static_cast<enum ValueExpected>(VE)
171 : getValueExpectedFlagDefault();
173 inline enum OptionHidden getOptionHiddenFlag() const {
174 return static_cast<enum OptionHidden>(Flags & HiddenMask);
176 inline enum FormattingFlags getFormattingFlag() const {
177 return static_cast<enum FormattingFlags>(Flags & FormattingMask);
179 inline unsigned getMiscFlags() const {
180 return Flags & MiscMask;
182 inline unsigned getPosition() const { return Position; }
183 inline unsigned getNumAdditionalVals() const { return AdditionalVals; }
185 // hasArgStr - Return true if the argstr != ""
186 bool hasArgStr() const { return ArgStr[0] != 0; }
188 //-------------------------------------------------------------------------===
189 // Accessor functions set by OptionModifiers
191 void setArgStr(const char *S) { ArgStr = S; }
192 void setDescription(const char *S) { HelpStr = S; }
193 void setValueStr(const char *S) { ValueStr = S; }
195 void setFlag(unsigned Flag, unsigned FlagMask) {
200 void setNumOccurrencesFlag(enum NumOccurrencesFlag Val) {
201 setFlag(Val, OccurrencesMask);
203 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
204 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
205 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
206 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
207 void setPosition(unsigned pos) { Position = pos; }
209 explicit Option(unsigned DefaultFlags)
210 : NumOccurrences(0), Flags(DefaultFlags | NormalFormatting), Position(0),
211 AdditionalVals(0), NextRegistered(0),
212 ArgStr(""), HelpStr(""), ValueStr("") {
213 assert(getNumOccurrencesFlag() != 0 &&
214 getOptionHiddenFlag() != 0 && "Not all default flags specified!");
217 inline void setNumAdditionalVals(unsigned n) { AdditionalVals = n; }
219 // addArgument - Register this argument with the commandline system.
223 Option *getNextRegisteredOption() const { return NextRegistered; }
225 // Return the width of the option tag for printing...
226 virtual size_t getOptionWidth() const = 0;
228 // printOptionInfo - Print out information about this option. The
229 // to-be-maintained width is specified.
231 virtual void printOptionInfo(size_t GlobalWidth) const = 0;
233 virtual void getExtraOptionNames(SmallVectorImpl<const char*> &) {}
235 // addOccurrence - Wrapper around handleOccurrence that enforces Flags.
237 bool addOccurrence(unsigned pos, StringRef ArgName,
238 StringRef Value, bool MultiArg = false);
240 // Prints option name followed by message. Always returns true.
241 bool error(const Twine &Message, StringRef ArgName = StringRef());
244 inline int getNumOccurrences() const { return NumOccurrences; }
249 //===----------------------------------------------------------------------===//
250 // Command line option modifiers that can be used to modify the behavior of
251 // command line option parsers...
254 // desc - Modifier to set the description shown in the --help output...
257 desc(const char *Str) : Desc(Str) {}
258 void apply(Option &O) const { O.setDescription(Desc); }
261 // value_desc - Modifier to set the value description shown in the --help
265 value_desc(const char *Str) : Desc(Str) {}
266 void apply(Option &O) const { O.setValueStr(Desc); }
269 // init - Specify a default (initial) value for the command line argument, if
270 // the default constructor for the argument type does not give you what you
271 // want. This is only valid on "opt" arguments, not on "list" arguments.
276 initializer(const Ty &Val) : Init(Val) {}
279 void apply(Opt &O) const { O.setInitialValue(Init); }
283 initializer<Ty> init(const Ty &Val) {
284 return initializer<Ty>(Val);
288 // location - Allow the user to specify which external variable they want to
289 // store the results of the command line argument processing into, if they don't
290 // want to store it in the option itself.
293 struct LocationClass {
295 LocationClass(Ty &L) : Loc(L) {}
298 void apply(Opt &O) const { O.setLocation(O, Loc); }
302 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
305 //===----------------------------------------------------------------------===//
306 // Enum valued command line option
308 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, int(ENUMVAL), DESC
309 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, int(ENUMVAL), DESC
310 #define clEnumValEnd (reinterpret_cast<void*>(0))
312 // values - For custom data types, allow specifying a group of values together
313 // as the values that go into the mapping that the option handler uses. Note
314 // that the values list must always have a 0 at the end of the list to indicate
315 // that the list has ended.
317 template<class DataType>
319 // Use a vector instead of a map, because the lists should be short,
320 // the overhead is less, and most importantly, it keeps them in the order
321 // inserted so we can print our option out nicely.
322 SmallVector<std::pair<const char *, std::pair<int, const char *> >,4> Values;
323 void processValues(va_list Vals);
325 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
327 // Insert the first value, which is required.
328 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
330 // Process the varargs portion of the values...
331 while (const char *enumName = va_arg(ValueArgs, const char *)) {
332 DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int));
333 const char *EnumDesc = va_arg(ValueArgs, const char *);
334 Values.push_back(std::make_pair(enumName, // Add value to value map
335 std::make_pair(EnumVal, EnumDesc)));
340 void apply(Opt &O) const {
341 for (unsigned i = 0, e = static_cast<unsigned>(Values.size());
343 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
344 Values[i].second.second);
348 template<class DataType>
349 ValuesClass<DataType> END_WITH_NULL values(const char *Arg, DataType Val,
350 const char *Desc, ...) {
352 va_start(ValueArgs, Desc);
353 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
359 //===----------------------------------------------------------------------===//
360 // parser class - Parameterizable parser for different data types. By default,
361 // known data types (string, int, bool) have specialized parsers, that do what
362 // you would expect. The default parser, used for data types that are not
363 // built-in, uses a mapping table to map specific options to values, which is
364 // used, among other things, to handle enum types.
366 //--------------------------------------------------
367 // generic_parser_base - This class holds all the non-generic code that we do
368 // not need replicated for every instance of the generic parser. This also
369 // allows us to put stuff into CommandLine.cpp
371 struct generic_parser_base {
372 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
374 // getNumOptions - Virtual function implemented by generic subclass to
375 // indicate how many entries are in Values.
377 virtual unsigned getNumOptions() const = 0;
379 // getOption - Return option name N.
380 virtual const char *getOption(unsigned N) const = 0;
382 // getDescription - Return description N
383 virtual const char *getDescription(unsigned N) const = 0;
385 // Return the width of the option tag for printing...
386 virtual size_t getOptionWidth(const Option &O) const;
388 // printOptionInfo - Print out information about this option. The
389 // to-be-maintained width is specified.
391 virtual void printOptionInfo(const Option &O, size_t GlobalWidth) const;
393 void initialize(Option &O) {
394 // All of the modifiers for the option have been processed by now, so the
395 // argstr field should be stable, copy it down now.
397 hasArgStr = O.hasArgStr();
400 void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) {
401 // If there has been no argstr specified, that means that we need to add an
402 // argument for every possible option. This ensures that our options are
405 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
406 OptionNames.push_back(getOption(i));
410 enum ValueExpected getValueExpectedFlagDefault() const {
411 // If there is an ArgStr specified, then we are of the form:
413 // -opt=O2 or -opt O2 or -optO2
415 // In which case, the value is required. Otherwise if an arg str has not
416 // been specified, we are of the form:
418 // -O2 or O2 or -la (where -l and -a are separate options)
420 // If this is the case, we cannot allow a value.
423 return ValueRequired;
425 return ValueDisallowed;
428 // findOption - Return the option number corresponding to the specified
429 // argument string. If the option is not found, getNumOptions() is returned.
431 unsigned findOption(const char *Name);
437 // Default parser implementation - This implementation depends on having a
438 // mapping of recognized options to values of some sort. In addition to this,
439 // each entry in the mapping also tracks a help message that is printed with the
440 // command line option for --help. Because this is a simple mapping parser, the
441 // data type can be any unsupported type.
443 template <class DataType>
444 class parser : public generic_parser_base {
446 SmallVector<std::pair<const char *,
447 std::pair<DataType, const char *> >, 8> Values;
449 typedef DataType parser_data_type;
451 // Implement virtual functions needed by generic_parser_base
452 unsigned getNumOptions() const { return unsigned(Values.size()); }
453 const char *getOption(unsigned N) const { return Values[N].first; }
454 const char *getDescription(unsigned N) const {
455 return Values[N].second.second;
458 // parse - Return true on error.
459 bool parse(Option &O, StringRef ArgName, StringRef Arg, DataType &V) {
466 for (unsigned i = 0, e = static_cast<unsigned>(Values.size());
468 if (Values[i].first == ArgVal) {
469 V = Values[i].second.first;
473 return O.error("Cannot find option named '" + ArgVal + "'!");
476 /// addLiteralOption - Add an entry to the mapping table.
479 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
480 assert(findOption(Name) == Values.size() && "Option already exists!");
481 Values.push_back(std::make_pair(Name,
482 std::make_pair(static_cast<DataType>(V),HelpStr)));
483 MarkOptionsChanged();
486 /// removeLiteralOption - Remove the specified option.
488 void removeLiteralOption(const char *Name) {
489 unsigned N = findOption(Name);
490 assert(N != Values.size() && "Option not found!");
491 Values.erase(Values.begin()+N);
495 //--------------------------------------------------
496 // basic_parser - Super class of parsers to provide boilerplate code
498 class basic_parser_impl { // non-template implementation of basic_parser<t>
500 virtual ~basic_parser_impl() {}
502 enum ValueExpected getValueExpectedFlagDefault() const {
503 return ValueRequired;
506 void getExtraOptionNames(SmallVectorImpl<const char*> &) {}
508 void initialize(Option &) {}
510 // Return the width of the option tag for printing...
511 size_t getOptionWidth(const Option &O) const;
513 // printOptionInfo - Print out information about this option. The
514 // to-be-maintained width is specified.
516 void printOptionInfo(const Option &O, size_t GlobalWidth) const;
518 // getValueName - Overload in subclass to provide a better default value.
519 virtual const char *getValueName() const { return "value"; }
521 // An out-of-line virtual method to provide a 'home' for this class.
522 virtual void anchor();
525 // basic_parser - The real basic parser is just a template wrapper that provides
526 // a typedef for the provided data type.
528 template<class DataType>
529 class basic_parser : public basic_parser_impl {
531 typedef DataType parser_data_type;
534 //--------------------------------------------------
538 class parser<bool> : public basic_parser<bool> {
542 // parse - Return true on error.
543 bool parse(Option &O, StringRef ArgName, StringRef Arg, bool &Val);
546 void initialize(Opt &O) {
550 enum ValueExpected getValueExpectedFlagDefault() const {
551 return ValueOptional;
554 // getValueName - Do not print =<value> at all.
555 virtual const char *getValueName() const { return 0; }
557 // An out-of-line virtual method to provide a 'home' for this class.
558 virtual void anchor();
561 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<bool>);
563 //--------------------------------------------------
564 // parser<boolOrDefault>
565 enum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE };
567 class parser<boolOrDefault> : public basic_parser<boolOrDefault> {
569 // parse - Return true on error.
570 bool parse(Option &O, StringRef ArgName, StringRef Arg, boolOrDefault &Val);
572 enum ValueExpected getValueExpectedFlagDefault() const {
573 return ValueOptional;
576 // getValueName - Do not print =<value> at all.
577 virtual const char *getValueName() const { return 0; }
579 // An out-of-line virtual method to provide a 'home' for this class.
580 virtual void anchor();
583 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<boolOrDefault>);
585 //--------------------------------------------------
589 class parser<int> : public basic_parser<int> {
591 // parse - Return true on error.
592 bool parse(Option &O, StringRef ArgName, StringRef Arg, int &Val);
594 // getValueName - Overload in subclass to provide a better default value.
595 virtual const char *getValueName() const { return "int"; }
597 // An out-of-line virtual method to provide a 'home' for this class.
598 virtual void anchor();
601 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<int>);
604 //--------------------------------------------------
608 class parser<unsigned> : public basic_parser<unsigned> {
610 // parse - Return true on error.
611 bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned &Val);
613 // getValueName - Overload in subclass to provide a better default value.
614 virtual const char *getValueName() const { return "uint"; }
616 // An out-of-line virtual method to provide a 'home' for this class.
617 virtual void anchor();
620 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<unsigned>);
622 //--------------------------------------------------
626 class parser<double> : public basic_parser<double> {
628 // parse - Return true on error.
629 bool parse(Option &O, StringRef ArgName, StringRef Arg, double &Val);
631 // getValueName - Overload in subclass to provide a better default value.
632 virtual const char *getValueName() const { return "number"; }
634 // An out-of-line virtual method to provide a 'home' for this class.
635 virtual void anchor();
638 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<double>);
640 //--------------------------------------------------
644 class parser<float> : public basic_parser<float> {
646 // parse - Return true on error.
647 bool parse(Option &O, StringRef ArgName, StringRef Arg, float &Val);
649 // getValueName - Overload in subclass to provide a better default value.
650 virtual const char *getValueName() const { return "number"; }
652 // An out-of-line virtual method to provide a 'home' for this class.
653 virtual void anchor();
656 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<float>);
658 //--------------------------------------------------
659 // parser<std::string>
662 class parser<std::string> : public basic_parser<std::string> {
664 // parse - Return true on error.
665 bool parse(Option &, StringRef, StringRef Arg, std::string &Value) {
670 // getValueName - Overload in subclass to provide a better default value.
671 virtual const char *getValueName() const { return "string"; }
673 // An out-of-line virtual method to provide a 'home' for this class.
674 virtual void anchor();
677 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<std::string>);
679 //--------------------------------------------------
683 class parser<char> : public basic_parser<char> {
685 // parse - Return true on error.
686 bool parse(Option &, StringRef, StringRef Arg, char &Value) {
691 // getValueName - Overload in subclass to provide a better default value.
692 virtual const char *getValueName() const { return "char"; }
694 // An out-of-line virtual method to provide a 'home' for this class.
695 virtual void anchor();
698 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<char>);
700 //===----------------------------------------------------------------------===//
701 // applicator class - This class is used because we must use partial
702 // specialization to handle literal string arguments specially (const char* does
703 // not correctly respond to the apply method). Because the syntax to use this
704 // is a pain, we have the 'apply' method below to handle the nastiness...
706 template<class Mod> struct applicator {
708 static void opt(const Mod &M, Opt &O) { M.apply(O); }
711 // Handle const char* as a special case...
712 template<unsigned n> struct applicator<char[n]> {
714 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
716 template<unsigned n> struct applicator<const char[n]> {
718 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
720 template<> struct applicator<const char*> {
722 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
725 template<> struct applicator<NumOccurrencesFlag> {
726 static void opt(NumOccurrencesFlag NO, Option &O) {
727 O.setNumOccurrencesFlag(NO);
730 template<> struct applicator<ValueExpected> {
731 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
733 template<> struct applicator<OptionHidden> {
734 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
736 template<> struct applicator<FormattingFlags> {
737 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
739 template<> struct applicator<MiscFlags> {
740 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
743 // apply method - Apply a modifier to an option in a type safe way.
744 template<class Mod, class Opt>
745 void apply(const Mod &M, Opt *O) {
746 applicator<Mod>::opt(M, *O);
750 //===----------------------------------------------------------------------===//
753 // Default storage class definition: external storage. This implementation
754 // assumes the user will specify a variable to store the data into with the
755 // cl::location(x) modifier.
757 template<class DataType, bool ExternalStorage, bool isClass>
759 DataType *Location; // Where to store the object...
762 assert(Location != 0 && "cl::location(...) not specified for a command "
763 "line option with external storage, "
764 "or cl::init specified before cl::location()!!");
767 opt_storage() : Location(0) {}
769 bool setLocation(Option &O, DataType &L) {
771 return O.error("cl::location(x) specified more than once!");
777 void setValue(const T &V) {
782 DataType &getValue() { check(); return *Location; }
783 const DataType &getValue() const { check(); return *Location; }
785 operator DataType() const { return this->getValue(); }
789 // Define how to hold a class type object, such as a string. Since we can
790 // inherit from a class, we do so. This makes us exactly compatible with the
791 // object in all cases that it is used.
793 template<class DataType>
794 class opt_storage<DataType,false,true> : public DataType {
797 void setValue(const T &V) { DataType::operator=(V); }
799 DataType &getValue() { return *this; }
800 const DataType &getValue() const { return *this; }
803 // Define a partial specialization to handle things we cannot inherit from. In
804 // this case, we store an instance through containment, and overload operators
805 // to get at the value.
807 template<class DataType>
808 class opt_storage<DataType, false, false> {
812 // Make sure we initialize the value with the default constructor for the
814 opt_storage() : Value(DataType()) {}
817 void setValue(const T &V) { Value = V; }
818 DataType &getValue() { return Value; }
819 DataType getValue() const { return Value; }
821 operator DataType() const { return getValue(); }
823 // If the datatype is a pointer, support -> on it.
824 DataType operator->() const { return Value; }
828 //===----------------------------------------------------------------------===//
829 // opt - A scalar command line option.
831 template <class DataType, bool ExternalStorage = false,
832 class ParserClass = parser<DataType> >
833 class opt : public Option,
834 public opt_storage<DataType, ExternalStorage,
835 is_class<DataType>::value> {
838 virtual bool handleOccurrence(unsigned pos, StringRef ArgName,
840 typename ParserClass::parser_data_type Val =
841 typename ParserClass::parser_data_type();
842 if (Parser.parse(*this, ArgName, Arg, Val))
843 return true; // Parse error!
845 this->setPosition(pos);
849 virtual enum ValueExpected getValueExpectedFlagDefault() const {
850 return Parser.getValueExpectedFlagDefault();
852 virtual void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) {
853 return Parser.getExtraOptionNames(OptionNames);
856 // Forward printing stuff to the parser...
857 virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);}
858 virtual void printOptionInfo(size_t GlobalWidth) const {
859 Parser.printOptionInfo(*this, GlobalWidth);
864 Parser.initialize(*this);
867 // setInitialValue - Used by the cl::init modifier...
868 void setInitialValue(const DataType &V) { this->setValue(V); }
870 ParserClass &getParser() { return Parser; }
873 DataType &operator=(const T &Val) {
875 return this->getValue();
880 explicit opt(const M0t &M0) : Option(Optional | NotHidden) {
886 template<class M0t, class M1t>
887 opt(const M0t &M0, const M1t &M1) : Option(Optional | NotHidden) {
888 apply(M0, this); apply(M1, this);
893 template<class M0t, class M1t, class M2t>
894 opt(const M0t &M0, const M1t &M1,
895 const M2t &M2) : Option(Optional | NotHidden) {
896 apply(M0, this); apply(M1, this); apply(M2, this);
900 template<class M0t, class M1t, class M2t, class M3t>
901 opt(const M0t &M0, const M1t &M1, const M2t &M2,
902 const M3t &M3) : Option(Optional | NotHidden) {
903 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
907 template<class M0t, class M1t, class M2t, class M3t, class M4t>
908 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
909 const M4t &M4) : Option(Optional | NotHidden) {
910 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
915 template<class M0t, class M1t, class M2t, class M3t,
916 class M4t, class M5t>
917 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
918 const M4t &M4, const M5t &M5) : Option(Optional | NotHidden) {
919 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
920 apply(M4, this); apply(M5, this);
924 template<class M0t, class M1t, class M2t, class M3t,
925 class M4t, class M5t, class M6t>
926 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
927 const M4t &M4, const M5t &M5,
928 const M6t &M6) : Option(Optional | NotHidden) {
929 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
930 apply(M4, this); apply(M5, this); apply(M6, this);
934 template<class M0t, class M1t, class M2t, class M3t,
935 class M4t, class M5t, class M6t, class M7t>
936 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
937 const M4t &M4, const M5t &M5, const M6t &M6,
938 const M7t &M7) : Option(Optional | NotHidden) {
939 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
940 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
945 EXTERN_TEMPLATE_INSTANTIATION(class opt<unsigned>);
946 EXTERN_TEMPLATE_INSTANTIATION(class opt<int>);
947 EXTERN_TEMPLATE_INSTANTIATION(class opt<std::string>);
948 EXTERN_TEMPLATE_INSTANTIATION(class opt<char>);
949 EXTERN_TEMPLATE_INSTANTIATION(class opt<bool>);
951 //===----------------------------------------------------------------------===//
952 // list_storage class
954 // Default storage class definition: external storage. This implementation
955 // assumes the user will specify a variable to store the data into with the
956 // cl::location(x) modifier.
958 template<class DataType, class StorageClass>
960 StorageClass *Location; // Where to store the object...
963 list_storage() : Location(0) {}
965 bool setLocation(Option &O, StorageClass &L) {
967 return O.error("cl::location(x) specified more than once!");
973 void addValue(const T &V) {
974 assert(Location != 0 && "cl::location(...) not specified for a command "
975 "line option with external storage!");
976 Location->push_back(V);
981 // Define how to hold a class type object, such as a string. Since we can
982 // inherit from a class, we do so. This makes us exactly compatible with the
983 // object in all cases that it is used.
985 template<class DataType>
986 class list_storage<DataType, bool> : public std::vector<DataType> {
989 void addValue(const T &V) { push_back(V); }
993 //===----------------------------------------------------------------------===//
994 // list - A list of command line options.
996 template <class DataType, class Storage = bool,
997 class ParserClass = parser<DataType> >
998 class list : public Option, public list_storage<DataType, Storage> {
999 std::vector<unsigned> Positions;
1002 virtual enum ValueExpected getValueExpectedFlagDefault() const {
1003 return Parser.getValueExpectedFlagDefault();
1005 virtual void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) {
1006 return Parser.getExtraOptionNames(OptionNames);
1009 virtual bool handleOccurrence(unsigned pos, StringRef ArgName, StringRef Arg){
1010 typename ParserClass::parser_data_type Val =
1011 typename ParserClass::parser_data_type();
1012 if (Parser.parse(*this, ArgName, Arg, Val))
1013 return true; // Parse Error!
1016 Positions.push_back(pos);
1020 // Forward printing stuff to the parser...
1021 virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);}
1022 virtual void printOptionInfo(size_t GlobalWidth) const {
1023 Parser.printOptionInfo(*this, GlobalWidth);
1028 Parser.initialize(*this);
1031 ParserClass &getParser() { return Parser; }
1033 unsigned getPosition(unsigned optnum) const {
1034 assert(optnum < this->size() && "Invalid option index");
1035 return Positions[optnum];
1038 void setNumAdditionalVals(unsigned n) {
1039 Option::setNumAdditionalVals(n);
1044 explicit list(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
1049 template<class M0t, class M1t>
1050 list(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
1051 apply(M0, this); apply(M1, this);
1055 template<class M0t, class M1t, class M2t>
1056 list(const M0t &M0, const M1t &M1, const M2t &M2)
1057 : Option(ZeroOrMore | NotHidden) {
1058 apply(M0, this); apply(M1, this); apply(M2, this);
1062 template<class M0t, class M1t, class M2t, class M3t>
1063 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1064 : Option(ZeroOrMore | NotHidden) {
1065 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1069 template<class M0t, class M1t, class M2t, class M3t, class M4t>
1070 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1071 const M4t &M4) : Option(ZeroOrMore | NotHidden) {
1072 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1077 template<class M0t, class M1t, class M2t, class M3t,
1078 class M4t, class M5t>
1079 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1080 const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
1081 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1082 apply(M4, this); apply(M5, this);
1086 template<class M0t, class M1t, class M2t, class M3t,
1087 class M4t, class M5t, class M6t>
1088 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1089 const M4t &M4, const M5t &M5, const M6t &M6)
1090 : Option(ZeroOrMore | NotHidden) {
1091 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1092 apply(M4, this); apply(M5, this); apply(M6, this);
1096 template<class M0t, class M1t, class M2t, class M3t,
1097 class M4t, class M5t, class M6t, class M7t>
1098 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1099 const M4t &M4, const M5t &M5, const M6t &M6,
1100 const M7t &M7) : Option(ZeroOrMore | NotHidden) {
1101 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1102 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1107 // multi_val - Modifier to set the number of additional values.
1109 unsigned AdditionalVals;
1110 explicit multi_val(unsigned N) : AdditionalVals(N) {}
1112 template <typename D, typename S, typename P>
1113 void apply(list<D, S, P> &L) const { L.setNumAdditionalVals(AdditionalVals); }
1117 //===----------------------------------------------------------------------===//
1118 // bits_storage class
1120 // Default storage class definition: external storage. This implementation
1121 // assumes the user will specify a variable to store the data into with the
1122 // cl::location(x) modifier.
1124 template<class DataType, class StorageClass>
1125 class bits_storage {
1126 unsigned *Location; // Where to store the bits...
1129 static unsigned Bit(const T &V) {
1130 unsigned BitPos = reinterpret_cast<unsigned>(V);
1131 assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
1132 "enum exceeds width of bit vector!");
1137 bits_storage() : Location(0) {}
1139 bool setLocation(Option &O, unsigned &L) {
1141 return O.error("cl::location(x) specified more than once!");
1147 void addValue(const T &V) {
1148 assert(Location != 0 && "cl::location(...) not specified for a command "
1149 "line option with external storage!");
1150 *Location |= Bit(V);
1153 unsigned getBits() { return *Location; }
1156 bool isSet(const T &V) {
1157 return (*Location & Bit(V)) != 0;
1162 // Define how to hold bits. Since we can inherit from a class, we do so.
1163 // This makes us exactly compatible with the bits in all cases that it is used.
1165 template<class DataType>
1166 class bits_storage<DataType, bool> {
1167 unsigned Bits; // Where to store the bits...
1170 static unsigned Bit(const T &V) {
1171 unsigned BitPos = reinterpret_cast<unsigned>(V);
1172 assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
1173 "enum exceeds width of bit vector!");
1179 void addValue(const T &V) {
1183 unsigned getBits() { return Bits; }
1186 bool isSet(const T &V) {
1187 return (Bits & Bit(V)) != 0;
1192 //===----------------------------------------------------------------------===//
1193 // bits - A bit vector of command options.
1195 template <class DataType, class Storage = bool,
1196 class ParserClass = parser<DataType> >
1197 class bits : public Option, public bits_storage<DataType, Storage> {
1198 std::vector<unsigned> Positions;
1201 virtual enum ValueExpected getValueExpectedFlagDefault() const {
1202 return Parser.getValueExpectedFlagDefault();
1204 virtual void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) {
1205 return Parser.getExtraOptionNames(OptionNames);
1208 virtual bool handleOccurrence(unsigned pos, StringRef ArgName, StringRef Arg){
1209 typename ParserClass::parser_data_type Val =
1210 typename ParserClass::parser_data_type();
1211 if (Parser.parse(*this, ArgName, Arg, Val))
1212 return true; // Parse Error!
1215 Positions.push_back(pos);
1219 // Forward printing stuff to the parser...
1220 virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);}
1221 virtual void printOptionInfo(size_t GlobalWidth) const {
1222 Parser.printOptionInfo(*this, GlobalWidth);
1227 Parser.initialize(*this);
1230 ParserClass &getParser() { return Parser; }
1232 unsigned getPosition(unsigned optnum) const {
1233 assert(optnum < this->size() && "Invalid option index");
1234 return Positions[optnum];
1239 explicit bits(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
1244 template<class M0t, class M1t>
1245 bits(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
1246 apply(M0, this); apply(M1, this);
1250 template<class M0t, class M1t, class M2t>
1251 bits(const M0t &M0, const M1t &M1, const M2t &M2)
1252 : Option(ZeroOrMore | NotHidden) {
1253 apply(M0, this); apply(M1, this); apply(M2, this);
1257 template<class M0t, class M1t, class M2t, class M3t>
1258 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1259 : Option(ZeroOrMore | NotHidden) {
1260 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1264 template<class M0t, class M1t, class M2t, class M3t, class M4t>
1265 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1266 const M4t &M4) : Option(ZeroOrMore | NotHidden) {
1267 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1272 template<class M0t, class M1t, class M2t, class M3t,
1273 class M4t, class M5t>
1274 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1275 const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
1276 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1277 apply(M4, this); apply(M5, this);
1281 template<class M0t, class M1t, class M2t, class M3t,
1282 class M4t, class M5t, class M6t>
1283 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1284 const M4t &M4, const M5t &M5, const M6t &M6)
1285 : Option(ZeroOrMore | NotHidden) {
1286 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1287 apply(M4, this); apply(M5, this); apply(M6, this);
1291 template<class M0t, class M1t, class M2t, class M3t,
1292 class M4t, class M5t, class M6t, class M7t>
1293 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1294 const M4t &M4, const M5t &M5, const M6t &M6,
1295 const M7t &M7) : Option(ZeroOrMore | NotHidden) {
1296 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1297 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1302 //===----------------------------------------------------------------------===//
1303 // Aliased command line option (alias this name to a preexisting name)
1306 class alias : public Option {
1308 virtual bool handleOccurrence(unsigned pos, StringRef /*ArgName*/,
1310 return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
1312 // Handle printing stuff...
1313 virtual size_t getOptionWidth() const;
1314 virtual void printOptionInfo(size_t GlobalWidth) const;
1318 error("cl::alias must have argument name specified!");
1320 error("cl::alias must have an cl::aliasopt(option) specified!");
1324 void setAliasFor(Option &O) {
1326 error("cl::alias must only have one cl::aliasopt(...) specified!");
1332 explicit alias(const M0t &M0) : Option(Optional | Hidden), AliasFor(0) {
1337 template<class M0t, class M1t>
1338 alias(const M0t &M0, const M1t &M1) : Option(Optional | Hidden), AliasFor(0) {
1339 apply(M0, this); apply(M1, this);
1343 template<class M0t, class M1t, class M2t>
1344 alias(const M0t &M0, const M1t &M1, const M2t &M2)
1345 : Option(Optional | Hidden), AliasFor(0) {
1346 apply(M0, this); apply(M1, this); apply(M2, this);
1350 template<class M0t, class M1t, class M2t, class M3t>
1351 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1352 : Option(Optional | Hidden), AliasFor(0) {
1353 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1358 // aliasfor - Modifier to set the option an alias aliases.
1361 explicit aliasopt(Option &O) : Opt(O) {}
1362 void apply(alias &A) const { A.setAliasFor(Opt); }
1365 // extrahelp - provide additional help at the end of the normal help
1366 // output. All occurrences of cl::extrahelp will be accumulated and
1367 // printed to stderr at the end of the regular help, just before
1370 const char * morehelp;
1371 explicit extrahelp(const char* help);
1374 void PrintVersionMessage();
1375 // This function just prints the help message, exactly the same way as if the
1376 // --help option had been given on the command line.
1377 // NOTE: THIS FUNCTION TERMINATES THE PROGRAM!
1378 void PrintHelpMessage();
1380 } // End namespace cl
1382 } // End namespace llvm