[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
https://github.com/pogo59 closed https://github.com/llvm/llvm-project/pull/67613 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
https://github.com/pogo59 updated https://github.com/llvm/llvm-project/pull/67613 >From 0f63068d1085e5064d47916a86fac089c4417e57 Mon Sep 17 00:00:00 2001 From: Paul Robinson Date: Wed, 27 Sep 2023 14:54:13 -0700 Subject: [PATCH 1/6] Make -frewrite-includes put an endif at the end of the included text Also add the filename to the comments it emits, to help identify where included text ends. --- .../Frontend/Rewrite/InclusionRewriter.cpp| 33 - .../Frontend/rewrite-includes-cli-include.c | 1 + .../test/Frontend/rewrite-includes-missing.c | 4 +- clang/test/Frontend/rewrite-includes.c| 130 ++ clang/test/Modules/preprocess-module.cpp | 6 +- 5 files changed, 106 insertions(+), 68 deletions(-) diff --git a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp index d3a3db0139c6d2f..6ab57adc41bfe8b 100644 --- a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp +++ b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp @@ -90,8 +90,10 @@ class InclusionRewriter : public PPCallbacks { bool EnsureNewline); void CommentOutDirective(Lexer , const Token , const MemoryBufferRef , StringRef EOL, - unsigned , int ); + unsigned , int , + 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; @@ -311,6 +313,17 @@ void InclusionRewriter::OutputContentUpTo(const MemoryBufferRef , 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 @@ -320,7 +333,8 @@ void InclusionRewriter::CommentOutDirective(Lexer , const Token , const MemoryBufferRef , StringRef LocalEOL, -unsigned , int ) { +unsigned , int , +const IncludedFile *Inc) { OutputContentUpTo(FromFile, NextToWrite, SM.getFileOffset(StartToken.getLocation()), LocalEOL, Line, false); @@ -332,12 +346,14 @@ void InclusionRewriter::CommentOutDirective(Lexer , // OutputContentUpTo() would not output anything anyway. return; } - OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL; + OS << "#if 0 /* " << getIncludedFileName(Inc) + << " 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. @@ -400,15 +416,16 @@ void InclusionRewriter::Process(FileID FileId, case tok::pp_include: case tok::pp_include_next: case tok::pp_import: { +SourceLocation Loc = HashToken.getLocation(); +const IncludedFile *Inc = FindIncludeAtLocation(Loc); CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, NextToWrite, - Line); + 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 " @@ -420,6 +437,8 @@ void InclusionRewriter::Process(FileID FileId, if (Mod) OS << "#pragma clang module end /*" <<
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
https://github.com/erichkeane approved this pull request. Approved, assuming this passes CI. https://github.com/llvm/llvm-project/pull/67613 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
https://github.com/pogo59 updated https://github.com/llvm/llvm-project/pull/67613 >From 0f63068d1085e5064d47916a86fac089c4417e57 Mon Sep 17 00:00:00 2001 From: Paul Robinson Date: Wed, 27 Sep 2023 14:54:13 -0700 Subject: [PATCH 1/5] Make -frewrite-includes put an endif at the end of the included text Also add the filename to the comments it emits, to help identify where included text ends. --- .../Frontend/Rewrite/InclusionRewriter.cpp| 33 - .../Frontend/rewrite-includes-cli-include.c | 1 + .../test/Frontend/rewrite-includes-missing.c | 4 +- clang/test/Frontend/rewrite-includes.c| 130 ++ clang/test/Modules/preprocess-module.cpp | 6 +- 5 files changed, 106 insertions(+), 68 deletions(-) diff --git a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp index d3a3db0139c6d2f..6ab57adc41bfe8b 100644 --- a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp +++ b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp @@ -90,8 +90,10 @@ class InclusionRewriter : public PPCallbacks { bool EnsureNewline); void CommentOutDirective(Lexer , const Token , const MemoryBufferRef , StringRef EOL, - unsigned , int ); + unsigned , int , + 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; @@ -311,6 +313,17 @@ void InclusionRewriter::OutputContentUpTo(const MemoryBufferRef , 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 @@ -320,7 +333,8 @@ void InclusionRewriter::CommentOutDirective(Lexer , const Token , const MemoryBufferRef , StringRef LocalEOL, -unsigned , int ) { +unsigned , int , +const IncludedFile *Inc) { OutputContentUpTo(FromFile, NextToWrite, SM.getFileOffset(StartToken.getLocation()), LocalEOL, Line, false); @@ -332,12 +346,14 @@ void InclusionRewriter::CommentOutDirective(Lexer , // OutputContentUpTo() would not output anything anyway. return; } - OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL; + OS << "#if 0 /* " << getIncludedFileName(Inc) + << " 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. @@ -400,15 +416,16 @@ void InclusionRewriter::Process(FileID FileId, case tok::pp_include: case tok::pp_include_next: case tok::pp_import: { +SourceLocation Loc = HashToken.getLocation(); +const IncludedFile *Inc = FindIncludeAtLocation(Loc); CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, NextToWrite, - Line); + 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 " @@ -420,6 +437,8 @@ void InclusionRewriter::Process(FileID FileId, if (Mod) OS << "#pragma clang module end /*" <<
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
https://github.com/erichkeane commented: The clang-format tool still has a complaint, so this isn't passing CI. Unfortunately it doesn't seem to print it in the CI, so you'll have to run it locally. Delta that, LGTM. https://github.com/llvm/llvm-project/pull/67613 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
pogo59 wrote: Ping https://github.com/llvm/llvm-project/pull/67613 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
danakj wrote: I was just lamenting the lack of this feature this week. Thank you, I can’t wait to use it. https://github.com/llvm/llvm-project/pull/67613 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
https://github.com/pogo59 updated https://github.com/llvm/llvm-project/pull/67613 >From 0f63068d1085e5064d47916a86fac089c4417e57 Mon Sep 17 00:00:00 2001 From: Paul Robinson Date: Wed, 27 Sep 2023 14:54:13 -0700 Subject: [PATCH 1/4] Make -frewrite-includes put an endif at the end of the included text Also add the filename to the comments it emits, to help identify where included text ends. --- .../Frontend/Rewrite/InclusionRewriter.cpp| 33 - .../Frontend/rewrite-includes-cli-include.c | 1 + .../test/Frontend/rewrite-includes-missing.c | 4 +- clang/test/Frontend/rewrite-includes.c| 130 ++ clang/test/Modules/preprocess-module.cpp | 6 +- 5 files changed, 106 insertions(+), 68 deletions(-) diff --git a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp index d3a3db0139c6d2f..6ab57adc41bfe8b 100644 --- a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp +++ b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp @@ -90,8 +90,10 @@ class InclusionRewriter : public PPCallbacks { bool EnsureNewline); void CommentOutDirective(Lexer , const Token , const MemoryBufferRef , StringRef EOL, - unsigned , int ); + unsigned , int , + 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; @@ -311,6 +313,17 @@ void InclusionRewriter::OutputContentUpTo(const MemoryBufferRef , 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 @@ -320,7 +333,8 @@ void InclusionRewriter::CommentOutDirective(Lexer , const Token , const MemoryBufferRef , StringRef LocalEOL, -unsigned , int ) { +unsigned , int , +const IncludedFile *Inc) { OutputContentUpTo(FromFile, NextToWrite, SM.getFileOffset(StartToken.getLocation()), LocalEOL, Line, false); @@ -332,12 +346,14 @@ void InclusionRewriter::CommentOutDirective(Lexer , // OutputContentUpTo() would not output anything anyway. return; } - OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL; + OS << "#if 0 /* " << getIncludedFileName(Inc) + << " 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. @@ -400,15 +416,16 @@ void InclusionRewriter::Process(FileID FileId, case tok::pp_include: case tok::pp_include_next: case tok::pp_import: { +SourceLocation Loc = HashToken.getLocation(); +const IncludedFile *Inc = FindIncludeAtLocation(Loc); CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, NextToWrite, - Line); + 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 " @@ -420,6 +437,8 @@ void InclusionRewriter::Process(FileID FileId, if (Mod) OS << "#pragma clang module end /*" <<
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
https://github.com/erichkeane commented: This looks great/useful to me. I have no problems with it, but would like to give this a bit of time for others to review. If this doesn't get other comments by friday next week, I'll approve. https://github.com/llvm/llvm-project/pull/67613 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
pogo59 wrote: Toggles done. Also got rid of some of the extra whitespace changes in the output. https://github.com/llvm/llvm-project/pull/67613 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
https://github.com/pogo59 updated https://github.com/llvm/llvm-project/pull/67613 >From 0f63068d1085e5064d47916a86fac089c4417e57 Mon Sep 17 00:00:00 2001 From: Paul Robinson Date: Wed, 27 Sep 2023 14:54:13 -0700 Subject: [PATCH 1/3] Make -frewrite-includes put an endif at the end of the included text Also add the filename to the comments it emits, to help identify where included text ends. --- .../Frontend/Rewrite/InclusionRewriter.cpp| 33 - .../Frontend/rewrite-includes-cli-include.c | 1 + .../test/Frontend/rewrite-includes-missing.c | 4 +- clang/test/Frontend/rewrite-includes.c| 130 ++ clang/test/Modules/preprocess-module.cpp | 6 +- 5 files changed, 106 insertions(+), 68 deletions(-) diff --git a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp index d3a3db0139c6d2f..6ab57adc41bfe8b 100644 --- a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp +++ b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp @@ -90,8 +90,10 @@ class InclusionRewriter : public PPCallbacks { bool EnsureNewline); void CommentOutDirective(Lexer , const Token , const MemoryBufferRef , StringRef EOL, - unsigned , int ); + unsigned , int , + 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; @@ -311,6 +313,17 @@ void InclusionRewriter::OutputContentUpTo(const MemoryBufferRef , 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 @@ -320,7 +333,8 @@ void InclusionRewriter::CommentOutDirective(Lexer , const Token , const MemoryBufferRef , StringRef LocalEOL, -unsigned , int ) { +unsigned , int , +const IncludedFile *Inc) { OutputContentUpTo(FromFile, NextToWrite, SM.getFileOffset(StartToken.getLocation()), LocalEOL, Line, false); @@ -332,12 +346,14 @@ void InclusionRewriter::CommentOutDirective(Lexer , // OutputContentUpTo() would not output anything anyway. return; } - OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL; + OS << "#if 0 /* " << getIncludedFileName(Inc) + << " 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. @@ -400,15 +416,16 @@ void InclusionRewriter::Process(FileID FileId, case tok::pp_include: case tok::pp_include_next: case tok::pp_import: { +SourceLocation Loc = HashToken.getLocation(); +const IncludedFile *Inc = FindIncludeAtLocation(Loc); CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, NextToWrite, - Line); + 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 " @@ -420,6 +437,8 @@ void InclusionRewriter::Process(FileID FileId, if (Mod) OS << "#pragma clang module end /*" <<
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
@@ -332,12 +346,14 @@ void InclusionRewriter::CommentOutDirective(Lexer , // OutputContentUpTo() would not output anything anyway. return; } - OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL; + OS << "#if 0 /* " << getIncludedFileName(Inc) pogo59 wrote: Aha, `-MM` has the same problem, so I'm less concerned about that now. I'll add the toggle for "system" headers later today. https://github.com/llvm/llvm-project/pull/67613 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
@@ -332,12 +346,14 @@ void InclusionRewriter::CommentOutDirective(Lexer , // OutputContentUpTo() would not output anything anyway. return; } - OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL; + OS << "#if 0 /* " << getIncludedFileName(Inc) erichkeane wrote: Hmm... ok. The 'all or nothing' is fine. As far as the system-includes, I wouldn't mind it if we were imperfect, as this is exclusively a debugging tool, thus any amount of ABI/API/etc stability doesn't really exist. So I'm ok with a bit of a 'best effort' here (same on the enabling individual headers). we can always revise this as we determine needs. BTW, I'm ok with this patch, but I am not the code owner here, so I'd like to give others a better chance to review this, particularly @MaskRay . https://github.com/llvm/llvm-project/pull/67613 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
@@ -332,12 +346,14 @@ void InclusionRewriter::CommentOutDirective(Lexer , // OutputContentUpTo() would not output anything anyway. return; } - OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL; + OS << "#if 0 /* " << getIncludedFileName(Inc) pogo59 wrote: TIL about `#pragma clang system_header` which turns a header into a system header, regardless of how it was found. But, by the time the rewriter sees the pragma, it has already emitted the `#include` and its wrapper `#if`. That makes the `__CLANG_REWRITTEN_SYSTEM_INCLUDES` not as reliable, and I didn't do that part (yet). But, I did do the `defined(__CLANG_REWRITTEN_INCLUDES)` part, so you can get all-or-nothing. https://github.com/llvm/llvm-project/pull/67613 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
https://github.com/pogo59 updated https://github.com/llvm/llvm-project/pull/67613 >From 0f63068d1085e5064d47916a86fac089c4417e57 Mon Sep 17 00:00:00 2001 From: Paul Robinson Date: Wed, 27 Sep 2023 14:54:13 -0700 Subject: [PATCH 1/2] Make -frewrite-includes put an endif at the end of the included text Also add the filename to the comments it emits, to help identify where included text ends. --- .../Frontend/Rewrite/InclusionRewriter.cpp| 33 - .../Frontend/rewrite-includes-cli-include.c | 1 + .../test/Frontend/rewrite-includes-missing.c | 4 +- clang/test/Frontend/rewrite-includes.c| 130 ++ clang/test/Modules/preprocess-module.cpp | 6 +- 5 files changed, 106 insertions(+), 68 deletions(-) diff --git a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp index d3a3db0139c6d2f..6ab57adc41bfe8b 100644 --- a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp +++ b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp @@ -90,8 +90,10 @@ class InclusionRewriter : public PPCallbacks { bool EnsureNewline); void CommentOutDirective(Lexer , const Token , const MemoryBufferRef , StringRef EOL, - unsigned , int ); + unsigned , int , + 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; @@ -311,6 +313,17 @@ void InclusionRewriter::OutputContentUpTo(const MemoryBufferRef , 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 @@ -320,7 +333,8 @@ void InclusionRewriter::CommentOutDirective(Lexer , const Token , const MemoryBufferRef , StringRef LocalEOL, -unsigned , int ) { +unsigned , int , +const IncludedFile *Inc) { OutputContentUpTo(FromFile, NextToWrite, SM.getFileOffset(StartToken.getLocation()), LocalEOL, Line, false); @@ -332,12 +346,14 @@ void InclusionRewriter::CommentOutDirective(Lexer , // OutputContentUpTo() would not output anything anyway. return; } - OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL; + OS << "#if 0 /* " << getIncludedFileName(Inc) + << " 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. @@ -400,15 +416,16 @@ void InclusionRewriter::Process(FileID FileId, case tok::pp_include: case tok::pp_include_next: case tok::pp_import: { +SourceLocation Loc = HashToken.getLocation(); +const IncludedFile *Inc = FindIncludeAtLocation(Loc); CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, NextToWrite, - Line); + 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 " @@ -420,6 +437,8 @@ void InclusionRewriter::Process(FileID FileId, if (Mod) OS << "#pragma clang module end /*" <<
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
@@ -98,7 +98,7 @@ // == file2.h // REWRITE: #if 0 // REWRITE: #include "file2.h" -// REWRITE: #endif +// REWRITE: #else /* file2.h expanded pogo59 wrote: Yes it does, but the point here is simply to identify that this is the correct `#else`. The Preprocessor tests look for the full comment. https://github.com/llvm/llvm-project/pull/67613 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
@@ -98,7 +98,7 @@ // == file2.h // REWRITE: #if 0 // REWRITE: #include "file2.h" -// REWRITE: #endif +// REWRITE: #else /* file2.h expanded MaskRay wrote: Does this line not have `by -frewrite-includes`? https://github.com/llvm/llvm-project/pull/67613 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
@@ -332,12 +346,14 @@ void InclusionRewriter::CommentOutDirective(Lexer , // OutputContentUpTo() would not output anything anyway. return; } - OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL; + OS << "#if 0 /* " << getIncludedFileName(Inc) erichkeane wrote: I DO think there is at least a good attempt that could be made to get a unique set of names for each (perhaps a stringify of the path, ala how `#line` does it?), but I think this is a good stand-alone feature here. One thing I MIGHT suggest anyway though is replacing the '0' with `defined(__CLANG_REWRITTEN_INCLUDES)`, so that turning on the `#include` version would be trivial. I might also suggest for certain lines, `defined(__CLANG_REWRITTEN_SYSTEM_INCLUDES)` such that only the system headers are used from the test machine. So that is, a system header would be: ```#if defined(__CLANG_REWRITTEN_INCLUDES) || defined(__CLANG_REWRITTEN_SYSTEM_INCLUDES) /* vector expanded by -frewrite-includes */``` https://github.com/llvm/llvm-project/pull/67613 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
https://github.com/erichkeane commented: >From what I can tell, this looks correct, though I don't know much about this >rewriter, I've only spent a little time reviewing it. That said, i think this >is a very nice improvement. One thing I might request (in addition to what I did in teh comments :D) is a similar change in `-frewrite-modules`. https://github.com/llvm/llvm-project/pull/67613 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
https://github.com/erichkeane edited https://github.com/llvm/llvm-project/pull/67613 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
github-actions[bot] wrote: :warning: C/C++ code formatter, clang-format found issues in your code. :warning: You can test this locally with the following command: ``bash git-clang-format --diff 8ff77a8f04e5a385115c1790a5f03ec263a00faf 0f63068d1085e5064d47916a86fac089c4417e57 -- clang/lib/Frontend/Rewrite/InclusionRewriter.cpp clang/test/Frontend/rewrite-includes-cli-include.c clang/test/Frontend/rewrite-includes-missing.c clang/test/Frontend/rewrite-includes.c clang/test/Modules/preprocess-module.cpp `` View the diff from clang-format here. ``diff diff --git a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp index 6ab57adc41bf..87f048d36b84 100644 --- a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp +++ b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp @@ -418,8 +418,8 @@ void InclusionRewriter::Process(FileID FileId, case tok::pp_import: { SourceLocation Loc = HashToken.getLocation(); const IncludedFile *Inc = FindIncludeAtLocation(Loc); -CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, NextToWrite, - Line, Inc); +CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, +NextToWrite, Line, Inc); if (FileId != PP.getPredefinesFileID()) WriteLineInfo(FileName, Line - 1, FileType, ""); StringRef LineInfoExtra; `` https://github.com/llvm/llvm-project/pull/67613 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
llvmbot wrote: @llvm/pr-subscribers-clang-modules Changes Also add the filename to the comments it emits, to help identify where included text ends. --- Patch is 25.05 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/67613.diff 5 Files Affected: - (modified) clang/lib/Frontend/Rewrite/InclusionRewriter.cpp (+26-7) - (modified) clang/test/Frontend/rewrite-includes-cli-include.c (+1) - (modified) clang/test/Frontend/rewrite-includes-missing.c (+2-2) - (modified) clang/test/Frontend/rewrite-includes.c (+74-56) - (modified) clang/test/Modules/preprocess-module.cpp (+3-3) ``diff diff --git a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp index d3a3db0139c6d2f..6ab57adc41bfe8b 100644 --- a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp +++ b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp @@ -90,8 +90,10 @@ class InclusionRewriter : public PPCallbacks { bool EnsureNewline); void CommentOutDirective(Lexer , const Token , const MemoryBufferRef , StringRef EOL, - unsigned , int ); + unsigned , int , + 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; @@ -311,6 +313,17 @@ void InclusionRewriter::OutputContentUpTo(const MemoryBufferRef , 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 @@ -320,7 +333,8 @@ void InclusionRewriter::CommentOutDirective(Lexer , const Token , const MemoryBufferRef , StringRef LocalEOL, -unsigned , int ) { +unsigned , int , +const IncludedFile *Inc) { OutputContentUpTo(FromFile, NextToWrite, SM.getFileOffset(StartToken.getLocation()), LocalEOL, Line, false); @@ -332,12 +346,14 @@ void InclusionRewriter::CommentOutDirective(Lexer , // OutputContentUpTo() would not output anything anyway. return; } - OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL; + OS << "#if 0 /* " << getIncludedFileName(Inc) + << " 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. @@ -400,15 +416,16 @@ void InclusionRewriter::Process(FileID FileId, case tok::pp_include: case tok::pp_include_next: case tok::pp_import: { +SourceLocation Loc = HashToken.getLocation(); +const IncludedFile *Inc = FindIncludeAtLocation(Loc); CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, NextToWrite, - Line); + 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 " @@ -420,6 +437,8 @@ void InclusionRewriter::Process(FileID FileId, if (Mod) OS << "#pragma clang module end /*" << Mod->getFullModuleName(true) << "*/\n"; + OS << "#endif /* " << getIncludedFileName(Inc)
[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)
https://github.com/pogo59 created https://github.com/llvm/llvm-project/pull/67613 Also add the filename to the comments it emits, to help identify where included text ends. >From 0f63068d1085e5064d47916a86fac089c4417e57 Mon Sep 17 00:00:00 2001 From: Paul Robinson Date: Wed, 27 Sep 2023 14:54:13 -0700 Subject: [PATCH] Make -frewrite-includes put an endif at the end of the included text Also add the filename to the comments it emits, to help identify where included text ends. --- .../Frontend/Rewrite/InclusionRewriter.cpp| 33 - .../Frontend/rewrite-includes-cli-include.c | 1 + .../test/Frontend/rewrite-includes-missing.c | 4 +- clang/test/Frontend/rewrite-includes.c| 130 ++ clang/test/Modules/preprocess-module.cpp | 6 +- 5 files changed, 106 insertions(+), 68 deletions(-) diff --git a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp index d3a3db0139c6d2f..6ab57adc41bfe8b 100644 --- a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp +++ b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp @@ -90,8 +90,10 @@ class InclusionRewriter : public PPCallbacks { bool EnsureNewline); void CommentOutDirective(Lexer , const Token , const MemoryBufferRef , StringRef EOL, - unsigned , int ); + unsigned , int , + 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; @@ -311,6 +313,17 @@ void InclusionRewriter::OutputContentUpTo(const MemoryBufferRef , 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 @@ -320,7 +333,8 @@ void InclusionRewriter::CommentOutDirective(Lexer , const Token , const MemoryBufferRef , StringRef LocalEOL, -unsigned , int ) { +unsigned , int , +const IncludedFile *Inc) { OutputContentUpTo(FromFile, NextToWrite, SM.getFileOffset(StartToken.getLocation()), LocalEOL, Line, false); @@ -332,12 +346,14 @@ void InclusionRewriter::CommentOutDirective(Lexer , // OutputContentUpTo() would not output anything anyway. return; } - OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL; + OS << "#if 0 /* " << getIncludedFileName(Inc) + << " 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. @@ -400,15 +416,16 @@ void InclusionRewriter::Process(FileID FileId, case tok::pp_include: case tok::pp_include_next: case tok::pp_import: { +SourceLocation Loc = HashToken.getLocation(); +const IncludedFile *Inc = FindIncludeAtLocation(Loc); CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, NextToWrite, - Line); + 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 " @@ -420,6 +437,8 @@ void InclusionRewriter::Process(FileID FileId,