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 virtual enum OptionHidden getOptionHiddenFlagDefault() const {
147 virtual enum FormattingFlags getFormattingFlagDefault() const {
148 return NormalFormatting;
151 // Out of line virtual function to provide home for the class.
152 virtual void anchor();
154 int NumOccurrences; // The number of times specified
155 int Flags; // Flags for the argument
156 unsigned Position; // Position of last occurrence of the option
158 const char *ArgStr; // The argument string itself (ex: "help", "o")
159 const char *HelpStr; // The descriptive text message for --help
160 const char *ValueStr; // String describing what the value of this option is
162 inline enum NumOccurrences getNumOccurrencesFlag() const {
163 return (enum NumOccurrences)(Flags & OccurrencesMask);
165 inline enum ValueExpected getValueExpectedFlag() const {
166 int VE = Flags & ValueMask;
167 return VE ? static_cast<enum ValueExpected>(VE)
168 : getValueExpectedFlagDefault();
170 inline enum OptionHidden getOptionHiddenFlag() const {
171 int OH = Flags & HiddenMask;
172 return OH ? static_cast<enum OptionHidden>(OH)
173 : getOptionHiddenFlagDefault();
175 inline enum FormattingFlags getFormattingFlag() const {
176 int OH = Flags & FormattingMask;
177 return OH ? static_cast<enum FormattingFlags>(OH)
178 : getFormattingFlagDefault();
180 inline unsigned getMiscFlags() const {
181 return Flags & MiscMask;
183 inline unsigned getPosition() const { return Position; }
185 // hasArgStr - Return true if the argstr != ""
186 bool hasArgStr() const { return ArgStr[0] != 0; }
188 //-------------------------------------------------------------------------===
189 // Accessor functions set by OptionModifiers
191 void setArgStr(const char *S) { ArgStr = S; }
192 void setDescription(const char *S) { HelpStr = S; }
193 void setValueStr(const char *S) { ValueStr = S; }
195 void setFlag(unsigned Flag, unsigned FlagMask) {
200 void setNumOccurrencesFlag(enum NumOccurrences Val) {
201 setFlag(Val, OccurrencesMask);
203 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
204 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
205 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
206 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
207 void setPosition(unsigned pos) { Position = pos; }
209 Option(enum NumOccurrences DefaultOccFlag)
210 : NumOccurrences(0), Flags(DefaultOccFlag), Position(0),
211 ArgStr(""), HelpStr(""), ValueStr("") {}
214 // addArgument - Tell the system that this Option subclass will handle all
215 // occurrences of -ArgStr on the command line.
217 void addArgument(const char *ArgStr);
219 // Return the width of the option tag for printing...
220 virtual unsigned getOptionWidth() const = 0;
222 // printOptionInfo - Print out information about this option. The
223 // to-be-maintained width is specified.
225 virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
227 // addOccurrence - Wrapper around handleOccurrence that enforces Flags
229 bool addOccurrence(unsigned pos, const char *ArgName,
230 const std::string &Value);
232 // Prints option name followed by message. Always returns true.
233 bool error(std::string Message, const char *ArgName = 0);
236 inline int getNumOccurrences() const { return NumOccurrences; }
241 //===----------------------------------------------------------------------===//
242 // Command line option modifiers that can be used to modify the behavior of
243 // command line option parsers...
246 // desc - Modifier to set the description shown in the --help output...
249 desc(const char *Str) : Desc(Str) {}
250 void apply(Option &O) const { O.setDescription(Desc); }
253 // value_desc - Modifier to set the value description shown in the --help
257 value_desc(const char *Str) : Desc(Str) {}
258 void apply(Option &O) const { O.setValueStr(Desc); }
261 // init - Specify a default (initial) value for the command line argument, if
262 // the default constructor for the argument type does not give you what you
263 // want. This is only valid on "opt" arguments, not on "list" arguments.
268 initializer(const Ty &Val) : Init(Val) {}
271 void apply(Opt &O) const { O.setInitialValue(Init); }
275 initializer<Ty> init(const Ty &Val) {
276 return initializer<Ty>(Val);
280 // location - Allow the user to specify which external variable they want to
281 // store the results of the command line argument processing into, if they don't
282 // want to store it in the option itself.
285 struct LocationClass {
287 LocationClass(Ty &L) : Loc(L) {}
290 void apply(Opt &O) const { O.setLocation(O, Loc); }
294 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
297 //===----------------------------------------------------------------------===//
298 // Enum valued command line option
300 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, int(ENUMVAL), DESC
301 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, int(ENUMVAL), DESC
302 #define clEnumValEnd (reinterpret_cast<void*>(0))
304 // values - For custom data types, allow specifying a group of values together
305 // as the values that go into the mapping that the option handler uses. Note
306 // that the values list must always have a 0 at the end of the list to indicate
307 // that the list has ended.
309 template<class DataType>
311 // Use a vector instead of a map, because the lists should be short,
312 // the overhead is less, and most importantly, it keeps them in the order
313 // inserted so we can print our option out nicely.
314 std::vector<std::pair<const char *, std::pair<int, const char *> > > Values;
315 void processValues(va_list Vals);
317 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
319 // Insert the first value, which is required.
320 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
322 // Process the varargs portion of the values...
323 while (const char *EnumName = va_arg(ValueArgs, const char *)) {
324 DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int));
325 const char *EnumDesc = va_arg(ValueArgs, const char *);
326 Values.push_back(std::make_pair(EnumName, // Add value to value map
327 std::make_pair(EnumVal, EnumDesc)));
332 void apply(Opt &O) const {
333 for (unsigned i = 0, e = Values.size(); i != e; ++i)
334 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
335 Values[i].second.second);
339 template<class DataType>
340 ValuesClass<DataType> END_WITH_NULL values(const char *Arg, DataType Val,
341 const char *Desc, ...) {
343 va_start(ValueArgs, Desc);
344 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
350 //===----------------------------------------------------------------------===//
351 // parser class - Parameterizable parser for different data types. By default,
352 // known data types (string, int, bool) have specialized parsers, that do what
353 // you would expect. The default parser, used for data types that are not
354 // built-in, uses a mapping table to map specific options to values, which is
355 // used, among other things, to handle enum types.
357 //--------------------------------------------------
358 // generic_parser_base - This class holds all the non-generic code that we do
359 // not need replicated for every instance of the generic parser. This also
360 // allows us to put stuff into CommandLine.cpp
362 struct generic_parser_base {
363 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
365 // getNumOptions - Virtual function implemented by generic subclass to
366 // indicate how many entries are in Values.
368 virtual unsigned getNumOptions() const = 0;
370 // getOption - Return option name N.
371 virtual const char *getOption(unsigned N) const = 0;
373 // getDescription - Return description N
374 virtual const char *getDescription(unsigned N) const = 0;
376 // Return the width of the option tag for printing...
377 virtual unsigned getOptionWidth(const Option &O) const;
379 // printOptionInfo - Print out information about this option. The
380 // to-be-maintained width is specified.
382 virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
384 void initialize(Option &O) {
385 // All of the modifiers for the option have been processed by now, so the
386 // argstr field should be stable, copy it down now.
388 hasArgStr = O.hasArgStr();
390 // If there has been no argstr specified, that means that we need to add an
391 // argument for every possible option. This ensures that our options are
395 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
396 O.addArgument(getOption(i));
399 enum ValueExpected getValueExpectedFlagDefault() const {
400 // If there is an ArgStr specified, then we are of the form:
402 // -opt=O2 or -opt O2 or -optO2
404 // In which case, the value is required. Otherwise if an arg str has not
405 // been specified, we are of the form:
407 // -O2 or O2 or -la (where -l and -a are separate options)
409 // If this is the case, we cannot allow a value.
412 return ValueRequired;
414 return ValueDisallowed;
417 // findOption - Return the option number corresponding to the specified
418 // argument string. If the option is not found, getNumOptions() is returned.
420 unsigned findOption(const char *Name);
426 // Default parser implementation - This implementation depends on having a
427 // mapping of recognized options to values of some sort. In addition to this,
428 // each entry in the mapping also tracks a help message that is printed with the
429 // command line option for --help. Because this is a simple mapping parser, the
430 // data type can be any unsupported type.
432 template <class DataType>
433 class parser : public generic_parser_base {
435 std::vector<std::pair<const char *,
436 std::pair<DataType, const char *> > > Values;
438 typedef DataType parser_data_type;
440 // Implement virtual functions needed by generic_parser_base
441 unsigned getNumOptions() const { return unsigned(Values.size()); }
442 const char *getOption(unsigned N) const { return Values[N].first; }
443 const char *getDescription(unsigned N) const {
444 return Values[N].second.second;
447 // parse - Return true on error.
448 bool parse(Option &O, const char *ArgName, const std::string &Arg,
456 for (unsigned i = 0, e = Values.size(); i != e; ++i)
457 if (ArgVal == Values[i].first) {
458 V = Values[i].second.first;
462 return O.error(": Cannot find option named '" + ArgVal + "'!");
465 // addLiteralOption - Add an entry to the mapping table...
467 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
468 assert(findOption(Name) == Values.size() && "Option already exists!");
469 Values.push_back(std::make_pair(Name,
470 std::make_pair(static_cast<DataType>(V),HelpStr)));
473 // removeLiteralOption - Remove the specified option.
475 void removeLiteralOption(const char *Name) {
476 unsigned N = findOption(Name);
477 assert(N != Values.size() && "Option not found!");
478 Values.erase(Values.begin()+N);
482 //--------------------------------------------------
483 // basic_parser - Super class of parsers to provide boilerplate code
485 struct basic_parser_impl { // non-template implementation of basic_parser<t>
486 virtual ~basic_parser_impl() {}
488 enum ValueExpected getValueExpectedFlagDefault() const {
489 return ValueRequired;
492 void initialize(Option &O) {}
494 // Return the width of the option tag for printing...
495 unsigned getOptionWidth(const Option &O) const;
497 // printOptionInfo - Print out information about this option. The
498 // to-be-maintained width is specified.
500 void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
502 // getValueName - Overload in subclass to provide a better default value.
503 virtual const char *getValueName() const { return "value"; }
505 // An out-of-line virtual method to provide a 'home' for this class.
506 virtual void anchor();
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;
517 //--------------------------------------------------
521 class parser<bool> : public basic_parser<bool> {
523 // parse - Return true on error.
524 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
526 enum ValueExpected getValueExpectedFlagDefault() const {
527 return ValueOptional;
530 // getValueName - Do not print =<value> at all.
531 virtual const char *getValueName() const { return 0; }
533 // An out-of-line virtual method to provide a 'home' for this class.
534 virtual void anchor();
537 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<bool>);
540 //--------------------------------------------------
544 class parser<int> : public basic_parser<int> {
546 // parse - Return true on error.
547 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
549 // getValueName - Overload in subclass to provide a better default value.
550 virtual const char *getValueName() const { return "int"; }
552 // An out-of-line virtual method to provide a 'home' for this class.
553 virtual void anchor();
556 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<int>);
559 //--------------------------------------------------
563 class parser<unsigned> : public basic_parser<unsigned> {
565 // parse - Return true on error.
566 bool parse(Option &O, const char *AN, const std::string &Arg, unsigned &Val);
568 // getValueName - Overload in subclass to provide a better default value.
569 virtual const char *getValueName() const { return "uint"; }
571 // An out-of-line virtual method to provide a 'home' for this class.
572 virtual void anchor();
575 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<unsigned>);
577 //--------------------------------------------------
581 class parser<double> : public basic_parser<double> {
583 // parse - Return true on error.
584 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
586 // getValueName - Overload in subclass to provide a better default value.
587 virtual const char *getValueName() const { return "number"; }
589 // An out-of-line virtual method to provide a 'home' for this class.
590 virtual void anchor();
593 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<double>);
595 //--------------------------------------------------
599 class parser<float> : public basic_parser<float> {
601 // parse - Return true on error.
602 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
604 // getValueName - Overload in subclass to provide a better default value.
605 virtual const char *getValueName() const { return "number"; }
607 // An out-of-line virtual method to provide a 'home' for this class.
608 virtual void anchor();
611 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<float>);
613 //--------------------------------------------------
614 // parser<std::string>
617 class parser<std::string> : public basic_parser<std::string> {
619 // parse - Return true on error.
620 bool parse(Option &O, const char *AN, const std::string &Arg,
621 std::string &Value) {
626 // getValueName - Overload in subclass to provide a better default value.
627 virtual const char *getValueName() const { return "string"; }
629 // An out-of-line virtual method to provide a 'home' for this class.
630 virtual void anchor();
633 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<std::string>);
635 //===----------------------------------------------------------------------===//
636 // applicator class - This class is used because we must use partial
637 // specialization to handle literal string arguments specially (const char* does
638 // not correctly respond to the apply method). Because the syntax to use this
639 // is a pain, we have the 'apply' method below to handle the nastiness...
641 template<class Mod> struct applicator {
643 static void opt(const Mod &M, Opt &O) { M.apply(O); }
646 // Handle const char* as a special case...
647 template<unsigned n> struct applicator<char[n]> {
649 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
651 template<unsigned n> struct applicator<const char[n]> {
653 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
655 template<> struct applicator<const char*> {
657 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
660 template<> struct applicator<NumOccurrences> {
661 static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); }
663 template<> struct applicator<ValueExpected> {
664 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
666 template<> struct applicator<OptionHidden> {
667 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
669 template<> struct applicator<FormattingFlags> {
670 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
672 template<> struct applicator<MiscFlags> {
673 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
676 // apply method - Apply a modifier to an option in a type safe way.
677 template<class Mod, class Opt>
678 void apply(const Mod &M, Opt *O) {
679 applicator<Mod>::opt(M, *O);
683 //===----------------------------------------------------------------------===//
686 // Default storage class definition: external storage. This implementation
687 // assumes the user will specify a variable to store the data into with the
688 // cl::location(x) modifier.
690 template<class DataType, bool ExternalStorage, bool isClass>
692 DataType *Location; // Where to store the object...
695 assert(Location != 0 && "cl::location(...) not specified for a command "
696 "line option with external storage, "
697 "or cl::init specified before cl::location()!!");
700 opt_storage() : Location(0) {}
702 bool setLocation(Option &O, DataType &L) {
704 return O.error(": cl::location(x) specified more than once!");
710 void setValue(const T &V) {
715 DataType &getValue() { check(); return *Location; }
716 const DataType &getValue() const { check(); return *Location; }
720 // Define how to hold a class type object, such as a string. Since we can
721 // inherit from a class, we do so. This makes us exactly compatible with the
722 // object in all cases that it is used.
724 template<class DataType>
725 class opt_storage<DataType,false,true> : public DataType {
728 void setValue(const T &V) { DataType::operator=(V); }
730 DataType &getValue() { return *this; }
731 const DataType &getValue() const { return *this; }
734 // Define a partial specialization to handle things we cannot inherit from. In
735 // this case, we store an instance through containment, and overload operators
736 // to get at the value.
738 template<class DataType>
739 class opt_storage<DataType, false, false> {
743 // Make sure we initialize the value with the default constructor for the
745 opt_storage() : Value(DataType()) {}
748 void setValue(const T &V) { Value = V; }
749 DataType &getValue() { return Value; }
750 DataType getValue() const { return Value; }
752 // If the datatype is a pointer, support -> on it.
753 DataType operator->() const { return Value; }
757 //===----------------------------------------------------------------------===//
758 // opt - A scalar command line option.
760 template <class DataType, bool ExternalStorage = false,
761 class ParserClass = parser<DataType> >
762 class opt : public Option,
763 public opt_storage<DataType, ExternalStorage,
764 is_class<DataType>::value> {
767 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
768 const std::string &Arg) {
769 typename ParserClass::parser_data_type Val =
770 typename ParserClass::parser_data_type();
771 if (Parser.parse(*this, ArgName, Arg, Val))
772 return true; // Parse error!
778 virtual enum ValueExpected getValueExpectedFlagDefault() const {
779 return Parser.getValueExpectedFlagDefault();
782 // Forward printing stuff to the parser...
783 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
784 virtual void printOptionInfo(unsigned GlobalWidth) const {
785 Parser.printOptionInfo(*this, GlobalWidth);
790 Parser.initialize(*this);
793 // setInitialValue - Used by the cl::init modifier...
794 void setInitialValue(const DataType &V) { this->setValue(V); }
796 ParserClass &getParser() { return Parser; }
798 operator DataType() const { return this->getValue(); }
801 DataType &operator=(const T &Val) {
803 return this->getValue();
808 opt(const M0t &M0) : Option(Optional) {
814 template<class M0t, class M1t>
815 opt(const M0t &M0, const M1t &M1) : Option(Optional) {
816 apply(M0, this); apply(M1, this);
821 template<class M0t, class M1t, class M2t>
822 opt(const M0t &M0, const M1t &M1, const M2t &M2) : Option(Optional) {
823 apply(M0, this); apply(M1, this); apply(M2, this);
827 template<class M0t, class M1t, class M2t, class M3t>
828 opt(const M0t &M0, const M1t &M1, const M2t &M2,
829 const M3t &M3) : Option(Optional) {
830 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
834 template<class M0t, class M1t, class M2t, class M3t, class M4t>
835 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
836 const M4t &M4) : Option(Optional) {
837 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
842 template<class M0t, class M1t, class M2t, class M3t,
843 class M4t, class M5t>
844 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
845 const M4t &M4, const M5t &M5) : Option(Optional) {
846 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
847 apply(M4, this); apply(M5, this);
851 template<class M0t, class M1t, class M2t, class M3t,
852 class M4t, class M5t, class M6t>
853 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
854 const M4t &M4, const M5t &M5, const M6t &M6) : Option(Optional) {
855 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
856 apply(M4, this); apply(M5, this); apply(M6, this);
860 template<class M0t, class M1t, class M2t, class M3t,
861 class M4t, class M5t, class M6t, class M7t>
862 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
863 const M4t &M4, const M5t &M5, const M6t &M6,
864 const M7t &M7) : Option(Optional) {
865 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
866 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
871 EXTERN_TEMPLATE_INSTANTIATION(class opt<unsigned>);
872 EXTERN_TEMPLATE_INSTANTIATION(class opt<int>);
873 EXTERN_TEMPLATE_INSTANTIATION(class opt<std::string>);
874 EXTERN_TEMPLATE_INSTANTIATION(class opt<bool>);
876 //===----------------------------------------------------------------------===//
877 // list_storage class
879 // Default storage class definition: external storage. This implementation
880 // assumes the user will specify a variable to store the data into with the
881 // cl::location(x) modifier.
883 template<class DataType, class StorageClass>
885 StorageClass *Location; // Where to store the object...
888 list_storage() : Location(0) {}
890 bool setLocation(Option &O, StorageClass &L) {
892 return O.error(": cl::location(x) specified more than once!");
898 void addValue(const T &V) {
899 assert(Location != 0 && "cl::location(...) not specified for a command "
900 "line option with external storage!");
901 Location->push_back(V);
906 // Define how to hold a class type object, such as a string. Since we can
907 // inherit from a class, we do so. This makes us exactly compatible with the
908 // object in all cases that it is used.
910 template<class DataType>
911 class list_storage<DataType, bool> : public std::vector<DataType> {
914 void addValue(const T &V) { push_back(V); }
918 //===----------------------------------------------------------------------===//
919 // list - A list of command line options.
921 template <class DataType, class Storage = bool,
922 class ParserClass = parser<DataType> >
923 class list : public Option, public list_storage<DataType, Storage> {
924 std::vector<unsigned> Positions;
927 virtual enum ValueExpected getValueExpectedFlagDefault() const {
928 return Parser.getValueExpectedFlagDefault();
931 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
932 const std::string &Arg) {
933 typename ParserClass::parser_data_type Val =
934 typename ParserClass::parser_data_type();
935 if (Parser.parse(*this, ArgName, Arg, Val))
936 return true; // Parse Error!
939 Positions.push_back(pos);
943 // Forward printing stuff to the parser...
944 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
945 virtual void printOptionInfo(unsigned GlobalWidth) const {
946 Parser.printOptionInfo(*this, GlobalWidth);
951 Parser.initialize(*this);
954 ParserClass &getParser() { return Parser; }
956 unsigned getPosition(unsigned optnum) const {
957 assert(optnum < this->size() && "Invalid option index");
958 return Positions[optnum];
963 list(const M0t &M0) : Option(ZeroOrMore) {
968 template<class M0t, class M1t>
969 list(const M0t &M0, const M1t &M1) : Option(ZeroOrMore) {
970 apply(M0, this); apply(M1, this);
974 template<class M0t, class M1t, class M2t>
975 list(const M0t &M0, const M1t &M1, const M2t &M2) : Option(ZeroOrMore) {
976 apply(M0, this); apply(M1, this); apply(M2, this);
980 template<class M0t, class M1t, class M2t, class M3t>
981 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
982 : Option(ZeroOrMore) {
983 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
987 template<class M0t, class M1t, class M2t, class M3t, class M4t>
988 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
989 const M4t &M4) : Option(ZeroOrMore) {
990 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
995 template<class M0t, class M1t, class M2t, class M3t,
996 class M4t, class M5t>
997 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
998 const M4t &M4, const M5t &M5) : Option(ZeroOrMore) {
999 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1000 apply(M4, this); apply(M5, this);
1004 template<class M0t, class M1t, class M2t, class M3t,
1005 class M4t, class M5t, class M6t>
1006 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1007 const M4t &M4, const M5t &M5, const M6t &M6) : Option(ZeroOrMore) {
1008 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1009 apply(M4, this); apply(M5, this); apply(M6, this);
1013 template<class M0t, class M1t, class M2t, class M3t,
1014 class M4t, class M5t, class M6t, class M7t>
1015 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1016 const M4t &M4, const M5t &M5, const M6t &M6,
1017 const M7t &M7) : Option(ZeroOrMore) {
1018 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1019 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1024 //===----------------------------------------------------------------------===//
1025 // bits_storage class
1027 // Default storage class definition: external storage. This implementation
1028 // assumes the user will specify a variable to store the data into with the
1029 // cl::location(x) modifier.
1031 template<class DataType, class StorageClass>
1032 class bits_storage {
1033 unsigned *Location; // Where to store the bits...
1036 static unsigned Bit(const T &V) {
1037 unsigned BitPos = reinterpret_cast<unsigned>(V);
1038 assert(BitPos < sizeof(unsigned) * 8 &&
1039 "enum exceeds width of bit vector!");
1044 bits_storage() : Location(0) {}
1046 bool setLocation(Option &O, unsigned &L) {
1048 return O.error(": cl::location(x) specified more than once!");
1054 void addValue(const T &V) {
1055 assert(Location != 0 && "cl::location(...) not specified for a command "
1056 "line option with external storage!");
1057 *Location |= Bit(V);
1060 unsigned getBits() { return *Location; }
1063 bool isSet(const T &V) {
1064 return (*Location & Bit(V)) != 0;
1069 // Define how to hold bits. Since we can inherit from a class, we do so.
1070 // This makes us exactly compatible with the bits in all cases that it is used.
1072 template<class DataType>
1073 class bits_storage<DataType, bool> {
1074 unsigned Bits; // Where to store the bits...
1077 static unsigned Bit(const T &V) {
1078 unsigned BitPos = reinterpret_cast<unsigned>(V);
1079 assert(BitPos < sizeof(unsigned) * 8 &&
1080 "enum exceeds width of bit vector!");
1086 void addValue(const T &V) {
1090 unsigned getBits() { return Bits; }
1093 bool isSet(const T &V) {
1094 return (Bits & Bit(V)) != 0;
1099 //===----------------------------------------------------------------------===//
1100 // bits - A bit vector of command options.
1102 template <class DataType, class Storage = bool,
1103 class ParserClass = parser<DataType> >
1104 class bits : public Option, public bits_storage<DataType, Storage> {
1105 std::vector<unsigned> Positions;
1108 virtual enum ValueExpected getValueExpectedFlagDefault() const {
1109 return Parser.getValueExpectedFlagDefault();
1112 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
1113 const std::string &Arg) {
1114 typename ParserClass::parser_data_type Val =
1115 typename ParserClass::parser_data_type();
1116 if (Parser.parse(*this, ArgName, Arg, Val))
1117 return true; // Parse Error!
1120 Positions.push_back(pos);
1124 // Forward printing stuff to the parser...
1125 virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
1126 virtual void printOptionInfo(unsigned GlobalWidth) const {
1127 Parser.printOptionInfo(*this, GlobalWidth);
1131 addArgument(ArgStr);
1132 Parser.initialize(*this);
1135 ParserClass &getParser() { return Parser; }
1137 unsigned getPosition(unsigned optnum) const {
1138 assert(optnum < this->size() && "Invalid option index");
1139 return Positions[optnum];
1144 bits(const M0t &M0) : Option(ZeroOrMore) {
1149 template<class M0t, class M1t>
1150 bits(const M0t &M0, const M1t &M1) : Option(ZeroOrMore) {
1151 apply(M0, this); apply(M1, this);
1155 template<class M0t, class M1t, class M2t>
1156 bits(const M0t &M0, const M1t &M1, const M2t &M2) : Option(ZeroOrMore) {
1157 apply(M0, this); apply(M1, this); apply(M2, this);
1161 template<class M0t, class M1t, class M2t, class M3t>
1162 bits(const M0t &M0, const M1t &M1, const M2t &M2,
1163 const M3t &M3) : Option(ZeroOrMore) {
1164 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1168 template<class M0t, class M1t, class M2t, class M3t, class M4t>
1169 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1170 const M4t &M4) : Option(ZeroOrMore) {
1171 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1176 template<class M0t, class M1t, class M2t, class M3t,
1177 class M4t, class M5t>
1178 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1179 const M4t &M4, const M5t &M5) : Option(ZeroOrMore) {
1180 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1181 apply(M4, this); apply(M5, this);
1185 template<class M0t, class M1t, class M2t, class M3t,
1186 class M4t, class M5t, class M6t>
1187 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1188 const M4t &M4, const M5t &M5, const M6t &M6) : Option(ZeroOrMore) {
1189 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1190 apply(M4, this); apply(M5, this); apply(M6, this);
1194 template<class M0t, class M1t, class M2t, class M3t,
1195 class M4t, class M5t, class M6t, class M7t>
1196 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1197 const M4t &M4, const M5t &M5, const M6t &M6,
1198 const M7t &M7) : Option(ZeroOrMore) {
1199 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1200 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1205 //===----------------------------------------------------------------------===//
1206 // Aliased command line option (alias this name to a preexisting name)
1209 class alias : public Option {
1211 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
1212 const std::string &Arg) {
1213 return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
1215 // Aliases default to be hidden...
1216 virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
1218 // Handle printing stuff...
1219 virtual unsigned getOptionWidth() const;
1220 virtual void printOptionInfo(unsigned GlobalWidth) const;
1224 error(": cl::alias must have argument name specified!");
1226 error(": cl::alias must have an cl::aliasopt(option) specified!");
1227 addArgument(ArgStr);
1230 void setAliasFor(Option &O) {
1232 error(": cl::alias must only have one cl::aliasopt(...) specified!");
1238 alias(const M0t &M0) : Option(Optional), AliasFor(0) {
1243 template<class M0t, class M1t>
1244 alias(const M0t &M0, const M1t &M1) : Option(Optional), AliasFor(0) {
1245 apply(M0, this); apply(M1, this);
1249 template<class M0t, class M1t, class M2t>
1250 alias(const M0t &M0, const M1t &M1, const M2t &M2)
1251 : Option(Optional), AliasFor(0) {
1252 apply(M0, this); apply(M1, this); apply(M2, this);
1256 template<class M0t, class M1t, class M2t, class M3t>
1257 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1258 : Option(Optional), AliasFor(0) {
1259 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1264 // aliasfor - Modifier to set the option an alias aliases.
1267 aliasopt(Option &O) : Opt(O) {}
1268 void apply(alias &A) const { A.setAliasFor(Opt); }
1271 // extrahelp - provide additional help at the end of the normal help
1272 // output. All occurrences of cl::extrahelp will be accumulated and
1273 // printed to std::cerr at the end of the regular help, just before
1276 const char * morehelp;
1277 extrahelp(const char* help);
1280 void PrintVersionMessage();
1281 // This function just prints the help message, exactly the same way as if the
1282 // --help option had been given on the command line.
1283 // NOTE: THIS FUNCTION TERMINATES THE PROGRAM!
1284 void PrintHelpMessage();
1286 } // End namespace cl
1288 } // End namespace llvm