[llvm] r186703 - [Option] Add inclusion and exclusion flags to option parsing

Reid Kleckner reid at kleckner.net
Fri Jul 19 11:04:57 PDT 2013


Author: rnk
Date: Fri Jul 19 13:04:57 2013
New Revision: 186703

URL: http://llvm.org/viewvc/llvm-project?rev=186703&view=rev
Log:
[Option] Add inclusion and exclusion flags to option parsing

Summary:
This allows the clang driver to put MSVC compatible options in the same
enumerator space as its normal options but exclude them from normal
option parsing.

Also changes the standard ParseArgs() method to consider unknown
arguments with a leading slash as being inputs rather than flags.

High level discussion for clang-cl is here:
http://lists.cs.uiuc.edu/pipermail/cfe-dev/2013-June/030404.html

CC: llvm-commits

Differential Revision: http://llvm-reviews.chandlerc.com/D1049

Modified:
    llvm/trunk/include/llvm/Option/OptTable.h
    llvm/trunk/lib/Option/OptTable.cpp

Modified: llvm/trunk/include/llvm/Option/OptTable.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Option/OptTable.h?rev=186703&r1=186702&r2=186703&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Option/OptTable.h (original)
+++ llvm/trunk/include/llvm/Option/OptTable.h Fri Jul 19 13:04:57 2013
@@ -116,11 +116,17 @@ public:
   /// \param [in,out] Index - The current parsing position in the argument
   /// string list; on return this will be the index of the next argument
   /// string to parse.
+  /// \param [in] FlagsToInclude - Only parse options with any of these flags.
+  /// Zero is the default which includes all flags.
+  /// \param [in] FlagsToExclude - Don't parse options with this flag.  Zero
+  /// is the default and means exclude nothing.
   ///
   /// \return The parsed argument, or 0 if the argument is missing values
   /// (in which case Index still points at the conceptual next argument string
   /// to parse).
-  Arg *ParseOneArg(const ArgList &Args, unsigned &Index) const;
+  Arg *ParseOneArg(const ArgList &Args, unsigned &Index,
+                   unsigned FlagsToInclude = 0,
+                   unsigned FlagsToExclude = 0) const;
 
   /// \brief Parse an list of arguments into an InputArgList.
   ///
@@ -136,12 +142,18 @@ public:
   /// \param MissingArgIndex - On error, the index of the option which could
   /// not be parsed.
   /// \param MissingArgCount - On error, the number of missing options.
+  /// \param FlagsToInclude - Only parse options with any of these flags.
+  /// Zero is the default which includes all flags.
+  /// \param FlagsToExclude - Don't parse options with this flag.  Zero
+  /// is the default and means exclude nothing.
   /// \return An InputArgList; on error this will contain all the options
   /// which could be parsed.
   InputArgList *ParseArgs(const char* const *ArgBegin,
                           const char* const *ArgEnd,
                           unsigned &MissingArgIndex,
-                          unsigned &MissingArgCount) const;
+                          unsigned &MissingArgCount,
+                          unsigned FlagsToInclude = 0,
+                          unsigned FlagsToExclude = 0) const;
 
   /// \brief Render the help text for an option table.
   ///

Modified: llvm/trunk/lib/Option/OptTable.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Option/OptTable.cpp?rev=186703&r1=186702&r2=186703&view=diff
==============================================================================
--- llvm/trunk/lib/Option/OptTable.cpp (original)
+++ llvm/trunk/lib/Option/OptTable.cpp Fri Jul 19 13:04:57 2013
@@ -180,7 +180,9 @@ static unsigned matchOption(const OptTab
   return 0;
 }
 
-Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index) const {
+Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index,
+                           unsigned FlagsToInclude,
+                           unsigned FlagsToExclude) const {
   unsigned Prev = Index;
   const char *Str = Args.getArgString(Index);
 
@@ -213,8 +215,15 @@ Arg *OptTable::ParseOneArg(const ArgList
     if (Start == End)
       break;
 
+    Option Opt(Start, this);
+
+    if (FlagsToInclude && !Opt.hasFlag(FlagsToInclude))
+      continue;
+    if (Opt.hasFlag(FlagsToExclude))
+      continue;
+
     // See if this option matches.
-    if (Arg *A = Option(Start, this).accept(Args, Index, ArgSize))
+    if (Arg *A = Opt.accept(Args, Index, ArgSize))
       return A;
 
     // Otherwise, see if this argument was missing values.
@@ -222,13 +231,20 @@ Arg *OptTable::ParseOneArg(const ArgList
       return 0;
   }
 
+  // If we failed to find an option and this arg started with /, then it's
+  // probably an input path.
+  if (Str[0] == '/')
+    return new Arg(getOption(TheInputOptionID), Str, Index++, Str);
+
   return new Arg(getOption(TheUnknownOptionID), Str, Index++, Str);
 }
 
-InputArgList *OptTable::ParseArgs(const char* const *ArgBegin,
-                                  const char* const *ArgEnd,
+InputArgList *OptTable::ParseArgs(const char *const *ArgBegin,
+                                  const char *const *ArgEnd,
                                   unsigned &MissingArgIndex,
-                                  unsigned &MissingArgCount) const {
+                                  unsigned &MissingArgCount,
+                                  unsigned FlagsToInclude,
+                                  unsigned FlagsToExclude) const {
   InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
 
   // FIXME: Handle '@' args (or at least error on them).
@@ -243,7 +259,7 @@ InputArgList *OptTable::ParseArgs(const
     }
 
     unsigned Prev = Index;
-    Arg *A = ParseOneArg(*Args, Index);
+    Arg *A = ParseOneArg(*Args, Index, FlagsToInclude, FlagsToExclude);
     assert(Index > Prev && "Parser failed to consume argument.");
 
     // Check for missing argument error.





More information about the llvm-commits mailing list