[PATCH] Fix dependency file escaping

Paul Robinson Paul_Robinson at playstation.sony.com
Wed Apr 22 13:32:31 PDT 2015


Hi silvas, emaste,

When writing a dependency (.d) file, if space or # is immediately
preceded by backslashes, escape the backslashes as well as the space
or # character.
This straddles the fence between BSD Make (which does no escaping at
all, and does not support space or # in filespecs) and GNU Make (which
does support escaping, but will fall back to the filespec as-written
if the escaping doesn't match an existing file).

http://reviews.llvm.org/D9208

Files:
  lib/Frontend/DependencyFile.cpp
  test/Frontend/dependency-gen-escaping.c

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"

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D9208.24255.patch
Type: text/x-patch
Size: 1631 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150422/c4372125/attachment.bin>


More information about the cfe-commits mailing list