1 //===- llvm/Support/CommandLine.h - Command line handler --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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"
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);
46 //===----------------------------------------------------------------------===//
47 // ParseEnvironmentOptions - Environment variable option processing alternate
50 void ParseEnvironmentOptions(const char *progName, const char *envvar,
51 const char *Overview = 0);
53 ///===---------------------------------------------------------------------===//
54 /// SetVersionPrinter - Override the default (LLVM specific) version printer
55 /// used to print out the version when --version is given
56 /// on the command line. This gives other systems using the
57 /// CommandLine utilities to print their own version string.
58 void SetVersionPrinter(void (*func)());
60 //===----------------------------------------------------------------------===//
61 // Flags permitted to be passed to command line arguments
64 enum NumOccurrences { // Flags for the number of occurrences allowed
65 Optional = 0x01, // Zero or One occurrence
66 ZeroOrMore = 0x02, // Zero or more occurrences allowed
67 Required = 0x03, // One occurrence required
68 OneOrMore = 0x04, // One or more occurrences required
70 // ConsumeAfter - Indicates that this option is fed anything that follows the
71 // last positional argument required by the application (it is an error if
72 // there are zero positional arguments, and a ConsumeAfter option is used).
73 // Thus, for example, all arguments to LLI are processed until a filename is
74 // found. Once a filename is found, all of the succeeding arguments are
75 // passed, unprocessed, to the ConsumeAfter option.
79 OccurrencesMask = 0x07
82 enum ValueExpected { // Is a value required for the option?
83 ValueOptional = 0x08, // The value can appear... or not
84 ValueRequired = 0x10, // The value is required to appear!
85 ValueDisallowed = 0x18, // A value may not be specified (for flags)
89 enum OptionHidden { // Control whether -help shows this option
90 NotHidden = 0x20, // Option included in --help & --help-hidden
91 Hidden = 0x40, // -help doesn't, but --help-hidden does
92 ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg
96 // Formatting flags - This controls special features that the option might have
97 // that cause it to be parsed differently...
99 // Prefix - This option allows arguments that are otherwise unrecognized to be
100 // matched by options that are a prefix of the actual value. This is useful for
101 // cases like a linker, where options are typically of the form '-lfoo' or
102 // '-L../../include' where -l or -L are the actual flags. When prefix is
103 // enabled, and used, the value for the flag comes from the suffix of the
106 // Grouping - With this option enabled, multiple letter options are allowed to
107 // bunch together with only a single hyphen for the whole group. This allows
108 // emulation of the behavior that ls uses for example: ls -la === ls -l -a
111 enum FormattingFlags {
112 NormalFormatting = 0x000, // Nothing special
113 Positional = 0x080, // Is a positional argument, no '-' required
114 Prefix = 0x100, // Can this option directly prefix its value?
115 Grouping = 0x180, // Can this option group with other options?
116 FormattingMask = 0x180 // Union of the above flags.
119 enum MiscFlags { // Miscellaneous flags to adjust argument
120 CommaSeparated = 0x200, // Should this cl::list split between commas?
121 PositionalEatsArgs = 0x400, // Should this positional cl::list eat -args?
122 MiscMask = 0x600 // Union of the above flags.
127 //===----------------------------------------------------------------------===//
132 friend void cl::ParseCommandLineOptions(int &, char **, const char *);
135 // handleOccurrences - Overriden by subclasses to handle the value passed into
136 // an argument. Should return true if there was an error processing the
137 // argument and the program should exit.
139 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
140 const std::string &Arg) = 0;
142 virtual enum ValueExpected getValueExpectedFlagDefault() const {
143 return ValueOptional;
146 // Out of line virtual function to provide home for the class.
147 virtual void anchor();
149 int NumOccurrences; // The number of times specified
150 int Flags; // Flags for the argument
151 unsigned Position; // Position of last occurrence of the option
152 Option *NextRegistered; // Singly linked list of registered options.
154 const char *ArgStr; // The argument string itself (ex: "help", "o")
155 const char *HelpStr; // The descriptive text message for --help
156 const char *ValueStr; // String describing what the value of this option is
158 inline enum NumOccurrences getNumOccurrencesFlag() const {
159 return static_cast<enum NumOccurrences>(Flags & OccurrencesMask);
161 inline enum ValueExpected getValueExpectedFlag() const {
162 int VE = Flags & ValueMask;
163 return VE ? static_cast<enum ValueExpected>(VE)
164 : getValueExpectedFlagDefault();
166 inline enum OptionHidden getOptionHiddenFlag() const {
167 return static_cast<enum OptionHidden>(Flags & HiddenMask);
169 inline enum FormattingFlags getFormattingFlag() const {
170 return static_cast<enum FormattingFlags>(Flags & FormattingMask);
172 inline unsigned getMiscFlags() const {
173 return Flags & MiscMask;
175 inline unsigned getPosition() const { return Position; }
177 // hasArgStr - Return true if the argstr != ""
178 bool hasArgStr() const { return ArgStr[0] != 0; }
180 //-------------------------------------------------------------------------===
181 // Accessor functions set by OptionModifiers
183 void setArgStr(const char *S) { ArgStr = S; }
184 void setDescription(const char *S) { HelpStr = S; }
185 void setValueStr(const char *S) { ValueStr = S; }
187 void setFlag(unsigned Flag, unsigned FlagMask) {
192 void setNumOccurrencesFlag(enum NumOccurrences Val) {
193 setFlag(Val, OccurrencesMask);
195 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
196 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
197 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
198 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
199 void setPosition(unsigned pos) { Position = pos; }
201 Option(unsigned DefaultFlags)
202 : NumOccurrences(0), Flags(DefaultFlags | NormalFormatting), Position(0),
203 NextRegistered(0), ArgStr(""), HelpStr(""), ValueStr("") {
204 assert(getNumOccurrencesFlag() != 0 &&
205 getOptionHiddenFlag() != 0 && "Not all default flags specified!");
209 // addArgument - Register this argument with the commandline system.
213 Option *getNextRegisteredOption() const { return NextRegistered; }
215 // Return the width of the option tag for printing...
216 virtual unsigned getOptionWidth() const = 0;
218 // printOptionInfo - Print out information about this option. The
219 // to-be-maintained width is specified.
221 virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
223 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {}
225 // addOccurrence - Wrapper around handleOccurrence that enforces Flags
227 bool addOccurrence(unsigned pos, const char *ArgName,
228 const std::string &Value);
230 // Prints option name followed by message. Always returns true.
231 bool error(std::string Message, const char *ArgName = 0);
234 inline int getNumOccurrences() const { return NumOccurrences; }
239 //===----------------------------------------------------------------------===//
240 // Command line option modifiers that can be used to modify the behavior of
241 // command line option parsers...
244 // desc - Modifier to set the description shown in the --help output...
247 desc(const char *Str) : Desc(Str) {}
248 void apply(Option &O) const { O.setDescription(Desc); }
251 // value_desc - Modifier to set the value description shown in the --help
255 value_desc(const char *Str) : Desc(Str) {}
256 void apply(Option &O) const { O.setValueStr(Desc); }
259 // init - Specify a default (initial) value for the command line argument, if
260 // the default constructor for the argument type does not give you what you
261 // want. This is only valid on "opt" arguments, not on "list" arguments.
266 initializer(const Ty &Val) : Init(Val) {}
269 void apply(Opt &O) const { O.setInitialValue(Init); }
273 initializer<Ty> init(const Ty &Val) {
274 return initializer<Ty>(Val);
278 // location - Allow the user to specify which external variable they want to
279 // store the results of the command line argument processing into, if they don't
280 // want to store it in the option itself.
283 struct LocationClass {
285 LocationClass(Ty &L) : Loc(L) {}
288 void apply(Opt &O) const { O.setLocation(O, Loc); }
292 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
295 //===----------------------------------------------------------------------===//
296 // Enum valued command line option
298 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, int(ENUMVAL), DESC
299 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, int(ENUMVAL), DESC
300 #define clEnumValEnd (reinterpret_cast<void*>(0))
302 // values - For custom data types, allow specifying a group of values together
303 // as the values that go into the mapping that the option handler uses. Note
304 // that the values list must always have a 0 at the end of the list to indicate
305 // that the list has ended.
307 template<class DataType>
309 // Use a vector instead of a map, because the lists should be short,
310 // the overhead is less, and most importantly, it keeps them in the order
311 // inserted so we can print our option out nicely.
312 SmallVector<std::pair<const char *, std::pair<int, const char *> >,4> Values;
313 void processValues(va_list Vals);
315 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
317 // Insert the first value, which is required.
318 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
320 // Process the varargs portion of the values...
321 while (const char *EnumName = va_arg(ValueArgs, const char *)) {
322 DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int));
323 const char *EnumDesc = va_arg(ValueArgs, const char *);
324 Values.push_back(std::make_pair(EnumName, // Add value to value map
325 std::make_pair(EnumVal, EnumDesc)));
330 void apply(Opt &O) const {
331 for (unsigned i = 0, e = Values.size(); i != e; ++i)
332 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
333 Values[i].second.second);
337 template<class DataType>
338 ValuesClass<DataType> END_WITH_NULL values(const char *Arg, DataType Val,
339 const char *Desc, ...) {
341 va_start(ValueArgs, Desc);
342 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
348 //===----------------------------------------------------------------------===//
349 // parser class - Parameterizable parser for different data types. By default,
350 // known data types (string, int, bool) have specialized parsers, that do what
351 // you would expect. The default parser, used for data types that are not
352 // built-in, uses a mapping table to map specific options to values, which is
353 // used, among other things, to handle enum types.
355 //--------------------------------------------------
356 // generic_parser_base - This class holds all the non-generic code that we do
357 // not need replicated for every instance of the generic parser. This also
358 // allows us to put stuff into CommandLine.cpp
360 struct generic_parser_base {
361 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
363 // getNumOptions - Virtual function implemented by generic subclass to
364 // indicate how many entries are in Values.
366 virtual unsigned getNumOptions() const = 0;
368 // getOption - Return option name N.
369 virtual const char *getOption(unsigned N) const = 0;
371 // getDescription - Return description N
372 virtual const char *getDescription(unsigned N) const = 0;
374 // Return the width of the option tag for printing...
375 virtual unsigned getOptionWidth(const Option &O) const;
377 // printOptionInfo - Print out information about this option. The
378 // to-be-maintained width is specified.
380 virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
382 void initialize(Option &O) {
383 // All of the modifiers for the option have been processed by now, so the
384 // argstr field should be stable, copy it down now.
386 hasArgStr = O.hasArgStr();
389 void getExtraOptionNames(std::vector<const char*> &OptionNames) {
390 // If there has been no argstr specified, that means that we need to add an
391 // argument for every possible option. This ensures that our options are
394 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
395 OptionNames.push_back(getOption(i));
399 enum ValueExpected getValueExpectedFlagDefault() const {
400 // If there is an ArgStr specified, then we are of the form:
402 // -opt=O2 or -opt O2 or -optO2
404 // In which case, the value is required. Otherwise if an arg str has not
405 // been specified, we are of the form:
407 // -O2 or O2 or -la (where -l and -a are separate options)
409 // If this is the case, we cannot allow a value.
412 return ValueRequired;
414 return ValueDisallowed;
417 // findOption - Return the option number corresponding to the specified
418 // argument string. If the option is not found, getNumOptions() is returned.
420 unsigned findOption(const char *Name);
426 // Default parser implementation - This implementation depends on having a
427 // mapping of recognized options to values of some sort. In addition to this,
428 // each entry in the mapping also tracks a help message that is printed with the
429 // command line option for --help. Because this is a simple mapping parser, the
430 // data type can be any unsupported type.
432 template <class DataType>
433 class parser : public generic_parser_base {
435 SmallVector<std::pair<const char *,
436 std::pair<DataType, const char *> >, 8> Values;
438 typedef DataType parser_data_type;
440 // Implement virtual functions needed by generic_parser_base
441 unsigned getNumOptions() const { return unsigned(Values.size()); }
442 const char *getOption(unsigned N) const { return Values[N].first; }
443 const char *getDescription(unsigned N) const {
444 return Values[N].second.second;
447 // parse - Return true on error.
448 bool parse(Option &O, const char *ArgName, const std::string &Arg,
456 for (unsigned i = 0, e = Values.size(); i != e; ++i)
457 if (ArgVal == Values[i].first) {
458 V = Values[i].second.first;
462 return O.error(": Cannot find option named '" + ArgVal + "'!");
465 /// addLiteralOption - Add an entry to the mapping table.
468 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
469 assert(findOption(Name) == Values.size() && "Option already exists!");
470 Values.push_back(std::make_pair(Name,
471 std::make_pair(static_cast<DataType>(V),HelpStr)));
474 /// removeLiteralOption - Remove the specified option.
476 void removeLiteralOption(const char *Name) {
477 unsigned N = findOption(Name);
478 assert(N != Values.size() && "Option not found!");
479 Values.erase(Values.begin()+N);
483 //--------------------------------------------------
484 // basic_parser - Super class of parsers to provide boilerplate code
486 struct basic_parser_impl { // non-template implementation of basic_parser<t>
487 virtual ~basic_parser_impl() {}
489 enum ValueExpected getValueExpectedFlagDefault() const {
490 return ValueRequired;
493 void getExtraOptionNames(std::vector<const char*> &OptionNames) {}
495 void initialize(Option &O) {}
497 // Return the width of the option tag for printing...
498 unsigned getOptionWidth(const Option &O) const;
500 // printOptionInfo - Print out information about this option. The
501 // to-be-maintained width is specified.
503 void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
505 // getValueName - Overload in subclass to provide a better default value.
506 virtual const char *getValueName() const { return "value"; }
508 // An out-of-line virtual method to provide a 'home' for this class.
509 virtual void anchor();
512 // basic_parser - The real basic parser is just a template wrapper that provides
513 // a typedef for the provided data type.
515 template<class DataType>
516 struct basic_parser : public basic_parser_impl {
517 typedef DataType parser_data_type;
520 //--------------------------------------------------
524 class parser<bool> : public basic_parser<bool> {
526 // parse - Return true on error.
527 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
529 enum ValueExpected getValueExpectedFlagDefault() const {
530 return ValueOptional;
533 // getValueName - Do not print =<value> at all.
534 virtual const char *getValueName() const { return 0; }
536 // An out-of-line virtual method to provide a 'home' for this class.
537 virtual void anchor();
540 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<bool>);
543 //--------------------------------------------------
547 class parser<int> : public basic_parser<int> {
549 // parse - Return true on error.
550 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
552 // getValueName - Overload in subclass to provide a better default value.
553 virtual const char *getValueName() const { return "int"; }
555 // An out-of-line virtual method to provide a 'home' for this class.
556 virtual void anchor();
559 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<int>);
562 //--------------------------------------------------
566 class parser<unsigned> : public basic_parser<unsigned> {
568 // parse - Return true on error.
569 bool parse(Option &O, const char *AN, const std::string &Arg, unsigned &Val);
571 // getValueName - Overload in subclass to provide a better default value.
572 virtual const char *getValueName() const { return "uint"; }
574 // An out-of-line virtual method to provide a 'home' for this class.
575 virtual void anchor();
578 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<unsigned>);
580 //--------------------------------------------------
584 class parser<double> : public basic_parser<double> {
586 // parse - Return true on error.
587 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
589 // getValueName - Overload in subclass to provide a better default value.
590 virtual const char *getValueName() const { return "number"; }
592 // An out-of-line virtual method to provide a 'home' for this class.
593 virtual void anchor();
596 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<double>);
598 //--------------------------------------------------
602 class parser<float> : public basic_parser<float> {
604 // parse - Return true on error.
605 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
607 // getValueName - Overload in subclass to provide a better default value.
608 virtual const char *getValueName() const { return "number"; }
610 // An out-of-line virtual method to provide a 'home' for this class.
611 virtual void anchor();
614 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<float>);
616 //--------------------------------------------------
617 // parser<std::string>
620 class parser<std::string> : public basic_parser<std::string> {
622 // parse - Return true on error.
623 bool parse(Option &O, const char *AN, const std::string &Arg,
624 std::string &Value) {
629 // getValueName - Overload in subclass to provide a better default value.
630 virtual const char *getValueName() const { return "string"; }
632 // An out-of-line virtual method to provide a 'home' for this class.
633 virtual void anchor();
636 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<std::string>);
638 //===----------------------------------------------------------------------===//
639 // applicator class - This class is used because we must use partial
640 // specialization to handle literal string arguments specially (const char* does
641 // not correctly respond to the apply method). Because the syntax to use this
642 // is a pain, we have the 'apply' method below to handle the nastiness...
644 template<class Mod> struct applicator {
646 static void opt(const Mod &M, Opt &O) { M.apply(O); }
649 // Handle const char* as a special case...
650 template<unsigned n> struct applicator<char[n]> {
652 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
654 template<unsigned n> struct applicator<const char[n]> {
656 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
658 template<> struct applicator<const char*> {
660 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
663 template<> struct applicator<NumOccurrences> {
664 static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); }
666 template<> struct applicator<ValueExpected> {
667 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
669 template<> struct applicator<OptionHidden> {
670 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
672 template<> struct applicator<FormattingFlags> {
673 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
675 template<> struct applicator<MiscFlags> {
676 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
679 // apply method - Apply a modifier to an option in a type safe way.
680 template<class Mod, class Opt>
681 void apply(const Mod &M, Opt *O) {
682 applicator<Mod>::opt(M, *O);
686 //===----------------------------------------------------------------------===//
689 // Default storage class definition: external storage. This implementation
690 // assumes the user will specify a variable to store the data into with the
691 // cl::location(x) modifier.
693 template<class DataType, bool ExternalStorage, bool isClass>
695 DataType *Location; // Where to store the object...
698 assert(Location != 0 && "cl::location(...) not specified for a command "
699 "line option with external storage, "
700 "or cl::init specified before cl::location()!!");
703 opt_storage() : Location(0) {}
705 bool setLocation(Option &O, DataType &L) {
707 return O.error(": cl::location(x) specified more than once!");
713 void setValue(const T &V) {
718 DataType &getValue() { check(); return *Location; }
719 const DataType &getValue() const { check(); return *Location; }
723 // Define how to hold a class type object, such as a string. Since we can
724 // inherit from a class, we do so. This makes us exactly compatible with the
725 // object in all cases that it is used.
727 template<class DataType>
728 class opt_storage<DataType,false,true> : public DataType {
731 void setValue(const T &V) { DataType::operator=(V); }
733 DataType &getValue() { return *this; }
734 const DataType &getValue() const { return *this; }
737 // Define a partial specialization to handle things we cannot inherit from. In
738 // this case, we store an instance through containment, and overload operators
739 // to get at the value.
741 template<class DataType>
742 class opt_storage<DataType, false, false> {
746 // Make sure we initialize the value with the default constructor for the
748 opt_storage() : Value(DataType()) {}
751 void setValue(const T &V) { Value = V; }
752 DataType &getValue() { return Value; }
753 DataType getValue() const { return Value; }
755 // If the datatype is a pointer, support -> on it.
756 DataType operator->() const { return Value; }
760 //===----------------------------------------------------------------------===//
761 // opt - A scalar command line option.
763 template <class DataType, bool ExternalStorage = false,
764 class ParserClass = parser<DataType> >
765 class opt : public Option,
766 public opt_storage<DataType, ExternalStorage,
767 is_class<DataType>::value> {
770 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
771 const std::string &Arg) {
772 typename ParserClass::parser_data_type Val =
773 typename ParserClass::parser_data_type();
774 if (Parser.parse(*this, ArgName, Arg, Val))
775 return true; // Parse error!
781 virtual enum ValueExpected getValueExpectedFlagDefault() const {
782 return Parser.getValueExpectedFlagDefault();
784 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
785 return Parser.getExtraOptionNames(OptionNames);
788 // Forward printing stuff to the parser...
789 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
790 virtual void printOptionInfo(unsigned GlobalWidth) const {
791 Parser.printOptionInfo(*this, GlobalWidth);
796 Parser.initialize(*this);
799 // setInitialValue - Used by the cl::init modifier...
800 void setInitialValue(const DataType &V) { this->setValue(V); }
802 ParserClass &getParser() { return Parser; }
804 operator DataType() const { return this->getValue(); }
807 DataType &operator=(const T &Val) {
809 return this->getValue();
814 opt(const M0t &M0) : Option(Optional | NotHidden) {
820 template<class M0t, class M1t>
821 opt(const M0t &M0, const M1t &M1) : Option(Optional | NotHidden) {
822 apply(M0, this); apply(M1, this);
827 template<class M0t, class M1t, class M2t>
828 opt(const M0t &M0, const M1t &M1,
829 const M2t &M2) : Option(Optional | NotHidden) {
830 apply(M0, this); apply(M1, this); apply(M2, this);
834 template<class M0t, class M1t, class M2t, class M3t>
835 opt(const M0t &M0, const M1t &M1, const M2t &M2,
836 const M3t &M3) : Option(Optional | NotHidden) {
837 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
841 template<class M0t, class M1t, class M2t, class M3t, class M4t>
842 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
843 const M4t &M4) : Option(Optional | NotHidden) {
844 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
849 template<class M0t, class M1t, class M2t, class M3t,
850 class M4t, class M5t>
851 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
852 const M4t &M4, const M5t &M5) : Option(Optional | NotHidden) {
853 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
854 apply(M4, this); apply(M5, this);
858 template<class M0t, class M1t, class M2t, class M3t,
859 class M4t, class M5t, class M6t>
860 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
861 const M4t &M4, const M5t &M5,
862 const M6t &M6) : Option(Optional | NotHidden) {
863 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
864 apply(M4, this); apply(M5, this); apply(M6, this);
868 template<class M0t, class M1t, class M2t, class M3t,
869 class M4t, class M5t, class M6t, class M7t>
870 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
871 const M4t &M4, const M5t &M5, const M6t &M6,
872 const M7t &M7) : Option(Optional | NotHidden) {
873 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
874 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
879 EXTERN_TEMPLATE_INSTANTIATION(class opt<unsigned>);
880 EXTERN_TEMPLATE_INSTANTIATION(class opt<int>);
881 EXTERN_TEMPLATE_INSTANTIATION(class opt<std::string>);
882 EXTERN_TEMPLATE_INSTANTIATION(class opt<bool>);
884 //===----------------------------------------------------------------------===//
885 // list_storage class
887 // Default storage class definition: external storage. This implementation
888 // assumes the user will specify a variable to store the data into with the
889 // cl::location(x) modifier.
891 template<class DataType, class StorageClass>
893 StorageClass *Location; // Where to store the object...
896 list_storage() : Location(0) {}
898 bool setLocation(Option &O, StorageClass &L) {
900 return O.error(": cl::location(x) specified more than once!");
906 void addValue(const T &V) {
907 assert(Location != 0 && "cl::location(...) not specified for a command "
908 "line option with external storage!");
909 Location->push_back(V);
914 // Define how to hold a class type object, such as a string. Since we can
915 // inherit from a class, we do so. This makes us exactly compatible with the
916 // object in all cases that it is used.
918 template<class DataType>
919 class list_storage<DataType, bool> : public std::vector<DataType> {
922 void addValue(const T &V) { push_back(V); }
926 //===----------------------------------------------------------------------===//
927 // list - A list of command line options.
929 template <class DataType, class Storage = bool,
930 class ParserClass = parser<DataType> >
931 class list : public Option, public list_storage<DataType, Storage> {
932 std::vector<unsigned> Positions;
935 virtual enum ValueExpected getValueExpectedFlagDefault() const {
936 return Parser.getValueExpectedFlagDefault();
938 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
939 return Parser.getExtraOptionNames(OptionNames);
942 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
943 const std::string &Arg) {
944 typename ParserClass::parser_data_type Val =
945 typename ParserClass::parser_data_type();
946 if (Parser.parse(*this, ArgName, Arg, Val))
947 return true; // Parse Error!
950 Positions.push_back(pos);
954 // Forward printing stuff to the parser...
955 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
956 virtual void printOptionInfo(unsigned GlobalWidth) const {
957 Parser.printOptionInfo(*this, GlobalWidth);
962 Parser.initialize(*this);
965 ParserClass &getParser() { return Parser; }
967 unsigned getPosition(unsigned optnum) const {
968 assert(optnum < this->size() && "Invalid option index");
969 return Positions[optnum];
974 list(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
979 template<class M0t, class M1t>
980 list(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
981 apply(M0, this); apply(M1, this);
985 template<class M0t, class M1t, class M2t>
986 list(const M0t &M0, const M1t &M1, const M2t &M2)
987 : Option(ZeroOrMore | NotHidden) {
988 apply(M0, this); apply(M1, this); apply(M2, this);
992 template<class M0t, class M1t, class M2t, class M3t>
993 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
994 : Option(ZeroOrMore | NotHidden) {
995 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
999 template<class M0t, class M1t, class M2t, class M3t, class M4t>
1000 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1001 const M4t &M4) : Option(ZeroOrMore | NotHidden) {
1002 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1007 template<class M0t, class M1t, class M2t, class M3t,
1008 class M4t, class M5t>
1009 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1010 const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
1011 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1012 apply(M4, this); apply(M5, this);
1016 template<class M0t, class M1t, class M2t, class M3t,
1017 class M4t, class M5t, class M6t>
1018 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1019 const M4t &M4, const M5t &M5, const M6t &M6)
1020 : Option(ZeroOrMore | NotHidden) {
1021 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1022 apply(M4, this); apply(M5, this); apply(M6, this);
1026 template<class M0t, class M1t, class M2t, class M3t,
1027 class M4t, class M5t, class M6t, class M7t>
1028 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1029 const M4t &M4, const M5t &M5, const M6t &M6,
1030 const M7t &M7) : Option(ZeroOrMore | NotHidden) {
1031 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1032 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1037 //===----------------------------------------------------------------------===//
1038 // bits_storage class
1040 // Default storage class definition: external storage. This implementation
1041 // assumes the user will specify a variable to store the data into with the
1042 // cl::location(x) modifier.
1044 template<class DataType, class StorageClass>
1045 class bits_storage {
1046 unsigned *Location; // Where to store the bits...
1049 static unsigned Bit(const T &V) {
1050 unsigned BitPos = reinterpret_cast<unsigned>(V);
1051 assert(BitPos < sizeof(unsigned) * 8 &&
1052 "enum exceeds width of bit vector!");
1057 bits_storage() : Location(0) {}
1059 bool setLocation(Option &O, unsigned &L) {
1061 return O.error(": cl::location(x) specified more than once!");
1067 void addValue(const T &V) {
1068 assert(Location != 0 && "cl::location(...) not specified for a command "
1069 "line option with external storage!");
1070 *Location |= Bit(V);
1073 unsigned getBits() { return *Location; }
1076 bool isSet(const T &V) {
1077 return (*Location & Bit(V)) != 0;
1082 // Define how to hold bits. Since we can inherit from a class, we do so.
1083 // This makes us exactly compatible with the bits in all cases that it is used.
1085 template<class DataType>
1086 class bits_storage<DataType, bool> {
1087 unsigned Bits; // Where to store the bits...
1090 static unsigned Bit(const T &V) {
1091 unsigned BitPos = reinterpret_cast<unsigned>(V);
1092 assert(BitPos < sizeof(unsigned) * 8 &&
1093 "enum exceeds width of bit vector!");
1099 void addValue(const T &V) {
1103 unsigned getBits() { return Bits; }
1106 bool isSet(const T &V) {
1107 return (Bits & Bit(V)) != 0;
1112 //===----------------------------------------------------------------------===//
1113 // bits - A bit vector of command options.
1115 template <class DataType, class Storage = bool,
1116 class ParserClass = parser<DataType> >
1117 class bits : public Option, public bits_storage<DataType, Storage> {
1118 std::vector<unsigned> Positions;
1121 virtual enum ValueExpected getValueExpectedFlagDefault() const {
1122 return Parser.getValueExpectedFlagDefault();
1124 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
1125 return Parser.getExtraOptionNames(OptionNames);
1128 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
1129 const std::string &Arg) {
1130 typename ParserClass::parser_data_type Val =
1131 typename ParserClass::parser_data_type();
1132 if (Parser.parse(*this, ArgName, Arg, Val))
1133 return true; // Parse Error!
1136 Positions.push_back(pos);
1140 // Forward printing stuff to the parser...
1141 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
1142 virtual void printOptionInfo(unsigned GlobalWidth) const {
1143 Parser.printOptionInfo(*this, GlobalWidth);
1148 Parser.initialize(*this);
1151 ParserClass &getParser() { return Parser; }
1153 unsigned getPosition(unsigned optnum) const {
1154 assert(optnum < this->size() && "Invalid option index");
1155 return Positions[optnum];
1160 bits(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
1165 template<class M0t, class M1t>
1166 bits(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
1167 apply(M0, this); apply(M1, this);
1171 template<class M0t, class M1t, class M2t>
1172 bits(const M0t &M0, const M1t &M1, const M2t &M2)
1173 : Option(ZeroOrMore | NotHidden) {
1174 apply(M0, this); apply(M1, this); apply(M2, this);
1178 template<class M0t, class M1t, class M2t, class M3t>
1179 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1180 : Option(ZeroOrMore | NotHidden) {
1181 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1185 template<class M0t, class M1t, class M2t, class M3t, class M4t>
1186 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1187 const M4t &M4) : Option(ZeroOrMore | NotHidden) {
1188 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1193 template<class M0t, class M1t, class M2t, class M3t,
1194 class M4t, class M5t>
1195 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1196 const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
1197 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1198 apply(M4, this); apply(M5, this);
1202 template<class M0t, class M1t, class M2t, class M3t,
1203 class M4t, class M5t, class M6t>
1204 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1205 const M4t &M4, const M5t &M5, const M6t &M6)
1206 : Option(ZeroOrMore | NotHidden) {
1207 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1208 apply(M4, this); apply(M5, this); apply(M6, this);
1212 template<class M0t, class M1t, class M2t, class M3t,
1213 class M4t, class M5t, class M6t, class M7t>
1214 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1215 const M4t &M4, const M5t &M5, const M6t &M6,
1216 const M7t &M7) : Option(ZeroOrMore | NotHidden) {
1217 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1218 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1223 //===----------------------------------------------------------------------===//
1224 // Aliased command line option (alias this name to a preexisting name)
1227 class alias : public Option {
1229 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
1230 const std::string &Arg) {
1231 return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
1233 // Handle printing stuff...
1234 virtual unsigned getOptionWidth() const;
1235 virtual void printOptionInfo(unsigned GlobalWidth) const;
1239 error(": cl::alias must have argument name specified!");
1241 error(": cl::alias must have an cl::aliasopt(option) specified!");
1245 void setAliasFor(Option &O) {
1247 error(": cl::alias must only have one cl::aliasopt(...) specified!");
1253 alias(const M0t &M0) : Option(Optional | Hidden), AliasFor(0) {
1258 template<class M0t, class M1t>
1259 alias(const M0t &M0, const M1t &M1) : Option(Optional | Hidden), AliasFor(0) {
1260 apply(M0, this); apply(M1, this);
1264 template<class M0t, class M1t, class M2t>
1265 alias(const M0t &M0, const M1t &M1, const M2t &M2)
1266 : Option(Optional | Hidden), AliasFor(0) {
1267 apply(M0, this); apply(M1, this); apply(M2, this);
1271 template<class M0t, class M1t, class M2t, class M3t>
1272 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1273 : Option(Optional | Hidden), AliasFor(0) {
1274 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1279 // aliasfor - Modifier to set the option an alias aliases.
1282 aliasopt(Option &O) : Opt(O) {}
1283 void apply(alias &A) const { A.setAliasFor(Opt); }
1286 // extrahelp - provide additional help at the end of the normal help
1287 // output. All occurrences of cl::extrahelp will be accumulated and
1288 // printed to std::cerr at the end of the regular help, just before
1291 const char * morehelp;
1292 extrahelp(const char* help);
1295 void PrintVersionMessage();
1296 // This function just prints the help message, exactly the same way as if the
1297 // --help option had been given on the command line.
1298 // NOTE: THIS FUNCTION TERMINATES THE PROGRAM!
1299 void PrintHelpMessage();
1301 } // End namespace cl
1303 } // End namespace llvm