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/DataTypes.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/ADT/SmallVector.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, char **argv,
45 const char *Overview = 0,
46 bool ReadResponseFiles = false);
48 //===----------------------------------------------------------------------===//
49 // ParseEnvironmentOptions - Environment variable option processing alternate
52 void ParseEnvironmentOptions(const char *progName, const char *envvar,
53 const char *Overview = 0,
54 bool ReadResponseFiles = false);
56 ///===---------------------------------------------------------------------===//
57 /// SetVersionPrinter - Override the default (LLVM specific) version printer
58 /// used to print out the version when --version is given
59 /// on the command line. This allows other systems using the
60 /// CommandLine utilities to print their own version string.
61 void SetVersionPrinter(void (*func)());
64 // MarkOptionsChanged - Internal helper function.
65 void MarkOptionsChanged();
67 //===----------------------------------------------------------------------===//
68 // Flags permitted to be passed to command line arguments
71 enum NumOccurrences { // Flags for the number of occurrences allowed
72 Optional = 0x01, // Zero or One occurrence
73 ZeroOrMore = 0x02, // Zero or more occurrences allowed
74 Required = 0x03, // One occurrence required
75 OneOrMore = 0x04, // One or more occurrences required
77 // ConsumeAfter - Indicates that this option is fed anything that follows the
78 // last positional argument required by the application (it is an error if
79 // there are zero positional arguments, and a ConsumeAfter option is used).
80 // Thus, for example, all arguments to LLI are processed until a filename is
81 // found. Once a filename is found, all of the succeeding arguments are
82 // passed, unprocessed, to the ConsumeAfter option.
86 OccurrencesMask = 0x07
89 enum ValueExpected { // Is a value required for the option?
90 ValueOptional = 0x08, // The value can appear... or not
91 ValueRequired = 0x10, // The value is required to appear!
92 ValueDisallowed = 0x18, // A value may not be specified (for flags)
96 enum OptionHidden { // Control whether -help shows this option
97 NotHidden = 0x20, // Option included in --help & --help-hidden
98 Hidden = 0x40, // -help doesn't, but --help-hidden does
99 ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg
103 // Formatting flags - This controls special features that the option might have
104 // that cause it to be parsed differently...
106 // Prefix - This option allows arguments that are otherwise unrecognized to be
107 // matched by options that are a prefix of the actual value. This is useful for
108 // cases like a linker, where options are typically of the form '-lfoo' or
109 // '-L../../include' where -l or -L are the actual flags. When prefix is
110 // enabled, and used, the value for the flag comes from the suffix of the
113 // Grouping - With this option enabled, multiple letter options are allowed to
114 // bunch together with only a single hyphen for the whole group. This allows
115 // emulation of the behavior that ls uses for example: ls -la === ls -l -a
118 enum FormattingFlags {
119 NormalFormatting = 0x000, // Nothing special
120 Positional = 0x080, // Is a positional argument, no '-' required
121 Prefix = 0x100, // Can this option directly prefix its value?
122 Grouping = 0x180, // Can this option group with other options?
123 FormattingMask = 0x180 // Union of the above flags.
126 enum MiscFlags { // Miscellaneous flags to adjust argument
127 CommaSeparated = 0x200, // Should this cl::list split between commas?
128 PositionalEatsArgs = 0x400, // Should this positional cl::list eat -args?
129 Sink = 0x800, // Should this cl::list eat all unknown options?
130 AllowInverse = 0x1000, // Can this option take a -Xno- form?
131 MiscMask = 0x1E00 // Union of the above flags.
136 //===----------------------------------------------------------------------===//
143 // handleOccurrences - Overriden by subclasses to handle the value passed into
144 // an argument. Should return true if there was an error processing the
145 // argument and the program should exit.
147 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
148 const std::string &Arg) = 0;
150 virtual enum ValueExpected getValueExpectedFlagDefault() const {
151 return ValueOptional;
154 // Out of line virtual function to provide home for the class.
155 virtual void anchor();
157 int NumOccurrences; // The number of times specified
158 int Flags; // Flags for the argument
159 unsigned Position; // Position of last occurrence of the option
160 unsigned AdditionalVals;// Greater than 0 for multi-valued option.
161 Option *NextRegistered; // Singly linked list of registered options.
163 const char *ArgStr; // The argument string itself (ex: "help", "o")
164 const char *HelpStr; // The descriptive text message for --help
165 const char *ValueStr; // String describing what the value of this option is
167 inline enum NumOccurrences getNumOccurrencesFlag() const {
168 return static_cast<enum NumOccurrences>(Flags & OccurrencesMask);
170 inline enum ValueExpected getValueExpectedFlag() const {
171 int VE = Flags & ValueMask;
172 return VE ? static_cast<enum ValueExpected>(VE)
173 : getValueExpectedFlagDefault();
175 inline enum OptionHidden getOptionHiddenFlag() const {
176 return static_cast<enum OptionHidden>(Flags & HiddenMask);
178 inline enum FormattingFlags getFormattingFlag() const {
179 return static_cast<enum FormattingFlags>(Flags & FormattingMask);
181 inline unsigned getMiscFlags() const {
182 return Flags & MiscMask;
184 inline unsigned getPosition() const { return Position; }
185 inline unsigned getNumAdditionalVals() const { return AdditionalVals; }
187 // hasArgStr - Return true if the argstr != ""
188 bool hasArgStr() const { return ArgStr[0] != 0; }
190 //-------------------------------------------------------------------------===
191 // Accessor functions set by OptionModifiers
193 void setArgStr(const char *S) { ArgStr = S; }
194 void setDescription(const char *S) { HelpStr = S; }
195 void setValueStr(const char *S) { ValueStr = S; }
197 void setFlag(unsigned Flag, unsigned FlagMask) {
202 void setNumOccurrencesFlag(enum NumOccurrences Val) {
203 setFlag(Val, OccurrencesMask);
205 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
206 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
207 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
208 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
209 void setPosition(unsigned pos) { Position = pos; }
211 explicit Option(unsigned DefaultFlags)
212 : NumOccurrences(0), Flags(DefaultFlags | NormalFormatting), Position(0),
213 AdditionalVals(0), NextRegistered(0),
214 ArgStr(""), HelpStr(""), ValueStr("") {
215 assert(getNumOccurrencesFlag() != 0 &&
216 getOptionHiddenFlag() != 0 && "Not all default flags specified!");
219 inline void setNumAdditionalVals(unsigned n)
220 { AdditionalVals = n; }
222 // addArgument - Register this argument with the commandline system.
226 Option *getNextRegisteredOption() const { return NextRegistered; }
228 // Return the width of the option tag for printing...
229 virtual size_t getOptionWidth() const = 0;
231 // printOptionInfo - Print out information about this option. The
232 // to-be-maintained width is specified.
234 virtual void printOptionInfo(size_t GlobalWidth) const = 0;
236 virtual void getExtraOptionNames(std::vector<const char*> &) {}
238 // addOccurrence - Wrapper around handleOccurrence that enforces Flags
240 bool addOccurrence(unsigned pos, const char *ArgName,
241 const std::string &Value, bool MultiArg = false);
243 // Prints option name followed by message. Always returns true.
244 bool error(std::string Message, const char *ArgName = 0);
247 inline int getNumOccurrences() const { return NumOccurrences; }
252 //===----------------------------------------------------------------------===//
253 // Command line option modifiers that can be used to modify the behavior of
254 // command line option parsers...
257 // desc - Modifier to set the description shown in the --help output...
260 desc(const char *Str) : Desc(Str) {}
261 void apply(Option &O) const { O.setDescription(Desc); }
264 // value_desc - Modifier to set the value description shown in the --help
268 value_desc(const char *Str) : Desc(Str) {}
269 void apply(Option &O) const { O.setValueStr(Desc); }
272 // init - Specify a default (initial) value for the command line argument, if
273 // the default constructor for the argument type does not give you what you
274 // want. This is only valid on "opt" arguments, not on "list" arguments.
279 initializer(const Ty &Val) : Init(Val) {}
282 void apply(Opt &O) const { O.setInitialValue(Init); }
286 initializer<Ty> init(const Ty &Val) {
287 return initializer<Ty>(Val);
291 // location - Allow the user to specify which external variable they want to
292 // store the results of the command line argument processing into, if they don't
293 // want to store it in the option itself.
296 struct LocationClass {
298 LocationClass(Ty &L) : Loc(L) {}
301 void apply(Opt &O) const { O.setLocation(O, Loc); }
305 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
308 //===----------------------------------------------------------------------===//
309 // Enum valued command line option
311 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, int(ENUMVAL), DESC
312 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, int(ENUMVAL), DESC
313 #define clEnumValEnd (reinterpret_cast<void*>(0))
315 // values - For custom data types, allow specifying a group of values together
316 // as the values that go into the mapping that the option handler uses. Note
317 // that the values list must always have a 0 at the end of the list to indicate
318 // that the list has ended.
320 template<class DataType>
322 // Use a vector instead of a map, because the lists should be short,
323 // the overhead is less, and most importantly, it keeps them in the order
324 // inserted so we can print our option out nicely.
325 SmallVector<std::pair<const char *, std::pair<int, const char *> >,4> Values;
326 void processValues(va_list Vals);
328 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
330 // Insert the first value, which is required.
331 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
333 // Process the varargs portion of the values...
334 while (const char *enumName = va_arg(ValueArgs, const char *)) {
335 DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int));
336 const char *EnumDesc = va_arg(ValueArgs, const char *);
337 Values.push_back(std::make_pair(enumName, // Add value to value map
338 std::make_pair(EnumVal, EnumDesc)));
343 void apply(Opt &O) const {
344 for (unsigned i = 0, e = static_cast<unsigned>(Values.size());
346 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
347 Values[i].second.second);
351 template<class DataType>
352 ValuesClass<DataType> END_WITH_NULL values(const char *Arg, DataType Val,
353 const char *Desc, ...) {
355 va_start(ValueArgs, Desc);
356 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
362 //===----------------------------------------------------------------------===//
363 // parser class - Parameterizable parser for different data types. By default,
364 // known data types (string, int, bool) have specialized parsers, that do what
365 // you would expect. The default parser, used for data types that are not
366 // built-in, uses a mapping table to map specific options to values, which is
367 // used, among other things, to handle enum types.
369 //--------------------------------------------------
370 // generic_parser_base - This class holds all the non-generic code that we do
371 // not need replicated for every instance of the generic parser. This also
372 // allows us to put stuff into CommandLine.cpp
374 struct generic_parser_base {
375 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
377 // getNumOptions - Virtual function implemented by generic subclass to
378 // indicate how many entries are in Values.
380 virtual unsigned getNumOptions() const = 0;
382 // getOption - Return option name N.
383 virtual const char *getOption(unsigned N) const = 0;
385 // getDescription - Return description N
386 virtual const char *getDescription(unsigned N) const = 0;
388 // Return the width of the option tag for printing...
389 virtual size_t getOptionWidth(const Option &O) const;
391 // printOptionInfo - Print out information about this option. The
392 // to-be-maintained width is specified.
394 virtual void printOptionInfo(const Option &O, size_t GlobalWidth) const;
396 void initialize(Option &O) {
397 // All of the modifiers for the option have been processed by now, so the
398 // argstr field should be stable, copy it down now.
400 hasArgStr = O.hasArgStr();
403 void getExtraOptionNames(std::vector<const char*> &OptionNames) {
404 // If there has been no argstr specified, that means that we need to add an
405 // argument for every possible option. This ensures that our options are
408 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
409 OptionNames.push_back(getOption(i));
413 enum ValueExpected getValueExpectedFlagDefault() const {
414 // If there is an ArgStr specified, then we are of the form:
416 // -opt=O2 or -opt O2 or -optO2
418 // In which case, the value is required. Otherwise if an arg str has not
419 // been specified, we are of the form:
421 // -O2 or O2 or -la (where -l and -a are separate options)
423 // If this is the case, we cannot allow a value.
426 return ValueRequired;
428 return ValueDisallowed;
431 // findOption - Return the option number corresponding to the specified
432 // argument string. If the option is not found, getNumOptions() is returned.
434 unsigned findOption(const char *Name);
440 // Default parser implementation - This implementation depends on having a
441 // mapping of recognized options to values of some sort. In addition to this,
442 // each entry in the mapping also tracks a help message that is printed with the
443 // command line option for --help. Because this is a simple mapping parser, the
444 // data type can be any unsupported type.
446 template <class DataType>
447 class parser : public generic_parser_base {
449 SmallVector<std::pair<const char *,
450 std::pair<DataType, const char *> >, 8> Values;
452 typedef DataType parser_data_type;
454 // Implement virtual functions needed by generic_parser_base
455 unsigned getNumOptions() const { return unsigned(Values.size()); }
456 const char *getOption(unsigned N) const { return Values[N].first; }
457 const char *getDescription(unsigned N) const {
458 return Values[N].second.second;
461 // parse - Return true on error.
462 bool parse(Option &O, const char *ArgName, const std::string &Arg,
470 for (unsigned i = 0, e = static_cast<unsigned>(Values.size());
472 if (ArgVal == Values[i].first) {
473 V = Values[i].second.first;
477 return O.error(": Cannot find option named '" + ArgVal + "'!");
480 /// addLiteralOption - Add an entry to the mapping table.
483 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
484 assert(findOption(Name) == Values.size() && "Option already exists!");
485 Values.push_back(std::make_pair(Name,
486 std::make_pair(static_cast<DataType>(V),HelpStr)));
487 MarkOptionsChanged();
490 /// removeLiteralOption - Remove the specified option.
492 void removeLiteralOption(const char *Name) {
493 unsigned N = findOption(Name);
494 assert(N != Values.size() && "Option not found!");
495 Values.erase(Values.begin()+N);
499 //--------------------------------------------------
500 // basic_parser - Super class of parsers to provide boilerplate code
502 struct basic_parser_impl { // non-template implementation of basic_parser<t>
503 virtual ~basic_parser_impl() {}
505 enum ValueExpected getValueExpectedFlagDefault() const {
506 return ValueRequired;
509 void getExtraOptionNames(std::vector<const char*> &) {}
511 void initialize(Option &) {}
513 // Return the width of the option tag for printing...
514 size_t getOptionWidth(const Option &O) const;
516 // printOptionInfo - Print out information about this option. The
517 // to-be-maintained width is specified.
519 void printOptionInfo(const Option &O, size_t GlobalWidth) const;
521 // getValueName - Overload in subclass to provide a better default value.
522 virtual const char *getValueName() const { return "value"; }
524 // An out-of-line virtual method to provide a 'home' for this class.
525 virtual void anchor();
528 // basic_parser - The real basic parser is just a template wrapper that provides
529 // a typedef for the provided data type.
531 template<class DataType>
532 struct basic_parser : public basic_parser_impl {
533 typedef DataType parser_data_type;
536 //--------------------------------------------------
540 class parser<bool> : public basic_parser<bool> {
541 bool IsInvertible; // Should we synthesize a -xno- style option?
544 void getExtraOptionNames(std::vector<const char*> &OptionNames);
546 // parse - Return true on error.
547 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
550 void initialize(Opt &O) {
551 IsInvertible = (O.getMiscFlags() & llvm::cl::AllowInverse);
555 enum ValueExpected getValueExpectedFlagDefault() const {
556 return ValueOptional;
559 // getValueName - Do not print =<value> at all.
560 virtual const char *getValueName() const { return 0; }
562 // An out-of-line virtual method to provide a 'home' for this class.
563 virtual void anchor();
566 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<bool>);
568 //--------------------------------------------------
569 // parser<boolOrDefault>
570 enum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE };
572 class parser<boolOrDefault> : public basic_parser<boolOrDefault> {
574 // parse - Return true on error.
575 bool parse(Option &O, const char *ArgName, const std::string &Arg,
578 enum ValueExpected getValueExpectedFlagDefault() const {
579 return ValueOptional;
582 // getValueName - Do not print =<value> at all.
583 virtual const char *getValueName() const { return 0; }
585 // An out-of-line virtual method to provide a 'home' for this class.
586 virtual void anchor();
589 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<boolOrDefault>);
591 //--------------------------------------------------
595 class parser<int> : public basic_parser<int> {
597 // parse - Return true on error.
598 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
600 // getValueName - Overload in subclass to provide a better default value.
601 virtual const char *getValueName() const { return "int"; }
603 // An out-of-line virtual method to provide a 'home' for this class.
604 virtual void anchor();
607 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<int>);
610 //--------------------------------------------------
614 class parser<unsigned> : public basic_parser<unsigned> {
616 // parse - Return true on error.
617 bool parse(Option &O, const char *AN, const std::string &Arg, unsigned &Val);
619 // getValueName - Overload in subclass to provide a better default value.
620 virtual const char *getValueName() const { return "uint"; }
622 // An out-of-line virtual method to provide a 'home' for this class.
623 virtual void anchor();
626 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<unsigned>);
628 //--------------------------------------------------
632 class parser<double> : public basic_parser<double> {
634 // parse - Return true on error.
635 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
637 // getValueName - Overload in subclass to provide a better default value.
638 virtual const char *getValueName() const { return "number"; }
640 // An out-of-line virtual method to provide a 'home' for this class.
641 virtual void anchor();
644 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<double>);
646 //--------------------------------------------------
650 class parser<float> : public basic_parser<float> {
652 // parse - Return true on error.
653 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
655 // getValueName - Overload in subclass to provide a better default value.
656 virtual const char *getValueName() const { return "number"; }
658 // An out-of-line virtual method to provide a 'home' for this class.
659 virtual void anchor();
662 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<float>);
664 //--------------------------------------------------
665 // parser<std::string>
668 class parser<std::string> : public basic_parser<std::string> {
670 // parse - Return true on error.
671 bool parse(Option &, const char *, const std::string &Arg,
672 std::string &Value) {
677 // getValueName - Overload in subclass to provide a better default value.
678 virtual const char *getValueName() const { return "string"; }
680 // An out-of-line virtual method to provide a 'home' for this class.
681 virtual void anchor();
684 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<std::string>);
686 //===----------------------------------------------------------------------===//
687 // applicator class - This class is used because we must use partial
688 // specialization to handle literal string arguments specially (const char* does
689 // not correctly respond to the apply method). Because the syntax to use this
690 // is a pain, we have the 'apply' method below to handle the nastiness...
692 template<class Mod> struct applicator {
694 static void opt(const Mod &M, Opt &O) { M.apply(O); }
697 // Handle const char* as a special case...
698 template<unsigned n> struct applicator<char[n]> {
700 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
702 template<unsigned n> struct applicator<const char[n]> {
704 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
706 template<> struct applicator<const char*> {
708 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
711 template<> struct applicator<NumOccurrences> {
712 static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); }
714 template<> struct applicator<ValueExpected> {
715 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
717 template<> struct applicator<OptionHidden> {
718 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
720 template<> struct applicator<FormattingFlags> {
721 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
723 template<> struct applicator<MiscFlags> {
724 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
727 // apply method - Apply a modifier to an option in a type safe way.
728 template<class Mod, class Opt>
729 void apply(const Mod &M, Opt *O) {
730 applicator<Mod>::opt(M, *O);
734 //===----------------------------------------------------------------------===//
737 // Default storage class definition: external storage. This implementation
738 // assumes the user will specify a variable to store the data into with the
739 // cl::location(x) modifier.
741 template<class DataType, bool ExternalStorage, bool isClass>
743 DataType *Location; // Where to store the object...
746 assert(Location != 0 && "cl::location(...) not specified for a command "
747 "line option with external storage, "
748 "or cl::init specified before cl::location()!!");
751 opt_storage() : Location(0) {}
753 bool setLocation(Option &O, DataType &L) {
755 return O.error(": cl::location(x) specified more than once!");
761 void setValue(const T &V) {
766 DataType &getValue() { check(); return *Location; }
767 const DataType &getValue() const { check(); return *Location; }
771 // Define how to hold a class type object, such as a string. Since we can
772 // inherit from a class, we do so. This makes us exactly compatible with the
773 // object in all cases that it is used.
775 template<class DataType>
776 class opt_storage<DataType,false,true> : public DataType {
779 void setValue(const T &V) { DataType::operator=(V); }
781 DataType &getValue() { return *this; }
782 const DataType &getValue() const { return *this; }
785 // Define a partial specialization to handle things we cannot inherit from. In
786 // this case, we store an instance through containment, and overload operators
787 // to get at the value.
789 template<class DataType>
790 class opt_storage<DataType, false, false> {
794 // Make sure we initialize the value with the default constructor for the
796 opt_storage() : Value(DataType()) {}
799 void setValue(const T &V) { Value = V; }
800 DataType &getValue() { return Value; }
801 DataType getValue() const { return Value; }
803 // If the datatype is a pointer, support -> on it.
804 DataType operator->() const { return Value; }
808 //===----------------------------------------------------------------------===//
809 // opt - A scalar command line option.
811 template <class DataType, bool ExternalStorage = false,
812 class ParserClass = parser<DataType> >
813 class opt : public Option,
814 public opt_storage<DataType, ExternalStorage,
815 is_class<DataType>::value> {
818 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
819 const std::string &Arg) {
820 typename ParserClass::parser_data_type Val =
821 typename ParserClass::parser_data_type();
822 if (Parser.parse(*this, ArgName, Arg, Val))
823 return true; // Parse error!
825 this->setPosition(pos);
829 virtual enum ValueExpected getValueExpectedFlagDefault() const {
830 return Parser.getValueExpectedFlagDefault();
832 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
833 return Parser.getExtraOptionNames(OptionNames);
836 // Forward printing stuff to the parser...
837 virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);}
838 virtual void printOptionInfo(size_t GlobalWidth) const {
839 Parser.printOptionInfo(*this, GlobalWidth);
844 Parser.initialize(*this);
847 // setInitialValue - Used by the cl::init modifier...
848 void setInitialValue(const DataType &V) { this->setValue(V); }
850 ParserClass &getParser() { return Parser; }
852 operator DataType() const { return this->getValue(); }
855 DataType &operator=(const T &Val) {
857 return this->getValue();
862 explicit opt(const M0t &M0) : Option(Optional | NotHidden) {
868 template<class M0t, class M1t>
869 opt(const M0t &M0, const M1t &M1) : Option(Optional | NotHidden) {
870 apply(M0, this); apply(M1, this);
875 template<class M0t, class M1t, class M2t>
876 opt(const M0t &M0, const M1t &M1,
877 const M2t &M2) : Option(Optional | NotHidden) {
878 apply(M0, this); apply(M1, this); apply(M2, this);
882 template<class M0t, class M1t, class M2t, class M3t>
883 opt(const M0t &M0, const M1t &M1, const M2t &M2,
884 const M3t &M3) : Option(Optional | NotHidden) {
885 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
889 template<class M0t, class M1t, class M2t, class M3t, class M4t>
890 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
891 const M4t &M4) : Option(Optional | NotHidden) {
892 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
897 template<class M0t, class M1t, class M2t, class M3t,
898 class M4t, class M5t>
899 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
900 const M4t &M4, const M5t &M5) : Option(Optional | NotHidden) {
901 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
902 apply(M4, this); apply(M5, this);
906 template<class M0t, class M1t, class M2t, class M3t,
907 class M4t, class M5t, class M6t>
908 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
909 const M4t &M4, const M5t &M5,
910 const M6t &M6) : Option(Optional | NotHidden) {
911 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
912 apply(M4, this); apply(M5, this); apply(M6, this);
916 template<class M0t, class M1t, class M2t, class M3t,
917 class M4t, class M5t, class M6t, class M7t>
918 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
919 const M4t &M4, const M5t &M5, const M6t &M6,
920 const M7t &M7) : Option(Optional | NotHidden) {
921 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
922 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
927 EXTERN_TEMPLATE_INSTANTIATION(class opt<unsigned>);
928 EXTERN_TEMPLATE_INSTANTIATION(class opt<int>);
929 EXTERN_TEMPLATE_INSTANTIATION(class opt<std::string>);
930 EXTERN_TEMPLATE_INSTANTIATION(class opt<bool>);
932 //===----------------------------------------------------------------------===//
933 // list_storage class
935 // Default storage class definition: external storage. This implementation
936 // assumes the user will specify a variable to store the data into with the
937 // cl::location(x) modifier.
939 template<class DataType, class StorageClass>
941 StorageClass *Location; // Where to store the object...
944 list_storage() : Location(0) {}
946 bool setLocation(Option &O, StorageClass &L) {
948 return O.error(": cl::location(x) specified more than once!");
954 void addValue(const T &V) {
955 assert(Location != 0 && "cl::location(...) not specified for a command "
956 "line option with external storage!");
957 Location->push_back(V);
962 // Define how to hold a class type object, such as a string. Since we can
963 // inherit from a class, we do so. This makes us exactly compatible with the
964 // object in all cases that it is used.
966 template<class DataType>
967 class list_storage<DataType, bool> : public std::vector<DataType> {
970 void addValue(const T &V) { push_back(V); }
974 //===----------------------------------------------------------------------===//
975 // list - A list of command line options.
977 template <class DataType, class Storage = bool,
978 class ParserClass = parser<DataType> >
979 class list : public Option, public list_storage<DataType, Storage> {
980 std::vector<unsigned> Positions;
983 virtual enum ValueExpected getValueExpectedFlagDefault() const {
984 return Parser.getValueExpectedFlagDefault();
986 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
987 return Parser.getExtraOptionNames(OptionNames);
990 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
991 const std::string &Arg) {
992 typename ParserClass::parser_data_type Val =
993 typename ParserClass::parser_data_type();
994 if (Parser.parse(*this, ArgName, Arg, Val))
995 return true; // Parse Error!
998 Positions.push_back(pos);
1002 // Forward printing stuff to the parser...
1003 virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);}
1004 virtual void printOptionInfo(size_t GlobalWidth) const {
1005 Parser.printOptionInfo(*this, GlobalWidth);
1010 Parser.initialize(*this);
1013 ParserClass &getParser() { return Parser; }
1015 unsigned getPosition(unsigned optnum) const {
1016 assert(optnum < this->size() && "Invalid option index");
1017 return Positions[optnum];
1020 void setNumAdditionalVals(unsigned n) {
1021 Option::setNumAdditionalVals(n);
1026 explicit list(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
1031 template<class M0t, class M1t>
1032 list(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
1033 apply(M0, this); apply(M1, this);
1037 template<class M0t, class M1t, class M2t>
1038 list(const M0t &M0, const M1t &M1, const M2t &M2)
1039 : Option(ZeroOrMore | NotHidden) {
1040 apply(M0, this); apply(M1, this); apply(M2, this);
1044 template<class M0t, class M1t, class M2t, class M3t>
1045 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1046 : Option(ZeroOrMore | NotHidden) {
1047 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1051 template<class M0t, class M1t, class M2t, class M3t, class M4t>
1052 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1053 const M4t &M4) : Option(ZeroOrMore | NotHidden) {
1054 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1059 template<class M0t, class M1t, class M2t, class M3t,
1060 class M4t, class M5t>
1061 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1062 const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
1063 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1064 apply(M4, this); apply(M5, this);
1068 template<class M0t, class M1t, class M2t, class M3t,
1069 class M4t, class M5t, class M6t>
1070 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1071 const M4t &M4, const M5t &M5, const M6t &M6)
1072 : Option(ZeroOrMore | NotHidden) {
1073 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1074 apply(M4, this); apply(M5, this); apply(M6, this);
1078 template<class M0t, class M1t, class M2t, class M3t,
1079 class M4t, class M5t, class M6t, class M7t>
1080 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1081 const M4t &M4, const M5t &M5, const M6t &M6,
1082 const M7t &M7) : Option(ZeroOrMore | NotHidden) {
1083 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1084 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1089 // multi_arg - Modifier to set the number of additional values.
1091 unsigned AdditionalVals;
1092 explicit multi_val(unsigned N) : AdditionalVals(N) {}
1094 template <typename D, typename S, typename P>
1095 void apply(list<D, S, P> &L) const { L.setNumAdditionalVals(AdditionalVals); }
1099 //===----------------------------------------------------------------------===//
1100 // bits_storage class
1102 // Default storage class definition: external storage. This implementation
1103 // assumes the user will specify a variable to store the data into with the
1104 // cl::location(x) modifier.
1106 template<class DataType, class StorageClass>
1107 class bits_storage {
1108 unsigned *Location; // Where to store the bits...
1111 static unsigned Bit(const T &V) {
1112 unsigned BitPos = reinterpret_cast<unsigned>(V);
1113 assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
1114 "enum exceeds width of bit vector!");
1119 bits_storage() : Location(0) {}
1121 bool setLocation(Option &O, unsigned &L) {
1123 return O.error(": cl::location(x) specified more than once!");
1129 void addValue(const T &V) {
1130 assert(Location != 0 && "cl::location(...) not specified for a command "
1131 "line option with external storage!");
1132 *Location |= Bit(V);
1135 unsigned getBits() { return *Location; }
1138 bool isSet(const T &V) {
1139 return (*Location & Bit(V)) != 0;
1144 // Define how to hold bits. Since we can inherit from a class, we do so.
1145 // This makes us exactly compatible with the bits in all cases that it is used.
1147 template<class DataType>
1148 class bits_storage<DataType, bool> {
1149 unsigned Bits; // Where to store the bits...
1152 static unsigned Bit(const T &V) {
1153 unsigned BitPos = reinterpret_cast<unsigned>(V);
1154 assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
1155 "enum exceeds width of bit vector!");
1161 void addValue(const T &V) {
1165 unsigned getBits() { return Bits; }
1168 bool isSet(const T &V) {
1169 return (Bits & Bit(V)) != 0;
1174 //===----------------------------------------------------------------------===//
1175 // bits - A bit vector of command options.
1177 template <class DataType, class Storage = bool,
1178 class ParserClass = parser<DataType> >
1179 class bits : public Option, public bits_storage<DataType, Storage> {
1180 std::vector<unsigned> Positions;
1183 virtual enum ValueExpected getValueExpectedFlagDefault() const {
1184 return Parser.getValueExpectedFlagDefault();
1186 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
1187 return Parser.getExtraOptionNames(OptionNames);
1190 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
1191 const std::string &Arg) {
1192 typename ParserClass::parser_data_type Val =
1193 typename ParserClass::parser_data_type();
1194 if (Parser.parse(*this, ArgName, Arg, Val))
1195 return true; // Parse Error!
1198 Positions.push_back(pos);
1202 // Forward printing stuff to the parser...
1203 virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);}
1204 virtual void printOptionInfo(size_t GlobalWidth) const {
1205 Parser.printOptionInfo(*this, GlobalWidth);
1210 Parser.initialize(*this);
1213 ParserClass &getParser() { return Parser; }
1215 unsigned getPosition(unsigned optnum) const {
1216 assert(optnum < this->size() && "Invalid option index");
1217 return Positions[optnum];
1222 explicit bits(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
1227 template<class M0t, class M1t>
1228 bits(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
1229 apply(M0, this); apply(M1, this);
1233 template<class M0t, class M1t, class M2t>
1234 bits(const M0t &M0, const M1t &M1, const M2t &M2)
1235 : Option(ZeroOrMore | NotHidden) {
1236 apply(M0, this); apply(M1, this); apply(M2, this);
1240 template<class M0t, class M1t, class M2t, class M3t>
1241 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1242 : Option(ZeroOrMore | NotHidden) {
1243 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1247 template<class M0t, class M1t, class M2t, class M3t, class M4t>
1248 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1249 const M4t &M4) : Option(ZeroOrMore | NotHidden) {
1250 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1255 template<class M0t, class M1t, class M2t, class M3t,
1256 class M4t, class M5t>
1257 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1258 const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
1259 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1260 apply(M4, this); apply(M5, this);
1264 template<class M0t, class M1t, class M2t, class M3t,
1265 class M4t, class M5t, class M6t>
1266 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1267 const M4t &M4, const M5t &M5, const M6t &M6)
1268 : Option(ZeroOrMore | NotHidden) {
1269 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1270 apply(M4, this); apply(M5, this); apply(M6, this);
1274 template<class M0t, class M1t, class M2t, class M3t,
1275 class M4t, class M5t, class M6t, class M7t>
1276 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1277 const M4t &M4, const M5t &M5, const M6t &M6,
1278 const M7t &M7) : Option(ZeroOrMore | NotHidden) {
1279 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1280 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1285 //===----------------------------------------------------------------------===//
1286 // Aliased command line option (alias this name to a preexisting name)
1289 class alias : public Option {
1291 virtual bool handleOccurrence(unsigned pos, const char * /*ArgName*/,
1292 const std::string &Arg) {
1293 return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
1295 // Handle printing stuff...
1296 virtual size_t getOptionWidth() const;
1297 virtual void printOptionInfo(size_t GlobalWidth) const;
1301 error(": cl::alias must have argument name specified!");
1303 error(": cl::alias must have an cl::aliasopt(option) specified!");
1307 void setAliasFor(Option &O) {
1309 error(": cl::alias must only have one cl::aliasopt(...) specified!");
1315 explicit alias(const M0t &M0) : Option(Optional | Hidden), AliasFor(0) {
1320 template<class M0t, class M1t>
1321 alias(const M0t &M0, const M1t &M1) : Option(Optional | Hidden), AliasFor(0) {
1322 apply(M0, this); apply(M1, this);
1326 template<class M0t, class M1t, class M2t>
1327 alias(const M0t &M0, const M1t &M1, const M2t &M2)
1328 : Option(Optional | Hidden), AliasFor(0) {
1329 apply(M0, this); apply(M1, this); apply(M2, this);
1333 template<class M0t, class M1t, class M2t, class M3t>
1334 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1335 : Option(Optional | Hidden), AliasFor(0) {
1336 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1341 // aliasfor - Modifier to set the option an alias aliases.
1344 explicit aliasopt(Option &O) : Opt(O) {}
1345 void apply(alias &A) const { A.setAliasFor(Opt); }
1348 // extrahelp - provide additional help at the end of the normal help
1349 // output. All occurrences of cl::extrahelp will be accumulated and
1350 // printed to std::cerr at the end of the regular help, just before
1353 const char * morehelp;
1354 explicit extrahelp(const char* help);
1357 void PrintVersionMessage();
1358 // This function just prints the help message, exactly the same way as if the
1359 // --help option had been given on the command line.
1360 // NOTE: THIS FUNCTION TERMINATES THE PROGRAM!
1361 void PrintHelpMessage();
1363 } // End namespace cl
1365 } // End namespace llvm