LLVM API Documentation

CommandLine.h

Go to the documentation of this file.
00001 //===- llvm/Support/CommandLine.h - Command line handler --------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This class implements a command line argument processor that is useful when
00011 // creating a tool.  It provides a simple, minimalistic interface that is easily
00012 // extensible and supports nonlocal (library) command line options.
00013 //
00014 // Note that rather than trying to figure out what this code does, you should
00015 // read the library documentation located in docs/CommandLine.html or looks at
00016 // the many example usages in tools/*/*.cpp
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #ifndef LLVM_SUPPORT_COMMANDLINE_H
00021 #define LLVM_SUPPORT_COMMANDLINE_H
00022 
00023 #include "llvm/Support/type_traits.h"
00024 #include "llvm/Support/DataTypes.h"
00025 #include <string>
00026 #include <vector>
00027 #include <utility>
00028 #include <cstdarg>
00029 #include <cassert>
00030 
00031 namespace llvm {
00032 
00033 /// cl Namespace - This namespace contains all of the command line option
00034 /// processing machinery.  It is intentionally a short name to make qualified
00035 /// usage concise.
00036 namespace cl {
00037 
00038 //===----------------------------------------------------------------------===//
00039 // ParseCommandLineOptions - Command line option processing entry point.
00040 //
00041 void ParseCommandLineOptions(int &argc, char **argv,
00042                              const char *Overview = 0);
00043 
00044 //===----------------------------------------------------------------------===//
00045 // ParseEnvironmentOptions - Environment variable option processing alternate
00046 //                           entry point.
00047 //
00048 void ParseEnvironmentOptions(const char *progName, const char *envvar,
00049                              const char *Overview = 0);
00050 
00051 ///===---------------------------------------------------------------------===//
00052 /// SetVersionPrinter - Override the default (LLVM specific) version printer
00053 ///                     used to print out the version when --version is given
00054 ///                     on the command line. This gives other systems using the
00055 ///                     CommandLine utilities to print their own version string.
00056 void SetVersionPrinter(void (*func)());
00057 
00058 //===----------------------------------------------------------------------===//
00059 // Flags permitted to be passed to command line arguments
00060 //
00061 
00062 enum NumOccurrences {          // Flags for the number of occurrences allowed
00063   Optional        = 0x01,      // Zero or One occurrence
00064   ZeroOrMore      = 0x02,      // Zero or more occurrences allowed
00065   Required        = 0x03,      // One occurrence required
00066   OneOrMore       = 0x04,      // One or more occurrences required
00067 
00068   // ConsumeAfter - Indicates that this option is fed anything that follows the
00069   // last positional argument required by the application (it is an error if
00070   // there are zero positional arguments, and a ConsumeAfter option is used).
00071   // Thus, for example, all arguments to LLI are processed until a filename is
00072   // found.  Once a filename is found, all of the succeeding arguments are
00073   // passed, unprocessed, to the ConsumeAfter option.
00074   //
00075   ConsumeAfter    = 0x05,
00076 
00077   OccurrencesMask  = 0x07
00078 };
00079 
00080 enum ValueExpected {           // Is a value required for the option?
00081   ValueOptional   = 0x08,      // The value can appear... or not
00082   ValueRequired   = 0x10,      // The value is required to appear!
00083   ValueDisallowed = 0x18,      // A value may not be specified (for flags)
00084   ValueMask       = 0x18
00085 };
00086 
00087 enum OptionHidden {            // Control whether -help shows this option
00088   NotHidden       = 0x20,      // Option included in --help & --help-hidden
00089   Hidden          = 0x40,      // -help doesn't, but --help-hidden does
00090   ReallyHidden    = 0x60,      // Neither --help nor --help-hidden show this arg
00091   HiddenMask      = 0x60
00092 };
00093 
00094 // Formatting flags - This controls special features that the option might have
00095 // that cause it to be parsed differently...
00096 //
00097 // Prefix - This option allows arguments that are otherwise unrecognized to be
00098 // matched by options that are a prefix of the actual value.  This is useful for
00099 // cases like a linker, where options are typically of the form '-lfoo' or
00100 // '-L../../include' where -l or -L are the actual flags.  When prefix is
00101 // enabled, and used, the value for the flag comes from the suffix of the
00102 // argument.
00103 //
00104 // Grouping - With this option enabled, multiple letter options are allowed to
00105 // bunch together with only a single hyphen for the whole group.  This allows
00106 // emulation of the behavior that ls uses for example: ls -la === ls -l -a
00107 //
00108 
00109 enum FormattingFlags {
00110   NormalFormatting = 0x000,     // Nothing special
00111   Positional       = 0x080,     // Is a positional argument, no '-' required
00112   Prefix           = 0x100,     // Can this option directly prefix its value?
00113   Grouping         = 0x180,     // Can this option group with other options?
00114   FormattingMask   = 0x180      // Union of the above flags.
00115 };
00116 
00117 enum MiscFlags {               // Miscellaneous flags to adjust argument
00118   CommaSeparated     = 0x200,  // Should this cl::list split between commas?
00119   PositionalEatsArgs = 0x400,  // Should this positional cl::list eat -args?
00120   MiscMask           = 0x600   // Union of the above flags.
00121 };
00122 
00123 
00124 
00125 //===----------------------------------------------------------------------===//
00126 // Option Base class
00127 //
00128 class alias;
00129 class Option {
00130   friend void cl::ParseCommandLineOptions(int &, char **, const char *);
00131   friend class alias;
00132 
00133   // handleOccurrences - Overriden by subclasses to handle the value passed into
00134   // an argument.  Should return true if there was an error processing the
00135   // argument and the program should exit.
00136   //
00137   virtual bool handleOccurrence(unsigned pos, const char *ArgName,
00138                                 const std::string &Arg) = 0;
00139 
00140   virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
00141     return Optional;
00142   }
00143   virtual enum ValueExpected getValueExpectedFlagDefault() const {
00144     return ValueOptional;
00145   }
00146   virtual enum OptionHidden getOptionHiddenFlagDefault() const {
00147     return NotHidden;
00148   }
00149   virtual enum FormattingFlags getFormattingFlagDefault() const {
00150     return NormalFormatting;
00151   }
00152 
00153   // Out of line virtual function to provide home for the class.
00154   virtual void anchor();
00155   
00156   int NumOccurrences;   // The number of times specified
00157   int Flags;            // Flags for the argument
00158   unsigned Position;    // Position of last occurrence of the option
00159 public:
00160   const char *ArgStr;   // The argument string itself (ex: "help", "o")
00161   const char *HelpStr;  // The descriptive text message for --help
00162   const char *ValueStr; // String describing what the value of this option is
00163 
00164   inline enum NumOccurrences getNumOccurrencesFlag() const {
00165     int NO = Flags & OccurrencesMask;
00166     return NO ? static_cast<enum NumOccurrences>(NO)
00167               : getNumOccurrencesFlagDefault();
00168   }
00169   inline enum ValueExpected getValueExpectedFlag() const {
00170     int VE = Flags & ValueMask;
00171     return VE ? static_cast<enum ValueExpected>(VE)
00172               : getValueExpectedFlagDefault();
00173   }
00174   inline enum OptionHidden getOptionHiddenFlag() const {
00175     int OH = Flags & HiddenMask;
00176     return OH ? static_cast<enum OptionHidden>(OH)
00177               : getOptionHiddenFlagDefault();
00178   }
00179   inline enum FormattingFlags getFormattingFlag() const {
00180     int OH = Flags & FormattingMask;
00181     return OH ? static_cast<enum FormattingFlags>(OH)
00182               : getFormattingFlagDefault();
00183   }
00184   inline unsigned getMiscFlags() const {
00185     return Flags & MiscMask;
00186   }
00187   inline unsigned getPosition() const { return Position; }
00188 
00189   // hasArgStr - Return true if the argstr != ""
00190   bool hasArgStr() const { return ArgStr[0] != 0; }
00191 
00192   //-------------------------------------------------------------------------===
00193   // Accessor functions set by OptionModifiers
00194   //
00195   void setArgStr(const char *S) { ArgStr = S; }
00196   void setDescription(const char *S) { HelpStr = S; }
00197   void setValueStr(const char *S) { ValueStr = S; }
00198 
00199   void setFlag(unsigned Flag, unsigned FlagMask) {
00200     if (Flags & FlagMask) {
00201       error(": Specified two settings for the same option!");
00202       exit(1);
00203     }
00204 
00205     Flags |= Flag;
00206   }
00207 
00208   void setNumOccurrencesFlag(enum NumOccurrences Val) {
00209     setFlag(Val, OccurrencesMask);
00210   }
00211   void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
00212   void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
00213   void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
00214   void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
00215   void setPosition(unsigned pos) { Position = pos; }
00216 protected:
00217   Option() : NumOccurrences(0), Flags(0), Position(0),
00218              ArgStr(""), HelpStr(""), ValueStr("") {}
00219 
00220 public:
00221   // addArgument - Tell the system that this Option subclass will handle all
00222   // occurrences of -ArgStr on the command line.
00223   //
00224   void addArgument(const char *ArgStr);
00225   void removeArgument(const char *ArgStr);
00226 
00227   // Return the width of the option tag for printing...
00228   virtual unsigned getOptionWidth() const = 0;
00229 
00230   // printOptionInfo - Print out information about this option.  The
00231   // to-be-maintained width is specified.
00232   //
00233   virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
00234 
00235   // addOccurrence - Wrapper around handleOccurrence that enforces Flags
00236   //
00237   bool addOccurrence(unsigned pos, const char *ArgName,
00238                      const std::string &Value);
00239 
00240   // Prints option name followed by message.  Always returns true.
00241   bool error(std::string Message, const char *ArgName = 0);
00242 
00243 public:
00244   inline int getNumOccurrences() const { return NumOccurrences; }
00245   virtual ~Option() {}
00246 };
00247 
00248 
00249 //===----------------------------------------------------------------------===//
00250 // Command line option modifiers that can be used to modify the behavior of
00251 // command line option parsers...
00252 //
00253 
00254 // desc - Modifier to set the description shown in the --help output...
00255 struct desc {
00256   const char *Desc;
00257   desc(const char *Str) : Desc(Str) {}
00258   void apply(Option &O) const { O.setDescription(Desc); }
00259 };
00260 
00261 // value_desc - Modifier to set the value description shown in the --help
00262 // output...
00263 struct value_desc {
00264   const char *Desc;
00265   value_desc(const char *Str) : Desc(Str) {}
00266   void apply(Option &O) const { O.setValueStr(Desc); }
00267 };
00268 
00269 // init - Specify a default (initial) value for the command line argument, if
00270 // the default constructor for the argument type does not give you what you
00271 // want.  This is only valid on "opt" arguments, not on "list" arguments.
00272 //
00273 template<class Ty>
00274 struct initializer {
00275   const Ty &Init;
00276   initializer(const Ty &Val) : Init(Val) {}
00277 
00278   template<class Opt>
00279   void apply(Opt &O) const { O.setInitialValue(Init); }
00280 };
00281 
00282 template<class Ty>
00283 initializer<Ty> init(const Ty &Val) {
00284   return initializer<Ty>(Val);
00285 }
00286 
00287 
00288 // location - Allow the user to specify which external variable they want to
00289 // store the results of the command line argument processing into, if they don't
00290 // want to store it in the option itself.
00291 //
00292 template<class Ty>
00293 struct LocationClass {
00294   Ty &Loc;
00295   LocationClass(Ty &L) : Loc(L) {}
00296 
00297   template<class Opt>
00298   void apply(Opt &O) const { O.setLocation(O, Loc); }
00299 };
00300 
00301 template<class Ty>
00302 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
00303 
00304 
00305 //===----------------------------------------------------------------------===//
00306 // Enum valued command line option
00307 //
00308 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, int(ENUMVAL), DESC
00309 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, int(ENUMVAL), DESC
00310 #define clEnumValEnd (reinterpret_cast<void*>(0))
00311 
00312 // values - For custom data types, allow specifying a group of values together
00313 // as the values that go into the mapping that the option handler uses.  Note
00314 // that the values list must always have a 0 at the end of the list to indicate
00315 // that the list has ended.
00316 //
00317 template<class DataType>
00318 class ValuesClass {
00319   // Use a vector instead of a map, because the lists should be short,
00320   // the overhead is less, and most importantly, it keeps them in the order
00321   // inserted so we can print our option out nicely.
00322   std::vector<std::pair<const char *, std::pair<int, const char *> > > Values;
00323   void processValues(va_list Vals);
00324 public:
00325   ValuesClass(const char *EnumName, DataType Val, const char *Desc,
00326               va_list ValueArgs) {
00327     // Insert the first value, which is required.
00328     Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
00329 
00330     // Process the varargs portion of the values...
00331     while (const char *EnumName = va_arg(ValueArgs, const char *)) {
00332       DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int));
00333       const char *EnumDesc = va_arg(ValueArgs, const char *);
00334       Values.push_back(std::make_pair(EnumName,      // Add value to value map
00335                                       std::make_pair(EnumVal, EnumDesc)));
00336     }
00337   }
00338 
00339   template<class Opt>
00340   void apply(Opt &O) const {
00341     for (unsigned i = 0, e = Values.size(); i != e; ++i)
00342       O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
00343                                      Values[i].second.second);
00344   }
00345 };
00346 
00347 template<class DataType>
00348 ValuesClass<DataType> END_WITH_NULL values(const char *Arg, DataType Val, 
00349                                            const char *Desc, ...) {
00350     va_list ValueArgs;
00351     va_start(ValueArgs, Desc);
00352     ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
00353     va_end(ValueArgs);
00354     return Vals;
00355 }
00356 
00357 
00358 //===----------------------------------------------------------------------===//
00359 // parser class - Parameterizable parser for different data types.  By default,
00360 // known data types (string, int, bool) have specialized parsers, that do what
00361 // you would expect.  The default parser, used for data types that are not
00362 // built-in, uses a mapping table to map specific options to values, which is
00363 // used, among other things, to handle enum types.
00364 
00365 //--------------------------------------------------
00366 // generic_parser_base - This class holds all the non-generic code that we do
00367 // not need replicated for every instance of the generic parser.  This also
00368 // allows us to put stuff into CommandLine.cpp
00369 //
00370 struct generic_parser_base {
00371   virtual ~generic_parser_base() {}  // Base class should have virtual-dtor
00372 
00373   // getNumOptions - Virtual function implemented by generic subclass to
00374   // indicate how many entries are in Values.
00375   //
00376   virtual unsigned getNumOptions() const = 0;
00377 
00378   // getOption - Return option name N.
00379   virtual const char *getOption(unsigned N) const = 0;
00380 
00381   // getDescription - Return description N
00382   virtual const char *getDescription(unsigned N) const = 0;
00383 
00384   // Return the width of the option tag for printing...
00385   virtual unsigned getOptionWidth(const Option &O) const;
00386 
00387   // printOptionInfo - Print out information about this option.  The
00388   // to-be-maintained width is specified.
00389   //
00390   virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
00391 
00392   void initialize(Option &O) {
00393     // All of the modifiers for the option have been processed by now, so the
00394     // argstr field should be stable, copy it down now.
00395     //
00396     hasArgStr = O.hasArgStr();
00397 
00398     // If there has been no argstr specified, that means that we need to add an
00399     // argument for every possible option.  This ensures that our options are
00400     // vectored to us.
00401     //
00402     if (!hasArgStr)
00403       for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
00404         O.addArgument(getOption(i));
00405   }
00406 
00407   enum ValueExpected getValueExpectedFlagDefault() const {
00408     // If there is an ArgStr specified, then we are of the form:
00409     //
00410     //    -opt=O2   or   -opt O2  or  -optO2
00411     //
00412     // In which case, the value is required.  Otherwise if an arg str has not
00413     // been specified, we are of the form:
00414     //
00415     //    -O2 or O2 or -la (where -l and -a are separate options)
00416     //
00417     // If this is the case, we cannot allow a value.
00418     //
00419     if (hasArgStr)
00420       return ValueRequired;
00421     else
00422       return ValueDisallowed;
00423   }
00424 
00425   // findOption - Return the option number corresponding to the specified
00426   // argument string.  If the option is not found, getNumOptions() is returned.
00427   //
00428   unsigned findOption(const char *Name);
00429 
00430 protected:
00431   bool hasArgStr;
00432 };
00433 
00434 // Default parser implementation - This implementation depends on having a
00435 // mapping of recognized options to values of some sort.  In addition to this,
00436 // each entry in the mapping also tracks a help message that is printed with the
00437 // command line option for --help.  Because this is a simple mapping parser, the
00438 // data type can be any unsupported type.
00439 //
00440 template <class DataType>
00441 class parser : public generic_parser_base {
00442 protected:
00443   std::vector<std::pair<const char *,
00444                         std::pair<DataType, const char *> > > Values;
00445 public:
00446   typedef DataType parser_data_type;
00447 
00448   // Implement virtual functions needed by generic_parser_base
00449   unsigned getNumOptions() const { return unsigned(Values.size()); }
00450   const char *getOption(unsigned N) const { return Values[N].first; }
00451   const char *getDescription(unsigned N) const {
00452     return Values[N].second.second;
00453   }
00454 
00455   // parse - Return true on error.
00456   bool parse(Option &O, const char *ArgName, const std::string &Arg,
00457              DataType &V) {
00458     std::string ArgVal;
00459     if (hasArgStr)
00460       ArgVal = Arg;
00461     else
00462       ArgVal = ArgName;
00463 
00464     for (unsigned i = 0, e = Values.size(); i != e; ++i)
00465       if (ArgVal == Values[i].first) {
00466         V = Values[i].second.first;
00467         return false;
00468       }
00469 
00470     return O.error(": Cannot find option named '" + ArgVal + "'!");
00471   }
00472 
00473   // addLiteralOption - Add an entry to the mapping table...
00474   template <class DT>
00475   void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
00476     assert(findOption(Name) == Values.size() && "Option already exists!");
00477     Values.push_back(std::make_pair(Name,
00478                              std::make_pair(static_cast<DataType>(V),HelpStr)));
00479   }
00480 
00481   // removeLiteralOption - Remove the specified option.
00482   //
00483   void removeLiteralOption(const char *Name) {
00484     unsigned N = findOption(Name);
00485     assert(N != Values.size() && "Option not found!");
00486     Values.erase(Values.begin()+N);
00487   }
00488 };
00489 
00490 //--------------------------------------------------
00491 // basic_parser - Super class of parsers to provide boilerplate code
00492 //
00493 struct basic_parser_impl {  // non-template implementation of basic_parser<t>
00494   virtual ~basic_parser_impl() {}
00495 
00496   enum ValueExpected getValueExpectedFlagDefault() const {
00497     return ValueRequired;
00498   }
00499 
00500   void initialize(Option &O) {}
00501 
00502   // Return the width of the option tag for printing...
00503   unsigned getOptionWidth(const Option &O) const;
00504 
00505   // printOptionInfo - Print out information about this option.  The
00506   // to-be-maintained width is specified.
00507   //
00508   void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
00509 
00510   // getValueName - Overload in subclass to provide a better default value.
00511   virtual const char *getValueName() const { return "value"; }
00512 };
00513 
00514 // basic_parser - The real basic parser is just a template wrapper that provides
00515 // a typedef for the provided data type.
00516 //
00517 template<class DataType>
00518 struct basic_parser : public basic_parser_impl {
00519   typedef DataType parser_data_type;
00520 };
00521 
00522 
00523 //--------------------------------------------------
00524 // parser<bool>
00525 //
00526 template<>
00527 class parser<bool> : public basic_parser<bool> {
00528 public:
00529   // parse - Return true on error.
00530   bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
00531 
00532   enum ValueExpected getValueExpectedFlagDefault() const {
00533     return ValueOptional;
00534   }
00535 
00536   // getValueName - Do not print =<value> at all
00537   virtual const char *getValueName() const { return 0; }
00538 };
00539 
00540 
00541 //--------------------------------------------------
00542 // parser<int>
00543 //
00544 template<>
00545 class parser<int> : public basic_parser<int> {
00546 public:
00547   // parse - Return true on error.
00548   bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
00549 
00550   // getValueName - Overload in subclass to provide a better default value.
00551   virtual const char *getValueName() const { return "int"; }
00552 };
00553 
00554 
00555 //--------------------------------------------------
00556 // parser<unsigned>
00557 //
00558 template<>
00559 class parser<unsigned> : public basic_parser<unsigned> {
00560 public:
00561   // parse - Return true on error.
00562   bool parse(Option &O, const char *AN, const std::string &Arg, unsigned &Val);
00563 
00564   // getValueName - Overload in subclass to provide a better default value.
00565   virtual const char *getValueName() const { return "uint"; }
00566 };
00567 
00568 
00569 //--------------------------------------------------
00570 // parser<double>
00571 //
00572 template<>
00573 class parser<double> : public basic_parser<double> {
00574 public:
00575   // parse - Return true on error.
00576   bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
00577 
00578   // getValueName - Overload in subclass to provide a better default value.
00579   virtual const char *getValueName() const { return "number"; }
00580 };
00581 
00582 
00583 //--------------------------------------------------
00584 // parser<float>
00585 //
00586 template<>
00587 class parser<float> : public basic_parser<float> {
00588 public:
00589   // parse - Return true on error.
00590   bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
00591 
00592   // getValueName - Overload in subclass to provide a better default value.
00593   virtual const char *getValueName() const { return "number"; }
00594 };
00595 
00596 
00597 //--------------------------------------------------
00598 // parser<std::string>
00599 //
00600 template<>
00601 class parser<std::string> : public basic_parser<std::string> {
00602 public:
00603   // parse - Return true on error.
00604   bool parse(Option &O, const char *AN, const std::string &Arg,
00605              std::string &Value) {
00606     Value = Arg;
00607     return false;
00608   }
00609 
00610   // getValueName - Overload in subclass to provide a better default value.
00611   virtual const char *getValueName() const { return "string"; }
00612 };
00613 
00614 //===----------------------------------------------------------------------===//
00615 // applicator class - This class is used because we must use partial
00616 // specialization to handle literal string arguments specially (const char* does
00617 // not correctly respond to the apply method).  Because the syntax to use this
00618 // is a pain, we have the 'apply' method below to handle the nastiness...
00619 //
00620 template<class Mod> struct applicator {
00621   template<class Opt>
00622   static void opt(const Mod &M, Opt &O) { M.apply(O); }
00623 };
00624 
00625 // Handle const char* as a special case...
00626 template<unsigned n> struct applicator<char[n]> {
00627   template<class Opt>
00628   static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
00629 };
00630 template<unsigned n> struct applicator<const char[n]> {
00631   template<class Opt>
00632   static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
00633 };
00634 template<> struct applicator<const char*> {
00635   template<class Opt>
00636   static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
00637 };
00638 
00639 template<> struct applicator<NumOccurrences> {
00640   static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); }
00641 };
00642 template<> struct applicator<ValueExpected> {
00643   static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
00644 };
00645 template<> struct applicator<OptionHidden> {
00646   static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
00647 };
00648 template<> struct applicator<FormattingFlags> {
00649   static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
00650 };
00651 template<> struct applicator<MiscFlags> {
00652   static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
00653 };
00654 
00655 // apply method - Apply a modifier to an option in a type safe way.
00656 template<class Mod, class Opt>
00657 void apply(const Mod &M, Opt *O) {
00658   applicator<Mod>::opt(M, *O);
00659 }
00660 
00661 
00662 //===----------------------------------------------------------------------===//
00663 // opt_storage class
00664 
00665 // Default storage class definition: external storage.  This implementation
00666 // assumes the user will specify a variable to store the data into with the
00667 // cl::location(x) modifier.
00668 //
00669 template<class DataType, bool ExternalStorage, bool isClass>
00670 class opt_storage {
00671   DataType *Location;   // Where to store the object...
00672 
00673   void check() {
00674     assert(Location != 0 && "cl::location(...) not specified for a command "
00675            "line option with external storage, "
00676            "or cl::init specified before cl::location()!!");
00677   }
00678 public:
00679   opt_storage() : Location(0) {}
00680 
00681   bool setLocation(Option &O, DataType &L) {
00682     if (Location)
00683       return O.error(": cl::location(x) specified more than once!");
00684     Location = &L;
00685     return false;
00686   }
00687 
00688   template<class T>
00689   void setValue(const T &V) {
00690     check();
00691     *Location = V;
00692   }
00693 
00694   DataType &getValue() { check(); return *Location; }
00695   const DataType &getValue() const { check(); return *Location; }
00696 };
00697 
00698 
00699 // Define how to hold a class type object, such as a string.  Since we can
00700 // inherit from a class, we do so.  This makes us exactly compatible with the
00701 // object in all cases that it is used.
00702 //
00703 template<class DataType>
00704 class opt_storage<DataType,false,true> : public DataType {
00705 public:
00706   template<class T>
00707   void setValue(const T &V) { DataType::operator=(V); }
00708 
00709   DataType &getValue() { return *this; }
00710   const DataType &getValue() const { return *this; }
00711 };
00712 
00713 // Define a partial specialization to handle things we cannot inherit from.  In
00714 // this case, we store an instance through containment, and overload operators
00715 // to get at the value.
00716 //
00717 template<class DataType>
00718 class opt_storage<DataType, false, false> {
00719 public:
00720   DataType Value;
00721 
00722   // Make sure we initialize the value with the default constructor for the
00723   // type.
00724   opt_storage() : Value(DataType()) {}
00725 
00726   template<class T>
00727   void setValue(const T &V) { Value = V; }
00728   DataType &getValue() { return Value; }
00729   DataType getValue() const { return Value; }
00730 
00731   // If the datatype is a pointer, support -> on it.
00732   DataType operator->() const { return Value; }
00733 };
00734 
00735 
00736 //===----------------------------------------------------------------------===//
00737 // opt - A scalar command line option.
00738 //
00739 template <class DataType, bool ExternalStorage = false,
00740           class ParserClass = parser<DataType> >
00741 class opt : public Option,
00742             public opt_storage<DataType, ExternalStorage,
00743                                is_class<DataType>::value> {
00744   ParserClass Parser;
00745 
00746   virtual bool handleOccurrence(unsigned pos, const char *ArgName,
00747                                 const std::string &Arg) {
00748     typename ParserClass::parser_data_type Val =
00749        typename ParserClass::parser_data_type();
00750     if (Parser.parse(*this, ArgName, Arg, Val))
00751       return true;                            // Parse error!
00752     setValue(Val);
00753     setPosition(pos);
00754     return false;
00755   }
00756 
00757   virtual enum ValueExpected getValueExpectedFlagDefault() const {
00758     return Parser.getValueExpectedFlagDefault();
00759   }
00760 
00761   // Forward printing stuff to the parser...
00762   virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
00763   virtual void printOptionInfo(unsigned GlobalWidth) const {
00764     Parser.printOptionInfo(*this, GlobalWidth);
00765   }
00766 
00767   void done() {
00768     addArgument(ArgStr);
00769     Parser.initialize(*this);
00770   }
00771 public:
00772   // setInitialValue - Used by the cl::init modifier...
00773   void setInitialValue(const DataType &V) { this->setValue(V); }
00774 
00775   ParserClass &getParser() { return Parser; }
00776 
00777   operator DataType() const { return this->getValue(); }
00778 
00779   template<class T>
00780   DataType &operator=(const T &Val) {
00781     this->setValue(Val);
00782     return this->getValue();
00783   }
00784 
00785   // One option...
00786   template<class M0t>
00787   opt(const M0t &M0) {
00788     apply(M0, this);
00789     done();
00790   }
00791 
00792   // Two options...
00793   template<class M0t, class M1t>
00794   opt(const M0t &M0, const M1t &M1) {
00795     apply(M0, this); apply(M1, this);
00796     done();
00797   }
00798 
00799   // Three options...
00800   template<class M0t, class M1t, class M2t>
00801   opt(const M0t &M0, const M1t &M1, const M2t &M2) {
00802     apply(M0, this); apply(M1, this); apply(M2, this);
00803     done();
00804   }
00805   // Four options...
00806   template<class M0t, class M1t, class M2t, class M3t>
00807   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
00808     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
00809     done();
00810   }
00811   // Five options...
00812   template<class M0t, class M1t, class M2t, class M3t, class M4t>
00813   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
00814       const M4t &M4) {
00815     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
00816     apply(M4, this);
00817     done();
00818   }
00819   // Six options...
00820   template<class M0t, class M1t, class M2t, class M3t,
00821            class M4t, class M5t>
00822   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
00823       const M4t &M4, const M5t &M5) {
00824     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
00825     apply(M4, this); apply(M5, this);
00826     done();
00827   }
00828   // Seven options...
00829   template<class M0t, class M1t, class M2t, class M3t,
00830            class M4t, class M5t, class M6t>
00831   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
00832       const M4t &M4, const M5t &M5, const M6t &M6) {
00833     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
00834     apply(M4, this); apply(M5, this); apply(M6, this);
00835     done();
00836   }
00837   // Eight options...
00838   template<class M0t, class M1t, class M2t, class M3t,
00839            class M4t, class M5t, class M6t, class M7t>
00840   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
00841       const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
00842     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
00843     apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
00844     done();
00845   }
00846 };
00847 
00848 //===----------------------------------------------------------------------===//
00849 // list_storage class
00850 
00851 // Default storage class definition: external storage.  This implementation
00852 // assumes the user will specify a variable to store the data into with the
00853 // cl::location(x) modifier.
00854 //
00855 template<class DataType, class StorageClass>
00856 class list_storage {
00857   StorageClass *Location;   // Where to store the object...
00858 
00859 public:
00860   list_storage() : Location(0) {}
00861 
00862   bool setLocation(Option &O, StorageClass &L) {
00863     if (Location)
00864       return O.error(": cl::location(x) specified more than once!");
00865     Location = &L;
00866     return false;
00867   }
00868 
00869   template<class T>
00870   void addValue(const T &V) {
00871     assert(Location != 0 && "cl::location(...) not specified for a command "
00872            "line option with external storage!");
00873     Location->push_back(V);
00874   }
00875 };
00876 
00877 
00878 // Define how to hold a class type object, such as a string.  Since we can
00879 // inherit from a class, we do so.  This makes us exactly compatible with the
00880 // object in all cases that it is used.
00881 //
00882 template<class DataType>
00883 class list_storage<DataType, bool> : public std::vector<DataType> {
00884 public:
00885   template<class T>
00886   void addValue(const T &V) { push_back(V); }
00887 };
00888 
00889 
00890 //===----------------------------------------------------------------------===//
00891 // list - A list of command line options.
00892 //
00893 template <class DataType, class Storage = bool,
00894           class ParserClass = parser<DataType> >
00895 class list : public Option, public list_storage<DataType, Storage> {
00896   std::vector<unsigned> Positions;
00897   ParserClass Parser;
00898 
00899   virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
00900     return ZeroOrMore;
00901   }
00902   virtual enum ValueExpected getValueExpectedFlagDefault() const {
00903     return Parser.getValueExpectedFlagDefault();
00904   }
00905 
00906   virtual bool handleOccurrence(unsigned pos, const char *ArgName,
00907                                 const std::string &Arg) {
00908     typename ParserClass::parser_data_type Val =
00909       typename ParserClass::parser_data_type();
00910     if (Parser.parse(*this, ArgName, Arg, Val))
00911       return true;  // Parse Error!
00912     addValue(Val);
00913     setPosition(pos);
00914     Positions.push_back(pos);
00915     return false;
00916   }
00917 
00918   // Forward printing stuff to the parser...
00919   virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
00920   virtual void printOptionInfo(unsigned GlobalWidth) const {
00921     Parser.printOptionInfo(*this, GlobalWidth);
00922   }
00923 
00924   void done() {
00925     addArgument(ArgStr);
00926     Parser.initialize(*this);
00927   }
00928 public:
00929   ParserClass &getParser() { return Parser; }
00930 
00931   unsigned getPosition(unsigned optnum) const {
00932     assert(optnum < this->size() && "Invalid option index");
00933     return Positions[optnum];
00934   }
00935 
00936   // One option...
00937   template<class M0t>
00938   list(const M0t &M0) {
00939     apply(M0, this);
00940     done();
00941   }
00942   // Two options...
00943   template<class M0t, class M1t>
00944   list(const M0t &M0, const M1t &M1) {
00945     apply(M0, this); apply(M1, this);
00946     done();
00947   }
00948   // Three options...
00949   template<class M0t, class M1t, class M2t>
00950   list(const M0t &M0, const M1t &M1, const M2t &M2) {
00951     apply(M0, this); apply(M1, this); apply(M2, this);
00952     done();
00953   }
00954   // Four options...
00955   template<class M0t, class M1t, class M2t, class M3t>
00956   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
00957     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
00958     done();
00959   }
00960   // Five options...
00961   template<class M0t, class M1t, class M2t, class M3t, class M4t>
00962   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
00963        const M4t &M4) {
00964     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
00965     apply(M4, this);
00966     done();
00967   }
00968   // Six options...
00969   template<class M0t, class M1t, class M2t, class M3t,
00970            class M4t, class M5t>
00971   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
00972        const M4t &M4, const M5t &M5) {
00973     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
00974     apply(M4, this); apply(M5, this);
00975     done();
00976   }
00977   // Seven options...
00978   template<class M0t, class M1t, class M2t, class M3t,
00979            class M4t, class M5t, class M6t>
00980   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
00981       const M4t &M4, const M5t &M5, const M6t &M6) {
00982     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
00983     apply(M4, this); apply(M5, this); apply(M6, this);
00984     done();
00985   }
00986   // Eight options...
00987   template<class M0t, class M1t, class M2t, class M3t,
00988            class M4t, class M5t, class M6t, class M7t>
00989   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
00990       const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
00991     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
00992     apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
00993     done();
00994   }
00995 };
00996 
00997 //===----------------------------------------------------------------------===//
00998 // bits_storage class
00999 
01000 // Default storage class definition: external storage.  This implementation
01001 // assumes the user will specify a variable to store the data into with the
01002 // cl::location(x) modifier.
01003 //
01004 template<class DataType, class StorageClass>
01005 class bits_storage {
01006   unsigned *Location;   // Where to store the bits...
01007   
01008   template<class T>
01009   static unsigned Bit(const T &V) {
01010     unsigned BitPos = reinterpret_cast<unsigned>(V);
01011     assert(BitPos < sizeof(unsigned) * 8 &&
01012           "enum exceeds width of bit vector!");
01013     return 1 << BitPos;
01014   }
01015 
01016 public:
01017   bits_storage() : Location(0) {}
01018 
01019   bool setLocation(Option &O, unsigned &L) {
01020     if (Location)
01021       return O.error(": cl::location(x) specified more than once!");
01022     Location = &L;
01023     return false;
01024   }
01025 
01026   template<class T>
01027   void addValue(const T &V) {
01028     assert(Location != 0 && "cl::location(...) not specified for a command "
01029            "line option with external storage!");
01030     *Location |= Bit(V);
01031   }
01032   
01033   unsigned getBits() { return *Location; }
01034   
01035   template<class T>
01036   bool isSet(const T &V) {
01037     return (*Location & Bit(V)) != 0;
01038   }
01039 };
01040 
01041 
01042 // Define how to hold bits.  Since we can inherit from a class, we do so. 
01043 // This makes us exactly compatible with the bits in all cases that it is used.
01044 //
01045 template<class DataType>
01046 class bits_storage<DataType, bool> {
01047   unsigned Bits;   // Where to store the bits...
01048   
01049   template<class T>
01050   static unsigned Bit(const T &V) {
01051     unsigned BitPos = reinterpret_cast<unsigned>(V);
01052     assert(BitPos < sizeof(unsigned) * 8 &&
01053           "enum exceeds width of bit vector!");
01054     return 1 << BitPos;
01055   }
01056   
01057 public:
01058   template<class T>
01059   void addValue(const T &V) {
01060     Bits |=  Bit(V);
01061   }
01062   
01063   unsigned getBits() { return Bits; }
01064   
01065   template<class T>
01066   bool isSet(const T &V) {
01067     return (Bits & Bit(V)) != 0;
01068   }
01069 };
01070 
01071 
01072 //===----------------------------------------------------------------------===//
01073 // bits - A bit vector of command options.
01074 //
01075 template <class DataType, class Storage = bool,
01076           class ParserClass = parser<DataType> >
01077 class bits : public Option, public bits_storage<DataType, Storage> {
01078   std::vector<unsigned> Positions;
01079   ParserClass Parser;
01080 
01081   virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
01082     return ZeroOrMore;
01083   }
01084   virtual enum ValueExpected getValueExpectedFlagDefault() const {
01085     return Parser.getValueExpectedFlagDefault();
01086   }
01087 
01088   virtual bool handleOccurrence(unsigned pos, const char *ArgName,
01089                                 const std::string &Arg) {
01090     typename ParserClass::parser_data_type Val =
01091       typename ParserClass::parser_data_type();
01092     if (Parser.parse(*this, ArgName, Arg, Val))
01093       return true;  // Parse Error!
01094     addValue(Val);
01095     setPosition(pos);
01096     Positions.push_back(pos);
01097     return false;
01098   }
01099 
01100   // Forward printing stuff to the parser...
01101   virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
01102   virtual void printOptionInfo(unsigned GlobalWidth) const {
01103     Parser.printOptionInfo(*this, GlobalWidth);
01104   }
01105 
01106   void done() {
01107     addArgument(ArgStr);
01108     Parser.initialize(*this);
01109   }
01110 public:
01111   ParserClass &getParser() { return Parser; }
01112 
01113   unsigned getPosition(unsigned optnum) const {
01114     assert(optnum < this->size() && "Invalid option index");
01115     return Positions[optnum];
01116   }
01117 
01118   // One option...
01119   template<class M0t>
01120   bits(const M0t &M0) {
01121     apply(M0, this);
01122     done();
01123   }
01124   // Two options...
01125   template<class M0t, class M1t>
01126   bits(const M0t &M0, const M1t &M1) {
01127     apply(M0, this); apply(M1, this);
01128     done();
01129   }
01130   // Three options...
01131   template<class M0t, class M1t, class M2t>
01132   bits(const M0t &M0, const M1t &M1, const M2t &M2) {
01133     apply(M0, this); apply(M1, this); apply(M2, this);
01134     done();
01135   }
01136   // Four options...
01137   template<class M0t, class M1t, class M2t, class M3t>
01138   bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
01139     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
01140     done();
01141   }
01142   // Five options...
01143   template<class M0t, class M1t, class M2t, class M3t, class M4t>
01144   bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
01145        const M4t &M4) {
01146     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
01147     apply(M4, this);
01148     done();
01149   }
01150   // Six options...
01151   template<class M0t, class M1t, class M2t, class M3t,
01152            class M4t, class M5t>
01153   bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
01154        const M4t &M4, const M5t &M5) {
01155     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
01156     apply(M4, this); apply(M5, this);
01157     done();
01158   }
01159   // Seven options...
01160   template<class M0t, class M1t, class M2t, class M3t,
01161            class M4t, class M5t, class M6t>
01162   bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
01163       const M4t &M4, const M5t &M5, const M6t &M6) {
01164     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
01165     apply(M4, this); apply(M5, this); apply(M6, this);
01166     done();
01167   }
01168   // Eight options...
01169   template<class M0t, class M1t, class M2t, class M3t,
01170            class M4t, class M5t, class M6t, class M7t>
01171   bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
01172       const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
01173     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
01174     apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
01175     done();
01176   }
01177 };
01178 
01179 //===----------------------------------------------------------------------===//
01180 // Aliased command line option (alias this name to a preexisting name)
01181 //
01182 
01183 class alias : public Option {
01184   Option *AliasFor;
01185   virtual bool handleOccurrence(unsigned pos, const char *ArgName,
01186                                 const std::string &Arg) {
01187     return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
01188   }
01189   // Aliases default to be hidden...
01190   virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
01191 
01192   // Handle printing stuff...
01193   virtual unsigned getOptionWidth() const;
01194   virtual void printOptionInfo(unsigned GlobalWidth) const;
01195 
01196   void done() {
01197     if (!hasArgStr())
01198       error(": cl::alias must have argument name specified!");
01199     if (AliasFor == 0)
01200       error(": cl::alias must have an cl::aliasopt(option) specified!");
01201     addArgument(ArgStr);
01202   }
01203 public:
01204   void setAliasFor(Option &O) {
01205     if (AliasFor)
01206       error(": cl::alias must only have one cl::aliasopt(...) specified!");
01207     AliasFor = &O;
01208   }
01209 
01210   // One option...
01211   template<class M0t>
01212   alias(const M0t &M0) : AliasFor(0) {
01213     apply(M0, this);
01214     done();
01215   }
01216   // Two options...
01217   template<class M0t, class M1t>
01218   alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
01219     apply(M0, this); apply(M1, this);
01220     done();
01221   }
01222   // Three options...
01223   template<class M0t, class M1t, class M2t>
01224   alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) {
01225     apply(M0, this); apply(M1, this); apply(M2, this);
01226     done();
01227   }
01228   // Four options...
01229   template<class M0t, class M1t, class M2t, class M3t>
01230   alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
01231     : AliasFor(0) {
01232     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
01233     done();
01234   }
01235 };
01236 
01237 // aliasfor - Modifier to set the option an alias aliases.
01238 struct aliasopt {
01239   Option &Opt;
01240   aliasopt(Option &O) : Opt(O) {}
01241   void apply(alias &A) const { A.setAliasFor(Opt); }
01242 };
01243 
01244 // extrahelp - provide additional help at the end of the normal help
01245 // output. All occurrences of cl::extrahelp will be accumulated and
01246 // printed to std::cerr at the end of the regular help, just before
01247 // exit is called.
01248 struct extrahelp {
01249   const char * morehelp;
01250   extrahelp(const char* help);
01251 };
01252 
01253 // This function just prints the help message, exactly the same way as if the
01254 // --help option had been given on the command line.
01255 // NOTE: THIS FUNCTION TERMINATES THE PROGRAM!
01256 void PrintHelpMessage();
01257 
01258 } // End namespace cl
01259 
01260 } // End namespace llvm
01261 
01262 #endif