Index: tools/clang/lib/Frontend/DependencyFile.cpp =================================================================== --- tools/clang/lib/Frontend/DependencyFile.cpp +++ tools/clang/lib/Frontend/DependencyFile.cpp @@ -293,8 +293,11 @@ } /// PrintFilename - GCC escapes spaces, # and $, but apparently not ' or " or -/// other scary characters. NMake/Jom has a different set of scary characters, -/// but wraps filespecs in double-quotes to avoid misinterpreting them; +/// 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. +/// NMake/Jom has a different set of scary characters, but wraps filespecs in +/// double-quotes to avoid misinterpreting them; see /// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info, /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx /// for Windows file-naming info. @@ -311,9 +314,12 @@ return; } 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: tools/clang/test/Frontend/dependency-gen-escaping.c =================================================================== --- tools/clang/test/Frontend/dependency-gen-escaping.c +++ tools/clang/test/Frontend/dependency-gen-escaping.c @@ -5,9 +5,11 @@ // CHECK: \ \ \ \ .h // CHECK: $$$$.h // CHECK: \#\#.h +// CHECK: a\b\\\#c\\\ d.h // NMAKE: " .h" // NMAKE: "$$.h" // NMAKE: "##.h" +// NMAKE: "a\b\#c\ d.h" // NMAKE-NOT: " // NMAKE: normal.h // NMAKE-NOT: " @@ -15,4 +17,5 @@ #include " .h" #include "$$.h" #include "##.h" +#include "a\b\#c\ d.h" #include "normal.h"