Index: lib/Frontend/DependencyFile.cpp =================================================================== --- lib/Frontend/DependencyFile.cpp +++ lib/Frontend/DependencyFile.cpp @@ -290,12 +290,17 @@ } /// PrintFilename - GCC escapes spaces, # and $, but apparently not ' or " or -/// other scary characters. +/// other scary characters, because they aren't special to Make. If one or +/// more backslashes immediately precedes space or #, it is also escaped; +/// backslash in other places is not escaped. static void PrintFilename(raw_ostream &OS, StringRef Filename) { for (unsigned i = 0, e = Filename.size(); i != e; ++i) { - if (Filename[i] == ' ' || Filename[i] == '#') + if (Filename[i] == ' ' || Filename[i] == '#') { OS << '\\'; - else if (Filename[i] == '$') // $ is escaped by $$. + unsigned j = i; + while (j > 0 && Filename[--j] == '\\') + OS << '\\'; + } else if (Filename[i] == '$') // $ is escaped by $$. OS << '$'; OS << Filename[i]; } Index: test/Frontend/dependency-gen-escaping.c =================================================================== --- test/Frontend/dependency-gen-escaping.c +++ test/Frontend/dependency-gen-escaping.c @@ -4,13 +4,16 @@ // RUN: echo > '%t.dir/ .h' // RUN: echo > '%t.dir/$$.h' // RUN: echo > '%t.dir/##.h' +// RUN: echo > '%t.dir/a\b\#c\ d.h' // RUN: cd %t.dir // RUN: %clang -MD -MF - %s -fsyntax-only -I. | FileCheck -strict-whitespace %s // CHECK: \ \ \ \ .h // CHECK: $$$$.h // CHECK: \#\#.h +// CHECK: a\b\\\#c\\\ d.h #include " .h" #include "$$.h" #include "##.h" +#include "a\b\#c\ d.h"