Regularize the names of #include-guards.
[oota-llvm.git] / include / Support / CommandLine.h
index 9e02a6cf41712a74637c8448506d3a3d407e0b34..728bb1467835f71e9a886e6f92e4dc27b161e035 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_SUPPORT_COMMANDLINE_H
-#define LLVM_SUPPORT_COMMANDLINE_H
+#ifndef SUPPORT_COMMANDLINE_H
+#define SUPPORT_COMMANDLINE_H
 
 #include <string>
 #include <vector>
 #include <utility>
-#include <stdarg.h>
+#include <cstdarg>
 #include "boost/type_traits/object_traits.hpp"
 
-namespace cl {   // Short namespace to make usage concise
+#include <assert.h>
+
+/// cl Namespace - This namespace contains all of the command line option
+/// processing machinery.  It is intentionally a short name to make qualified
+/// usage concise.
+namespace cl {
 
 //===----------------------------------------------------------------------===//
 // ParseCommandLineOptions - Command line option processing entry point.
@@ -50,7 +55,7 @@ enum NumOccurances {           // Flags for the number of occurances allowed...
 };
 
 enum ValueExpected {           // Is a value required for the option?
-  ValueOptional   = 0x08,      // The value can oppear... or not
+  ValueOptional   = 0x08,      // The value can appear... or not
   ValueRequired   = 0x10,      // The value is required to appear!
   ValueDisallowed = 0x18,      // A value may not be specified (for flags)
   ValueMask       = 0x18,
@@ -86,6 +91,12 @@ enum FormattingFlags {
   FormattingMask   = 0x180,
 };
 
+enum MiscFlags {                // Miscellaneous flags to adjust argument
+  CommaSeparated   = 0x200,     // Should this cl::list split between commas?
+  MiscMask         = 0x200,
+};
+
+
 
 //===----------------------------------------------------------------------===//
 // Option Base class
@@ -137,6 +148,9 @@ public:
     int OH = Flags & FormattingMask;
     return OH ? (enum FormattingFlags)OH : getFormattingFlagDefault();
   }
+  inline unsigned getMiscFlags() const {
+    return Flags & MiscMask;
+  }
 
   // hasArgStr - Return true if the argstr != ""
   bool hasArgStr() const { return ArgStr[0] != 0; }
@@ -163,7 +177,7 @@ public:
   void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
   void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
   void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
-
+  void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
 protected:
   Option() : NumOccurances(0), Flags(0),
              ArgStr(""), HelpStr(""), ValueStr("") {}
@@ -372,6 +386,11 @@ struct generic_parser_base {
       return ValueDisallowed;
   }
 
+  // findOption - Return the option number corresponding to the specified
+  // argument string.  If the option is not found, getNumOptions() is returned.
+  //
+  unsigned findOption(const char *Name);
+
 protected:
   bool hasArgStr;
 };
@@ -384,8 +403,11 @@ protected:
 //
 template <class DataType>
 class parser : public generic_parser_base {
+protected:
   std::vector<std::pair<const char *,
                         std::pair<DataType, const char *> > > Values;
+public:
+  typedef DataType parser_data_type;
 
   // Implement virtual functions needed by generic_parser_base
   unsigned getNumOptions() const { return Values.size(); }
@@ -394,11 +416,10 @@ class parser : public generic_parser_base {
     return Values[N].second.second;
   }
 
-public:
-  // Default implementation, requires user to populate it with values somehow.
-  template<class Opt>   // parse - Return true on error.
-  bool parse(Opt &O, const char *ArgName, const string &Arg) {
-    string ArgVal;
+  // parse - Return true on error.
+  bool parse(Option &O, const char *ArgName, const std::string &Arg,
+             DataType &V) {
+    std::string ArgVal;
     if (hasArgStr)
       ArgVal = Arg;
     else
@@ -406,7 +427,7 @@ public:
 
     for (unsigned i = 0, e = Values.size(); i != e; ++i)
       if (ArgVal == Values[i].first) {
-        O.addValue(Values[i].second.first);
+        V = Values[i].second.first;
         return false;
       }
 
@@ -416,137 +437,125 @@ public:
   // addLiteralOption - Add an entry to the mapping table...
   template <class DT>
   void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
+    assert(findOption(Name) == Values.size() && "Option already exists!");
     Values.push_back(std::make_pair(Name, std::make_pair((DataType)V,HelpStr)));
   }
-};
 
+  // removeLiteralOption - Remove the specified option.
+  //
+  void removeLiteralOption(const char *Name) {
+    unsigned N = findOption(Name);
+    assert(N != Values.size() && "Option not found!");
+    Values.erase(Values.begin()+N);
+  }
+};
 
 //--------------------------------------------------
-// parser<bool>
+// basic_parser - Super class of parsers to provide boilerplate code
 //
-template<>
-class parser<bool> {
-  static bool parseImpl(Option &O, const string &Arg, bool &Val);
-public:
-  
-  template<class Opt>     // parse - Return true on error.
-  bool parse(Opt &O, const char *ArgName, const string &Arg) {
-    bool Val;
-    bool Error = parseImpl(O, Arg, Val);
-    if (!Error) O.addValue(Val);
-    return Error;
-  }
+struct basic_parser_impl {  // non-template implementation of basic_parser<t>
+  virtual ~basic_parser_impl() {}
 
   enum ValueExpected getValueExpectedFlagDefault() const {
-    return ValueOptional; 
+    return ValueRequired;
   }
-
+  
   void initialize(Option &O) {}
-
+  
   // Return the width of the option tag for printing...
-  virtual unsigned getOptionWidth(const Option &O) const;
-
-  // printOptionInfo - Print out information about this option.  The 
+  unsigned getOptionWidth(const Option &O) const;
+  
+  // printOptionInfo - Print out information about this option.  The
   // to-be-maintained width is specified.
   //
-  virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
+  void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
+
+
+  // getValueName - Overload in subclass to provide a better default value.
+  virtual const char *getValueName() const { return "value"; }
+};
+
+// basic_parser - The real basic parser is just a template wrapper that provides
+// a typedef for the provided data type.
+//
+template<class DataType>
+struct basic_parser : public basic_parser_impl {
+  typedef DataType parser_data_type;
 };
 
 
 //--------------------------------------------------
-// parser<int>
+// parser<bool>
 //
 template<>
-class parser<int> {
-  static bool parseImpl(Option &O, const string &Arg, int &Val);
-public:
-  
+struct parser<bool> : public basic_parser<bool> {
+
   // parse - Return true on error.
-  template<class Opt>
-  bool parse(Opt &O, const char *ArgName, const string &Arg) {
-    int Val;
-    bool Error = parseImpl(O, Arg, Val);
-    if (!Error) O.addValue(Val);
-    return Error;
-  }
+  bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
 
   enum ValueExpected getValueExpectedFlagDefault() const {
-    return ValueRequired
+    return ValueOptional
   }
 
-  void initialize(Option &O) {}
-
-  // Return the width of the option tag for printing...
-  virtual unsigned getOptionWidth(const Option &O) const;
-
-  // printOptionInfo - Print out information about this option.  The 
-  // to-be-maintained width is specified.
-  //
-  virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
+  // getValueName - Do not print =<value> at all
+  virtual const char *getValueName() const { return 0; }
 };
 
 
 //--------------------------------------------------
-// parser<double>
+// parser<int>
 //
 template<>
-class parser<double> {
-  static bool parseImpl(Option &O, const string &Arg, double &Val);
-public:
+struct parser<int> : public basic_parser<int> {
   
   // parse - Return true on error.
-  template<class Opt>
-  bool parse(Opt &O, const char *ArgName, const string &Arg) {
-    double Val;
-    bool Error = parseImpl(O, Arg, Val);
-    if (!Error) O.addValue(Val);
-    return Error;
-  }
+  bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
 
-  enum ValueExpected getValueExpectedFlagDefault() const {
-    return ValueRequired; 
-  }
+  // getValueName - Overload in subclass to provide a better default value.
+  virtual const char *getValueName() const { return "int"; }
+};
 
-  void initialize(Option &O) {}
 
-  // Return the width of the option tag for printing...
-  virtual unsigned getOptionWidth(const Option &O) const;
+//--------------------------------------------------
+// parser<double>
+//
+template<>
+struct parser<double> : public basic_parser<double> {
+  // parse - Return true on error.
+  bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
 
-  // printOptionInfo - Print out information about this option.  The 
-  // to-be-maintained width is specified.
-  //
-  virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
+  // getValueName - Overload in subclass to provide a better default value.
+  virtual const char *getValueName() const { return "number"; }
 };
 
-// Parser<float> is the same as parser<double>
-template<> struct parser<float> : public parser<double> {};
 
+//--------------------------------------------------
+// parser<float>
+//
+template<>
+struct parser<float> : public basic_parser<float> {
+  // parse - Return true on error.
+  bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
+
+  // getValueName - Overload in subclass to provide a better default value.
+  virtual const char *getValueName() const { return "number"; }
+};
 
 
 //--------------------------------------------------
-// parser<string>
+// parser<std::string>
 //
 template<>
-struct parser<string> {
+struct parser<std::string> : public basic_parser<std::string> {
   // parse - Return true on error.
-  template<class Opt>
-  bool parse(Opt &O, const char *ArgName, const string &Arg) {
-    O.addValue(Arg);
+  bool parse(Option &O, const char *ArgName, const std::string &Arg,
+             std::string &Value) {
+    Value = Arg;
     return false;
   }
-  enum ValueExpected getValueExpectedFlagDefault() const {
-    return ValueRequired;
-  }
-
-  void initialize(Option &O) {}
 
-  // Return the width of the option tag for printing...
-  virtual unsigned getOptionWidth(const Option &O) const;
-
-  // printOptionInfo - Print out information about this option.  The 
-  // to-be-maintained width is specified.
-  //
-  virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
+  // getValueName - Overload in subclass to provide a better default value.
+  virtual const char *getValueName() const { return "string"; }
 };
 
 
@@ -567,6 +576,10 @@ template<unsigned n> struct applicator<char[n]> {
   template<class Opt>
   static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
 };
+template<unsigned n> struct applicator<const char[n]> {
+  template<class Opt>
+  static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
+};
 template<> struct applicator<const char*> {
   template<class Opt>
   static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
@@ -584,6 +597,9 @@ template<> struct applicator<OptionHidden> {
 template<> struct applicator<FormattingFlags> {
   static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
 };
+template<> struct applicator<MiscFlags> {
+  static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
+};
 
 // apply method - Apply a modifier to an option in a type safe way.
 template<class Mod, class Opt>
@@ -668,11 +684,15 @@ template <class DataType, bool ExternalStorage = false,
           class ParserClass = parser<DataType> >
 class opt : public Option, 
             public opt_storage<DataType, ExternalStorage,
-                               ::boost::is_class<DataType>::value>{
+                               ::boost::is_class<DataType>::value> {
   ParserClass Parser;
 
   virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
-    return Parser.parse(*this, ArgName, Arg);
+    typename ParserClass::parser_data_type Val;
+    if (Parser.parse(*this, ArgName, Arg, Val))
+      return true;                            // Parse error!
+    setValue(Val);
+    return false;
   }
 
   virtual enum ValueExpected getValueExpectedFlagDefault() const {
@@ -693,9 +713,6 @@ public:
   // setInitialValue - Used by the cl::init modifier...
   void setInitialValue(const DataType &V) { setValue(V); }
 
-  // addValue - Used by the parser to add a value to the option
-  template<class T>
-  void addValue(const T &V) { setValue(V); }
   ParserClass &getParser() { return Parser; }
 
   operator DataType() const { return getValue(); }
@@ -824,7 +841,11 @@ class list : public Option, public list_storage<DataType, Storage> {
   }
 
   virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
-    return Parser.parse(*this, ArgName, Arg);
+    typename ParserClass::parser_data_type Val;
+    if (Parser.parse(*this, ArgName, Arg, Val))
+      return true;  // Parse Error!
+    addValue(Val);
+    return false;
   }
 
   // Forward printing stuff to the parser...