The -frewrite-includes flag incorrectly uses at the beginning a linemarker 
for the main file with the "1" flag which suggests that the contents have 
been in fact included from another file. This for example 
disables -Wunused-macros, which warns only for macros from the main file:

$ cat a.cpp
#define FOO 1
$ clang++ -E -frewrite-includes a.cpp | clang++ -Wall -Wunused-macros -x 
c++ -c -
$ clang++ -Wall -Wunused-macros -c a.cpp
a.cpp:1:9: warning: macro is not used [-Wunused-macros]
#define FOO 1
        ^
1 warning generated.

 The attached patch fixes the code to start the resulting file with the 
correct linemarker.

-- 
 Lubos Lunak
From 27778c2cb8eac373636ea3f50cde73ddd770e79e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lubo=C5=A1=20Lu=C5=88=C3=A1k?= <[email protected]>
Date: Thu, 20 Feb 2014 23:27:44 +0100
Subject: [PATCH 1/3] do not use "1" for line marker for the main file

"1" means entering a new file (from a different one), but the main
file is not included from anything (and this would e.g. confuse -Wunused-macros
to not report unused macros in the main file, see pr15610).
The line marker is still useful e.g. if the resulting file is renamed or used
via a pipe.
---
 lib/Rewrite/Frontend/InclusionRewriter.cpp | 7 +++++--
 test/Frontend/rewrite-includes.c           | 1 +
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/lib/Rewrite/Frontend/InclusionRewriter.cpp b/lib/Rewrite/Frontend/InclusionRewriter.cpp
index 058960d..3704760 100644
--- a/lib/Rewrite/Frontend/InclusionRewriter.cpp
+++ b/lib/Rewrite/Frontend/InclusionRewriter.cpp
@@ -352,8 +352,11 @@ bool InclusionRewriter::Process(FileID FileId,
 
   StringRef EOL = DetectEOL(FromFile);
 
-  // Per the GNU docs: "1" indicates the start of a new file.
-  WriteLineInfo(FileName, 1, FileType, EOL, " 1");
+  // Per the GNU docs: "1" indicates entering a new file.
+  if (FileId == SM.getMainFileID())
+    WriteLineInfo(FileName, 1, FileType, EOL, "");
+  else if (FileId != PP.getPredefinesFileID())
+    WriteLineInfo(FileName, 1, FileType, EOL, " 1");
 
   if (SM.getFileIDSize(FileId) == 0)
     return false;
diff --git a/test/Frontend/rewrite-includes.c b/test/Frontend/rewrite-includes.c
index 2158dd0..619d34a 100644
--- a/test/Frontend/rewrite-includes.c
+++ b/test/Frontend/rewrite-includes.c
@@ -21,6 +21,7 @@ A(1,2)
 #include "rewrite-includes7.h"
 #include "rewrite-includes8.h"
 // ENDCOMPARE
+// CHECK: {{^}}# 1 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK: {{^}}// STARTCOMPARE{{$}}
 // CHECK-NEXT: {{^}}#define A(a,b) a ## b{{$}}
 // CHECK-NEXT: {{^}}A(1,2){{$}}
-- 
1.8.4.5

_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to