LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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