From 957f4a25f60ecf6404aa2df3dd1d24ff0519616f Mon Sep 17 00:00:00 2001 From: Lubos Lunak Date: Thu, 1 May 2014 21:11:57 +0000 Subject: [PATCH] write a line marker right before adding included file Enclosing the original #include directive inside #if 0 adds lines, so warning/errors messages would have the line number off in "In file included from ::", so add line marker to fix this. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@207795 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Rewrite/Frontend/InclusionRewriter.cpp | 2 ++ test/Frontend/Inputs/rewrite-includes-messages.h | 4 ++++ test/Frontend/rewrite-includes-messages.c | 8 ++++++++ test/Frontend/rewrite-includes-missing.c | 1 + test/Frontend/rewrite-includes-modules.c | 2 ++ test/Frontend/rewrite-includes.c | 10 ++++++++++ 6 files changed, 27 insertions(+) create mode 100644 test/Frontend/Inputs/rewrite-includes-messages.h create mode 100644 test/Frontend/rewrite-includes-messages.c diff --git a/lib/Rewrite/Frontend/InclusionRewriter.cpp b/lib/Rewrite/Frontend/InclusionRewriter.cpp index d6a434d80a..7700096a9a 100644 --- a/lib/Rewrite/Frontend/InclusionRewriter.cpp +++ b/lib/Rewrite/Frontend/InclusionRewriter.cpp @@ -390,6 +390,8 @@ bool InclusionRewriter::Process(FileID FileId, case tok::pp_import: { CommentOutDirective(RawLex, HashToken, FromFile, EOL, NextToWrite, Line); + if (FileId != PP.getPredefinesFileID()) + WriteLineInfo(FileName, Line - 1, FileType, EOL, ""); StringRef LineInfoExtra; if (const FileChange *Change = FindFileChangeLocation( HashToken.getLocation())) { diff --git a/test/Frontend/Inputs/rewrite-includes-messages.h b/test/Frontend/Inputs/rewrite-includes-messages.h new file mode 100644 index 0000000000..e5f0eb2514 --- /dev/null +++ b/test/Frontend/Inputs/rewrite-includes-messages.h @@ -0,0 +1,4 @@ +void f() +{ + int unused_variable; +} diff --git a/test/Frontend/rewrite-includes-messages.c b/test/Frontend/rewrite-includes-messages.c new file mode 100644 index 0000000000..37b970669f --- /dev/null +++ b/test/Frontend/rewrite-includes-messages.c @@ -0,0 +1,8 @@ +// RUN: %clang -E -frewrite-includes %s -I%S/Inputs/ | %clang -Wall -Wunused-macros -x c -c - 2> %t.1 +// RUN: %clang -I%S/Inputs/ -Wall -Wunused-macros -c %s 2> %t.2 +// RUN: cmp -s %t.1 %t.2 +// expected-no-diagnostics +// REQUIRES: shell + +#include "rewrite-includes-messages.h" +#define UNUSED_MACRO diff --git a/test/Frontend/rewrite-includes-missing.c b/test/Frontend/rewrite-includes-missing.c index da4e209bc1..25a59a0321 100644 --- a/test/Frontend/rewrite-includes-missing.c +++ b/test/Frontend/rewrite-includes-missing.c @@ -4,4 +4,5 @@ // CHECK: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}} // CHECK-NEXT: {{^}}#include "foobar.h" // CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}} +// CHECK-NEXT: {{^}}# 3 "{{.*}}rewrite-includes-missing.c"{{$}} // CHECK-NEXT: {{^}}# 4 "{{.*}}rewrite-includes-missing.c"{{$}} diff --git a/test/Frontend/rewrite-includes-modules.c b/test/Frontend/rewrite-includes-modules.c index 783a96739a..58d7809909 100644 --- a/test/Frontend/rewrite-includes-modules.c +++ b/test/Frontend/rewrite-includes-modules.c @@ -10,11 +10,13 @@ int foo(); // CHECK-NEXT: #if 0 /* expanded by -frewrite-includes */{{$}} // CHECK-NEXT: #include {{$}} // CHECK-NEXT: #endif /* expanded by -frewrite-includes */{{$}} +// CHECK-NEXT: # 5 "{{.*[/\\]}}rewrite-includes-modules.c"{{$}} // CHECK-NEXT: @import Module; /* clang -frewrite-includes: implicit import */{{$}} // CHECK-NEXT: # 6 "{{.*[/\\]}}rewrite-includes-modules.c"{{$}} // CHECK-NEXT: int foo();{{$}} // CHECK-NEXT: #if 0 /* expanded by -frewrite-includes */{{$}} // CHECK-NEXT: #include {{$}} // CHECK-NEXT: #endif /* expanded by -frewrite-includes */{{$}} +// CHECK-NEXT: # 7 "{{.*[/\\]}}rewrite-includes-modules.c"{{$}} // CHECK-NEXT: @import Module; /* clang -frewrite-includes: implicit import */{{$}} // CHECK-NEXT: # 8 "{{.*[/\\]}}rewrite-includes-modules.c"{{$}} diff --git a/test/Frontend/rewrite-includes.c b/test/Frontend/rewrite-includes.c index 619d34a22d..bed87efa1f 100644 --- a/test/Frontend/rewrite-includes.c +++ b/test/Frontend/rewrite-includes.c @@ -28,6 +28,7 @@ A(1,2) // CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}} // CHECK-NEXT: {{^}}#include "rewrite-includes1.h"{{$}} // CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}} +// CHECK-NEXT: {{^}}# 6 "{{.*}}rewrite-includes.c"{{$}} // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes1.h" 1{{$}} // CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}} // CHECK-NEXT: {{^}}#pragma clang system_header{{$}} @@ -37,6 +38,7 @@ A(1,2) // CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}} // CHECK-NEXT: {{^}}#include "rewrite-includes2.h"{{$}} // CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}} +// CHECK-NEXT: {{^}}# 3 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes1.h" 3{{$}} // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes2.h" 1 3{{$}} // CHECK-NEXT: {{^}}included_line2{{$}} // CHECK-NEXT: {{^}}# 4 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes1.h" 2 3{{$}} @@ -46,6 +48,7 @@ A(1,2) // CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}} // CHECK-NEXT: {{^}}#include HEADER{{$}} // CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}} +// CHECK-NEXT: {{^}}# 9 "{{.*}}rewrite-includes.c"{{$}} // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes3.h" 1{{$}} // CHECK-NEXT: {{^}}included_line3{{$}} // CHECK-NEXT: {{^}}# 10 "{{.*}}rewrite-includes.c" 2{{$}} @@ -54,6 +57,7 @@ A(1,2) // CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}} // CHECK-NEXT: {{^}}#include "rewrite-includes4.h"{{$}} // CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}} +// CHECK-NEXT: {{^}}# 11 "{{.*}}rewrite-includes.c"{{$}} // CHECK-NEXT: {{^}}# 12 "{{.*}}rewrite-includes.c"{{$}} // CHECK-NEXT: {{^}}#endif{{$}} // CHECK-NEXT: {{^}}# 13 "{{.*}}rewrite-includes.c"{{$}} @@ -62,12 +66,14 @@ A(1,2) // CHECK-NEXT: {{^}}#/**/include /**/ "rewrite-includes5.h" /**/ {{\\}}{{$}} // CHECK-NEXT: {{^}} {{$}} // CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}} +// CHECK-NEXT: {{^}}# 15 "{{.*}}rewrite-includes.c"{{$}} // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes5.h" 1{{$}} // CHECK-NEXT: {{^}}included_line5{{$}} // CHECK-NEXT: {{^}}# 16 "{{.*}}rewrite-includes.c" 2{{$}} // CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}} // CHECK-NEXT: {{^}}#include "rewrite-includes6.h" // comment{{$}} // CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}} +// CHECK-NEXT: {{^}}# 16 "{{.*}}rewrite-includes.c"{{$}} // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes6.h" 1{{$}} // CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}} // CHECK-NEXT: {{^}}#pragma once{{$}} @@ -80,10 +86,12 @@ A(1,2) // CHECK-NEXT: {{^}}#include "rewrite-includes6.h" /* comment{{$}} // CHECK-NEXT: {{^}} continues */{{$}} // CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}} +// CHECK-NEXT: {{^}}# 19 "{{.*}}rewrite-includes.c"{{$}} // CHECK-NEXT: {{^}}# 20 "{{.*}}rewrite-includes.c"{{$}} // CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}} // CHECK-NEXT: {{^}}#include "rewrite-includes7.h"{{$}} // CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}} +// CHECK-NEXT: {{^}}# 20 "{{.*}}rewrite-includes.c"{{$}} // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes7.h" 1{{$}} // CHECK-NEXT: {{^}}#ifndef REWRITE_INCLUDES_7{{$}} // CHECK-NEXT: {{^}}#define REWRITE_INCLUDES_7{{$}} @@ -94,10 +102,12 @@ A(1,2) // CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}} // CHECK-NEXT: {{^}}#include "rewrite-includes7.h"{{$}} // CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}} +// CHECK-NEXT: {{^}}# 21 "{{.*}}rewrite-includes.c"{{$}} // CHECK-NEXT: {{^}}# 22 "{{.*}}rewrite-includes.c"{{$}} // CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}} // CHECK-NEXT: {{^}}#include "rewrite-includes8.h"{{$}} // CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}} +// CHECK-NEXT: {{^}}# 22 "{{.*}}rewrite-includes.c"{{$}} // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes8.h" 1{{$}} // CHECK-NEXT: {{^}}#if (1)/*__has_include_next()*/{{$}} // CHECK-NEXT: {{^}}#elif (0)/*__has_include()*/{{$}} -- 2.40.0