Skip to content

Commit 513b950

Browse files
committed
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 4a16b51.
1 parent 4c600bd commit 513b950

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

clang/lib/Frontend/Rewrite/InclusionRewriter.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,11 @@ void InclusionRewriter::Process(FileID FileId,
444444
if (Mod)
445445
OS << "#pragma clang module end /*"
446446
<< Mod->getFullModuleName(true) << "*/\n";
447-
OS << "#endif /* " << getIncludedFileName(Inc)
448-
<< " expanded by -frewrite-includes */" << LocalEOL;
447+
// There's no #include, therefore no #if, for -include files.
448+
if (FromFile != PredefinesBuffer) {
449+
OS << "#endif /* " << getIncludedFileName(Inc)
450+
<< " expanded by -frewrite-includes */" << LocalEOL;
451+
}
449452

450453
// Add line marker to indicate we're returning from an included
451454
// file.

clang/test/Frontend/rewrite-includes-cli-include.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ main_file_line
33
// CHECK: {{^}}# 1 "<built-in>"{{$}}
44
// CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes2.h" 1{{$}}
55
// CHECK-NEXT: {{^}}int included_line2;{{$}}
6-
// CHECK-NEXT: {{^}}#endif /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
76
// CHECK-NEXT: {{^}}# 1 "<built-in>" 2{{$}}
87
// CHECK-NEXT: {{^}}# 1 "{{.*}}rewrite-includes-cli-include.c"{{$}}
98
// CHECK-NEXT: FileCheck

0 commit comments

Comments
 (0)