Skip to content

Make -frewrite-includes put an endif at the end of the included text #67613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ Modified Compiler Flags

* ``-Woverriding-t-option`` is renamed to ``-Woverriding-option``.
* ``-Winterrupt-service-routine`` is renamed to ``-Wexcessive-regsave`` as a generalization
* ``-frewrite-includes`` now guards the original #include directives with
``__CLANG_REWRITTEN_INCLUDES``, and ``__CLANG_REWRITTEN_SYSTEM_INCLUDES`` as
appropriate.

Removed Compiler Flags
-------------------------
Expand Down
42 changes: 34 additions & 8 deletions clang/lib/Frontend/Rewrite/InclusionRewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ class InclusionRewriter : public PPCallbacks {
bool EnsureNewline);
void CommentOutDirective(Lexer &DirectivesLex, const Token &StartToken,
const MemoryBufferRef &FromFile, StringRef EOL,
unsigned &NextToWrite, int &Lines);
unsigned &NextToWrite, int &Lines,
const IncludedFile *Inc = nullptr);
const IncludedFile *FindIncludeAtLocation(SourceLocation Loc) const;
StringRef getIncludedFileName(const IncludedFile *Inc) const;
const Module *FindModuleAtLocation(SourceLocation Loc) const;
const Module *FindEnteredModule(SourceLocation Loc) const;
bool IsIfAtLocationTrue(SourceLocation Loc) const;
Expand Down Expand Up @@ -311,6 +313,17 @@ void InclusionRewriter::OutputContentUpTo(const MemoryBufferRef &FromFile,
WriteFrom = WriteTo;
}

StringRef
InclusionRewriter::getIncludedFileName(const IncludedFile *Inc) const {
if (Inc) {
auto B = SM.getBufferOrNone(Inc->Id);
assert(B && "Attempting to process invalid inclusion");
if (B)
return llvm::sys::path::filename(B->getBufferIdentifier());
}
return StringRef();
}

/// Print characters from \p FromFile starting at \p NextToWrite up until the
/// inclusion directive at \p StartToken, then print out the inclusion
/// inclusion directive disabled by a #if directive, updating \p NextToWrite
Expand All @@ -320,7 +333,8 @@ void InclusionRewriter::CommentOutDirective(Lexer &DirectiveLex,
const Token &StartToken,
const MemoryBufferRef &FromFile,
StringRef LocalEOL,
unsigned &NextToWrite, int &Line) {
unsigned &NextToWrite, int &Line,
const IncludedFile *Inc) {
OutputContentUpTo(FromFile, NextToWrite,
SM.getFileOffset(StartToken.getLocation()), LocalEOL, Line,
false);
Expand All @@ -332,12 +346,21 @@ void InclusionRewriter::CommentOutDirective(Lexer &DirectiveLex,
// OutputContentUpTo() would not output anything anyway.
return;
}
OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL;
if (Inc) {
OS << "#if defined(__CLANG_REWRITTEN_INCLUDES) ";
if (isSystem(Inc->FileType))
OS << "|| defined(__CLANG_REWRITTEN_SYSTEM_INCLUDES) ";
OS << "/* " << getIncludedFileName(Inc);
} else {
OS << "#if 0 /*";
}
OS << " expanded by -frewrite-includes */" << MainEOL;
OutputContentUpTo(FromFile, NextToWrite,
SM.getFileOffset(DirectiveToken.getLocation()) +
DirectiveToken.getLength(),
LocalEOL, Line, true);
OS << "#endif /* expanded by -frewrite-includes */" << MainEOL;
OS << (Inc ? "#else /* " : "#endif /*") << getIncludedFileName(Inc)
<< " expanded by -frewrite-includes */" << MainEOL;
}

/// Find the next identifier in the pragma directive specified by \p RawToken.
Expand Down Expand Up @@ -400,15 +423,16 @@ void InclusionRewriter::Process(FileID FileId,
case tok::pp_include:
case tok::pp_include_next:
case tok::pp_import: {
CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, NextToWrite,
Line);
SourceLocation Loc = HashToken.getLocation();
const IncludedFile *Inc = FindIncludeAtLocation(Loc);
CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
NextToWrite, Line, Inc);
if (FileId != PP.getPredefinesFileID())
WriteLineInfo(FileName, Line - 1, FileType, "");
StringRef LineInfoExtra;
SourceLocation Loc = HashToken.getLocation();
if (const Module *Mod = FindModuleAtLocation(Loc))
WriteImplicitModuleImport(Mod);
else if (const IncludedFile *Inc = FindIncludeAtLocation(Loc)) {
else if (Inc) {
const Module *Mod = FindEnteredModule(Loc);
if (Mod)
OS << "#pragma clang module begin "
Expand All @@ -420,6 +444,8 @@ 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;

// Add line marker to indicate we're returning from an included
// file.
Expand Down
1 change: 1 addition & 0 deletions clang/test/Frontend/rewrite-includes-cli-include.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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
Expand Down
Loading