[clang] Make -frewrite-includes put an endif at the end of the included text (PR #67613)

Paul T Robinson via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 6 10:26:34 PDT 2023


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 <paul.robinson at sony.com>
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 &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;
@@ -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
@@ -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);
@@ -332,12 +346,14 @@ void InclusionRewriter::CommentOutDirective(Lexer &DirectiveLex,
     // 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)
+                 << " 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 d63f966f79ca6af..437bc2ffadf1960 100644
--- a/clang/test/Frontend/rewrite-includes-cli-include.c
+++ b/clang/test/Frontend/rewrite-includes-cli-include.c
@@ -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
diff --git a/clang/test/Frontend/rewrite-includes-missing.c b/clang/test/Frontend/rewrite-includes-missing.c
index 25a59a03217ff73..984c322fd2a51f6 100644
--- a/clang/test/Frontend/rewrite-includes-missing.c
+++ b/clang/test/Frontend/rewrite-includes-missing.c
@@ -1,8 +1,8 @@
 // RUN: %clang_cc1 -verify -E -frewrite-includes %s -o - | FileCheck -strict-whitespace %s
 
 #include "foobar.h" // expected-error {{'foobar.h' file not found}}
-// CHECK: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECK: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "foobar.h"
-// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 3 "{{.*}}rewrite-includes-missing.c"{{$}}
 // CHECK-NEXT: {{^}}# 4 "{{.*}}rewrite-includes-missing.c"{{$}}
diff --git a/clang/test/Frontend/rewrite-includes.c b/clang/test/Frontend/rewrite-includes.c
index 15254931250c1af..20571f99803ca12 100644
--- a/clang/test/Frontend/rewrite-includes.c
+++ b/clang/test/Frontend/rewrite-includes.c
@@ -28,72 +28,77 @@ static int unused;
 // CHECK: {{^}}// STARTCOMPARE{{$}}
 // CHECK-NEXT: {{^}}#define A(a,b) a ## b{{$}}
 // CHECK-NEXT: {{^}}A(in,t) a;{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if 0 /* rewrite-includes1.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes1.h"{{$}}
-// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#else /* rewrite-includes1.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 7 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes1.h" 1{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#pragma clang system_header{{$}}
-// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 2 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes1.h" 3{{$}}
 // CHECK-NEXT: {{^}}int included_line1;{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if 0 /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes2.h"{{$}}
-// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#else /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 3 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes1.h" 3{{$}}
 // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes2.h" 1 3{{$}}
 // CHECK-NEXT: {{^}}int included_line2;{{$}}
+// CHECK-NEXT: {{^}}#endif /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 4 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes1.h" 2 3{{$}}
+// CHECK-NEXT: {{^}}#endif /* rewrite-includes1.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 8 "{{.*}}rewrite-includes.c" 2{{$}}
 // CHECK-NEXT: {{^}}#ifdef FIRST{{$}}
 // CHECK-NEXT: {{^}}#define HEADER "rewrite-includes3.h"{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if 0 /* rewrite-includes3.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include HEADER{{$}}
-// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#else /* rewrite-includes3.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 10 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes3.h" 1{{$}}
 // CHECK-NEXT: {{^}}unsigned int included_line3 = -10;{{$}}
+// CHECK-NEXT: {{^}}#endif /* rewrite-includes3.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 11 "{{.*}}rewrite-includes.c" 2{{$}}
 // CHECK-NEXT: {{^}}#else{{$}}
 // CHECK-NEXT: {{^}}# 12 "{{.*}}rewrite-includes.c"{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes4.h"{{$}}
-// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 12 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}# 13 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}#endif{{$}}
 // CHECK-NEXT: {{^}}# 14 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}  // indented{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if 0 /* rewrite-includes5.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#/**/include /**/ "rewrite-includes5.h" /**/ {{\\}}{{$}}
 // CHECK-NEXT: {{^}} {{$}}
-// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#else /* rewrite-includes5.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 16 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes5.h" 1{{$}}
 // CHECK-NEXT: {{^}}int included_line5;{{$}}
+// CHECK-NEXT: {{^}}#endif /* rewrite-includes5.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 17 "{{.*}}rewrite-includes.c" 2{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if 0 /* rewrite-includes6.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes6.h" // comment{{$}}
-// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#else /* rewrite-includes6.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 17 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes6.h" 1{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#pragma once{{$}}
-// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 2 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes6.h"{{$}}
 // CHECK-NEXT: {{^}}int included_line6;{{$}}
+// CHECK-NEXT: {{^}}#endif /* rewrite-includes6.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 18 "{{.*}}rewrite-includes.c" 2{{$}}
 // CHECK-NEXT: {{^}} {{$}}
-// CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes6.h" /* comment{{$}}
 // CHECK-NEXT: {{^}}                                  continues */{{$}}
-// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 20 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}# 21 "{{.*}}rewrite-includes.c"{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if 0 /* rewrite-includes7.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes7.h"{{$}}
-// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#else /* rewrite-includes7.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 21 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes7.h" 1{{$}}
 // CHECK-NEXT: {{^}}#ifndef REWRITE_INCLUDES_7{{$}}
@@ -101,15 +106,16 @@ static int unused;
 // CHECK-NEXT: {{^}}int included_line7;{{$}}
 // CHECK-NEXT: {{^}}#endif{{$}}
 // CHECK-NEXT: {{^}}# 5 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes7.h"{{$}}
+// CHECK-NEXT: {{^}}#endif /* rewrite-includes7.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 22 "{{.*}}rewrite-includes.c" 2{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes7.h"{{$}}
-// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 22 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}# 23 "{{.*}}rewrite-includes.c"{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if 0 /* rewrite-includes8.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes8.h"{{$}}
-// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#else /* rewrite-includes8.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 23 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes8.h" 1{{$}}
 // CHECK-NEXT: {{^}}#if 0 /* disabled by -frewrite-includes */{{$}}
@@ -135,10 +141,11 @@ static int unused;
 // CHECK-NEXT: {{^}}# 5 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes8.h"{{$}}
 // CHECK-NEXT: {{^}}#endif{{$}}
 // CHECK-NEXT: {{^}}# 6 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes8.h"{{$}}
+// CHECK-NEXT: {{^}}#endif /* rewrite-includes8.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 24 "{{.*}}rewrite-includes.c" 2{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if 0 /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes9.h"{{$}}
-// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#else /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 24 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes9.h" 1{{$}}
 // CHECK-NEXT: {{^}}#if 0 /* disabled by -frewrite-includes */{{$}}
@@ -147,15 +154,17 @@ static int unused;
 // CHECK-NEXT: {{^}}#endif /* disabled by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#if 1 /* evaluated by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 2 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes9.h"{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if 0 /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include_next <rewrite-includes9.h>{{$}}
-// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#else /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 2 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes9.h"{{$}}
 // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)NextIncludes(/|\\\\)}}rewrite-includes9.h" 1{{$}}
 // CHECK-NEXT: {{^}}int included_line9;{{$}}
+// CHECK-NEXT: {{^}}#endif /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 3 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes9.h" 2{{$}}
 // CHECK-NEXT: {{^}}#endif{{$}}
 // CHECK-NEXT: {{^}}# 4 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes9.h"{{$}}
+// CHECK-NEXT: {{^}}#endif /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 25 "{{.*}}rewrite-includes.c" 2{{$}}
 // CHECK-NEXT: {{^}}static int unused;{{$}}
 // CHECK-NEXT: {{^}}// ENDCOMPARE{{$}}
@@ -163,59 +172,65 @@ static int unused;
 // CHECKNL: {{^}}// STARTCOMPARE{{$}}
 // CHECKNL-NEXT: {{^}}#define A(a,b) a ## b{{$}}
 // CHECKNL-NEXT: {{^}}A(in,t) a;{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if 0 /* rewrite-includes1.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes1.h"{{$}}
-// CHECKNL-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#else /* rewrite-includes1.h expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#pragma clang system_header{{$}}
-// CHECKNL-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}int included_line1;{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if 0 /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes2.h"{{$}}
-// CHECKNL-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#else /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}int included_line2;{{$}}
+// CHECKNL-NEXT: {{^}}#endif /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#endif /* rewrite-includes1.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#ifdef FIRST{{$}}
 // CHECKNL-NEXT: {{^}}#define HEADER "rewrite-includes3.h"{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if 0 /* rewrite-includes3.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include HEADER{{$}}
-// CHECKNL-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#else /* rewrite-includes3.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}unsigned int included_line3 = -10;{{$}}
+// CHECKNL-NEXT: {{^}}#endif /* rewrite-includes3.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#else{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes4.h"{{$}}
-// CHECKNL-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#endif{{$}}
 // CHECKNL-NEXT: {{^}}  // indented{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if 0 /* rewrite-includes5.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#/**/include /**/ "rewrite-includes5.h" /**/ {{\\}}{{$}}
 // CHECKNL-NEXT: {{^}} {{$}}
-// CHECKNL-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#else /* rewrite-includes5.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}int included_line5;{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#endif /* rewrite-includes5.h expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if 0 /* rewrite-includes6.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes6.h" // comment{{$}}
-// CHECKNL-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#else /* rewrite-includes6.h expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#pragma once{{$}}
-// CHECKNL-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}int included_line6;{{$}}
+// CHECKNL-NEXT: {{^}}#endif /* rewrite-includes6.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}} {{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes6.h" /* comment{{$}}
 // CHECKNL-NEXT: {{^}}                                  continues */{{$}}
-// CHECKNL-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if 0 /* rewrite-includes7.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes7.h"{{$}}
-// CHECKNL-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#else /* rewrite-includes7.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#ifndef REWRITE_INCLUDES_7{{$}}
 // CHECKNL-NEXT: {{^}}#define REWRITE_INCLUDES_7{{$}}
 // CHECKNL-NEXT: {{^}}int included_line7;{{$}}
 // CHECKNL-NEXT: {{^}}#endif{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#endif /* rewrite-includes7.h expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes7.h"{{$}}
-// CHECKNL-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if 0 /* rewrite-includes8.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes8.h"{{$}}
-// CHECKNL-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#else /* rewrite-includes8.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#if 0 /* disabled by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#if __has_include_next(<rewrite-includes8.h>){{$}}
 // CHECKNL-NEXT: {{^}}#endif{{$}}
@@ -234,19 +249,22 @@ static int unused;
 // CHECKNL-NEXT: {{^}}#endif /* disabled by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#if 0 /* evaluated by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#endif{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#endif /* rewrite-includes8.h expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if 0 /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes9.h"{{$}}
-// CHECKNL-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#else /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#if 0 /* disabled by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#if __has_include_next(<rewrite-includes9.h>){{$}}
 // CHECKNL-NEXT: {{^}}#endif{{$}}
 // CHECKNL-NEXT: {{^}}#endif /* disabled by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#if 1 /* evaluated by -frewrite-includes */{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if 0 /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include_next <rewrite-includes9.h>{{$}}
-// CHECKNL-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#else /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}int included_line9;{{$}}
+// CHECKNL-NEXT: {{^}}#endif /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#endif{{$}}
+// CHECKNL-NEXT: {{^}}#endif /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}static int unused;{{$}}
 // CHECKNL-NEXT: {{^}}// ENDCOMPARE{{$}}
 
diff --git a/clang/test/Modules/preprocess-module.cpp b/clang/test/Modules/preprocess-module.cpp
index ee909a811836f67..513cd8ea3e1dbe3 100644
--- a/clang/test/Modules/preprocess-module.cpp
+++ b/clang/test/Modules/preprocess-module.cpp
@@ -73,7 +73,7 @@
 // CHECK: # 1 "<module-includes>"
 // REWRITE: #if 0
 // REWRITE: #include "file.h"
-// REWRITE: #endif
+// REWRITE: #else /* file.h expanded by -frewrite-includes
 //
 // FIXME: It would be preferable to consistently put the module begin/end in
 // the same file, but the relative ordering of PP callbacks and module
@@ -98,7 +98,7 @@
 // == file2.h
 // REWRITE: #if 0
 // REWRITE: #include "file2.h"
-// REWRITE: #endif
+// REWRITE: #else /* file2.h expanded
 //
 // REWRITE: #pragma clang module begin file
 // CHECK: # 1 "{{.*}}file2.h" 1
@@ -107,7 +107,7 @@
 // ==== recursively re-enter file.h
 // REWRITE: #if 0
 // REWRITE: #include "file.h"
-// REWRITE: #endif
+// REWRITE: #else /* file.h expanded
 //
 // REWRITE: #pragma clang module begin file
 // CHECK: # 1 "{{.*}}file.h" 1

>From 7f08bd9c5e82701e4229ab9ba4ca0d9c979326e0 Mon Sep 17 00:00:00 2001
From: Paul Robinson <paul.robinson at sony.com>
Date: Fri, 29 Sep 2023 09:36:01 -0700
Subject: [PATCH 2/5] Add a toggle for rewritten includes

---
 .../Frontend/Rewrite/InclusionRewriter.cpp    |  8 ++-
 .../test/Frontend/rewrite-includes-missing.c  |  2 +-
 clang/test/Frontend/rewrite-includes.c        | 56 +++++++++----------
 clang/test/Modules/preprocess-module.cpp      |  6 +-
 4 files changed, 38 insertions(+), 34 deletions(-)

diff --git a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp
index 6ab57adc41bfe8b..eabc63d6566284d 100644
--- a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp
+++ b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp
@@ -346,8 +346,12 @@ void InclusionRewriter::CommentOutDirective(Lexer &DirectiveLex,
     // OutputContentUpTo() would not output anything anyway.
     return;
   }
-  OS << "#if 0 /* " << getIncludedFileName(Inc)
-     << " expanded by -frewrite-includes */" << MainEOL;
+  if (Inc)
+    OS << "#if defined(__CLANG_REWRITTEN_INCLUDES) /* "
+       << getIncludedFileName(Inc);
+  else
+    OS << "#if 0 /*";
+  OS << " expanded by -frewrite-includes */" << MainEOL;
   OutputContentUpTo(FromFile, NextToWrite,
                     SM.getFileOffset(DirectiveToken.getLocation()) +
                         DirectiveToken.getLength(),
diff --git a/clang/test/Frontend/rewrite-includes-missing.c b/clang/test/Frontend/rewrite-includes-missing.c
index 984c322fd2a51f6..688e6de7bab3119 100644
--- a/clang/test/Frontend/rewrite-includes-missing.c
+++ b/clang/test/Frontend/rewrite-includes-missing.c
@@ -1,7 +1,7 @@
 // RUN: %clang_cc1 -verify -E -frewrite-includes %s -o - | FileCheck -strict-whitespace %s
 
 #include "foobar.h" // expected-error {{'foobar.h' file not found}}
-// CHECK: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
+// 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"{{$}}
diff --git a/clang/test/Frontend/rewrite-includes.c b/clang/test/Frontend/rewrite-includes.c
index 20571f99803ca12..0de0143032978ef 100644
--- a/clang/test/Frontend/rewrite-includes.c
+++ b/clang/test/Frontend/rewrite-includes.c
@@ -28,17 +28,17 @@ static int unused;
 // CHECK: {{^}}// STARTCOMPARE{{$}}
 // CHECK-NEXT: {{^}}#define A(a,b) a ## b{{$}}
 // CHECK-NEXT: {{^}}A(in,t) a;{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* rewrite-includes1.h expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes1.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes1.h"{{$}}
 // CHECK-NEXT: {{^}}#else /* rewrite-includes1.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 7 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes1.h" 1{{$}}
-// CHECK-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#pragma clang system_header{{$}}
 // CHECK-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 2 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes1.h" 3{{$}}
 // CHECK-NEXT: {{^}}int included_line1;{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes2.h"{{$}}
 // CHECK-NEXT: {{^}}#else /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 3 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes1.h" 3{{$}}
@@ -50,7 +50,7 @@ static int unused;
 // CHECK-NEXT: {{^}}# 8 "{{.*}}rewrite-includes.c" 2{{$}}
 // CHECK-NEXT: {{^}}#ifdef FIRST{{$}}
 // CHECK-NEXT: {{^}}#define HEADER "rewrite-includes3.h"{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* rewrite-includes3.h expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes3.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include HEADER{{$}}
 // CHECK-NEXT: {{^}}#else /* rewrite-includes3.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 10 "{{.*}}rewrite-includes.c"{{$}}
@@ -60,7 +60,7 @@ static int unused;
 // CHECK-NEXT: {{^}}# 11 "{{.*}}rewrite-includes.c" 2{{$}}
 // CHECK-NEXT: {{^}}#else{{$}}
 // CHECK-NEXT: {{^}}# 12 "{{.*}}rewrite-includes.c"{{$}}
-// CHECK-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes4.h"{{$}}
 // CHECK-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 12 "{{.*}}rewrite-includes.c"{{$}}
@@ -68,7 +68,7 @@ static int unused;
 // CHECK-NEXT: {{^}}#endif{{$}}
 // CHECK-NEXT: {{^}}# 14 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}  // indented{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* rewrite-includes5.h expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes5.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#/**/include /**/ "rewrite-includes5.h" /**/ {{\\}}{{$}}
 // CHECK-NEXT: {{^}} {{$}}
 // CHECK-NEXT: {{^}}#else /* rewrite-includes5.h expanded by -frewrite-includes */{{$}}
@@ -77,12 +77,12 @@ static int unused;
 // CHECK-NEXT: {{^}}int included_line5;{{$}}
 // CHECK-NEXT: {{^}}#endif /* rewrite-includes5.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 17 "{{.*}}rewrite-includes.c" 2{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* rewrite-includes6.h expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes6.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes6.h" // comment{{$}}
 // CHECK-NEXT: {{^}}#else /* rewrite-includes6.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 17 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes6.h" 1{{$}}
-// CHECK-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#pragma once{{$}}
 // CHECK-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 2 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes6.h"{{$}}
@@ -90,13 +90,13 @@ static int unused;
 // CHECK-NEXT: {{^}}#endif /* rewrite-includes6.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 18 "{{.*}}rewrite-includes.c" 2{{$}}
 // CHECK-NEXT: {{^}} {{$}}
-// CHECK-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes6.h" /* comment{{$}}
 // CHECK-NEXT: {{^}}                                  continues */{{$}}
 // CHECK-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 20 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}# 21 "{{.*}}rewrite-includes.c"{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* rewrite-includes7.h expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes7.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes7.h"{{$}}
 // CHECK-NEXT: {{^}}#else /* rewrite-includes7.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 21 "{{.*}}rewrite-includes.c"{{$}}
@@ -108,12 +108,12 @@ static int unused;
 // CHECK-NEXT: {{^}}# 5 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes7.h"{{$}}
 // CHECK-NEXT: {{^}}#endif /* rewrite-includes7.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 22 "{{.*}}rewrite-includes.c" 2{{$}}
-// CHECK-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes7.h"{{$}}
 // CHECK-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 22 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}# 23 "{{.*}}rewrite-includes.c"{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* rewrite-includes8.h expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes8.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes8.h"{{$}}
 // CHECK-NEXT: {{^}}#else /* rewrite-includes8.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 23 "{{.*}}rewrite-includes.c"{{$}}
@@ -143,7 +143,7 @@ static int unused;
 // CHECK-NEXT: {{^}}# 6 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes8.h"{{$}}
 // CHECK-NEXT: {{^}}#endif /* rewrite-includes8.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 24 "{{.*}}rewrite-includes.c" 2{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes9.h"{{$}}
 // CHECK-NEXT: {{^}}#else /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 24 "{{.*}}rewrite-includes.c"{{$}}
@@ -154,7 +154,7 @@ static int unused;
 // CHECK-NEXT: {{^}}#endif /* disabled by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#if 1 /* evaluated by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 2 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes9.h"{{$}}
-// CHECK-NEXT: {{^}}#if 0 /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include_next <rewrite-includes9.h>{{$}}
 // CHECK-NEXT: {{^}}#else /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 2 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes9.h"{{$}}
@@ -172,14 +172,14 @@ static int unused;
 // CHECKNL: {{^}}// STARTCOMPARE{{$}}
 // CHECKNL-NEXT: {{^}}#define A(a,b) a ## b{{$}}
 // CHECKNL-NEXT: {{^}}A(in,t) a;{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* rewrite-includes1.h expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes1.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes1.h"{{$}}
 // CHECKNL-NEXT: {{^}}#else /* rewrite-includes1.h expanded by -frewrite-includes */{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#pragma clang system_header{{$}}
 // CHECKNL-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}int included_line1;{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes2.h"{{$}}
 // CHECKNL-NEXT: {{^}}#else /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}int included_line2;{{$}}
@@ -187,37 +187,37 @@ static int unused;
 // CHECKNL-NEXT: {{^}}#endif /* rewrite-includes1.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#ifdef FIRST{{$}}
 // CHECKNL-NEXT: {{^}}#define HEADER "rewrite-includes3.h"{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* rewrite-includes3.h expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes3.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include HEADER{{$}}
 // CHECKNL-NEXT: {{^}}#else /* rewrite-includes3.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}unsigned int included_line3 = -10;{{$}}
 // CHECKNL-NEXT: {{^}}#endif /* rewrite-includes3.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#else{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes4.h"{{$}}
 // CHECKNL-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#endif{{$}}
 // CHECKNL-NEXT: {{^}}  // indented{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* rewrite-includes5.h expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes5.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#/**/include /**/ "rewrite-includes5.h" /**/ {{\\}}{{$}}
 // CHECKNL-NEXT: {{^}} {{$}}
 // CHECKNL-NEXT: {{^}}#else /* rewrite-includes5.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}int included_line5;{{$}}
 // CHECKNL-NEXT: {{^}}#endif /* rewrite-includes5.h expanded by -frewrite-includes */{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* rewrite-includes6.h expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes6.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes6.h" // comment{{$}}
 // CHECKNL-NEXT: {{^}}#else /* rewrite-includes6.h expanded by -frewrite-includes */{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#pragma once{{$}}
 // CHECKNL-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}int included_line6;{{$}}
 // CHECKNL-NEXT: {{^}}#endif /* rewrite-includes6.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}} {{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes6.h" /* comment{{$}}
 // CHECKNL-NEXT: {{^}}                                  continues */{{$}}
 // CHECKNL-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* rewrite-includes7.h expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes7.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes7.h"{{$}}
 // CHECKNL-NEXT: {{^}}#else /* rewrite-includes7.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#ifndef REWRITE_INCLUDES_7{{$}}
@@ -225,10 +225,10 @@ static int unused;
 // CHECKNL-NEXT: {{^}}int included_line7;{{$}}
 // CHECKNL-NEXT: {{^}}#endif{{$}}
 // CHECKNL-NEXT: {{^}}#endif /* rewrite-includes7.h expanded by -frewrite-includes */{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /*  expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes7.h"{{$}}
 // CHECKNL-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* rewrite-includes8.h expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes8.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes8.h"{{$}}
 // CHECKNL-NEXT: {{^}}#else /* rewrite-includes8.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#if 0 /* disabled by -frewrite-includes */{{$}}
@@ -250,7 +250,7 @@ static int unused;
 // CHECKNL-NEXT: {{^}}#if 0 /* evaluated by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#endif{{$}}
 // CHECKNL-NEXT: {{^}}#endif /* rewrite-includes8.h expanded by -frewrite-includes */{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes9.h"{{$}}
 // CHECKNL-NEXT: {{^}}#else /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#if 0 /* disabled by -frewrite-includes */{{$}}
@@ -258,7 +258,7 @@ static int unused;
 // CHECKNL-NEXT: {{^}}#endif{{$}}
 // CHECKNL-NEXT: {{^}}#endif /* disabled by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#if 1 /* evaluated by -frewrite-includes */{{$}}
-// CHECKNL-NEXT: {{^}}#if 0 /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include_next <rewrite-includes9.h>{{$}}
 // CHECKNL-NEXT: {{^}}#else /* rewrite-includes9.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}int included_line9;{{$}}
diff --git a/clang/test/Modules/preprocess-module.cpp b/clang/test/Modules/preprocess-module.cpp
index 513cd8ea3e1dbe3..7000a58adf01d63 100644
--- a/clang/test/Modules/preprocess-module.cpp
+++ b/clang/test/Modules/preprocess-module.cpp
@@ -71,7 +71,7 @@
 
 // == file.h
 // CHECK: # 1 "<module-includes>"
-// REWRITE: #if 0
+// REWRITE: #if defined(__CLANG_REWRITTEN_INCLUDES)
 // REWRITE: #include "file.h"
 // REWRITE: #else /* file.h expanded by -frewrite-includes
 //
@@ -96,7 +96,7 @@
 // NO-REWRITE: #pragma clang module end
 
 // == file2.h
-// REWRITE: #if 0
+// REWRITE: #if defined(__CLANG_REWRITTEN_INCLUDES)
 // REWRITE: #include "file2.h"
 // REWRITE: #else /* file2.h expanded
 //
@@ -105,7 +105,7 @@
 // NO-REWRITE: #pragma clang module begin file
 //
 // ==== recursively re-enter file.h
-// REWRITE: #if 0
+// REWRITE: #if defined(__CLANG_REWRITTEN_INCLUDES)
 // REWRITE: #include "file.h"
 // REWRITE: #else /* file.h expanded
 //

>From fe6eb4b1b32d23edc180f757ef4c046267cffcd3 Mon Sep 17 00:00:00 2001
From: Paul Robinson <paul.robinson at sony.com>
Date: Fri, 29 Sep 2023 11:56:22 -0700
Subject: [PATCH 3/5] Add toggle for system includes

---
 .../Frontend/Rewrite/InclusionRewriter.cpp    | 13 ++++++----
 .../test/Frontend/rewrite-includes-missing.c  |  2 +-
 clang/test/Frontend/rewrite-includes.c        | 24 +++++++++----------
 3 files changed, 21 insertions(+), 18 deletions(-)

diff --git a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp
index eabc63d6566284d..029e923ba8fed63 100644
--- a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp
+++ b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp
@@ -346,17 +346,20 @@ void InclusionRewriter::CommentOutDirective(Lexer &DirectiveLex,
     // OutputContentUpTo() would not output anything anyway.
     return;
   }
-  if (Inc)
-    OS << "#if defined(__CLANG_REWRITTEN_INCLUDES) /* "
-       << getIncludedFileName(Inc);
-  else
+  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 << (Inc ? "#else" : "#endif") << " /* " << getIncludedFileName(Inc)
+  OS << (Inc ? "#else /* " : "#endif /*") << getIncludedFileName(Inc)
      << " expanded by -frewrite-includes */" << MainEOL;
 }
 
diff --git a/clang/test/Frontend/rewrite-includes-missing.c b/clang/test/Frontend/rewrite-includes-missing.c
index 688e6de7bab3119..25a59a03217ff73 100644
--- a/clang/test/Frontend/rewrite-includes-missing.c
+++ b/clang/test/Frontend/rewrite-includes-missing.c
@@ -3,6 +3,6 @@
 #include "foobar.h" // expected-error {{'foobar.h' file not found}}
 // CHECK: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "foobar.h"
-// CHECK-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 3 "{{.*}}rewrite-includes-missing.c"{{$}}
 // CHECK-NEXT: {{^}}# 4 "{{.*}}rewrite-includes-missing.c"{{$}}
diff --git a/clang/test/Frontend/rewrite-includes.c b/clang/test/Frontend/rewrite-includes.c
index 0de0143032978ef..7cca01c11e585f1 100644
--- a/clang/test/Frontend/rewrite-includes.c
+++ b/clang/test/Frontend/rewrite-includes.c
@@ -35,10 +35,10 @@ static int unused;
 // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes1.h" 1{{$}}
 // CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#pragma clang system_header{{$}}
-// CHECK-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 2 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes1.h" 3{{$}}
 // CHECK-NEXT: {{^}}int included_line1;{{$}}
-// CHECK-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) || defined(__CLANG_REWRITTEN_SYSTEM_INCLUDES) /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes2.h"{{$}}
 // CHECK-NEXT: {{^}}#else /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 3 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes1.h" 3{{$}}
@@ -62,7 +62,7 @@ static int unused;
 // CHECK-NEXT: {{^}}# 12 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes4.h"{{$}}
-// CHECK-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 12 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}# 13 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}#endif{{$}}
@@ -84,7 +84,7 @@ static int unused;
 // CHECK-NEXT: {{^}}# 1 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes6.h" 1{{$}}
 // CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#pragma once{{$}}
-// CHECK-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 2 "{{.*[/\\]Inputs(/|\\\\)}}rewrite-includes6.h"{{$}}
 // CHECK-NEXT: {{^}}int included_line6;{{$}}
 // CHECK-NEXT: {{^}}#endif /* rewrite-includes6.h expanded by -frewrite-includes */{{$}}
@@ -93,7 +93,7 @@ static int unused;
 // CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes6.h" /* comment{{$}}
 // CHECK-NEXT: {{^}}                                  continues */{{$}}
-// CHECK-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 20 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}# 21 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes7.h expanded by -frewrite-includes */{{$}}
@@ -110,7 +110,7 @@ static int unused;
 // CHECK-NEXT: {{^}}# 22 "{{.*}}rewrite-includes.c" 2{{$}}
 // CHECK-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}#include "rewrite-includes7.h"{{$}}
-// CHECK-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
+// CHECK-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
 // CHECK-NEXT: {{^}}# 22 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}# 23 "{{.*}}rewrite-includes.c"{{$}}
 // CHECK-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes8.h expanded by -frewrite-includes */{{$}}
@@ -177,9 +177,9 @@ static int unused;
 // CHECKNL-NEXT: {{^}}#else /* rewrite-includes1.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#pragma clang system_header{{$}}
-// CHECKNL-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}int included_line1;{{$}}
-// CHECKNL-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) || defined(__CLANG_REWRITTEN_SYSTEM_INCLUDES) /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes2.h"{{$}}
 // CHECKNL-NEXT: {{^}}#else /* rewrite-includes2.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}int included_line2;{{$}}
@@ -195,7 +195,7 @@ static int unused;
 // CHECKNL-NEXT: {{^}}#else{{$}}
 // CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes4.h"{{$}}
-// CHECKNL-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#endif{{$}}
 // CHECKNL-NEXT: {{^}}  // indented{{$}}
 // CHECKNL-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes5.h expanded by -frewrite-includes */{{$}}
@@ -209,14 +209,14 @@ static int unused;
 // CHECKNL-NEXT: {{^}}#else /* rewrite-includes6.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#pragma once{{$}}
-// CHECKNL-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}int included_line6;{{$}}
 // CHECKNL-NEXT: {{^}}#endif /* rewrite-includes6.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}} {{$}}
 // CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes6.h" /* comment{{$}}
 // CHECKNL-NEXT: {{^}}                                  continues */{{$}}
-// CHECKNL-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes7.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes7.h"{{$}}
 // CHECKNL-NEXT: {{^}}#else /* rewrite-includes7.h expanded by -frewrite-includes */{{$}}
@@ -227,7 +227,7 @@ static int unused;
 // CHECKNL-NEXT: {{^}}#endif /* rewrite-includes7.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#if 0 /* expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes7.h"{{$}}
-// CHECKNL-NEXT: {{^}}#endif /*  expanded by -frewrite-includes */{{$}}
+// CHECKNL-NEXT: {{^}}#endif /* expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#if defined(__CLANG_REWRITTEN_INCLUDES) /* rewrite-includes8.h expanded by -frewrite-includes */{{$}}
 // CHECKNL-NEXT: {{^}}#include "rewrite-includes8.h"{{$}}
 // CHECKNL-NEXT: {{^}}#else /* rewrite-includes8.h expanded by -frewrite-includes */{{$}}

>From debc210d0f3424e32a1cabe66b6462243b14fb6b Mon Sep 17 00:00:00 2001
From: Paul Robinson <paul.robinson at sony.com>
Date: Fri, 29 Sep 2023 14:13:28 -0700
Subject: [PATCH 4/5] Add release note

---
 clang/docs/ReleaseNotes.rst | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 68172d5317a13ba..82229ba0f25799c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -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
 -------------------------

>From dad0b34f5f565e1264e4084d6c9318690da0e59d Mon Sep 17 00:00:00 2001
From: Paul Robinson <paul.robinson at sony.com>
Date: Fri, 6 Oct 2023 10:26:19 -0700
Subject: [PATCH 5/5] Fix clang-format complaint

---
 clang/lib/Frontend/Rewrite/InclusionRewriter.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp
index 029e923ba8fed63..2e116a1961c7e5e 100644
--- a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp
+++ b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp
@@ -426,7 +426,7 @@ void InclusionRewriter::Process(FileID FileId,
             SourceLocation Loc = HashToken.getLocation();
             const IncludedFile *Inc = FindIncludeAtLocation(Loc);
             CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, NextToWrite,
-              Line, Inc);
+                                Line, Inc);
             if (FileId != PP.getPredefinesFileID())
               WriteLineInfo(FileName, Line - 1, FileType, "");
             StringRef LineInfoExtra;



More information about the cfe-commits mailing list