1 //===- llvm/Support/CommandLine.h - Command line handler --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements a command line argument processor that is useful when
11 // creating a tool. It provides a simple, minimalistic interface that is easily
12 // extensible and supports nonlocal (library) command line options.
14 // Note that rather than trying to figure out what this code does, you should
15 // read the library documentation located in docs/CommandLine.html or looks at
16 // the many example usages in tools/*/*.cpp
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_SUPPORT_COMMANDLINE_H
21 #define LLVM_SUPPORT_COMMANDLINE_H
23 #include "llvm/Support/type_traits.h"
24 #include "llvm/Support/DataTypes.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/ADT/SmallVector.h"
36 /// cl Namespace - This namespace contains all of the command line option
37 /// processing machinery. It is intentionally a short name to make qualified
41 //===----------------------------------------------------------------------===//
42 // ParseCommandLineOptions - Command line option processing entry point.
44 void ParseCommandLineOptions(int argc, char **argv,
45 const char *Overview = 0,
46 bool ReadResponseFiles = false);
48 //===----------------------------------------------------------------------===//
49 // ParseEnvironmentOptions - Environment variable option processing alternate
52 void ParseEnvironmentOptions(const char *progName, const char *envvar,
53 const char *Overview = 0,
54 bool ReadResponseFiles = false);
56 ///===---------------------------------------------------------------------===//
57 /// SetVersionPrinter - Override the default (LLVM specific) version printer
58 /// used to print out the version when --version is given
59 /// on the command line. This allows other systems using the
60 /// CommandLine utilities to print their own version string.
61 void SetVersionPrinter(void (*func)());
64 // MarkOptionsChanged - Internal helper function.
65 void MarkOptionsChanged();
67 //===----------------------------------------------------------------------===//
68 // Flags permitted to be passed to command line arguments
71 enum NumOccurrences { // Flags for the number of occurrences allowed
72 Optional = 0x01, // Zero or One occurrence
73 ZeroOrMore = 0x02, // Zero or more occurrences allowed
74 Required = 0x03, // One occurrence required
75 OneOrMore = 0x04, // One or more occurrences required
77 // ConsumeAfter - Indicates that this option is fed anything that follows the
78 // last positional argument required by the application (it is an error if
79 // there are zero positional arguments, and a ConsumeAfter option is used).
80 // Thus, for example, all arguments to LLI are processed until a filename is
81 // found. Once a filename is found, all of the succeeding arguments are
82 // passed, unprocessed, to the ConsumeAfter option.
86 OccurrencesMask = 0x07
89 enum ValueExpected { // Is a value required for the option?
90 ValueOptional = 0x08, // The value can appear... or not
91 ValueRequired = 0x10, // The value is required to appear!
92 ValueDisallowed = 0x18, // A value may not be specified (for flags)
96 enum OptionHidden { // Control whether -help shows this option
97 NotHidden = 0x20, // Option included in --help & --help-hidden
98 Hidden = 0x40, // -help doesn't, but --help-hidden does
99 ReallyHidden = 0x60, // Neither --help nor --help-hidden show this arg
103 // Formatting flags - This controls special features that the option might have
104 // that cause it to be parsed differently...
106 // Prefix - This option allows arguments that are otherwise unrecognized to be
107 // matched by options that are a prefix of the actual value. This is useful for
108 // cases like a linker, where options are typically of the form '-lfoo' or
109 // '-L../../include' where -l or -L are the actual flags. When prefix is
110 // enabled, and used, the value for the flag comes from the suffix of the
113 // Grouping - With this option enabled, multiple letter options are allowed to
114 // bunch together with only a single hyphen for the whole group. This allows
115 // emulation of the behavior that ls uses for example: ls -la === ls -l -a
118 enum FormattingFlags {
119 NormalFormatting = 0x000, // Nothing special
120 Positional = 0x080, // Is a positional argument, no '-' required
121 Prefix = 0x100, // Can this option directly prefix its value?
122 Grouping = 0x180, // Can this option group with other options?
123 FormattingMask = 0x180 // Union of the above flags.
126 enum MiscFlags { // Miscellaneous flags to adjust argument
127 CommaSeparated = 0x200, // Should this cl::list split between commas?
128 PositionalEatsArgs = 0x400, // Should this positional cl::list eat -args?
129 Sink = 0x800, // Should this cl::list eat all unknown options?
130 MiscMask = 0xE00 // Union of the above flags.
135 //===----------------------------------------------------------------------===//
142 // handleOccurrences - Overriden by subclasses to handle the value passed into
143 // an argument. Should return true if there was an error processing the
144 // argument and the program should exit.
146 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
147 const std::string &Arg) = 0;
149 virtual enum ValueExpected getValueExpectedFlagDefault() const {
150 return ValueOptional;
153 // Out of line virtual function to provide home for the class.
154 virtual void anchor();
156 int NumOccurrences; // The number of times specified
157 int Flags; // Flags for the argument
158 unsigned Position; // Position of last occurrence of the option
159 unsigned AdditionalVals;// Greater than 0 for multi-valued option.
160 Option *NextRegistered; // Singly linked list of registered options.
162 const char *ArgStr; // The argument string itself (ex: "help", "o")
163 const char *HelpStr; // The descriptive text message for --help
164 const char *ValueStr; // String describing what the value of this option is
166 inline enum NumOccurrences getNumOccurrencesFlag() const {
167 return static_cast<enum NumOccurrences>(Flags & OccurrencesMask);
169 inline enum ValueExpected getValueExpectedFlag() const {
170 int VE = Flags & ValueMask;
171 return VE ? static_cast<enum ValueExpected>(VE)
172 : getValueExpectedFlagDefault();
174 inline enum OptionHidden getOptionHiddenFlag() const {
175 return static_cast<enum OptionHidden>(Flags & HiddenMask);
177 inline enum FormattingFlags getFormattingFlag() const {
178 return static_cast<enum FormattingFlags>(Flags & FormattingMask);
180 inline unsigned getMiscFlags() const {
181 return Flags & MiscMask;
183 inline unsigned getPosition() const { return Position; }
184 inline unsigned getNumAdditionalVals() const { return AdditionalVals; }
186 // hasArgStr - Return true if the argstr != ""
187 bool hasArgStr() const { return ArgStr[0] != 0; }
189 //-------------------------------------------------------------------------===
190 // Accessor functions set by OptionModifiers
192 void setArgStr(const char *S) { ArgStr = S; }
193 void setDescription(const char *S) { HelpStr = S; }
194 void setValueStr(const char *S) { ValueStr = S; }
196 void setFlag(unsigned Flag, unsigned FlagMask) {
201 void setNumOccurrencesFlag(enum NumOccurrences Val) {
202 setFlag(Val, OccurrencesMask);
204 void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
205 void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
206 void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
207 void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
208 void setPosition(unsigned pos) { Position = pos; }
210 explicit Option(unsigned DefaultFlags)
211 : NumOccurrences(0), Flags(DefaultFlags | NormalFormatting), Position(0),
212 AdditionalVals(0), NextRegistered(0),
213 ArgStr(""), HelpStr(""), ValueStr("") {
214 assert(getNumOccurrencesFlag() != 0 &&
215 getOptionHiddenFlag() != 0 && "Not all default flags specified!");
218 inline void setNumAdditionalVals(unsigned n)
219 { AdditionalVals = n; }
221 // addArgument - Register this argument with the commandline system.
225 Option *getNextRegisteredOption() const { return NextRegistered; }
227 // Return the width of the option tag for printing...
228 virtual size_t getOptionWidth() const = 0;
230 // printOptionInfo - Print out information about this option. The
231 // to-be-maintained width is specified.
233 virtual void printOptionInfo(size_t GlobalWidth) const = 0;
235 virtual void getExtraOptionNames(std::vector<const char*> &) {}
237 // addOccurrence - Wrapper around handleOccurrence that enforces Flags
239 bool addOccurrence(unsigned pos, const char *ArgName,
240 const std::string &Value, bool MultiArg = false);
242 // Prints option name followed by message. Always returns true.
243 bool error(std::string Message, const char *ArgName = 0);
246 inline int getNumOccurrences() const { return NumOccurrences; }
251 //===----------------------------------------------------------------------===//
252 // Command line option modifiers that can be used to modify the behavior of
253 // command line option parsers...
256 // desc - Modifier to set the description shown in the --help output...
259 desc(const char *Str) : Desc(Str) {}
260 void apply(Option &O) const { O.setDescription(Desc); }
263 // value_desc - Modifier to set the value description shown in the --help
267 value_desc(const char *Str) : Desc(Str) {}
268 void apply(Option &O) const { O.setValueStr(Desc); }
271 // init - Specify a default (initial) value for the command line argument, if
272 // the default constructor for the argument type does not give you what you
273 // want. This is only valid on "opt" arguments, not on "list" arguments.
278 initializer(const Ty &Val) : Init(Val) {}
281 void apply(Opt &O) const { O.setInitialValue(Init); }
285 initializer<Ty> init(const Ty &Val) {
286 return initializer<Ty>(Val);
290 // location - Allow the user to specify which external variable they want to
291 // store the results of the command line argument processing into, if they don't
292 // want to store it in the option itself.
295 struct LocationClass {
297 LocationClass(Ty &L) : Loc(L) {}
300 void apply(Opt &O) const { O.setLocation(O, Loc); }
304 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
307 //===----------------------------------------------------------------------===//
308 // Enum valued command line option
310 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, int(ENUMVAL), DESC
311 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, int(ENUMVAL), DESC
312 #define clEnumValEnd (reinterpret_cast<void*>(0))
314 // values - For custom data types, allow specifying a group of values together
315 // as the values that go into the mapping that the option handler uses. Note
316 // that the values list must always have a 0 at the end of the list to indicate
317 // that the list has ended.
319 template<class DataType>
321 // Use a vector instead of a map, because the lists should be short,
322 // the overhead is less, and most importantly, it keeps them in the order
323 // inserted so we can print our option out nicely.
324 SmallVector<std::pair<const char *, std::pair<int, const char *> >,4> Values;
325 void processValues(va_list Vals);
327 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
329 // Insert the first value, which is required.
330 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
332 // Process the varargs portion of the values...
333 while (const char *enumName = va_arg(ValueArgs, const char *)) {
334 DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int));
335 const char *EnumDesc = va_arg(ValueArgs, const char *);
336 Values.push_back(std::make_pair(enumName, // Add value to value map
337 std::make_pair(EnumVal, EnumDesc)));
342 void apply(Opt &O) const {
343 for (unsigned i = 0, e = static_cast<unsigned>(Values.size());
345 O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
346 Values[i].second.second);
350 template<class DataType>
351 ValuesClass<DataType> END_WITH_NULL values(const char *Arg, DataType Val,
352 const char *Desc, ...) {
354 va_start(ValueArgs, Desc);
355 ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
361 //===----------------------------------------------------------------------===//
362 // parser class - Parameterizable parser for different data types. By default,
363 // known data types (string, int, bool) have specialized parsers, that do what
364 // you would expect. The default parser, used for data types that are not
365 // built-in, uses a mapping table to map specific options to values, which is
366 // used, among other things, to handle enum types.
368 //--------------------------------------------------
369 // generic_parser_base - This class holds all the non-generic code that we do
370 // not need replicated for every instance of the generic parser. This also
371 // allows us to put stuff into CommandLine.cpp
373 struct generic_parser_base {
374 virtual ~generic_parser_base() {} // Base class should have virtual-dtor
376 // getNumOptions - Virtual function implemented by generic subclass to
377 // indicate how many entries are in Values.
379 virtual unsigned getNumOptions() const = 0;
381 // getOption - Return option name N.
382 virtual const char *getOption(unsigned N) const = 0;
384 // getDescription - Return description N
385 virtual const char *getDescription(unsigned N) const = 0;
387 // Return the width of the option tag for printing...
388 virtual size_t getOptionWidth(const Option &O) const;
390 // printOptionInfo - Print out information about this option. The
391 // to-be-maintained width is specified.
393 virtual void printOptionInfo(const Option &O, size_t GlobalWidth) const;
395 void initialize(Option &O) {
396 // All of the modifiers for the option have been processed by now, so the
397 // argstr field should be stable, copy it down now.
399 hasArgStr = O.hasArgStr();
402 void getExtraOptionNames(std::vector<const char*> &OptionNames) {
403 // If there has been no argstr specified, that means that we need to add an
404 // argument for every possible option. This ensures that our options are
407 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
408 OptionNames.push_back(getOption(i));
412 enum ValueExpected getValueExpectedFlagDefault() const {
413 // If there is an ArgStr specified, then we are of the form:
415 // -opt=O2 or -opt O2 or -optO2
417 // In which case, the value is required. Otherwise if an arg str has not
418 // been specified, we are of the form:
420 // -O2 or O2 or -la (where -l and -a are separate options)
422 // If this is the case, we cannot allow a value.
425 return ValueRequired;
427 return ValueDisallowed;
430 // findOption - Return the option number corresponding to the specified
431 // argument string. If the option is not found, getNumOptions() is returned.
433 unsigned findOption(const char *Name);
439 // Default parser implementation - This implementation depends on having a
440 // mapping of recognized options to values of some sort. In addition to this,
441 // each entry in the mapping also tracks a help message that is printed with the
442 // command line option for --help. Because this is a simple mapping parser, the
443 // data type can be any unsupported type.
445 template <class DataType>
446 class parser : public generic_parser_base {
448 SmallVector<std::pair<const char *,
449 std::pair<DataType, const char *> >, 8> Values;
451 typedef DataType parser_data_type;
453 // Implement virtual functions needed by generic_parser_base
454 unsigned getNumOptions() const { return unsigned(Values.size()); }
455 const char *getOption(unsigned N) const { return Values[N].first; }
456 const char *getDescription(unsigned N) const {
457 return Values[N].second.second;
460 // parse - Return true on error.
461 bool parse(Option &O, const char *ArgName, const std::string &Arg,
469 for (unsigned i = 0, e = static_cast<unsigned>(Values.size());
471 if (ArgVal == Values[i].first) {
472 V = Values[i].second.first;
476 return O.error(": Cannot find option named '" + ArgVal + "'!");
479 /// addLiteralOption - Add an entry to the mapping table.
482 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
483 assert(findOption(Name) == Values.size() && "Option already exists!");
484 Values.push_back(std::make_pair(Name,
485 std::make_pair(static_cast<DataType>(V),HelpStr)));
486 MarkOptionsChanged();
489 /// removeLiteralOption - Remove the specified option.
491 void removeLiteralOption(const char *Name) {
492 unsigned N = findOption(Name);
493 assert(N != Values.size() && "Option not found!");
494 Values.erase(Values.begin()+N);
498 //--------------------------------------------------
499 // basic_parser - Super class of parsers to provide boilerplate code
501 struct basic_parser_impl { // non-template implementation of basic_parser<t>
502 virtual ~basic_parser_impl() {}
504 enum ValueExpected getValueExpectedFlagDefault() const {
505 return ValueRequired;
508 void getExtraOptionNames(std::vector<const char*> &) {}
510 void initialize(Option &) {}
512 // Return the width of the option tag for printing...
513 size_t getOptionWidth(const Option &O) const;
515 // printOptionInfo - Print out information about this option. The
516 // to-be-maintained width is specified.
518 void printOptionInfo(const Option &O, size_t GlobalWidth) const;
520 // getValueName - Overload in subclass to provide a better default value.
521 virtual const char *getValueName() const { return "value"; }
523 // An out-of-line virtual method to provide a 'home' for this class.
524 virtual void anchor();
527 // basic_parser - The real basic parser is just a template wrapper that provides
528 // a typedef for the provided data type.
530 template<class DataType>
531 struct basic_parser : public basic_parser_impl {
532 typedef DataType parser_data_type;
535 //--------------------------------------------------
539 class parser<bool> : public basic_parser<bool> {
543 // parse - Return true on error.
544 bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
547 void initialize(Opt &O) {
551 enum ValueExpected getValueExpectedFlagDefault() const {
552 return ValueOptional;
555 // getValueName - Do not print =<value> at all.
556 virtual const char *getValueName() const { return 0; }
558 // An out-of-line virtual method to provide a 'home' for this class.
559 virtual void anchor();
562 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<bool>);
564 //--------------------------------------------------
565 // parser<boolOrDefault>
566 enum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE };
568 class parser<boolOrDefault> : public basic_parser<boolOrDefault> {
570 // parse - Return true on error.
571 bool parse(Option &O, const char *ArgName, const std::string &Arg,
574 enum ValueExpected getValueExpectedFlagDefault() const {
575 return ValueOptional;
578 // getValueName - Do not print =<value> at all.
579 virtual const char *getValueName() const { return 0; }
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<boolOrDefault>);
587 //--------------------------------------------------
591 class parser<int> : public basic_parser<int> {
593 // parse - Return true on error.
594 bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
596 // getValueName - Overload in subclass to provide a better default value.
597 virtual const char *getValueName() const { return "int"; }
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<int>);
606 //--------------------------------------------------
610 class parser<unsigned> : public basic_parser<unsigned> {
612 // parse - Return true on error.
613 bool parse(Option &O, const char *AN, const std::string &Arg, unsigned &Val);
615 // getValueName - Overload in subclass to provide a better default value.
616 virtual const char *getValueName() const { return "uint"; }
618 // An out-of-line virtual method to provide a 'home' for this class.
619 virtual void anchor();
622 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<unsigned>);
624 //--------------------------------------------------
628 class parser<double> : public basic_parser<double> {
630 // parse - Return true on error.
631 bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
633 // getValueName - Overload in subclass to provide a better default value.
634 virtual const char *getValueName() const { return "number"; }
636 // An out-of-line virtual method to provide a 'home' for this class.
637 virtual void anchor();
640 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<double>);
642 //--------------------------------------------------
646 class parser<float> : public basic_parser<float> {
648 // parse - Return true on error.
649 bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
651 // getValueName - Overload in subclass to provide a better default value.
652 virtual const char *getValueName() const { return "number"; }
654 // An out-of-line virtual method to provide a 'home' for this class.
655 virtual void anchor();
658 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<float>);
660 //--------------------------------------------------
661 // parser<std::string>
664 class parser<std::string> : public basic_parser<std::string> {
666 // parse - Return true on error.
667 bool parse(Option &, const char *, const std::string &Arg,
668 std::string &Value) {
673 // getValueName - Overload in subclass to provide a better default value.
674 virtual const char *getValueName() const { return "string"; }
676 // An out-of-line virtual method to provide a 'home' for this class.
677 virtual void anchor();
680 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<std::string>);
682 //===----------------------------------------------------------------------===//
683 // applicator class - This class is used because we must use partial
684 // specialization to handle literal string arguments specially (const char* does
685 // not correctly respond to the apply method). Because the syntax to use this
686 // is a pain, we have the 'apply' method below to handle the nastiness...
688 template<class Mod> struct applicator {
690 static void opt(const Mod &M, Opt &O) { M.apply(O); }
693 // Handle const char* as a special case...
694 template<unsigned n> struct applicator<char[n]> {
696 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
698 template<unsigned n> struct applicator<const char[n]> {
700 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
702 template<> struct applicator<const char*> {
704 static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
707 template<> struct applicator<NumOccurrences> {
708 static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); }
710 template<> struct applicator<ValueExpected> {
711 static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
713 template<> struct applicator<OptionHidden> {
714 static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
716 template<> struct applicator<FormattingFlags> {
717 static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
719 template<> struct applicator<MiscFlags> {
720 static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
723 // apply method - Apply a modifier to an option in a type safe way.
724 template<class Mod, class Opt>
725 void apply(const Mod &M, Opt *O) {
726 applicator<Mod>::opt(M, *O);
730 //===----------------------------------------------------------------------===//
733 // Default storage class definition: external storage. This implementation
734 // assumes the user will specify a variable to store the data into with the
735 // cl::location(x) modifier.
737 template<class DataType, bool ExternalStorage, bool isClass>
739 DataType *Location; // Where to store the object...
742 assert(Location != 0 && "cl::location(...) not specified for a command "
743 "line option with external storage, "
744 "or cl::init specified before cl::location()!!");
747 opt_storage() : Location(0) {}
749 bool setLocation(Option &O, DataType &L) {
751 return O.error(": cl::location(x) specified more than once!");
757 void setValue(const T &V) {
762 DataType &getValue() { check(); return *Location; }
763 const DataType &getValue() const { check(); return *Location; }
767 // Define how to hold a class type object, such as a string. Since we can
768 // inherit from a class, we do so. This makes us exactly compatible with the
769 // object in all cases that it is used.
771 template<class DataType>
772 class opt_storage<DataType,false,true> : public DataType {
775 void setValue(const T &V) { DataType::operator=(V); }
777 DataType &getValue() { return *this; }
778 const DataType &getValue() const { return *this; }
781 // Define a partial specialization to handle things we cannot inherit from. In
782 // this case, we store an instance through containment, and overload operators
783 // to get at the value.
785 template<class DataType>
786 class opt_storage<DataType, false, false> {
790 // Make sure we initialize the value with the default constructor for the
792 opt_storage() : Value(DataType()) {}
795 void setValue(const T &V) { Value = V; }
796 DataType &getValue() { return Value; }
797 DataType getValue() const { return Value; }
799 // If the datatype is a pointer, support -> on it.
800 DataType operator->() const { return Value; }
804 //===----------------------------------------------------------------------===//
805 // opt - A scalar command line option.
807 template <class DataType, bool ExternalStorage = false,
808 class ParserClass = parser<DataType> >
809 class opt : public Option,
810 public opt_storage<DataType, ExternalStorage,
811 is_class<DataType>::value> {
814 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
815 const std::string &Arg) {
816 typename ParserClass::parser_data_type Val =
817 typename ParserClass::parser_data_type();
818 if (Parser.parse(*this, ArgName, Arg, Val))
819 return true; // Parse error!
821 this->setPosition(pos);
825 virtual enum ValueExpected getValueExpectedFlagDefault() const {
826 return Parser.getValueExpectedFlagDefault();
828 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
829 return Parser.getExtraOptionNames(OptionNames);
832 // Forward printing stuff to the parser...
833 virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);}
834 virtual void printOptionInfo(size_t GlobalWidth) const {
835 Parser.printOptionInfo(*this, GlobalWidth);
840 Parser.initialize(*this);
843 // setInitialValue - Used by the cl::init modifier...
844 void setInitialValue(const DataType &V) { this->setValue(V); }
846 ParserClass &getParser() { return Parser; }
848 operator DataType() const { return this->getValue(); }
851 DataType &operator=(const T &Val) {
853 return this->getValue();
858 explicit opt(const M0t &M0) : Option(Optional | NotHidden) {
864 template<class M0t, class M1t>
865 opt(const M0t &M0, const M1t &M1) : Option(Optional | NotHidden) {
866 apply(M0, this); apply(M1, this);
871 template<class M0t, class M1t, class M2t>
872 opt(const M0t &M0, const M1t &M1,
873 const M2t &M2) : Option(Optional | NotHidden) {
874 apply(M0, this); apply(M1, this); apply(M2, this);
878 template<class M0t, class M1t, class M2t, class M3t>
879 opt(const M0t &M0, const M1t &M1, const M2t &M2,
880 const M3t &M3) : Option(Optional | NotHidden) {
881 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
885 template<class M0t, class M1t, class M2t, class M3t, class M4t>
886 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
887 const M4t &M4) : Option(Optional | NotHidden) {
888 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
893 template<class M0t, class M1t, class M2t, class M3t,
894 class M4t, class M5t>
895 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
896 const M4t &M4, const M5t &M5) : Option(Optional | NotHidden) {
897 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
898 apply(M4, this); apply(M5, this);
902 template<class M0t, class M1t, class M2t, class M3t,
903 class M4t, class M5t, class M6t>
904 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
905 const M4t &M4, const M5t &M5,
906 const M6t &M6) : Option(Optional | NotHidden) {
907 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
908 apply(M4, this); apply(M5, this); apply(M6, this);
912 template<class M0t, class M1t, class M2t, class M3t,
913 class M4t, class M5t, class M6t, class M7t>
914 opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
915 const M4t &M4, const M5t &M5, const M6t &M6,
916 const M7t &M7) : Option(Optional | NotHidden) {
917 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
918 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
923 EXTERN_TEMPLATE_INSTANTIATION(class opt<unsigned>);
924 EXTERN_TEMPLATE_INSTANTIATION(class opt<int>);
925 EXTERN_TEMPLATE_INSTANTIATION(class opt<std::string>);
926 EXTERN_TEMPLATE_INSTANTIATION(class opt<bool>);
928 //===----------------------------------------------------------------------===//
929 // list_storage class
931 // Default storage class definition: external storage. This implementation
932 // assumes the user will specify a variable to store the data into with the
933 // cl::location(x) modifier.
935 template<class DataType, class StorageClass>
937 StorageClass *Location; // Where to store the object...
940 list_storage() : Location(0) {}
942 bool setLocation(Option &O, StorageClass &L) {
944 return O.error(": cl::location(x) specified more than once!");
950 void addValue(const T &V) {
951 assert(Location != 0 && "cl::location(...) not specified for a command "
952 "line option with external storage!");
953 Location->push_back(V);
958 // Define how to hold a class type object, such as a string. Since we can
959 // inherit from a class, we do so. This makes us exactly compatible with the
960 // object in all cases that it is used.
962 template<class DataType>
963 class list_storage<DataType, bool> : public std::vector<DataType> {
966 void addValue(const T &V) { push_back(V); }
970 //===----------------------------------------------------------------------===//
971 // list - A list of command line options.
973 template <class DataType, class Storage = bool,
974 class ParserClass = parser<DataType> >
975 class list : public Option, public list_storage<DataType, Storage> {
976 std::vector<unsigned> Positions;
979 virtual enum ValueExpected getValueExpectedFlagDefault() const {
980 return Parser.getValueExpectedFlagDefault();
982 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
983 return Parser.getExtraOptionNames(OptionNames);
986 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
987 const std::string &Arg) {
988 typename ParserClass::parser_data_type Val =
989 typename ParserClass::parser_data_type();
990 if (Parser.parse(*this, ArgName, Arg, Val))
991 return true; // Parse Error!
994 Positions.push_back(pos);
998 // Forward printing stuff to the parser...
999 virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);}
1000 virtual void printOptionInfo(size_t GlobalWidth) const {
1001 Parser.printOptionInfo(*this, GlobalWidth);
1006 Parser.initialize(*this);
1009 ParserClass &getParser() { return Parser; }
1011 unsigned getPosition(unsigned optnum) const {
1012 assert(optnum < this->size() && "Invalid option index");
1013 return Positions[optnum];
1016 void setNumAdditionalVals(unsigned n) {
1017 Option::setNumAdditionalVals(n);
1022 explicit list(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
1027 template<class M0t, class M1t>
1028 list(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
1029 apply(M0, this); apply(M1, this);
1033 template<class M0t, class M1t, class M2t>
1034 list(const M0t &M0, const M1t &M1, const M2t &M2)
1035 : Option(ZeroOrMore | NotHidden) {
1036 apply(M0, this); apply(M1, this); apply(M2, this);
1040 template<class M0t, class M1t, class M2t, class M3t>
1041 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1042 : Option(ZeroOrMore | NotHidden) {
1043 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1047 template<class M0t, class M1t, class M2t, class M3t, class M4t>
1048 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1049 const M4t &M4) : Option(ZeroOrMore | NotHidden) {
1050 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1055 template<class M0t, class M1t, class M2t, class M3t,
1056 class M4t, class M5t>
1057 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1058 const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
1059 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1060 apply(M4, this); apply(M5, this);
1064 template<class M0t, class M1t, class M2t, class M3t,
1065 class M4t, class M5t, class M6t>
1066 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1067 const M4t &M4, const M5t &M5, const M6t &M6)
1068 : Option(ZeroOrMore | NotHidden) {
1069 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1070 apply(M4, this); apply(M5, this); apply(M6, this);
1074 template<class M0t, class M1t, class M2t, class M3t,
1075 class M4t, class M5t, class M6t, class M7t>
1076 list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1077 const M4t &M4, const M5t &M5, const M6t &M6,
1078 const M7t &M7) : Option(ZeroOrMore | NotHidden) {
1079 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1080 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1085 // multi_arg - Modifier to set the number of additional values.
1087 unsigned AdditionalVals;
1088 explicit multi_val(unsigned N) : AdditionalVals(N) {}
1090 template <typename D, typename S, typename P>
1091 void apply(list<D, S, P> &L) const { L.setNumAdditionalVals(AdditionalVals); }
1095 //===----------------------------------------------------------------------===//
1096 // bits_storage class
1098 // Default storage class definition: external storage. This implementation
1099 // assumes the user will specify a variable to store the data into with the
1100 // cl::location(x) modifier.
1102 template<class DataType, class StorageClass>
1103 class bits_storage {
1104 unsigned *Location; // Where to store the bits...
1107 static unsigned Bit(const T &V) {
1108 unsigned BitPos = reinterpret_cast<unsigned>(V);
1109 assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
1110 "enum exceeds width of bit vector!");
1115 bits_storage() : Location(0) {}
1117 bool setLocation(Option &O, unsigned &L) {
1119 return O.error(": cl::location(x) specified more than once!");
1125 void addValue(const T &V) {
1126 assert(Location != 0 && "cl::location(...) not specified for a command "
1127 "line option with external storage!");
1128 *Location |= Bit(V);
1131 unsigned getBits() { return *Location; }
1134 bool isSet(const T &V) {
1135 return (*Location & Bit(V)) != 0;
1140 // Define how to hold bits. Since we can inherit from a class, we do so.
1141 // This makes us exactly compatible with the bits in all cases that it is used.
1143 template<class DataType>
1144 class bits_storage<DataType, bool> {
1145 unsigned Bits; // Where to store the bits...
1148 static unsigned Bit(const T &V) {
1149 unsigned BitPos = reinterpret_cast<unsigned>(V);
1150 assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
1151 "enum exceeds width of bit vector!");
1157 void addValue(const T &V) {
1161 unsigned getBits() { return Bits; }
1164 bool isSet(const T &V) {
1165 return (Bits & Bit(V)) != 0;
1170 //===----------------------------------------------------------------------===//
1171 // bits - A bit vector of command options.
1173 template <class DataType, class Storage = bool,
1174 class ParserClass = parser<DataType> >
1175 class bits : public Option, public bits_storage<DataType, Storage> {
1176 std::vector<unsigned> Positions;
1179 virtual enum ValueExpected getValueExpectedFlagDefault() const {
1180 return Parser.getValueExpectedFlagDefault();
1182 virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
1183 return Parser.getExtraOptionNames(OptionNames);
1186 virtual bool handleOccurrence(unsigned pos, const char *ArgName,
1187 const std::string &Arg) {
1188 typename ParserClass::parser_data_type Val =
1189 typename ParserClass::parser_data_type();
1190 if (Parser.parse(*this, ArgName, Arg, Val))
1191 return true; // Parse Error!
1194 Positions.push_back(pos);
1198 // Forward printing stuff to the parser...
1199 virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);}
1200 virtual void printOptionInfo(size_t GlobalWidth) const {
1201 Parser.printOptionInfo(*this, GlobalWidth);
1206 Parser.initialize(*this);
1209 ParserClass &getParser() { return Parser; }
1211 unsigned getPosition(unsigned optnum) const {
1212 assert(optnum < this->size() && "Invalid option index");
1213 return Positions[optnum];
1218 explicit bits(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
1223 template<class M0t, class M1t>
1224 bits(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
1225 apply(M0, this); apply(M1, this);
1229 template<class M0t, class M1t, class M2t>
1230 bits(const M0t &M0, const M1t &M1, const M2t &M2)
1231 : Option(ZeroOrMore | NotHidden) {
1232 apply(M0, this); apply(M1, this); apply(M2, this);
1236 template<class M0t, class M1t, class M2t, class M3t>
1237 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1238 : Option(ZeroOrMore | NotHidden) {
1239 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1243 template<class M0t, class M1t, class M2t, class M3t, class M4t>
1244 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1245 const M4t &M4) : Option(ZeroOrMore | NotHidden) {
1246 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1251 template<class M0t, class M1t, class M2t, class M3t,
1252 class M4t, class M5t>
1253 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1254 const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
1255 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1256 apply(M4, this); apply(M5, this);
1260 template<class M0t, class M1t, class M2t, class M3t,
1261 class M4t, class M5t, class M6t>
1262 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1263 const M4t &M4, const M5t &M5, const M6t &M6)
1264 : Option(ZeroOrMore | NotHidden) {
1265 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1266 apply(M4, this); apply(M5, this); apply(M6, this);
1270 template<class M0t, class M1t, class M2t, class M3t,
1271 class M4t, class M5t, class M6t, class M7t>
1272 bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1273 const M4t &M4, const M5t &M5, const M6t &M6,
1274 const M7t &M7) : Option(ZeroOrMore | NotHidden) {
1275 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1276 apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1281 //===----------------------------------------------------------------------===//
1282 // Aliased command line option (alias this name to a preexisting name)
1285 class alias : public Option {
1287 virtual bool handleOccurrence(unsigned pos, const char * /*ArgName*/,
1288 const std::string &Arg) {
1289 return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
1291 // Handle printing stuff...
1292 virtual size_t getOptionWidth() const;
1293 virtual void printOptionInfo(size_t GlobalWidth) const;
1297 error(": cl::alias must have argument name specified!");
1299 error(": cl::alias must have an cl::aliasopt(option) specified!");
1303 void setAliasFor(Option &O) {
1305 error(": cl::alias must only have one cl::aliasopt(...) specified!");
1311 explicit alias(const M0t &M0) : Option(Optional | Hidden), AliasFor(0) {
1316 template<class M0t, class M1t>
1317 alias(const M0t &M0, const M1t &M1) : Option(Optional | Hidden), AliasFor(0) {
1318 apply(M0, this); apply(M1, this);
1322 template<class M0t, class M1t, class M2t>
1323 alias(const M0t &M0, const M1t &M1, const M2t &M2)
1324 : Option(Optional | Hidden), AliasFor(0) {
1325 apply(M0, this); apply(M1, this); apply(M2, this);
1329 template<class M0t, class M1t, class M2t, class M3t>
1330 alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1331 : Option(Optional | Hidden), AliasFor(0) {
1332 apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1337 // aliasfor - Modifier to set the option an alias aliases.
1340 explicit aliasopt(Option &O) : Opt(O) {}
1341 void apply(alias &A) const { A.setAliasFor(Opt); }
1344 // extrahelp - provide additional help at the end of the normal help
1345 // output. All occurrences of cl::extrahelp will be accumulated and
1346 // printed to std::cerr at the end of the regular help, just before
1349 const char * morehelp;
1350 explicit extrahelp(const char* help);
1353 void PrintVersionMessage();
1354 // This function just prints the help message, exactly the same way as if the
1355 // --help option had been given on the command line.
1356 // NOTE: THIS FUNCTION TERMINATES THE PROGRAM!
1357 void PrintHelpMessage();
1359 } // End namespace cl
1361 } // End namespace llvm