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 // Silly GCC doesn't allow attributes on a function definition.
338 template<class DataType>
339 ValuesClass<DataType> values(const char *Arg, DataType Val, const char *Desc,
342 template<class DataType>
343 ValuesClass<DataType> values(const char *Arg, DataType Val, const char *Desc,
346 va_start(ValueArgs, Desc);
347 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
353 //===----------------------------------------------------------------------===//
354 // parser class - Parameterizable parser for different data types. By default,
355 // known data types (string, int, bool) have specialized parsers, that do what
356 // you would expect. The default parser, used for data types that are not
357 // built-in, uses a mapping table to map specific options to values, which is
358 // used, among other things, to handle enum types.
360 //--------------------------------------------------
361 // generic_parser_base - This class holds all the non-generic code that we do
362 // not need replicated for every instance of the generic parser. This also
363 // allows us to put stuff into CommandLine.cpp
365 struct generic_parser_base {
366 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
368 // getNumOptions - Virtual function implemented by generic subclass to
369 // indicate how many entries are in Values.
371 virtual unsigned getNumOptions() const = 0;
373 // getOption - Return option name N.
374 virtual const char *getOption(unsigned N) const = 0;
376 // getDescription - Return description N
377 virtual const char *getDescription(unsigned N) const = 0;
379 // Return the width of the option tag for printing...
380 virtual unsigned getOptionWidth(const Option &O) const;
382 // printOptionInfo - Print out information about this option. The
383 // to-be-maintained width is specified.
385 virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
387 void initialize(Option &O) {
388 // All of the modifiers for the option have been processed by now, so the
389 // argstr field should be stable, copy it down now.
391 hasArgStr = O.hasArgStr();
393 // If there has been no argstr specified, that means that we need to add an
394 // argument for every possible option. This ensures that our options are
398 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
399 O.addArgument(getOption(i));
402 enum ValueExpected getValueExpectedFlagDefault() const {
403 // If there is an ArgStr specified, then we are of the form:
405 // -opt=O2 or -opt O2 or -optO2
407 // In which case, the value is required. Otherwise if an arg str has not
408 // been specified, we are of the form:
410 // -O2 or O2 or -la (where -l and -a are separate options)
412 // If this is the case, we cannot allow a value.
415 return ValueRequired;
417 return ValueDisallowed;
420 // findOption - Return the option number corresponding to the specified
421 // argument string. If the option is not found, getNumOptions() is returned.
423 unsigned findOption(const char *Name);
429 // Default parser implementation - This implementation depends on having a
430 // mapping of recognized options to values of some sort. In addition to this,
431 // each entry in the mapping also tracks a help message that is printed with the
432 // command line option for --help. Because this is a simple mapping parser, the
433 // data type can be any unsupported type.
435 template <class DataType>
436 class parser : public generic_parser_base {
438 std::vector<std::pair<const char *,
439 std::pair<DataType, const char *> > > Values;
441 typedef DataType parser_data_type;
443 // Implement virtual functions needed by generic_parser_base
444 unsigned getNumOptions() const { return unsigned(Values.size()); }
445 const char *getOption(unsigned N) const { return Values[N].first; }
446 const char *getDescription(unsigned N) const {
447 return Values[N].second.second;
450 // parse - Return true on error.
451 bool parse(Option &O, const char *ArgName, const std::string &Arg,
459 for (unsigned i = 0, e = Values.size(); i != e; ++i)
460 if (ArgVal == Values[i].first) {
461 V = Values[i].second.first;
465 return O.error(": Cannot find option named '" + ArgVal + "'!");
468 // addLiteralOption - Add an entry to the mapping table...
470 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
471 assert(findOption(Name) == Values.size() && "Option already exists!");
472 Values.push_back(std::make_pair(Name,
473 std::make_pair(static_cast<DataType>(V),HelpStr)));
476 // removeLiteralOption - Remove the specified option.
478 void removeLiteralOption(const char *Name) {
479 unsigned N = findOption(Name);
480 assert(N != Values.size() && "Option not found!");
481 Values.erase(Values.begin()+N);
485 //--------------------------------------------------
486 // basic_parser - Super class of parsers to provide boilerplate code
488 struct basic_parser_impl { // non-template implementation of basic_parser<t>
489 virtual ~basic_parser_impl() {}
491 enum ValueExpected getValueExpectedFlagDefault() const {
492 return ValueRequired;
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"; }
509 // basic_parser - The real basic parser is just a template wrapper that provides
510 // a typedef for the provided data type.
512 template<class DataType>
513 struct basic_parser : public basic_parser_impl {
514 typedef DataType parser_data_type;
518 //--------------------------------------------------
522 class parser<bool> : public basic_parser<bool> {
524 // parse - Return true on error.
525 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
527 enum ValueExpected getValueExpectedFlagDefault() const {
528 return ValueOptional;
531 // getValueName - Do not print =<value> at all
532 virtual const char *getValueName() const { return 0; }
536 //--------------------------------------------------
540 class parser<int> : public basic_parser<int> {
542 // parse - Return true on error.
543 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
545 // getValueName - Overload in subclass to provide a better default value.
546 virtual const char *getValueName() const { return "int"; }
550 //--------------------------------------------------
554 class parser<unsigned> : public basic_parser<unsigned> {
556 // parse - Return true on error.
557 bool parse(Option &O, const char *AN, const std::string &Arg, unsigned &Val);
559 // getValueName - Overload in subclass to provide a better default value.
560 virtual const char *getValueName() const { return "uint"; }
564 //--------------------------------------------------
568 class parser<double> : public basic_parser<double> {
570 // parse - Return true on error.
571 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
573 // getValueName - Overload in subclass to provide a better default value.
574 virtual const char *getValueName() const { return "number"; }
578 //--------------------------------------------------
582 class parser<float> : public basic_parser<float> {
584 // parse - Return true on error.
585 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
587 // getValueName - Overload in subclass to provide a better default value.
588 virtual const char *getValueName() const { return "number"; }
592 //--------------------------------------------------
593 // parser<std::string>
596 class parser<std::string> : public basic_parser<std::string> {
598 // parse - Return true on error.
599 bool parse(Option &O, const char *AN, const std::string &Arg,
600 std::string &Value) {
605 // getValueName - Overload in subclass to provide a better default value.
606 virtual const char *getValueName() const { return "string"; }
609 //===----------------------------------------------------------------------===//
610 // applicator class - This class is used because we must use partial
611 // specialization to handle literal string arguments specially (const char* does
612 // not correctly respond to the apply method). Because the syntax to use this
613 // is a pain, we have the 'apply' method below to handle the nastiness...
615 template<class Mod> struct applicator {
617 static void opt(const Mod &M, Opt &O) { M.apply(O); }
620 // Handle const char* as a special case...
621 template<unsigned n> struct applicator<char[n]> {
623 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
625 template<unsigned n> struct applicator<const char[n]> {
627 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
629 template<> struct applicator<const char*> {
631 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
634 template<> struct applicator<NumOccurrences> {
635 static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); }
637 template<> struct applicator<ValueExpected> {
638 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
640 template<> struct applicator<OptionHidden> {
641 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
643 template<> struct applicator<FormattingFlags> {
644 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
646 template<> struct applicator<MiscFlags> {
647 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
650 // apply method - Apply a modifier to an option in a type safe way.
651 template<class Mod, class Opt>
652 void apply(const Mod &M, Opt *O) {
653 applicator<Mod>::opt(M, *O);
657 //===----------------------------------------------------------------------===//
660 // Default storage class definition: external storage. This implementation
661 // assumes the user will specify a variable to store the data into with the
662 // cl::location(x) modifier.
664 template<class DataType, bool ExternalStorage, bool isClass>
666 DataType *Location; // Where to store the object...
669 assert(Location != 0 && "cl::location(...) not specified for a command "
670 "line option with external storage, "
671 "or cl::init specified before cl::location()!!");
674 opt_storage() : Location(0) {}
676 bool setLocation(Option &O, DataType &L) {
678 return O.error(": cl::location(x) specified more than once!");
684 void setValue(const T &V) {
689 DataType &getValue() { check(); return *Location; }
690 const DataType &getValue() const { check(); return *Location; }
694 // Define how to hold a class type object, such as a string. Since we can
695 // inherit from a class, we do so. This makes us exactly compatible with the
696 // object in all cases that it is used.
698 template<class DataType>
699 class opt_storage<DataType,false,true> : public DataType {
702 void setValue(const T &V) { DataType::operator=(V); }
704 DataType &getValue() { return *this; }
705 const DataType &getValue() const { return *this; }
708 // Define a partial specialization to handle things we cannot inherit from. In
709 // this case, we store an instance through containment, and overload operators
710 // to get at the value.
712 template<class DataType>
713 class opt_storage<DataType, false, false> {
717 // Make sure we initialize the value with the default constructor for the
719 opt_storage() : Value(DataType()) {}
722 void setValue(const T &V) { Value = V; }
723 DataType &getValue() { return Value; }
724 DataType getValue() const { return Value; }
726 // If the datatype is a pointer, support -> on it.
727 DataType operator->() const { return Value; }
731 //===----------------------------------------------------------------------===//
732 // opt - A scalar command line option.
734 template <class DataType, bool ExternalStorage = false,
735 class ParserClass = parser<DataType> >
736 class opt : public Option,
737 public opt_storage<DataType, ExternalStorage,
738 is_class<DataType>::value> {
741 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
742 const std::string &Arg) {
743 typename ParserClass::parser_data_type Val =
744 typename ParserClass::parser_data_type();
745 if (Parser.parse(*this, ArgName, Arg, Val))
746 return true; // Parse error!
752 virtual enum ValueExpected getValueExpectedFlagDefault() const {
753 return Parser.getValueExpectedFlagDefault();
756 // Forward printing stuff to the parser...
757 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
758 virtual void printOptionInfo(unsigned GlobalWidth) const {
759 Parser.printOptionInfo(*this, GlobalWidth);
764 Parser.initialize(*this);
767 // setInitialValue - Used by the cl::init modifier...
768 void setInitialValue(const DataType &V) { this->setValue(V); }
770 ParserClass &getParser() { return Parser; }
772 operator DataType() const { return this->getValue(); }
775 DataType &operator=(const T &Val) {
777 return this->getValue();
788 template<class M0t, class M1t>
789 opt(const M0t &M0, const M1t &M1) {
790 apply(M0, this); apply(M1, this);
795 template<class M0t, class M1t, class M2t>
796 opt(const M0t &M0, const M1t &M1, const M2t &M2) {
797 apply(M0, this); apply(M1, this); apply(M2, this);
801 template<class M0t, class M1t, class M2t, class M3t>
802 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
803 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
807 template<class M0t, class M1t, class M2t, class M3t, class M4t>
808 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
810 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
815 template<class M0t, class M1t, class M2t, class M3t,
816 class M4t, class M5t>
817 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
818 const M4t &M4, const M5t &M5) {
819 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
820 apply(M4, this); apply(M5, this);
824 template<class M0t, class M1t, class M2t, class M3t,
825 class M4t, class M5t, class M6t>
826 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
827 const M4t &M4, const M5t &M5, const M6t &M6) {
828 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
829 apply(M4, this); apply(M5, this); apply(M6, this);
833 template<class M0t, class M1t, class M2t, class M3t,
834 class M4t, class M5t, class M6t, class M7t>
835 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
836 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
837 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
838 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
843 //===----------------------------------------------------------------------===//
844 // list_storage class
846 // Default storage class definition: external storage. This implementation
847 // assumes the user will specify a variable to store the data into with the
848 // cl::location(x) modifier.
850 template<class DataType, class StorageClass>
852 StorageClass *Location; // Where to store the object...
855 list_storage() : Location(0) {}
857 bool setLocation(Option &O, StorageClass &L) {
859 return O.error(": cl::location(x) specified more than once!");
865 void addValue(const T &V) {
866 assert(Location != 0 && "cl::location(...) not specified for a command "
867 "line option with external storage!");
868 Location->push_back(V);
873 // Define how to hold a class type object, such as a string. Since we can
874 // inherit from a class, we do so. This makes us exactly compatible with the
875 // object in all cases that it is used.
877 template<class DataType>
878 class list_storage<DataType, bool> : public std::vector<DataType> {
881 void addValue(const T &V) { push_back(V); }
885 //===----------------------------------------------------------------------===//
886 // list - A list of command line options.
888 template <class DataType, class Storage = bool,
889 class ParserClass = parser<DataType> >
890 class list : public Option, public list_storage<DataType, Storage> {
891 std::vector<unsigned> Positions;
894 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
897 virtual enum ValueExpected getValueExpectedFlagDefault() const {
898 return Parser.getValueExpectedFlagDefault();
901 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
902 const std::string &Arg) {
903 typename ParserClass::parser_data_type Val =
904 typename ParserClass::parser_data_type();
905 if (Parser.parse(*this, ArgName, Arg, Val))
906 return true; // Parse Error!
909 Positions.push_back(pos);
913 // Forward printing stuff to the parser...
914 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
915 virtual void printOptionInfo(unsigned GlobalWidth) const {
916 Parser.printOptionInfo(*this, GlobalWidth);
921 Parser.initialize(*this);
924 ParserClass &getParser() { return Parser; }
926 unsigned getPosition(unsigned optnum) const {
927 assert(optnum < this->size() && "Invalid option index");
928 return Positions[optnum];
933 list(const M0t &M0) {
938 template<class M0t, class M1t>
939 list(const M0t &M0, const M1t &M1) {
940 apply(M0, this); apply(M1, this);
944 template<class M0t, class M1t, class M2t>
945 list(const M0t &M0, const M1t &M1, const M2t &M2) {
946 apply(M0, this); apply(M1, this); apply(M2, this);
950 template<class M0t, class M1t, class M2t, class M3t>
951 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
952 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
956 template<class M0t, class M1t, class M2t, class M3t, class M4t>
957 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
959 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
964 template<class M0t, class M1t, class M2t, class M3t,
965 class M4t, class M5t>
966 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
967 const M4t &M4, const M5t &M5) {
968 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
969 apply(M4, this); apply(M5, this);
973 template<class M0t, class M1t, class M2t, class M3t,
974 class M4t, class M5t, class M6t>
975 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
976 const M4t &M4, const M5t &M5, const M6t &M6) {
977 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
978 apply(M4, this); apply(M5, this); apply(M6, this);
982 template<class M0t, class M1t, class M2t, class M3t,
983 class M4t, class M5t, class M6t, class M7t>
984 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
985 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
986 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
987 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
992 //===----------------------------------------------------------------------===//
993 // bits_storage class
995 // Default storage class definition: external storage. This implementation
996 // assumes the user will specify a variable to store the data into with the
997 // cl::location(x) modifier.
999 template<class DataType, class StorageClass>
1000 class bits_storage {
1001 unsigned *Location; // Where to store the bits...
1004 static unsigned Bit(const T &V) {
1005 unsigned BitPos = (unsigned)V;
1006 assert(BitPos < sizeof(unsigned) * 8 &&
1007 "enum exceeds width of bit vector!");
1012 bits_storage() : Location(0) {}
1014 bool setLocation(Option &O, unsigned &L) {
1016 return O.error(": cl::location(x) specified more than once!");
1022 void addValue(const T &V) {
1023 assert(Location != 0 && "cl::location(...) not specified for a command "
1024 "line option with external storage!");
1025 *Location |= Bit(V);
1028 unsigned getBits() { return *Location; }
1031 bool isSet(const T &V) {
1032 return (*Location & Bit(V)) != 0;
1037 // Define how to hold bits. Since we can inherit from a class, we do so.
1038 // This makes us exactly compatible with the bits in all cases that it is used.
1040 template<class DataType>
1041 class bits_storage<DataType, bool> {
1042 unsigned Bits; // Where to store the bits...
1045 static unsigned Bit(const T &V) {
1046 unsigned BitPos = (unsigned)V;
1047 assert(BitPos < sizeof(unsigned) * 8 &&
1048 "enum exceeds width of bit vector!");
1054 void addValue(const T &V) {
1058 unsigned getBits() { return Bits; }
1061 bool isSet(const T &V) {
1062 return (Bits & Bit(V)) != 0;
1067 //===----------------------------------------------------------------------===//
1068 // bits - A bit vector of command options.
1070 template <class DataType, class Storage = bool,
1071 class ParserClass = parser<DataType> >
1072 class bits : public Option, public bits_storage<DataType, Storage> {
1073 std::vector<unsigned> Positions;
1076 virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
1079 virtual enum ValueExpected getValueExpectedFlagDefault() const {
1080 return Parser.getValueExpectedFlagDefault();
1083 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
1084 const std::string &Arg) {
1085 typename ParserClass::parser_data_type Val =
1086 typename ParserClass::parser_data_type();
1087 if (Parser.parse(*this, ArgName, Arg, Val))
1088 return true; // Parse Error!
1091 Positions.push_back(pos);
1095 // Forward printing stuff to the parser...
1096 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
1097 virtual void printOptionInfo(unsigned GlobalWidth) const {
1098 Parser.printOptionInfo(*this, GlobalWidth);
1102 addArgument(ArgStr);
1103 Parser.initialize(*this);
1106 ParserClass &getParser() { return Parser; }
1108 unsigned getPosition(unsigned optnum) const {
1109 assert(optnum < this->size() && "Invalid option index");
1110 return Positions[optnum];
1115 bits(const M0t &M0) {
1120 template<class M0t, class M1t>
1121 bits(const M0t &M0, const M1t &M1) {
1122 apply(M0, this); apply(M1, this);
1126 template<class M0t, class M1t, class M2t>
1127 bits(const M0t &M0, const M1t &M1, const M2t &M2) {
1128 apply(M0, this); apply(M1, this); apply(M2, this);
1132 template<class M0t, class M1t, class M2t, class M3t>
1133 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
1134 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1138 template<class M0t, class M1t, class M2t, class M3t, class M4t>
1139 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1141 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1146 template<class M0t, class M1t, class M2t, class M3t,
1147 class M4t, class M5t>
1148 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1149 const M4t &M4, const M5t &M5) {
1150 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1151 apply(M4, this); apply(M5, this);
1155 template<class M0t, class M1t, class M2t, class M3t,
1156 class M4t, class M5t, class M6t>
1157 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1158 const M4t &M4, const M5t &M5, const M6t &M6) {
1159 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1160 apply(M4, this); apply(M5, this); apply(M6, this);
1164 template<class M0t, class M1t, class M2t, class M3t,
1165 class M4t, class M5t, class M6t, class M7t>
1166 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1167 const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
1168 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1169 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1174 //===----------------------------------------------------------------------===//
1175 // Aliased command line option (alias this name to a preexisting name)
1178 class alias : public Option {
1180 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
1181 const std::string &Arg) {
1182 return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
1184 // Aliases default to be hidden...
1185 virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
1187 // Handle printing stuff...
1188 virtual unsigned getOptionWidth() const;
1189 virtual void printOptionInfo(unsigned GlobalWidth) const;
1193 error(": cl::alias must have argument name specified!");
1195 error(": cl::alias must have an cl::aliasopt(option) specified!");
1196 addArgument(ArgStr);
1199 void setAliasFor(Option &O) {
1201 error(": cl::alias must only have one cl::aliasopt(...) specified!");
1207 alias(const M0t &M0) : AliasFor(0) {
1212 template<class M0t, class M1t>
1213 alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
1214 apply(M0, this); apply(M1, this);
1218 template<class M0t, class M1t, class M2t>
1219 alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) {
1220 apply(M0, this); apply(M1, this); apply(M2, this);
1224 template<class M0t, class M1t, class M2t, class M3t>
1225 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1227 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1232 // aliasfor - Modifier to set the option an alias aliases.
1235 aliasopt(Option &O) : Opt(O) {}
1236 void apply(alias &A) const { A.setAliasFor(Opt); }
1239 // extrahelp - provide additional help at the end of the normal help
1240 // output. All occurrences of cl::extrahelp will be accumulated and
1241 // printed to std::cerr at the end of the regular help, just before
1244 const char * morehelp;
1245 extrahelp(const char* help);
1248 // This function just prints the help message, exactly the same way as if the
1249 // --help option had been given on the command line.
1250 // NOTE: THIS FUNCTION TERMINATES THE PROGRAM!
1251 void PrintHelpMessage();
1253 } // End namespace cl
1255 } // End namespace llvm