[PATCH] Option parsing: recognize the special -- token

Hans Wennborg hans at chromium.org
Fri Aug 2 13:50:55 PDT 2013


Hi rnk,

Everything that comes after -- should be 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 /, easily conflated with Unix-style path names.

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

Files:
  lib/Option/OptTable.cpp
  unittests/Option/OptionParsingTest.cpp

Index: lib/Option/OptTable.cpp
===================================================================
--- lib/Option/OptTable.cpp
+++ lib/Option/OptTable.cpp
@@ -249,11 +249,28 @@
 
   // FIXME: Handle '@' args (or at least error on them).
 
+  bool AfterDashDash = false;
   MissingArgIndex = MissingArgCount = 0;
   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 (!AfterDashDash && Str == "--") {
+      ++Index;
+      AfterDashDash = true;
+      continue;
+    }
+
+    if (AfterDashDash) {
+      // Everything after -- is a filename.
+      assert(TheInputOptionID != 0 && "Invalid input option ID.");
+      Args->append(new Arg(getOption(TheInputOptionID), Str.data(), Index,
+                                     Str.data()));
       ++Index;
       continue;
     }
Index: unittests/Option/OptionParsingTest.cpp
===================================================================
--- unittests/Option/OptionParsingTest.cpp
+++ unittests/Option/OptionParsingTest.cpp
@@ -156,3 +156,16 @@
   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(), 2);
+  EXPECT_EQ(AL->getAllArgValues(OPT_INPUT)[0], "-B");
+  EXPECT_EQ(AL->getAllArgValues(OPT_INPUT)[1], "--");
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1274.1.patch
Type: text/x-patch
Size: 1822 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130802/7f339a1e/attachment.bin>


More information about the llvm-commits mailing list