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"
33 /// cl Namespace - This namespace contains all of the command line option
34 /// processing machinery. It is intentionally a short name to make qualified
38 //===----------------------------------------------------------------------===//
39 // ParseCommandLineOptions - Command line option processing entry point.
41 void ParseCommandLineOptions(int &argc, char **argv,
42 const char *Overview = 0);
44 //===----------------------------------------------------------------------===//
45 // ParseEnvironmentOptions - Environment variable option processing alternate
48 void ParseEnvironmentOptions(const char *progName, const char *envvar,
49 const char *Overview = 0);
51 //===----------------------------------------------------------------------===//
52 // Flags permitted to be passed to command line arguments
55 enum NumOccurrences { // Flags for the number of occurrences allowed
56 Optional = 0x01, // Zero or One occurrence
57 ZeroOrMore = 0x02, // Zero or more occurrences allowed
58 Required = 0x03, // One occurrence required
59 OneOrMore = 0x04, // One or more occurrences required
61 // ConsumeAfter - Indicates that this option is fed anything that follows the
62 // last positional argument required by the application (it is an error if
63 // there are zero positional arguments, and a ConsumeAfter option is used).
64 // Thus, for example, all arguments to LLI are processed until a filename is
65 // found. Once a filename is found, all of the succeeding arguments are
66 // passed, unprocessed, to the ConsumeAfter option.
70 OccurrencesMask = 0x07
73 enum ValueExpected { // Is a value required for the option?
74 ValueOptional = 0x08, // The value can appear... or not
75 ValueRequired = 0x10, // The value is required to appear!
76 ValueDisallowed = 0x18, // A value may not be specified (for flags)
80 enum OptionHidden { // Control whether -help shows this option
81 NotHidden = 0x20, // Option included in --help & --help-hidden
82 Hidden = 0x40, // -help doesn't, but --help-hidden does
83 ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg
87 // Formatting flags - This controls special features that the option might have
88 // that cause it to be parsed differently...
90 // Prefix - This option allows arguments that are otherwise unrecognized to be
91 // matched by options that are a prefix of the actual value. This is useful for
92 // cases like a linker, where options are typically of the form '-lfoo' or
93 // '-L../../include' where -l or -L are the actual flags. When prefix is
94 // enabled, and used, the value for the flag comes from the suffix of the
97 // Grouping - With this option enabled, multiple letter options are allowed to
98 // bunch together with only a single hyphen for the whole group. This allows
99 // emulation of the behavior that ls uses for example: ls -la === ls -l -a
102 enum FormattingFlags {
103 NormalFormatting = 0x000, // Nothing special
104 Positional = 0x080, // Is a positional argument, no '-' required
105 Prefix = 0x100, // Can this option directly prefix its value?
106 Grouping = 0x180, // Can this option group with other options?
107 FormattingMask = 0x180 // Union of the above flags.
110 enum MiscFlags { // Miscellaneous flags to adjust argument
111 CommaSeparated = 0x200, // Should this cl::list split between commas?
112 PositionalEatsArgs = 0x400, // Should this positional cl::list eat -args?
113 MiscMask = 0x600 // Union of the above flags.
118 //===----------------------------------------------------------------------===//
123 friend void cl::ParseCommandLineOptions(int &, char **, const char *);
126 // handleOccurrences - Overriden by subclasses to handle the value passed into
127 // an argument. Should return true if there was an error processing the
128 // argument and the program should exit.
130 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
131 const std::string &Arg) = 0;
133 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
136 virtual enum ValueExpected getValueExpectedFlagDefault() const {
137 return ValueOptional;
139 virtual enum OptionHidden getOptionHiddenFlagDefault() const {
142 virtual enum FormattingFlags getFormattingFlagDefault() const {
143 return NormalFormatting;
146 int NumOccurrences; // The number of times specified
147 int Flags; // Flags for the argument
148 unsigned Position; // Position of last occurrence of the option
150 const char *ArgStr; // The argument string itself (ex: "help", "o")
151 const char *HelpStr; // The descriptive text message for --help
152 const char *ValueStr; // String describing what the value of this option is
154 inline enum NumOccurrences getNumOccurrencesFlag() const {
155 int NO = Flags & OccurrencesMask;
156 return NO ? static_cast<enum NumOccurrences>(NO)
157 : getNumOccurrencesFlagDefault();
159 inline enum ValueExpected getValueExpectedFlag() const {
160 int VE = Flags & ValueMask;
161 return VE ? static_cast<enum ValueExpected>(VE)
162 : getValueExpectedFlagDefault();
164 inline enum OptionHidden getOptionHiddenFlag() const {
165 int OH = Flags & HiddenMask;
166 return OH ? static_cast<enum OptionHidden>(OH)
167 : getOptionHiddenFlagDefault();
169 inline enum FormattingFlags getFormattingFlag() const {
170 int OH = Flags & FormattingMask;
171 return OH ? static_cast<enum FormattingFlags>(OH)
172 : getFormattingFlagDefault();
174 inline unsigned getMiscFlags() const {
175 return Flags & MiscMask;
177 inline unsigned getPosition() const { return Position; }
179 // hasArgStr - Return true if the argstr != ""
180 bool hasArgStr() const { return ArgStr[0] != 0; }
182 //-------------------------------------------------------------------------===
183 // Accessor functions set by OptionModifiers
185 void setArgStr(const char *S) { ArgStr = S; }
186 void setDescription(const char *S) { HelpStr = S; }
187 void setValueStr(const char *S) { ValueStr = S; }
189 void setFlag(unsigned Flag, unsigned FlagMask) {
190 if (Flags & FlagMask) {
191 error(": Specified two settings for the same option!");
198 void setNumOccurrencesFlag(enum NumOccurrences Val) {
199 setFlag(Val, OccurrencesMask);
201 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
202 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
203 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
204 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
205 void setPosition(unsigned pos) { Position = pos; }
207 Option() : NumOccurrences(0), Flags(0), Position(0),
208 ArgStr(""), HelpStr(""), ValueStr("") {}
211 // addArgument - Tell the system that this Option subclass will handle all
212 // occurrences of -ArgStr on the command line.
214 void addArgument(const char *ArgStr);
215 void removeArgument(const char *ArgStr);
217 // Return the width of the option tag for printing...
218 virtual unsigned getOptionWidth() const = 0;
220 // printOptionInfo - Print out information about this option. The
221 // to-be-maintained width is specified.
223 virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
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 std::vector<std::pair<const char *, std::pair<int, const char *> > > 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();
388 // If there has been no argstr specified, that means that we need to add an
389 // argument for every possible option. This ensures that our options are
393 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
394 O.addArgument(getOption(i));
397 enum ValueExpected getValueExpectedFlagDefault() const {
398 // If there is an ArgStr specified, then we are of the form:
400 // -opt=O2 or -opt O2 or -optO2
402 // In which case, the value is required. Otherwise if an arg str has not
403 // been specified, we are of the form:
405 // -O2 or O2 or -la (where -l and -a are separate options)
407 // If this is the case, we cannot allow a value.
410 return ValueRequired;
412 return ValueDisallowed;
415 // findOption - Return the option number corresponding to the specified
416 // argument string. If the option is not found, getNumOptions() is returned.
418 unsigned findOption(const char *Name);
424 // Default parser implementation - This implementation depends on having a
425 // mapping of recognized options to values of some sort. In addition to this,
426 // each entry in the mapping also tracks a help message that is printed with the
427 // command line option for --help. Because this is a simple mapping parser, the
428 // data type can be any unsupported type.
430 template <class DataType>
431 class parser : public generic_parser_base {
433 std::vector<std::pair<const char *,
434 std::pair<DataType, const char *> > > Values;
436 typedef DataType parser_data_type;
438 // Implement virtual functions needed by generic_parser_base
439 unsigned getNumOptions() const { return unsigned(Values.size()); }
440 const char *getOption(unsigned N) const { return Values[N].first; }
441 const char *getDescription(unsigned N) const {
442 return Values[N].second.second;
445 // parse - Return true on error.
446 bool parse(Option &O, const char *ArgName, const std::string &Arg,
454 for (unsigned i = 0, e = Values.size(); i != e; ++i)
455 if (ArgVal == Values[i].first) {
456 V = Values[i].second.first;
460 return O.error(": Cannot find option named '" + ArgVal + "'!");
463 // addLiteralOption - Add an entry to the mapping table...
465 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
466 assert(findOption(Name) == Values.size() && "Option already exists!");
467 Values.push_back(std::make_pair(Name,
468 std::make_pair(static_cast<DataType>(V),HelpStr)));
471 // removeLiteralOption - Remove the specified option.
473 void removeLiteralOption(const char *Name) {
474 unsigned N = findOption(Name);
475 assert(N != Values.size() && "Option not found!");
476 Values.erase(Values.begin()+N);
480 //--------------------------------------------------
481 // basic_parser - Super class of parsers to provide boilerplate code
483 struct basic_parser_impl { // non-template implementation of basic_parser<t>
484 virtual ~basic_parser_impl() {}
486 enum ValueExpected getValueExpectedFlagDefault() const {
487 return ValueRequired;
490 void initialize(Option &O) {}
492 // Return the width of the option tag for printing...
493 unsigned getOptionWidth(const Option &O) const;
495 // printOptionInfo - Print out information about this option. The
496 // to-be-maintained width is specified.
498 void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
500 // getValueName - Overload in subclass to provide a better default value.
501 virtual const char *getValueName() const { return "value"; }
504 // basic_parser - The real basic parser is just a template wrapper that provides
505 // a typedef for the provided data type.
507 template<class DataType>
508 struct basic_parser : public basic_parser_impl {
509 typedef DataType parser_data_type;
513 //--------------------------------------------------
517 class parser<bool> : public basic_parser<bool> {
519 // parse - Return true on error.
520 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
522 enum ValueExpected getValueExpectedFlagDefault() const {
523 return ValueOptional;
526 // getValueName - Do not print =<value> at all
527 virtual const char *getValueName() const { return 0; }
531 //--------------------------------------------------
535 class parser<int> : public basic_parser<int> {
537 // parse - Return true on error.
538 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
540 // getValueName - Overload in subclass to provide a better default value.
541 virtual const char *getValueName() const { return "int"; }
545 //--------------------------------------------------
549 class parser<unsigned> : public basic_parser<unsigned> {
551 // parse - Return true on error.
552 bool parse(Option &O, const char *AN, const std::string &Arg, unsigned &Val);
554 // getValueName - Overload in subclass to provide a better default value.
555 virtual const char *getValueName() const { return "uint"; }
559 //--------------------------------------------------
563 class parser<double> : public basic_parser<double> {
565 // parse - Return true on error.
566 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
568 // getValueName - Overload in subclass to provide a better default value.
569 virtual const char *getValueName() const { return "number"; }
573 //--------------------------------------------------
577 class parser<float> : public basic_parser<float> {
579 // parse - Return true on error.
580 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
582 // getValueName - Overload in subclass to provide a better default value.
583 virtual const char *getValueName() const { return "number"; }
587 //--------------------------------------------------
588 // parser<std::string>
591 class parser<std::string> : public basic_parser<std::string> {
593 // parse - Return true on error.
594 bool parse(Option &O, const char *AN, const std::string &Arg,
595 std::string &Value) {
600 // getValueName - Overload in subclass to provide a better default value.
601 virtual const char *getValueName() const { return "string"; }
604 //===----------------------------------------------------------------------===//
605 // applicator class - This class is used because we must use partial
606 // specialization to handle literal string arguments specially (const char* does
607 // not correctly respond to the apply method). Because the syntax to use this
608 // is a pain, we have the 'apply' method below to handle the nastiness...
610 template<class Mod> struct applicator {
612 static void opt(const Mod &M, Opt &O) { M.apply(O); }
615 // Handle const char* as a special case...
616 template<unsigned n> struct applicator<char[n]> {
618 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
620 template<unsigned n> struct applicator<const char[n]> {
622 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
624 template<> struct applicator<const char*> {
626 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
629 template<> struct applicator<NumOccurrences> {
630 static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); }
632 template<> struct applicator<ValueExpected> {
633 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
635 template<> struct applicator<OptionHidden> {
636 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
638 template<> struct applicator<FormattingFlags> {
639 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
641 template<> struct applicator<MiscFlags> {
642 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
645 // apply method - Apply a modifier to an option in a type safe way.
646 template<class Mod, class Opt>
647 void apply(const Mod &M, Opt *O) {
648 applicator<Mod>::opt(M, *O);
652 //===----------------------------------------------------------------------===//
655 // Default storage class definition: external storage. This implementation
656 // assumes the user will specify a variable to store the data into with the
657 // cl::location(x) modifier.
659 template<class DataType, bool ExternalStorage, bool isClass>
661 DataType *Location; // Where to store the object...
664 assert(Location != 0 && "cl::location(...) not specified for a command "
665 "line option with external storage, "
666 "or cl::init specified before cl::location()!!");
669 opt_storage() : Location(0) {}
671 bool setLocation(Option &O, DataType &L) {
673 return O.error(": cl::location(x) specified more than once!");
679 void setValue(const T &V) {
684 DataType &getValue() { check(); return *Location; }
685 const DataType &getValue() const { check(); return *Location; }
689 // Define how to hold a class type object, such as a string. Since we can
690 // inherit from a class, we do so. This makes us exactly compatible with the
691 // object in all cases that it is used.
693 template<class DataType>
694 class opt_storage<DataType,false,true> : public DataType {
697 void setValue(const T &V) { DataType::operator=(V); }
699 DataType &getValue() { return *this; }
700 const DataType &getValue() const { return *this; }
703 // Define a partial specialization to handle things we cannot inherit from. In
704 // this case, we store an instance through containment, and overload operators
705 // to get at the value.
707 template<class DataType>
708 class opt_storage<DataType, false, false> {
712 // Make sure we initialize the value with the default constructor for the
714 opt_storage() : Value(DataType()) {}
717 void setValue(const T &V) { Value = V; }
718 DataType &getValue() { return Value; }
719 DataType getValue() const { return Value; }
721 // If the datatype is a pointer, support -> on it.
722 DataType operator->() const { return Value; }
726 //===----------------------------------------------------------------------===//
727 // opt - A scalar command line option.
729 template <class DataType, bool ExternalStorage = false,
730 class ParserClass = parser<DataType> >
731 class opt : public Option,
732 public opt_storage<DataType, ExternalStorage,
733 is_class<DataType>::value> {
736 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
737 const std::string &Arg) {
738 typename ParserClass::parser_data_type Val =
739 typename ParserClass::parser_data_type();
740 if (Parser.parse(*this, ArgName, Arg, Val))
741 return true; // Parse error!
747 virtual enum ValueExpected getValueExpectedFlagDefault() const {
748 return Parser.getValueExpectedFlagDefault();
751 // Forward printing stuff to the parser...
752 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
753 virtual void printOptionInfo(unsigned GlobalWidth) const {
754 Parser.printOptionInfo(*this, GlobalWidth);
759 Parser.initialize(*this);
762 // setInitialValue - Used by the cl::init modifier...
763 void setInitialValue(const DataType &V) { this->setValue(V); }
765 ParserClass &getParser() { return Parser; }
767 operator DataType() const { return this->getValue(); }
770 DataType &operator=(const T &Val) {
772 return this->getValue();
783 template<class M0t, class M1t>
784 opt(const M0t &M0, const M1t &M1) {
785 apply(M0, this); apply(M1, this);
790 template<class M0t, class M1t, class M2t>
791 opt(const M0t &M0, const M1t &M1, const M2t &M2) {
792 apply(M0, this); apply(M1, this); apply(M2, this);
796 template<class M0t, class M1t, class M2t, class M3t>
797 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
798 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
802 template<class M0t, class M1t, class M2t, class M3t, class M4t>
803 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
805 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
810 template<class M0t, class M1t, class M2t, class M3t,
811 class M4t, class M5t>
812 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
813 const M4t &M4, const M5t &M5) {
814 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
815 apply(M4, this); apply(M5, this);
819 template<class M0t, class M1t, class M2t, class M3t,
820 class M4t, class M5t, class M6t>
821 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
822 const M4t &M4, const M5t &M5, const M6t &M6) {
823 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
824 apply(M4, this); apply(M5, this); apply(M6, this);
828 template<class M0t, class M1t, class M2t, class M3t,
829 class M4t, class M5t, class M6t, class M7t>
830 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
831 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
832 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
833 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
838 //===----------------------------------------------------------------------===//
839 // list_storage class
841 // Default storage class definition: external storage. This implementation
842 // assumes the user will specify a variable to store the data into with the
843 // cl::location(x) modifier.
845 template<class DataType, class StorageClass>
847 StorageClass *Location; // Where to store the object...
850 list_storage() : Location(0) {}
852 bool setLocation(Option &O, StorageClass &L) {
854 return O.error(": cl::location(x) specified more than once!");
860 void addValue(const T &V) {
861 assert(Location != 0 && "cl::location(...) not specified for a command "
862 "line option with external storage!");
863 Location->push_back(V);
868 // Define how to hold a class type object, such as a string. Since we can
869 // inherit from a class, we do so. This makes us exactly compatible with the
870 // object in all cases that it is used.
872 template<class DataType>
873 class list_storage<DataType, bool> : public std::vector<DataType> {
876 void addValue(const T &V) { push_back(V); }
880 //===----------------------------------------------------------------------===//
881 // list - A list of command line options.
883 template <class DataType, class Storage = bool,
884 class ParserClass = parser<DataType> >
885 class list : public Option, public list_storage<DataType, Storage> {
886 std::vector<unsigned> Positions;
889 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
892 virtual enum ValueExpected getValueExpectedFlagDefault() const {
893 return Parser.getValueExpectedFlagDefault();
896 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
897 const std::string &Arg) {
898 typename ParserClass::parser_data_type Val =
899 typename ParserClass::parser_data_type();
900 if (Parser.parse(*this, ArgName, Arg, Val))
901 return true; // Parse Error!
904 Positions.push_back(pos);
908 // Forward printing stuff to the parser...
909 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
910 virtual void printOptionInfo(unsigned GlobalWidth) const {
911 Parser.printOptionInfo(*this, GlobalWidth);
916 Parser.initialize(*this);
919 ParserClass &getParser() { return Parser; }
921 unsigned getPosition(unsigned optnum) const {
922 assert(optnum < this->size() && "Invalid option index");
923 return Positions[optnum];
928 list(const M0t &M0) {
933 template<class M0t, class M1t>
934 list(const M0t &M0, const M1t &M1) {
935 apply(M0, this); apply(M1, this);
939 template<class M0t, class M1t, class M2t>
940 list(const M0t &M0, const M1t &M1, const M2t &M2) {
941 apply(M0, this); apply(M1, this); apply(M2, this);
945 template<class M0t, class M1t, class M2t, class M3t>
946 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
947 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
951 template<class M0t, class M1t, class M2t, class M3t, class M4t>
952 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
954 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
959 template<class M0t, class M1t, class M2t, class M3t,
960 class M4t, class M5t>
961 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
962 const M4t &M4, const M5t &M5) {
963 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
964 apply(M4, this); apply(M5, this);
968 template<class M0t, class M1t, class M2t, class M3t,
969 class M4t, class M5t, class M6t>
970 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
971 const M4t &M4, const M5t &M5, const M6t &M6) {
972 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
973 apply(M4, this); apply(M5, this); apply(M6, this);
977 template<class M0t, class M1t, class M2t, class M3t,
978 class M4t, class M5t, class M6t, class M7t>
979 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
980 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
981 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
982 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
987 //===----------------------------------------------------------------------===//
988 // bits_storage class
990 // Default storage class definition: external storage. This implementation
991 // assumes the user will specify a variable to store the data into with the
992 // cl::location(x) modifier.
994 template<class DataType, class StorageClass>
996 unsigned *Location; // Where to store the bits...
999 static unsigned Bit(const T &V) {
1000 unsigned BitPos = (unsigned)V;
1001 assert(BitPos < sizeof(unsigned) * 8 &&
1002 "enum exceeds width of bit vector!");
1007 bits_storage() : Location(0) {}
1009 bool setLocation(Option &O, unsigned &L) {
1011 return O.error(": cl::location(x) specified more than once!");
1017 void addValue(const T &V) {
1018 assert(Location != 0 && "cl::location(...) not specified for a command "
1019 "line option with external storage!");
1020 *Location |= Bit(V);
1023 unsigned getBits() { return *Location; }
1026 bool isSet(const T &V) {
1027 return (*Location & Bit(V)) != 0;
1032 // Define how to hold bits. Since we can inherit from a class, we do so.
1033 // This makes us exactly compatible with the bits in all cases that it is used.
1035 template<class DataType>
1036 class bits_storage<DataType, bool> {
1037 unsigned Bits; // Where to store the bits...
1040 static unsigned Bit(const T &V) {
1041 unsigned BitPos = (unsigned)V;
1042 assert(BitPos < sizeof(unsigned) * 8 &&
1043 "enum exceeds width of bit vector!");
1049 void addValue(const T &V) {
1053 unsigned getBits() { return Bits; }
1056 bool isSet(const T &V) {
1057 return (Bits & Bit(V)) != 0;
1062 //===----------------------------------------------------------------------===//
1063 // bits - A bit vector of command options.
1065 template <class DataType, class Storage = bool,
1066 class ParserClass = parser<DataType> >
1067 class bits : public Option, public bits_storage<DataType, Storage> {
1068 std::vector<unsigned> Positions;
1071 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
1074 virtual enum ValueExpected getValueExpectedFlagDefault() const {
1075 return Parser.getValueExpectedFlagDefault();
1078 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
1079 const std::string &Arg) {
1080 typename ParserClass::parser_data_type Val =
1081 typename ParserClass::parser_data_type();
1082 if (Parser.parse(*this, ArgName, Arg, Val))
1083 return true; // Parse Error!
1086 Positions.push_back(pos);
1090 // Forward printing stuff to the parser...
1091 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
1092 virtual void printOptionInfo(unsigned GlobalWidth) const {
1093 Parser.printOptionInfo(*this, GlobalWidth);
1097 addArgument(ArgStr);
1098 Parser.initialize(*this);
1101 ParserClass &getParser() { return Parser; }
1103 unsigned getPosition(unsigned optnum) const {
1104 assert(optnum < this->size() && "Invalid option index");
1105 return Positions[optnum];
1110 bits(const M0t &M0) {
1115 template<class M0t, class M1t>
1116 bits(const M0t &M0, const M1t &M1) {
1117 apply(M0, this); apply(M1, this);
1121 template<class M0t, class M1t, class M2t>
1122 bits(const M0t &M0, const M1t &M1, const M2t &M2) {
1123 apply(M0, this); apply(M1, this); apply(M2, this);
1127 template<class M0t, class M1t, class M2t, class M3t>
1128 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
1129 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1133 template<class M0t, class M1t, class M2t, class M3t, class M4t>
1134 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1136 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1141 template<class M0t, class M1t, class M2t, class M3t,
1142 class M4t, class M5t>
1143 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1144 const M4t &M4, const M5t &M5) {
1145 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1146 apply(M4, this); apply(M5, this);
1150 template<class M0t, class M1t, class M2t, class M3t,
1151 class M4t, class M5t, class M6t>
1152 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1153 const M4t &M4, const M5t &M5, const M6t &M6) {
1154 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1155 apply(M4, this); apply(M5, this); apply(M6, this);
1159 template<class M0t, class M1t, class M2t, class M3t,
1160 class M4t, class M5t, class M6t, class M7t>
1161 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1162 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
1163 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1164 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1169 //===----------------------------------------------------------------------===//
1170 // Aliased command line option (alias this name to a preexisting name)
1173 class alias : public Option {
1175 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
1176 const std::string &Arg) {
1177 return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
1179 // Aliases default to be hidden...
1180 virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
1182 // Handle printing stuff...
1183 virtual unsigned getOptionWidth() const;
1184 virtual void printOptionInfo(unsigned GlobalWidth) const;
1188 error(": cl::alias must have argument name specified!");
1190 error(": cl::alias must have an cl::aliasopt(option) specified!");
1191 addArgument(ArgStr);
1194 void setAliasFor(Option &O) {
1196 error(": cl::alias must only have one cl::aliasopt(...) specified!");
1202 alias(const M0t &M0) : AliasFor(0) {
1207 template<class M0t, class M1t>
1208 alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
1209 apply(M0, this); apply(M1, this);
1213 template<class M0t, class M1t, class M2t>
1214 alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) {
1215 apply(M0, this); apply(M1, this); apply(M2, this);
1219 template<class M0t, class M1t, class M2t, class M3t>
1220 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1222 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1227 // aliasfor - Modifier to set the option an alias aliases.
1230 aliasopt(Option &O) : Opt(O) {}
1231 void apply(alias &A) const { A.setAliasFor(Opt); }
1234 // extrahelp - provide additional help at the end of the normal help
1235 // output. All occurrences of cl::extrahelp will be accumulated and
1236 // printed to std::cerr at the end of the regular help, just before
1239 const char * morehelp;
1240 extrahelp(const char* help);
1243 // This function just prints the help message, exactly the same way as if the
1244 // --help option had been given on the command line.
1245 // NOTE: THIS FUNCTION TERMINATES THE PROGRAM!
1246 void PrintHelpMessage();
1248 } // End namespace cl
1250 } // End namespace llvm