[clang] 513b950 - Make -frewrite-includes handle -include correctly
Paul Robinson via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 24 09:08:39 PDT 2023
Author: Paul Robinson
Date: 2023-10-24T09:08:31-07:00
New Revision: 513b950a79d0e92af1baa969207dd296068a71b9
URL: https://github.com/llvm/llvm-project/commit/513b950a79d0e92af1baa969207dd296068a71b9
DIFF: https://github.com/llvm/llvm-project/commit/513b950a79d0e92af1baa969207dd296068a71b9.diff
LOG: Make -frewrite-includes handle -include correctly
The `-include "file"` option implicitly adds a leading #include
to the main source; however, there's no explicit #include, which
left -frewrite-includes emitting an unmatched #endif at the end
of the included text.
This corrects a bug in 4a16b51f.
Added:
Modified:
clang/lib/Frontend/Rewrite/InclusionRewriter.cpp
clang/test/Frontend/rewrite-includes-cli-include.c
Removed:
################################################################################
diff --git a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp
index 28f7b0b9edfc5c2..2c3a253a67d5c93 100644
--- a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp
+++ b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp
@@ -444,8 +444,11 @@ void InclusionRewriter::Process(FileID FileId,
if (Mod)
OS << "#pragma clang module end /*"
<< Mod->getFullModuleName(true) << "*/\n";
- OS << "#endif /* " << getIncludedFileName(Inc)
- << " expanded by -frewrite-includes */" << LocalEOL;
+ // There's no #include, therefore no #if, for -include files.
+ if (FromFile != PredefinesBuffer) {
+ OS << "#endif /* " << getIncludedFileName(Inc)
+ << " expanded by -frewrite-includes */" << LocalEOL;
+ }
// Add line marker to indicate we're returning from an included
// file.
diff --git a/clang/test/Frontend/rewrite-includes-cli-include.c b/clang/test/Frontend/rewrite-includes-cli-include.c
index 437bc2ffadf1960..d63f966f79ca6af 100644
--- a/clang/test/Frontend/rewrite-includes-cli-include.c
+++ b/clang/test/Frontend/rewrite-includes-cli-include.c
@@ -3,7 +3,6 @@ main_file_line
// CHECK: {{^}}# 1 "<built-in>"{{$}}
// CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes2.h" 1{{$}}
// CHECK-NEXT: {{^}}int included_line2;{{$}}
-// CHECK-NEXT: {{^}}#endif /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
// CHECK-NEXT: {{^}}# 1 "<built-in>" 2{{$}}
// CHECK-NEXT: {{^}}# 1 "{{.*}}rewrite-includes-cli-include.c"{{$}}
// CHECK-NEXT: FileCheck
More information about the cfe-commits
mailing list