Pass argc by value, not by reference, since it isn't modified.
[oota-llvm.git] / lib / Support / CommandLine.cpp
index 3d30e7a9f52ec1103af43a1a9a63a2c966d349fb..eed780437bb0b97416c42eb59e0c9990c1534a6d 100644 (file)
@@ -36,6 +36,7 @@ using namespace cl;
 // Template instantiations and anchors.
 //
 TEMPLATE_INSTANTIATION(class basic_parser<bool>);
+TEMPLATE_INSTANTIATION(class basic_parser<boolOrDefault>);
 TEMPLATE_INSTANTIATION(class basic_parser<int>);
 TEMPLATE_INSTANTIATION(class basic_parser<unsigned>);
 TEMPLATE_INSTANTIATION(class basic_parser<double>);
@@ -50,6 +51,7 @@ TEMPLATE_INSTANTIATION(class opt<bool>);
 void Option::anchor() {}
 void basic_parser_impl::anchor() {}
 void parser<bool>::anchor() {}
+void parser<boolOrDefault>::anchor() {}
 void parser<int>::anchor() {}
 void parser<unsigned>::anchor() {}
 void parser<double>::anchor() {}
@@ -71,6 +73,13 @@ extrahelp::extrahelp(const char *Help)
   MoreHelp->push_back(Help);
 }
 
+static bool OptionListChanged = false;
+
+// MarkOptionsChanged - Internal helper function.
+void cl::MarkOptionsChanged() {
+  OptionListChanged = true;
+}
+
 /// RegisteredOptionList - This is the list of the command line options that
 /// have statically constructed themselves.
 static Option *RegisteredOptionList = 0;
@@ -80,8 +89,10 @@ void Option::addArgument() {
   
   NextRegistered = RegisteredOptionList;
   RegisteredOptionList = this;
+  MarkOptionsChanged();
 }
 
+
 //===----------------------------------------------------------------------===//
 // Basic, shared command line option processing machinery.
 //
@@ -321,7 +332,7 @@ void cl::ParseEnvironmentOptions(const char *progName, const char *envVar,
     free (*i);
 }
 
-void cl::ParseCommandLineOptions(int &argc, char **argv,
+void cl::ParseCommandLineOptions(int argc, char **argv,
                                  const char *Overview) {
   // Process all registered options.
   std::vector<Option*> PositionalOpts;
@@ -394,10 +405,6 @@ void cl::ParseCommandLineOptions(int &argc, char **argv,
   // the positional args into the PositionalVals list...
   Option *ActivePositionalArg = 0;
 
-  // Keep track of the option list so far so that we can tell if it is ever
-  // extended.
-  Option *CurOptionList = RegisteredOptionList;
-  
   // Loop over all of the arguments... processing them.
   bool DashDashFound = false;  // Have we read '--'?
   for (int i = 1; i < argc; ++i) {
@@ -405,14 +412,14 @@ void cl::ParseCommandLineOptions(int &argc, char **argv,
     const char *Value = 0;
     const char *ArgName = "";
 
-    // If the head of the option list changed, this means that some command line
+    // If the option list changed, this means that some command line
     // option has just been registered or deregistered.  This can occur in
     // response to things like -load, etc.  If this happens, rescan the options.
-    if (CurOptionList != RegisteredOptionList) {
+    if (OptionListChanged) {
       PositionalOpts.clear();
       Opts.clear();
       GetOptionInfo(PositionalOpts, Opts);
-      CurOptionList = RegisteredOptionList;
+      OptionListChanged = false;
     }
     
     // Check to see if this is a positional argument.  This argument is
@@ -762,6 +769,22 @@ bool parser<bool>::parse(Option &O, const char *ArgName,
   return false;
 }
 
+// parser<boolOrDefault> implementation
+//
+bool parser<boolOrDefault>::parse(Option &O, const char *ArgName,
+                         const std::string &Arg, boolOrDefault &Value) {
+  if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
+      Arg == "1") {
+    Value = BOU_TRUE;
+  } else if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
+    Value = BOU_FALSE;
+  } else {
+    return O.error(": '" + Arg +
+                   "' is invalid value for boolean argument! Try 0 or 1");
+  }
+  return false;
+}
+
 // parser<int> implementation
 //
 bool parser<int>::parse(Option &O, const char *ArgName,
@@ -928,7 +951,7 @@ public:
     }
 
     if (ProgramOverview)
-      cout << "OVERVIEW:" << ProgramOverview << "\n";
+      cout << "OVERVIEW: " << ProgramOverview << "\n";
 
     cout << "USAGE: " << ProgramName << " [options]";