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"
34 /// cl Namespace - This namespace contains all of the command line option
35 /// processing machinery. It is intentionally a short name to make qualified
39 //===----------------------------------------------------------------------===//
40 // ParseCommandLineOptions - Command line option processing entry point.
42 void ParseCommandLineOptions(int &argc, char **argv,
43 const char *Overview = 0);
45 //===----------------------------------------------------------------------===//
46 // ParseEnvironmentOptions - Environment variable option processing alternate
49 void ParseEnvironmentOptions(const char *progName, const char *envvar,
50 const char *Overview = 0);
52 ///===---------------------------------------------------------------------===//
53 /// SetVersionPrinter - Override the default (LLVM specific) version printer
54 /// used to print out the version when --version is given
55 /// on the command line. This gives other systems using the
56 /// CommandLine utilities to print their own version string.
57 void SetVersionPrinter(void (*func)());
59 //===----------------------------------------------------------------------===//
60 // Flags permitted to be passed to command line arguments
63 enum NumOccurrences { // Flags for the number of occurrences allowed
64 Optional = 0x01, // Zero or One occurrence
65 ZeroOrMore = 0x02, // Zero or more occurrences allowed
66 Required = 0x03, // One occurrence required
67 OneOrMore = 0x04, // One or more occurrences required
69 // ConsumeAfter - Indicates that this option is fed anything that follows the
70 // last positional argument required by the application (it is an error if
71 // there are zero positional arguments, and a ConsumeAfter option is used).
72 // Thus, for example, all arguments to LLI are processed until a filename is
73 // found. Once a filename is found, all of the succeeding arguments are
74 // passed, unprocessed, to the ConsumeAfter option.
78 OccurrencesMask = 0x07
81 enum ValueExpected { // Is a value required for the option?
82 ValueOptional = 0x08, // The value can appear... or not
83 ValueRequired = 0x10, // The value is required to appear!
84 ValueDisallowed = 0x18, // A value may not be specified (for flags)
88 enum OptionHidden { // Control whether -help shows this option
89 NotHidden = 0x20, // Option included in --help & --help-hidden
90 Hidden = 0x40, // -help doesn't, but --help-hidden does
91 ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg
95 // Formatting flags - This controls special features that the option might have
96 // that cause it to be parsed differently...
98 // Prefix - This option allows arguments that are otherwise unrecognized to be
99 // matched by options that are a prefix of the actual value. This is useful for
100 // cases like a linker, where options are typically of the form '-lfoo' or
101 // '-L../../include' where -l or -L are the actual flags. When prefix is
102 // enabled, and used, the value for the flag comes from the suffix of the
105 // Grouping - With this option enabled, multiple letter options are allowed to
106 // bunch together with only a single hyphen for the whole group. This allows
107 // emulation of the behavior that ls uses for example: ls -la === ls -l -a
110 enum FormattingFlags {
111 NormalFormatting = 0x000, // Nothing special
112 Positional = 0x080, // Is a positional argument, no '-' required
113 Prefix = 0x100, // Can this option directly prefix its value?
114 Grouping = 0x180, // Can this option group with other options?
115 FormattingMask = 0x180 // Union of the above flags.
118 enum MiscFlags { // Miscellaneous flags to adjust argument
119 CommaSeparated = 0x200, // Should this cl::list split between commas?
120 PositionalEatsArgs = 0x400, // Should this positional cl::list eat -args?
121 MiscMask = 0x600 // Union of the above flags.
126 //===----------------------------------------------------------------------===//
131 friend void cl::ParseCommandLineOptions(int &, char **, const char *);
134 // handleOccurrences - Overriden by subclasses to handle the value passed into
135 // an argument. Should return true if there was an error processing the
136 // argument and the program should exit.
138 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
139 const std::string &Arg) = 0;
141 virtual enum ValueExpected getValueExpectedFlagDefault() const {
142 return ValueOptional;
144 // Out of line virtual function to provide home for the class.
145 virtual void anchor();
147 int NumOccurrences; // The number of times specified
148 int Flags; // Flags for the argument
149 unsigned Position; // Position of last occurrence of the option
151 const char *ArgStr; // The argument string itself (ex: "help", "o")
152 const char *HelpStr; // The descriptive text message for --help
153 const char *ValueStr; // String describing what the value of this option is
155 inline enum NumOccurrences getNumOccurrencesFlag() const {
156 return static_cast<enum NumOccurrences>(Flags & OccurrencesMask);
158 inline enum ValueExpected getValueExpectedFlag() const {
159 int VE = Flags & ValueMask;
160 return VE ? static_cast<enum ValueExpected>(VE)
161 : getValueExpectedFlagDefault();
163 inline enum OptionHidden getOptionHiddenFlag() const {
164 return static_cast<enum OptionHidden>(Flags & HiddenMask);
166 inline enum FormattingFlags getFormattingFlag() const {
167 return static_cast<enum FormattingFlags>(Flags & FormattingMask);
169 inline unsigned getMiscFlags() const {
170 return Flags & MiscMask;
172 inline unsigned getPosition() const { return Position; }
174 // hasArgStr - Return true if the argstr != ""
175 bool hasArgStr() const { return ArgStr[0] != 0; }
177 //-------------------------------------------------------------------------===
178 // Accessor functions set by OptionModifiers
180 void setArgStr(const char *S) { ArgStr = S; }
181 void setDescription(const char *S) { HelpStr = S; }
182 void setValueStr(const char *S) { ValueStr = S; }
184 void setFlag(unsigned Flag, unsigned FlagMask) {
189 void setNumOccurrencesFlag(enum NumOccurrences Val) {
190 setFlag(Val, OccurrencesMask);
192 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
193 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
194 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
195 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
196 void setPosition(unsigned pos) { Position = pos; }
198 Option(unsigned DefaultFlags)
199 : NumOccurrences(0), Flags(DefaultFlags | NormalFormatting), Position(0),
200 ArgStr(""), HelpStr(""), ValueStr("") {
201 assert(getNumOccurrencesFlag() != 0 &&
202 getOptionHiddenFlag() != 0 && "Not all default flags specified!");
206 // addArgument - Tell the system that this Option subclass will handle all
207 // occurrences of -ArgStr on the command line.
209 void addArgument(const char *ArgStr);
211 // Return the width of the option tag for printing...
212 virtual unsigned getOptionWidth() const = 0;
214 // printOptionInfo - Print out information about this option. The
215 // to-be-maintained width is specified.
217 virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
219 // addOccurrence - Wrapper around handleOccurrence that enforces Flags
221 bool addOccurrence(unsigned pos, const char *ArgName,
222 const std::string &Value);
224 // Prints option name followed by message. Always returns true.
225 bool error(std::string Message, const char *ArgName = 0);
228 inline int getNumOccurrences() const { return NumOccurrences; }
233 //===----------------------------------------------------------------------===//
234 // Command line option modifiers that can be used to modify the behavior of
235 // command line option parsers...
238 // desc - Modifier to set the description shown in the --help output...
241 desc(const char *Str) : Desc(Str) {}
242 void apply(Option &O) const { O.setDescription(Desc); }
245 // value_desc - Modifier to set the value description shown in the --help
249 value_desc(const char *Str) : Desc(Str) {}
250 void apply(Option &O) const { O.setValueStr(Desc); }
253 // init - Specify a default (initial) value for the command line argument, if
254 // the default constructor for the argument type does not give you what you
255 // want. This is only valid on "opt" arguments, not on "list" arguments.
260 initializer(const Ty &Val) : Init(Val) {}
263 void apply(Opt &O) const { O.setInitialValue(Init); }
267 initializer<Ty> init(const Ty &Val) {
268 return initializer<Ty>(Val);
272 // location - Allow the user to specify which external variable they want to
273 // store the results of the command line argument processing into, if they don't
274 // want to store it in the option itself.
277 struct LocationClass {
279 LocationClass(Ty &L) : Loc(L) {}
282 void apply(Opt &O) const { O.setLocation(O, Loc); }
286 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
289 //===----------------------------------------------------------------------===//
290 // Enum valued command line option
292 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, int(ENUMVAL), DESC
293 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, int(ENUMVAL), DESC
294 #define clEnumValEnd (reinterpret_cast<void*>(0))
296 // values - For custom data types, allow specifying a group of values together
297 // as the values that go into the mapping that the option handler uses. Note
298 // that the values list must always have a 0 at the end of the list to indicate
299 // that the list has ended.
301 template<class DataType>
303 // Use a vector instead of a map, because the lists should be short,
304 // the overhead is less, and most importantly, it keeps them in the order
305 // inserted so we can print our option out nicely.
306 std::vector<std::pair<const char *, std::pair<int, const char *> > > Values;
307 void processValues(va_list Vals);
309 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
311 // Insert the first value, which is required.
312 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
314 // Process the varargs portion of the values...
315 while (const char *EnumName = va_arg(ValueArgs, const char *)) {
316 DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int));
317 const char *EnumDesc = va_arg(ValueArgs, const char *);
318 Values.push_back(std::make_pair(EnumName, // Add value to value map
319 std::make_pair(EnumVal, EnumDesc)));
324 void apply(Opt &O) const {
325 for (unsigned i = 0, e = Values.size(); i != e; ++i)
326 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
327 Values[i].second.second);
331 template<class DataType>
332 ValuesClass<DataType> END_WITH_NULL values(const char *Arg, DataType Val,
333 const char *Desc, ...) {
335 va_start(ValueArgs, Desc);
336 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
342 //===----------------------------------------------------------------------===//
343 // parser class - Parameterizable parser for different data types. By default,
344 // known data types (string, int, bool) have specialized parsers, that do what
345 // you would expect. The default parser, used for data types that are not
346 // built-in, uses a mapping table to map specific options to values, which is
347 // used, among other things, to handle enum types.
349 //--------------------------------------------------
350 // generic_parser_base - This class holds all the non-generic code that we do
351 // not need replicated for every instance of the generic parser. This also
352 // allows us to put stuff into CommandLine.cpp
354 struct generic_parser_base {
355 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
357 // getNumOptions - Virtual function implemented by generic subclass to
358 // indicate how many entries are in Values.
360 virtual unsigned getNumOptions() const = 0;
362 // getOption - Return option name N.
363 virtual const char *getOption(unsigned N) const = 0;
365 // getDescription - Return description N
366 virtual const char *getDescription(unsigned N) const = 0;
368 // Return the width of the option tag for printing...
369 virtual unsigned getOptionWidth(const Option &O) const;
371 // printOptionInfo - Print out information about this option. The
372 // to-be-maintained width is specified.
374 virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
376 void initialize(Option &O) {
377 // All of the modifiers for the option have been processed by now, so the
378 // argstr field should be stable, copy it down now.
380 hasArgStr = O.hasArgStr();
382 // If there has been no argstr specified, that means that we need to add an
383 // argument for every possible option. This ensures that our options are
387 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
388 O.addArgument(getOption(i));
391 enum ValueExpected getValueExpectedFlagDefault() const {
392 // If there is an ArgStr specified, then we are of the form:
394 // -opt=O2 or -opt O2 or -optO2
396 // In which case, the value is required. Otherwise if an arg str has not
397 // been specified, we are of the form:
399 // -O2 or O2 or -la (where -l and -a are separate options)
401 // If this is the case, we cannot allow a value.
404 return ValueRequired;
406 return ValueDisallowed;
409 // findOption - Return the option number corresponding to the specified
410 // argument string. If the option is not found, getNumOptions() is returned.
412 unsigned findOption(const char *Name);
418 // Default parser implementation - This implementation depends on having a
419 // mapping of recognized options to values of some sort. In addition to this,
420 // each entry in the mapping also tracks a help message that is printed with the
421 // command line option for --help. Because this is a simple mapping parser, the
422 // data type can be any unsupported type.
424 template <class DataType>
425 class parser : public generic_parser_base {
427 std::vector<std::pair<const char *,
428 std::pair<DataType, const char *> > > Values;
430 typedef DataType parser_data_type;
432 // Implement virtual functions needed by generic_parser_base
433 unsigned getNumOptions() const { return unsigned(Values.size()); }
434 const char *getOption(unsigned N) const { return Values[N].first; }
435 const char *getDescription(unsigned N) const {
436 return Values[N].second.second;
439 // parse - Return true on error.
440 bool parse(Option &O, const char *ArgName, const std::string &Arg,
448 for (unsigned i = 0, e = Values.size(); i != e; ++i)
449 if (ArgVal == Values[i].first) {
450 V = Values[i].second.first;
454 return O.error(": Cannot find option named '" + ArgVal + "'!");
457 // addLiteralOption - Add an entry to the mapping table...
459 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
460 assert(findOption(Name) == Values.size() && "Option already exists!");
461 Values.push_back(std::make_pair(Name,
462 std::make_pair(static_cast<DataType>(V),HelpStr)));
465 // removeLiteralOption - Remove the specified option.
467 void removeLiteralOption(const char *Name) {
468 unsigned N = findOption(Name);
469 assert(N != Values.size() && "Option not found!");
470 Values.erase(Values.begin()+N);
474 //--------------------------------------------------
475 // basic_parser - Super class of parsers to provide boilerplate code
477 struct basic_parser_impl { // non-template implementation of basic_parser<t>
478 virtual ~basic_parser_impl() {}
480 enum ValueExpected getValueExpectedFlagDefault() const {
481 return ValueRequired;
484 void initialize(Option &O) {}
486 // Return the width of the option tag for printing...
487 unsigned getOptionWidth(const Option &O) const;
489 // printOptionInfo - Print out information about this option. The
490 // to-be-maintained width is specified.
492 void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
494 // getValueName - Overload in subclass to provide a better default value.
495 virtual const char *getValueName() const { return "value"; }
497 // An out-of-line virtual method to provide a 'home' for this class.
498 virtual void anchor();
501 // basic_parser - The real basic parser is just a template wrapper that provides
502 // a typedef for the provided data type.
504 template<class DataType>
505 struct basic_parser : public basic_parser_impl {
506 typedef DataType parser_data_type;
509 //--------------------------------------------------
513 class parser<bool> : public basic_parser<bool> {
515 // parse - Return true on error.
516 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
518 enum ValueExpected getValueExpectedFlagDefault() const {
519 return ValueOptional;
522 // getValueName - Do not print =<value> at all.
523 virtual const char *getValueName() const { return 0; }
525 // An out-of-line virtual method to provide a 'home' for this class.
526 virtual void anchor();
529 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<bool>);
532 //--------------------------------------------------
536 class parser<int> : public basic_parser<int> {
538 // parse - Return true on error.
539 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
541 // getValueName - Overload in subclass to provide a better default value.
542 virtual const char *getValueName() const { return "int"; }
544 // An out-of-line virtual method to provide a 'home' for this class.
545 virtual void anchor();
548 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<int>);
551 //--------------------------------------------------
555 class parser<unsigned> : public basic_parser<unsigned> {
557 // parse - Return true on error.
558 bool parse(Option &O, const char *AN, const std::string &Arg, unsigned &Val);
560 // getValueName - Overload in subclass to provide a better default value.
561 virtual const char *getValueName() const { return "uint"; }
563 // An out-of-line virtual method to provide a 'home' for this class.
564 virtual void anchor();
567 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<unsigned>);
569 //--------------------------------------------------
573 class parser<double> : public basic_parser<double> {
575 // parse - Return true on error.
576 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
578 // getValueName - Overload in subclass to provide a better default value.
579 virtual const char *getValueName() const { return "number"; }
581 // An out-of-line virtual method to provide a 'home' for this class.
582 virtual void anchor();
585 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<double>);
587 //--------------------------------------------------
591 class parser<float> : public basic_parser<float> {
593 // parse - Return true on error.
594 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
596 // getValueName - Overload in subclass to provide a better default value.
597 virtual const char *getValueName() const { return "number"; }
599 // An out-of-line virtual method to provide a 'home' for this class.
600 virtual void anchor();
603 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<float>);
605 //--------------------------------------------------
606 // parser<std::string>
609 class parser<std::string> : public basic_parser<std::string> {
611 // parse - Return true on error.
612 bool parse(Option &O, const char *AN, const std::string &Arg,
613 std::string &Value) {
618 // getValueName - Overload in subclass to provide a better default value.
619 virtual const char *getValueName() const { return "string"; }
621 // An out-of-line virtual method to provide a 'home' for this class.
622 virtual void anchor();
625 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<std::string>);
627 //===----------------------------------------------------------------------===//
628 // applicator class - This class is used because we must use partial
629 // specialization to handle literal string arguments specially (const char* does
630 // not correctly respond to the apply method). Because the syntax to use this
631 // is a pain, we have the 'apply' method below to handle the nastiness...
633 template<class Mod> struct applicator {
635 static void opt(const Mod &M, Opt &O) { M.apply(O); }
638 // Handle const char* as a special case...
639 template<unsigned n> struct applicator<char[n]> {
641 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
643 template<unsigned n> struct applicator<const char[n]> {
645 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
647 template<> struct applicator<const char*> {
649 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
652 template<> struct applicator<NumOccurrences> {
653 static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); }
655 template<> struct applicator<ValueExpected> {
656 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
658 template<> struct applicator<OptionHidden> {
659 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
661 template<> struct applicator<FormattingFlags> {
662 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
664 template<> struct applicator<MiscFlags> {
665 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
668 // apply method - Apply a modifier to an option in a type safe way.
669 template<class Mod, class Opt>
670 void apply(const Mod &M, Opt *O) {
671 applicator<Mod>::opt(M, *O);
675 //===----------------------------------------------------------------------===//
678 // Default storage class definition: external storage. This implementation
679 // assumes the user will specify a variable to store the data into with the
680 // cl::location(x) modifier.
682 template<class DataType, bool ExternalStorage, bool isClass>
684 DataType *Location; // Where to store the object...
687 assert(Location != 0 && "cl::location(...) not specified for a command "
688 "line option with external storage, "
689 "or cl::init specified before cl::location()!!");
692 opt_storage() : Location(0) {}
694 bool setLocation(Option &O, DataType &L) {
696 return O.error(": cl::location(x) specified more than once!");
702 void setValue(const T &V) {
707 DataType &getValue() { check(); return *Location; }
708 const DataType &getValue() const { check(); return *Location; }
712 // Define how to hold a class type object, such as a string. Since we can
713 // inherit from a class, we do so. This makes us exactly compatible with the
714 // object in all cases that it is used.
716 template<class DataType>
717 class opt_storage<DataType,false,true> : public DataType {
720 void setValue(const T &V) { DataType::operator=(V); }
722 DataType &getValue() { return *this; }
723 const DataType &getValue() const { return *this; }
726 // Define a partial specialization to handle things we cannot inherit from. In
727 // this case, we store an instance through containment, and overload operators
728 // to get at the value.
730 template<class DataType>
731 class opt_storage<DataType, false, false> {
735 // Make sure we initialize the value with the default constructor for the
737 opt_storage() : Value(DataType()) {}
740 void setValue(const T &V) { Value = V; }
741 DataType &getValue() { return Value; }
742 DataType getValue() const { return Value; }
744 // If the datatype is a pointer, support -> on it.
745 DataType operator->() const { return Value; }
749 //===----------------------------------------------------------------------===//
750 // opt - A scalar command line option.
752 template <class DataType, bool ExternalStorage = false,
753 class ParserClass = parser<DataType> >
754 class opt : public Option,
755 public opt_storage<DataType, ExternalStorage,
756 is_class<DataType>::value> {
759 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
760 const std::string &Arg) {
761 typename ParserClass::parser_data_type Val =
762 typename ParserClass::parser_data_type();
763 if (Parser.parse(*this, ArgName, Arg, Val))
764 return true; // Parse error!
770 virtual enum ValueExpected getValueExpectedFlagDefault() const {
771 return Parser.getValueExpectedFlagDefault();
774 // Forward printing stuff to the parser...
775 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
776 virtual void printOptionInfo(unsigned GlobalWidth) const {
777 Parser.printOptionInfo(*this, GlobalWidth);
782 Parser.initialize(*this);
785 // setInitialValue - Used by the cl::init modifier...
786 void setInitialValue(const DataType &V) { this->setValue(V); }
788 ParserClass &getParser() { return Parser; }
790 operator DataType() const { return this->getValue(); }
793 DataType &operator=(const T &Val) {
795 return this->getValue();
800 opt(const M0t &M0) : Option(Optional | NotHidden) {
806 template<class M0t, class M1t>
807 opt(const M0t &M0, const M1t &M1) : Option(Optional | NotHidden) {
808 apply(M0, this); apply(M1, this);
813 template<class M0t, class M1t, class M2t>
814 opt(const M0t &M0, const M1t &M1,
815 const M2t &M2) : Option(Optional | NotHidden) {
816 apply(M0, this); apply(M1, this); apply(M2, this);
820 template<class M0t, class M1t, class M2t, class M3t>
821 opt(const M0t &M0, const M1t &M1, const M2t &M2,
822 const M3t &M3) : Option(Optional | NotHidden) {
823 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
827 template<class M0t, class M1t, class M2t, class M3t, class M4t>
828 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
829 const M4t &M4) : Option(Optional | NotHidden) {
830 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
835 template<class M0t, class M1t, class M2t, class M3t,
836 class M4t, class M5t>
837 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
838 const M4t &M4, const M5t &M5) : Option(Optional | NotHidden) {
839 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
840 apply(M4, this); apply(M5, this);
844 template<class M0t, class M1t, class M2t, class M3t,
845 class M4t, class M5t, class M6t>
846 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
847 const M4t &M4, const M5t &M5,
848 const M6t &M6) : Option(Optional | NotHidden) {
849 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
850 apply(M4, this); apply(M5, this); apply(M6, this);
854 template<class M0t, class M1t, class M2t, class M3t,
855 class M4t, class M5t, class M6t, class M7t>
856 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
857 const M4t &M4, const M5t &M5, const M6t &M6,
858 const M7t &M7) : Option(Optional | NotHidden) {
859 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
860 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
865 EXTERN_TEMPLATE_INSTANTIATION(class opt<unsigned>);
866 EXTERN_TEMPLATE_INSTANTIATION(class opt<int>);
867 EXTERN_TEMPLATE_INSTANTIATION(class opt<std::string>);
868 EXTERN_TEMPLATE_INSTANTIATION(class opt<bool>);
870 //===----------------------------------------------------------------------===//
871 // list_storage class
873 // Default storage class definition: external storage. This implementation
874 // assumes the user will specify a variable to store the data into with the
875 // cl::location(x) modifier.
877 template<class DataType, class StorageClass>
879 StorageClass *Location; // Where to store the object...
882 list_storage() : Location(0) {}
884 bool setLocation(Option &O, StorageClass &L) {
886 return O.error(": cl::location(x) specified more than once!");
892 void addValue(const T &V) {
893 assert(Location != 0 && "cl::location(...) not specified for a command "
894 "line option with external storage!");
895 Location->push_back(V);
900 // Define how to hold a class type object, such as a string. Since we can
901 // inherit from a class, we do so. This makes us exactly compatible with the
902 // object in all cases that it is used.
904 template<class DataType>
905 class list_storage<DataType, bool> : public std::vector<DataType> {
908 void addValue(const T &V) { push_back(V); }
912 //===----------------------------------------------------------------------===//
913 // list - A list of command line options.
915 template <class DataType, class Storage = bool,
916 class ParserClass = parser<DataType> >
917 class list : public Option, public list_storage<DataType, Storage> {
918 std::vector<unsigned> Positions;
921 virtual enum ValueExpected getValueExpectedFlagDefault() const {
922 return Parser.getValueExpectedFlagDefault();
925 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
926 const std::string &Arg) {
927 typename ParserClass::parser_data_type Val =
928 typename ParserClass::parser_data_type();
929 if (Parser.parse(*this, ArgName, Arg, Val))
930 return true; // Parse Error!
933 Positions.push_back(pos);
937 // Forward printing stuff to the parser...
938 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
939 virtual void printOptionInfo(unsigned GlobalWidth) const {
940 Parser.printOptionInfo(*this, GlobalWidth);
945 Parser.initialize(*this);
948 ParserClass &getParser() { return Parser; }
950 unsigned getPosition(unsigned optnum) const {
951 assert(optnum < this->size() && "Invalid option index");
952 return Positions[optnum];
957 list(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
962 template<class M0t, class M1t>
963 list(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
964 apply(M0, this); apply(M1, this);
968 template<class M0t, class M1t, class M2t>
969 list(const M0t &M0, const M1t &M1, const M2t &M2)
970 : Option(ZeroOrMore | NotHidden) {
971 apply(M0, this); apply(M1, this); apply(M2, this);
975 template<class M0t, class M1t, class M2t, class M3t>
976 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
977 : Option(ZeroOrMore | NotHidden) {
978 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
982 template<class M0t, class M1t, class M2t, class M3t, class M4t>
983 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
984 const M4t &M4) : Option(ZeroOrMore | NotHidden) {
985 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
990 template<class M0t, class M1t, class M2t, class M3t,
991 class M4t, class M5t>
992 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
993 const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
994 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
995 apply(M4, this); apply(M5, this);
999 template<class M0t, class M1t, class M2t, class M3t,
1000 class M4t, class M5t, class M6t>
1001 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1002 const M4t &M4, const M5t &M5, const M6t &M6)
1003 : Option(ZeroOrMore | NotHidden) {
1004 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1005 apply(M4, this); apply(M5, this); apply(M6, this);
1009 template<class M0t, class M1t, class M2t, class M3t,
1010 class M4t, class M5t, class M6t, class M7t>
1011 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1012 const M4t &M4, const M5t &M5, const M6t &M6,
1013 const M7t &M7) : Option(ZeroOrMore | NotHidden) {
1014 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1015 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1020 //===----------------------------------------------------------------------===//
1021 // bits_storage class
1023 // Default storage class definition: external storage. This implementation
1024 // assumes the user will specify a variable to store the data into with the
1025 // cl::location(x) modifier.
1027 template<class DataType, class StorageClass>
1028 class bits_storage {
1029 unsigned *Location; // Where to store the bits...
1032 static unsigned Bit(const T &V) {
1033 unsigned BitPos = reinterpret_cast<unsigned>(V);
1034 assert(BitPos < sizeof(unsigned) * 8 &&
1035 "enum exceeds width of bit vector!");
1040 bits_storage() : Location(0) {}
1042 bool setLocation(Option &O, unsigned &L) {
1044 return O.error(": cl::location(x) specified more than once!");
1050 void addValue(const T &V) {
1051 assert(Location != 0 && "cl::location(...) not specified for a command "
1052 "line option with external storage!");
1053 *Location |= Bit(V);
1056 unsigned getBits() { return *Location; }
1059 bool isSet(const T &V) {
1060 return (*Location & Bit(V)) != 0;
1065 // Define how to hold bits. Since we can inherit from a class, we do so.
1066 // This makes us exactly compatible with the bits in all cases that it is used.
1068 template<class DataType>
1069 class bits_storage<DataType, bool> {
1070 unsigned Bits; // Where to store the bits...
1073 static unsigned Bit(const T &V) {
1074 unsigned BitPos = reinterpret_cast<unsigned>(V);
1075 assert(BitPos < sizeof(unsigned) * 8 &&
1076 "enum exceeds width of bit vector!");
1082 void addValue(const T &V) {
1086 unsigned getBits() { return Bits; }
1089 bool isSet(const T &V) {
1090 return (Bits & Bit(V)) != 0;
1095 //===----------------------------------------------------------------------===//
1096 // bits - A bit vector of command options.
1098 template <class DataType, class Storage = bool,
1099 class ParserClass = parser<DataType> >
1100 class bits : public Option, public bits_storage<DataType, Storage> {
1101 std::vector<unsigned> Positions;
1104 virtual enum ValueExpected getValueExpectedFlagDefault() const {
1105 return Parser.getValueExpectedFlagDefault();
1108 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
1109 const std::string &Arg) {
1110 typename ParserClass::parser_data_type Val =
1111 typename ParserClass::parser_data_type();
1112 if (Parser.parse(*this, ArgName, Arg, Val))
1113 return true; // Parse Error!
1116 Positions.push_back(pos);
1120 // Forward printing stuff to the parser...
1121 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
1122 virtual void printOptionInfo(unsigned GlobalWidth) const {
1123 Parser.printOptionInfo(*this, GlobalWidth);
1127 addArgument(ArgStr);
1128 Parser.initialize(*this);
1131 ParserClass &getParser() { return Parser; }
1133 unsigned getPosition(unsigned optnum) const {
1134 assert(optnum < this->size() && "Invalid option index");
1135 return Positions[optnum];
1140 bits(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
1145 template<class M0t, class M1t>
1146 bits(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
1147 apply(M0, this); apply(M1, this);
1151 template<class M0t, class M1t, class M2t>
1152 bits(const M0t &M0, const M1t &M1, const M2t &M2)
1153 : Option(ZeroOrMore | NotHidden) {
1154 apply(M0, this); apply(M1, this); apply(M2, this);
1158 template<class M0t, class M1t, class M2t, class M3t>
1159 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1160 : Option(ZeroOrMore | NotHidden) {
1161 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1165 template<class M0t, class M1t, class M2t, class M3t, class M4t>
1166 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1167 const M4t &M4) : Option(ZeroOrMore | NotHidden) {
1168 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1173 template<class M0t, class M1t, class M2t, class M3t,
1174 class M4t, class M5t>
1175 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1176 const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
1177 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1178 apply(M4, this); apply(M5, this);
1182 template<class M0t, class M1t, class M2t, class M3t,
1183 class M4t, class M5t, class M6t>
1184 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1185 const M4t &M4, const M5t &M5, const M6t &M6)
1186 : Option(ZeroOrMore | NotHidden) {
1187 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1188 apply(M4, this); apply(M5, this); apply(M6, this);
1192 template<class M0t, class M1t, class M2t, class M3t,
1193 class M4t, class M5t, class M6t, class M7t>
1194 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1195 const M4t &M4, const M5t &M5, const M6t &M6,
1196 const M7t &M7) : Option(ZeroOrMore | NotHidden) {
1197 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1198 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1203 //===----------------------------------------------------------------------===//
1204 // Aliased command line option (alias this name to a preexisting name)
1207 class alias : public Option {
1209 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
1210 const std::string &Arg) {
1211 return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
1213 // Handle printing stuff...
1214 virtual unsigned getOptionWidth() const;
1215 virtual void printOptionInfo(unsigned GlobalWidth) const;
1219 error(": cl::alias must have argument name specified!");
1221 error(": cl::alias must have an cl::aliasopt(option) specified!");
1222 addArgument(ArgStr);
1225 void setAliasFor(Option &O) {
1227 error(": cl::alias must only have one cl::aliasopt(...) specified!");
1233 alias(const M0t &M0) : Option(Optional | Hidden), AliasFor(0) {
1238 template<class M0t, class M1t>
1239 alias(const M0t &M0, const M1t &M1) : Option(Optional | Hidden), AliasFor(0) {
1240 apply(M0, this); apply(M1, this);
1244 template<class M0t, class M1t, class M2t>
1245 alias(const M0t &M0, const M1t &M1, const M2t &M2)
1246 : Option(Optional | Hidden), AliasFor(0) {
1247 apply(M0, this); apply(M1, this); apply(M2, this);
1251 template<class M0t, class M1t, class M2t, class M3t>
1252 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1253 : Option(Optional | Hidden), AliasFor(0) {
1254 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1259 // aliasfor - Modifier to set the option an alias aliases.
1262 aliasopt(Option &O) : Opt(O) {}
1263 void apply(alias &A) const { A.setAliasFor(Opt); }
1266 // extrahelp - provide additional help at the end of the normal help
1267 // output. All occurrences of cl::extrahelp will be accumulated and
1268 // printed to std::cerr at the end of the regular help, just before
1271 const char * morehelp;
1272 extrahelp(const char* help);
1275 void PrintVersionMessage();
1276 // This function just prints the help message, exactly the same way as if the
1277 // --help option had been given on the command line.
1278 // NOTE: THIS FUNCTION TERMINATES THE PROGRAM!
1279 void PrintHelpMessage();
1281 } // End namespace cl
1283 } // End namespace llvm