r189557 - Properly escape filenames in line directives.

Eli Friedman eli.friedman at gmail.com
Wed Aug 28 18:42:42 PDT 2013


Author: efriedma
Date: Wed Aug 28 20:42:42 2013
New Revision: 189557

URL: http://llvm.org/viewvc/llvm-project?rev=189557&view=rev
Log:
Properly escape filenames in line directives.

Fixes PR17018.  Only partial test coverage because I don't want
to try to write a test which generates a file whose name contains a newline.

Modified:
    cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp
    cfe/trunk/lib/Rewrite/Frontend/InclusionRewriter.cpp
    cfe/trunk/test/Preprocessor/line-directive-output.c

Modified: cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp?rev=189557&r1=189556&r2=189557&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp (original)
+++ cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp Wed Aug 28 20:42:42 2013
@@ -190,11 +190,11 @@ void PrintPPOutputPPCallbacks::WriteLine
   // Emit #line directives or GNU line markers depending on what mode we're in.
   if (UseLineDirective) {
     OS << "#line" << ' ' << LineNo << ' ' << '"';
-    OS.write(CurFilename.data(), CurFilename.size());
+    OS.write_escaped(CurFilename);
     OS << '"';
   } else {
     OS << '#' << ' ' << LineNo << ' ' << '"';
-    OS.write(CurFilename.data(), CurFilename.size());
+    OS.write_escaped(CurFilename);
     OS << '"';
 
     if (ExtraLen)
@@ -285,7 +285,6 @@ void PrintPPOutputPPCallbacks::FileChang
 
   CurFilename.clear();
   CurFilename += UserLoc.getFilename();
-  Lexer::Stringify(CurFilename);
   FileType = NewFileType;
 
   if (DisableLineMarkers) {

Modified: cfe/trunk/lib/Rewrite/Frontend/InclusionRewriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/Frontend/InclusionRewriter.cpp?rev=189557&r1=189556&r2=189557&view=diff
==============================================================================
--- cfe/trunk/lib/Rewrite/Frontend/InclusionRewriter.cpp (original)
+++ cfe/trunk/lib/Rewrite/Frontend/InclusionRewriter.cpp Wed Aug 28 20:42:42 2013
@@ -114,7 +114,9 @@ void InclusionRewriter::WriteLineInfo(co
   } else {
     // Use GNU linemarkers as described here:
     // http://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html
-    OS << '#' << ' ' << Line << ' ' << '"' << Filename << '"';
+    OS << '#' << ' ' << Line << ' ' << '"';
+    OS.write_escaped(Filename);
+    OS << '"';
     if (!Extra.empty())
       OS << Extra;
     if (FileType == SrcMgr::C_System)

Modified: cfe/trunk/test/Preprocessor/line-directive-output.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/line-directive-output.c?rev=189557&r1=189556&r2=189557&view=diff
==============================================================================
--- cfe/trunk/test/Preprocessor/line-directive-output.c (original)
+++ cfe/trunk/test/Preprocessor/line-directive-output.c Wed Aug 28 20:42:42 2013
@@ -73,3 +73,6 @@ extern int z;
 # 42 "A.c"
 # 44 "A.c"
 # 49 "A.c"
+
+// CHECK: # 50 "a\n.c"
+# 50 "a\012.c"





More information about the cfe-commits mailing list