[llvm] r187675 - Option parsing: recognize the special -- token

Hans Wennborg hans at hanshq.net
Fri Aug 2 14:20:27 PDT 2013


Author: hans
Date: Fri Aug  2 16:20:27 2013
New Revision: 187675

URL: http://llvm.org/viewvc/llvm-project?rev=187675&view=rev
Log:
Option parsing: recognize the special -- token

Everything that comes after -- should be treated as a filename. This
enables passing in filenames that would otherwise be conflated with
command-line options.

This is especially important for clang-cl which supports options
starting with /, which are easily conflatable with Unix-style
path names.

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

Modified:
    llvm/trunk/lib/Option/OptTable.cpp
    llvm/trunk/unittests/Option/OptionParsingTest.cpp

Modified: llvm/trunk/lib/Option/OptTable.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Option/OptTable.cpp?rev=187675&r1=187674&r2=187675&view=diff
==============================================================================
--- llvm/trunk/lib/Option/OptTable.cpp (original)
+++ llvm/trunk/lib/Option/OptTable.cpp Fri Aug  2 16:20:27 2013
@@ -253,11 +253,26 @@ InputArgList *OptTable::ParseArgs(const
   unsigned Index = 0, End = ArgEnd - ArgBegin;
   while (Index < End) {
     // Ignore empty arguments (other things may still take them as arguments).
-    if (Args->getArgString(Index)[0] == '\0') {
+    StringRef Str = Args->getArgString(Index);
+    if (Str == "") {
       ++Index;
       continue;
     }
 
+    if (Str == "--") {
+      // Everything after -- is a filename.
+      ++Index;
+
+      assert(TheInputOptionID != 0 && "Invalid input option ID.");
+      while (Index < End) {
+        Args->append(new Arg(getOption(TheInputOptionID),
+                             Args->getArgString(Index), Index,
+                             Args->getArgString(Index)));
+        ++Index;
+      }
+      break;
+    }
+
     unsigned Prev = Index;
     Arg *A = ParseOneArg(*Args, Index, FlagsToInclude, FlagsToExclude);
     assert(Index > Prev && "Parser failed to consume argument.");

Modified: llvm/trunk/unittests/Option/OptionParsingTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Option/OptionParsingTest.cpp?rev=187675&r1=187674&r2=187675&view=diff
==============================================================================
--- llvm/trunk/unittests/Option/OptionParsingTest.cpp (original)
+++ llvm/trunk/unittests/Option/OptionParsingTest.cpp Fri Aug  2 16:20:27 2013
@@ -156,3 +156,16 @@ TEST(Option, AliasArgs) {
   EXPECT_EQ(AL->getAllArgValues(OPT_B)[0], "foo");
   EXPECT_EQ(AL->getAllArgValues(OPT_B)[1], "bar");
 }
+
+TEST(Option, DashDash) {
+  TestOptTable T;
+  unsigned MAI, MAC;
+
+  const char *MyArgs[] = { "-A", "--", "-B", "--" };
+  OwningPtr<InputArgList> AL(T.ParseArgs(MyArgs, array_endof(MyArgs), MAI, MAC));
+  EXPECT_TRUE(AL->hasArg(OPT_A));
+  EXPECT_FALSE(AL->hasArg(OPT_B));
+  EXPECT_EQ(AL->getAllArgValues(OPT_INPUT).size(), 2U);
+  EXPECT_EQ(AL->getAllArgValues(OPT_INPUT)[0], "-B");
+  EXPECT_EQ(AL->getAllArgValues(OPT_INPUT)[1], "--");
+}





More information about the llvm-commits mailing list