Option parsing: recognize the special -- token
authorHans Wennborg <hans@hanshq.net>
Fri, 2 Aug 2013 21:20:27 +0000 (21:20 +0000)
committerHans Wennborg <hans@hanshq.net>
Fri, 2 Aug 2013 21:20:27 +0000 (21:20 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@187675 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 11439d3e56fab8e3fb9ac6360e5e89f86eb3a196..98e63bc2de9e7c0bcd9e7ac2bf28880eabd7f6f8 100644 (file)
@@ -253,11 +253,26 @@ InputArgList *OptTable::ParseArgs(const char *const *ArgBegin,
   unsigned Index = 0, End = ArgEnd - ArgBegin;
   while (Index < End) {
     // Ignore empty arguments (other things may still take them as arguments).
   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;
     }
 
       ++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.");
     unsigned Prev = Index;
     Arg *A = ParseOneArg(*Args, Index, FlagsToInclude, FlagsToExclude);
     assert(Index > Prev && "Parser failed to consume argument.");
index 2a5a5a9c947c9b4e344d8f2b4569cb8db8a5da4b..5a76d65d0fa382d342aa1b6da27cb651cc05eb8a 100644 (file)
@@ -156,3 +156,16 @@ TEST(Option, AliasArgs) {
   EXPECT_EQ(AL->getAllArgValues(OPT_B)[0], "foo");
   EXPECT_EQ(AL->getAllArgValues(OPT_B)[1], "bar");
 }
   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], "--");
+}