Driver: Fix PR4062 by dissecting one particular -Wp, form.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@105966 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp
index 4351433..0da26b7 100644
--- a/lib/Driver/Driver.cpp
+++ b/lib/Driver/Driver.cpp
@@ -120,12 +120,12 @@
     // Unfortunately, we have to parse some forwarding options (-Xassembler,
     // -Xlinker, -Xpreprocessor) because we either integrate their functionality
     // (assembler and preprocessor), or bypass a previous driver ('collect2').
-    if (A->getOption().matches(options::OPT_Xlinker) &&
-        A->getValue(Args) == llvm::StringRef("--no-demangle")) {
-      DAL->AddFlagArg(A, Opts->getOption(options::OPT_Z_Xlinker__no_demangle));
-      continue;
-    } else if (A->getOption().matches(options::OPT_Wl_COMMA) &&
-               A->containsValue("--no-demangle")) {
+
+    // Rewrite linker options, to replace --no-demangle with a custom internal
+    // option.
+    if ((A->getOption().matches(options::OPT_Wl_COMMA) ||
+         A->getOption().matches(options::OPT_Xlinker)) &&
+        A->containsValue("--no-demangle")) {
       // Add the rewritten no-demangle argument.
       DAL->AddFlagArg(A, Opts->getOption(options::OPT_Z_Xlinker__no_demangle));
 
@@ -138,6 +138,19 @@
       continue;
     }
 
+    // Rewrite preprocessor options, to replace -Wp,-MD,FOO which is used by
+    // some build systems. We don't try to be complete here because we don't
+    // care to encourage this usage model.
+    if (A->getOption().matches(options::OPT_Wp_COMMA) &&
+        A->getNumValues() == 2 &&
+        A->getValue(Args, 0) == llvm::StringRef("-MD")) {
+      // Rewrite to -MD along with -MF.
+      DAL->AddFlagArg(A, Opts->getOption(options::OPT_MD));
+      DAL->AddSeparateArg(A, Opts->getOption(options::OPT_MF),
+                          A->getValue(Args, 1));
+      continue;
+    }
+
     DAL->append(*it);
   }
 
diff --git a/test/Driver/Wp-args.c b/test/Driver/Wp-args.c
new file mode 100644
index 0000000..e072263
--- /dev/null
+++ b/test/Driver/Wp-args.c
@@ -0,0 +1,13 @@
+// Check that we extract -MD from '-Wp,-MD,FOO', which is used by a number of
+// major projects (e.g., FireFox and the Linux Kernel).
+
+// RUN: %clang --ccc-host-triple i386-pc-linux-gnu -### \
+// RUN:   -Wp,-MD,FOO.d -fsyntax-only %s 2> %t
+// RUN: FileCheck < %t %s
+//
+// CHECK: "-cc1"
+// CHECK-NOT: -MD
+// CHECK: "-dependency-file" "FOO.d"
+// CHECK: "-MT"
+//
+// PR4062