[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-10-06 Thread Paul T Robinson via cfe-commits

https://github.com/pogo59 closed https://github.com/llvm/llvm-project/pull/67684
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-10-06 Thread Paul T Robinson via cfe-commits

pogo59 wrote:

Something got screwed up when I tried to squash all but the first commit 
together. I put it back together manually. The NFC commit was pushed as 
9500616, the functional stuff squashed and committed as 71d83bb.

https://github.com/llvm/llvm-project/pull/67684
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-10-06 Thread Paul T Robinson via cfe-commits

https://github.com/pogo59 updated 
https://github.com/llvm/llvm-project/pull/67684

>From 2a0dd575dbbcf80d1cd264d6dbc8de8fa897 Mon Sep 17 00:00:00 2001
From: Paul Robinson 
Date: Wed, 27 Sep 2023 15:30:52 -0700
Subject: [PATCH 1/5] [NFC] Change a reference member to pointer

This will allow the raw_ostream to be redirected in a subsequent commit.
---
 .../lib/Frontend/PrintPreprocessedOutput.cpp  | 208 +-
 1 file changed, 104 insertions(+), 104 deletions(-)

diff --git a/clang/lib/Frontend/PrintPreprocessedOutput.cpp 
b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
index 1b262d9e6f7cb3b..f86ba08d36223be 100644
--- a/clang/lib/Frontend/PrintPreprocessedOutput.cpp
+++ b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
@@ -32,42 +32,42 @@ using namespace clang;
 /// PrintMacroDefinition - Print a macro definition in a form that will be
 /// properly accepted back as a definition.
 static void PrintMacroDefinition(const IdentifierInfo , const MacroInfo ,
- Preprocessor , raw_ostream ) {
-  OS << "#define " << II.getName();
+ Preprocessor , raw_ostream *OS) {
+  *OS << "#define " << II.getName();
 
   if (MI.isFunctionLike()) {
-OS << '(';
+*OS << '(';
 if (!MI.param_empty()) {
   MacroInfo::param_iterator AI = MI.param_begin(), E = MI.param_end();
   for (; AI+1 != E; ++AI) {
-OS << (*AI)->getName();
-OS << ',';
+*OS << (*AI)->getName();
+*OS << ',';
   }
 
   // Last argument.
   if ((*AI)->getName() == "__VA_ARGS__")
-OS << "...";
+*OS << "...";
   else
-OS << (*AI)->getName();
+*OS << (*AI)->getName();
 }
 
 if (MI.isGNUVarargs())
-  OS << "...";  // #define foo(x...)
+  *OS << "...";  // #define foo(x...)
 
-OS << ')';
+*OS << ')';
   }
 
   // GCC always emits a space, even if the macro body is empty.  However, do 
not
   // want to emit two spaces if the first token has a leading space.
   if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace())
-OS << ' ';
+*OS << ' ';
 
   SmallString<128> SpellingBuffer;
   for (const auto  : MI.tokens()) {
 if (T.hasLeadingSpace())
-  OS << ' ';
+  *OS << ' ';
 
-OS << PP.getSpelling(T, SpellingBuffer);
+*OS << PP.getSpelling(T, SpellingBuffer);
   }
 }
 
@@ -81,7 +81,7 @@ class PrintPPOutputPPCallbacks : public PPCallbacks {
   SourceManager 
   TokenConcatenation ConcatInfo;
 public:
-  raw_ostream 
+  raw_ostream *OS;
 private:
   unsigned CurLine;
 
@@ -102,7 +102,7 @@ class PrintPPOutputPPCallbacks : public PPCallbacks {
   Token PrevPrevTok;
 
 public:
-  PrintPPOutputPPCallbacks(Preprocessor , raw_ostream , bool lineMarkers,
+  PrintPPOutputPPCallbacks(Preprocessor , raw_ostream *os, bool lineMarkers,
bool defines, bool DumpIncludeDirectives,
bool UseLineDirectives, bool MinimizeWhitespace,
bool DirectivesOnly)
@@ -235,23 +235,23 @@ void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned 
LineNo,
 
   // Emit #line directives or GNU line markers depending on what mode we're in.
   if (UseLineDirectives) {
-OS << "#line" << ' ' << LineNo << ' ' << '"';
-OS.write_escaped(CurFilename);
-OS << '"';
+*OS << "#line" << ' ' << LineNo << ' ' << '"';
+OS->write_escaped(CurFilename);
+*OS << '"';
   } else {
-OS << '#' << ' ' << LineNo << ' ' << '"';
-OS.write_escaped(CurFilename);
-OS << '"';
+*OS << '#' << ' ' << LineNo << ' ' << '"';
+OS->write_escaped(CurFilename);
+*OS << '"';
 
 if (ExtraLen)
-  OS.write(Extra, ExtraLen);
+  OS->write(Extra, ExtraLen);
 
 if (FileType == SrcMgr::C_System)
-  OS.write(" 3", 2);
+  OS->write(" 3", 2);
 else if (FileType == SrcMgr::C_ExternCSystem)
-  OS.write(" 3 4", 4);
+  OS->write(" 3 4", 4);
   }
-  OS << '\n';
+  *OS << '\n';
 }
 
 /// MoveToLine - Move the output to the source line specified by the location
@@ -266,7 +266,7 @@ bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo,
   bool StartedNewLine = false;
   if ((RequireStartOfLine && EmittedTokensOnThisLine) ||
   EmittedDirectiveOnThisLine) {
-OS << '\n';
+*OS << '\n';
 StartedNewLine = true;
 CurLine += 1;
 EmittedTokensOnThisLine = false;
@@ -283,12 +283,12 @@ bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo,
 // Printing a single line has priority over printing a #line directive, 
even
 // when minimizing whitespace which otherwise would print #line directives
 // for every single line.
-OS << '\n';
+*OS << '\n';
 StartedNewLine = true;
   } else if (!DisableLineMarkers) {
 if (LineNo - CurLine <= 8) {
   const char *NewLines = "\n\n\n\n\n\n\n\n";
-  OS.write(NewLines, LineNo - CurLine);
+  OS->write(NewLines, LineNo - CurLine);
 } else {
   // Emit a #line or line marker.
   

[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-10-06 Thread Erich Keane via cfe-commits

https://github.com/erichkeane approved this pull request.


https://github.com/llvm/llvm-project/pull/67684
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-10-06 Thread Paul T Robinson via cfe-commits

https://github.com/pogo59 updated 
https://github.com/llvm/llvm-project/pull/67684

>From 2a0dd575dbbcf80d1cd264d6dbc8de8fa897 Mon Sep 17 00:00:00 2001
From: Paul Robinson 
Date: Wed, 27 Sep 2023 15:30:52 -0700
Subject: [PATCH 1/5] [NFC] Change a reference member to pointer

This will allow the raw_ostream to be redirected in a subsequent commit.
---
 .../lib/Frontend/PrintPreprocessedOutput.cpp  | 208 +-
 1 file changed, 104 insertions(+), 104 deletions(-)

diff --git a/clang/lib/Frontend/PrintPreprocessedOutput.cpp 
b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
index 1b262d9e6f7cb3b..f86ba08d36223be 100644
--- a/clang/lib/Frontend/PrintPreprocessedOutput.cpp
+++ b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
@@ -32,42 +32,42 @@ using namespace clang;
 /// PrintMacroDefinition - Print a macro definition in a form that will be
 /// properly accepted back as a definition.
 static void PrintMacroDefinition(const IdentifierInfo , const MacroInfo ,
- Preprocessor , raw_ostream ) {
-  OS << "#define " << II.getName();
+ Preprocessor , raw_ostream *OS) {
+  *OS << "#define " << II.getName();
 
   if (MI.isFunctionLike()) {
-OS << '(';
+*OS << '(';
 if (!MI.param_empty()) {
   MacroInfo::param_iterator AI = MI.param_begin(), E = MI.param_end();
   for (; AI+1 != E; ++AI) {
-OS << (*AI)->getName();
-OS << ',';
+*OS << (*AI)->getName();
+*OS << ',';
   }
 
   // Last argument.
   if ((*AI)->getName() == "__VA_ARGS__")
-OS << "...";
+*OS << "...";
   else
-OS << (*AI)->getName();
+*OS << (*AI)->getName();
 }
 
 if (MI.isGNUVarargs())
-  OS << "...";  // #define foo(x...)
+  *OS << "...";  // #define foo(x...)
 
-OS << ')';
+*OS << ')';
   }
 
   // GCC always emits a space, even if the macro body is empty.  However, do 
not
   // want to emit two spaces if the first token has a leading space.
   if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace())
-OS << ' ';
+*OS << ' ';
 
   SmallString<128> SpellingBuffer;
   for (const auto  : MI.tokens()) {
 if (T.hasLeadingSpace())
-  OS << ' ';
+  *OS << ' ';
 
-OS << PP.getSpelling(T, SpellingBuffer);
+*OS << PP.getSpelling(T, SpellingBuffer);
   }
 }
 
@@ -81,7 +81,7 @@ class PrintPPOutputPPCallbacks : public PPCallbacks {
   SourceManager 
   TokenConcatenation ConcatInfo;
 public:
-  raw_ostream 
+  raw_ostream *OS;
 private:
   unsigned CurLine;
 
@@ -102,7 +102,7 @@ class PrintPPOutputPPCallbacks : public PPCallbacks {
   Token PrevPrevTok;
 
 public:
-  PrintPPOutputPPCallbacks(Preprocessor , raw_ostream , bool lineMarkers,
+  PrintPPOutputPPCallbacks(Preprocessor , raw_ostream *os, bool lineMarkers,
bool defines, bool DumpIncludeDirectives,
bool UseLineDirectives, bool MinimizeWhitespace,
bool DirectivesOnly)
@@ -235,23 +235,23 @@ void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned 
LineNo,
 
   // Emit #line directives or GNU line markers depending on what mode we're in.
   if (UseLineDirectives) {
-OS << "#line" << ' ' << LineNo << ' ' << '"';
-OS.write_escaped(CurFilename);
-OS << '"';
+*OS << "#line" << ' ' << LineNo << ' ' << '"';
+OS->write_escaped(CurFilename);
+*OS << '"';
   } else {
-OS << '#' << ' ' << LineNo << ' ' << '"';
-OS.write_escaped(CurFilename);
-OS << '"';
+*OS << '#' << ' ' << LineNo << ' ' << '"';
+OS->write_escaped(CurFilename);
+*OS << '"';
 
 if (ExtraLen)
-  OS.write(Extra, ExtraLen);
+  OS->write(Extra, ExtraLen);
 
 if (FileType == SrcMgr::C_System)
-  OS.write(" 3", 2);
+  OS->write(" 3", 2);
 else if (FileType == SrcMgr::C_ExternCSystem)
-  OS.write(" 3 4", 4);
+  OS->write(" 3 4", 4);
   }
-  OS << '\n';
+  *OS << '\n';
 }
 
 /// MoveToLine - Move the output to the source line specified by the location
@@ -266,7 +266,7 @@ bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo,
   bool StartedNewLine = false;
   if ((RequireStartOfLine && EmittedTokensOnThisLine) ||
   EmittedDirectiveOnThisLine) {
-OS << '\n';
+*OS << '\n';
 StartedNewLine = true;
 CurLine += 1;
 EmittedTokensOnThisLine = false;
@@ -283,12 +283,12 @@ bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo,
 // Printing a single line has priority over printing a #line directive, 
even
 // when minimizing whitespace which otherwise would print #line directives
 // for every single line.
-OS << '\n';
+*OS << '\n';
 StartedNewLine = true;
   } else if (!DisableLineMarkers) {
 if (LineNo - CurLine <= 8) {
   const char *NewLines = "\n\n\n\n\n\n\n\n";
-  OS.write(NewLines, LineNo - CurLine);
+  OS->write(NewLines, LineNo - CurLine);
 } else {
   // Emit a #line or line marker.
   

[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-10-06 Thread Paul T Robinson via cfe-commits

pogo59 wrote:

Many of the clang-format complaints on this one are from the first commit, 
which I wanted to make as minimalist as possible. I believe I didn't make the 
file any _less_ conformant in that commit. :)  I will fix the others.

https://github.com/llvm/llvm-project/pull/67684
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-10-06 Thread Erich Keane via cfe-commits

https://github.com/erichkeane commented:

This too fails the formatter.  That said, I have no comments, though 
preprocessing isn't my place of expertise.  Most of the changes are pretty mild 
though, and the approach of just replacing the output file with a null-OS seems 
appropriate.  Fix the formatting, and I'll accept, but please give others 
(Aaron in particular) time to take a look before committing.

https://github.com/llvm/llvm-project/pull/67684
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-10-06 Thread Paul T Robinson via cfe-commits

pogo59 wrote:

Ping @erichkeane were you willing to approve both this and #67613?

https://github.com/llvm/llvm-project/pull/67684
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-09-29 Thread Paul T Robinson via cfe-commits

pogo59 wrote:

Also added a release note.

https://github.com/llvm/llvm-project/pull/67684
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-09-29 Thread Paul T Robinson via cfe-commits

https://github.com/pogo59 updated 
https://github.com/llvm/llvm-project/pull/67684

>From 2a0dd575dbbcf80d1cd264d6dbc8de8fa897 Mon Sep 17 00:00:00 2001
From: Paul Robinson 
Date: Wed, 27 Sep 2023 15:30:52 -0700
Subject: [PATCH 1/4] [NFC] Change a reference member to pointer

This will allow the raw_ostream to be redirected in a subsequent commit.
---
 .../lib/Frontend/PrintPreprocessedOutput.cpp  | 208 +-
 1 file changed, 104 insertions(+), 104 deletions(-)

diff --git a/clang/lib/Frontend/PrintPreprocessedOutput.cpp 
b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
index 1b262d9e6f7cb3b..f86ba08d36223be 100644
--- a/clang/lib/Frontend/PrintPreprocessedOutput.cpp
+++ b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
@@ -32,42 +32,42 @@ using namespace clang;
 /// PrintMacroDefinition - Print a macro definition in a form that will be
 /// properly accepted back as a definition.
 static void PrintMacroDefinition(const IdentifierInfo , const MacroInfo ,
- Preprocessor , raw_ostream ) {
-  OS << "#define " << II.getName();
+ Preprocessor , raw_ostream *OS) {
+  *OS << "#define " << II.getName();
 
   if (MI.isFunctionLike()) {
-OS << '(';
+*OS << '(';
 if (!MI.param_empty()) {
   MacroInfo::param_iterator AI = MI.param_begin(), E = MI.param_end();
   for (; AI+1 != E; ++AI) {
-OS << (*AI)->getName();
-OS << ',';
+*OS << (*AI)->getName();
+*OS << ',';
   }
 
   // Last argument.
   if ((*AI)->getName() == "__VA_ARGS__")
-OS << "...";
+*OS << "...";
   else
-OS << (*AI)->getName();
+*OS << (*AI)->getName();
 }
 
 if (MI.isGNUVarargs())
-  OS << "...";  // #define foo(x...)
+  *OS << "...";  // #define foo(x...)
 
-OS << ')';
+*OS << ')';
   }
 
   // GCC always emits a space, even if the macro body is empty.  However, do 
not
   // want to emit two spaces if the first token has a leading space.
   if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace())
-OS << ' ';
+*OS << ' ';
 
   SmallString<128> SpellingBuffer;
   for (const auto  : MI.tokens()) {
 if (T.hasLeadingSpace())
-  OS << ' ';
+  *OS << ' ';
 
-OS << PP.getSpelling(T, SpellingBuffer);
+*OS << PP.getSpelling(T, SpellingBuffer);
   }
 }
 
@@ -81,7 +81,7 @@ class PrintPPOutputPPCallbacks : public PPCallbacks {
   SourceManager 
   TokenConcatenation ConcatInfo;
 public:
-  raw_ostream 
+  raw_ostream *OS;
 private:
   unsigned CurLine;
 
@@ -102,7 +102,7 @@ class PrintPPOutputPPCallbacks : public PPCallbacks {
   Token PrevPrevTok;
 
 public:
-  PrintPPOutputPPCallbacks(Preprocessor , raw_ostream , bool lineMarkers,
+  PrintPPOutputPPCallbacks(Preprocessor , raw_ostream *os, bool lineMarkers,
bool defines, bool DumpIncludeDirectives,
bool UseLineDirectives, bool MinimizeWhitespace,
bool DirectivesOnly)
@@ -235,23 +235,23 @@ void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned 
LineNo,
 
   // Emit #line directives or GNU line markers depending on what mode we're in.
   if (UseLineDirectives) {
-OS << "#line" << ' ' << LineNo << ' ' << '"';
-OS.write_escaped(CurFilename);
-OS << '"';
+*OS << "#line" << ' ' << LineNo << ' ' << '"';
+OS->write_escaped(CurFilename);
+*OS << '"';
   } else {
-OS << '#' << ' ' << LineNo << ' ' << '"';
-OS.write_escaped(CurFilename);
-OS << '"';
+*OS << '#' << ' ' << LineNo << ' ' << '"';
+OS->write_escaped(CurFilename);
+*OS << '"';
 
 if (ExtraLen)
-  OS.write(Extra, ExtraLen);
+  OS->write(Extra, ExtraLen);
 
 if (FileType == SrcMgr::C_System)
-  OS.write(" 3", 2);
+  OS->write(" 3", 2);
 else if (FileType == SrcMgr::C_ExternCSystem)
-  OS.write(" 3 4", 4);
+  OS->write(" 3 4", 4);
   }
-  OS << '\n';
+  *OS << '\n';
 }
 
 /// MoveToLine - Move the output to the source line specified by the location
@@ -266,7 +266,7 @@ bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo,
   bool StartedNewLine = false;
   if ((RequireStartOfLine && EmittedTokensOnThisLine) ||
   EmittedDirectiveOnThisLine) {
-OS << '\n';
+*OS << '\n';
 StartedNewLine = true;
 CurLine += 1;
 EmittedTokensOnThisLine = false;
@@ -283,12 +283,12 @@ bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo,
 // Printing a single line has priority over printing a #line directive, 
even
 // when minimizing whitespace which otherwise would print #line directives
 // for every single line.
-OS << '\n';
+*OS << '\n';
 StartedNewLine = true;
   } else if (!DisableLineMarkers) {
 if (LineNo - CurLine <= 8) {
   const char *NewLines = "\n\n\n\n\n\n\n\n";
-  OS.write(NewLines, LineNo - CurLine);
+  OS->write(NewLines, LineNo - CurLine);
 } else {
   // Emit a #line or line marker.
   

[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-09-29 Thread Paul T Robinson via cfe-commits

pogo59 wrote:

> The first link does, somewhat, and seems like the right place to add this new 
> option. I'll have a go at that.

Well, that one appears to be generated from the Options.td file, and simply 
includes the help text, so in effect I've already done that. :)

UsersManual.html doesn't have a preprocessor options section at all, and I 
think it's outside the scope of this patch to add one.

clang.html (actually CommandGuide/clang.rst) doesn't really talk about the 
other `-E` affecting options, but at least it has a subsection about 
preprocessor options, so I'll see what I can do there.

https://github.com/llvm/llvm-project/pull/67684
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-09-29 Thread Paul T Robinson via cfe-commits

pogo59 wrote:

Aha! Thank you. I should have found that third one in my searches, although it 
doesn't really describe the options that affect `-E` mode. The first link does, 
somewhat, and seems like the right place to add this new option. I'll have a go 
at that.

https://github.com/llvm/llvm-project/pull/67684
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-09-29 Thread Jessica Clarke via cfe-commits

jrtc27 wrote:

Oh and https://clang.llvm.org/docs/CommandGuide/clang.html.

https://github.com/llvm/llvm-project/pull/67684
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-09-29 Thread Jessica Clarke via cfe-commits

jrtc27 wrote:

I mean places like https://clang.llvm.org/docs/ClangCommandLineReference.html 
and https://clang.llvm.org/docs/UsersManual.html#command-line-options.

https://github.com/llvm/llvm-project/pull/67684
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-09-29 Thread Paul T Robinson via cfe-commits

pogo59 wrote:

@jrtc27 I've beefed up the help text some. I'm unable to find any "rendered 
documentation" for `-E` and friends; if you can point me to it, I'd be happy to 
add the new option there.

https://github.com/llvm/llvm-project/pull/67684
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-09-29 Thread Paul T Robinson via cfe-commits

https://github.com/pogo59 updated 
https://github.com/llvm/llvm-project/pull/67684

>From 2a0dd575dbbcf80d1cd264d6dbc8de8fa897 Mon Sep 17 00:00:00 2001
From: Paul Robinson 
Date: Wed, 27 Sep 2023 15:30:52 -0700
Subject: [PATCH 1/3] [NFC] Change a reference member to pointer

This will allow the raw_ostream to be redirected in a subsequent commit.
---
 .../lib/Frontend/PrintPreprocessedOutput.cpp  | 208 +-
 1 file changed, 104 insertions(+), 104 deletions(-)

diff --git a/clang/lib/Frontend/PrintPreprocessedOutput.cpp 
b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
index 1b262d9e6f7cb3b..f86ba08d36223be 100644
--- a/clang/lib/Frontend/PrintPreprocessedOutput.cpp
+++ b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
@@ -32,42 +32,42 @@ using namespace clang;
 /// PrintMacroDefinition - Print a macro definition in a form that will be
 /// properly accepted back as a definition.
 static void PrintMacroDefinition(const IdentifierInfo , const MacroInfo ,
- Preprocessor , raw_ostream ) {
-  OS << "#define " << II.getName();
+ Preprocessor , raw_ostream *OS) {
+  *OS << "#define " << II.getName();
 
   if (MI.isFunctionLike()) {
-OS << '(';
+*OS << '(';
 if (!MI.param_empty()) {
   MacroInfo::param_iterator AI = MI.param_begin(), E = MI.param_end();
   for (; AI+1 != E; ++AI) {
-OS << (*AI)->getName();
-OS << ',';
+*OS << (*AI)->getName();
+*OS << ',';
   }
 
   // Last argument.
   if ((*AI)->getName() == "__VA_ARGS__")
-OS << "...";
+*OS << "...";
   else
-OS << (*AI)->getName();
+*OS << (*AI)->getName();
 }
 
 if (MI.isGNUVarargs())
-  OS << "...";  // #define foo(x...)
+  *OS << "...";  // #define foo(x...)
 
-OS << ')';
+*OS << ')';
   }
 
   // GCC always emits a space, even if the macro body is empty.  However, do 
not
   // want to emit two spaces if the first token has a leading space.
   if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace())
-OS << ' ';
+*OS << ' ';
 
   SmallString<128> SpellingBuffer;
   for (const auto  : MI.tokens()) {
 if (T.hasLeadingSpace())
-  OS << ' ';
+  *OS << ' ';
 
-OS << PP.getSpelling(T, SpellingBuffer);
+*OS << PP.getSpelling(T, SpellingBuffer);
   }
 }
 
@@ -81,7 +81,7 @@ class PrintPPOutputPPCallbacks : public PPCallbacks {
   SourceManager 
   TokenConcatenation ConcatInfo;
 public:
-  raw_ostream 
+  raw_ostream *OS;
 private:
   unsigned CurLine;
 
@@ -102,7 +102,7 @@ class PrintPPOutputPPCallbacks : public PPCallbacks {
   Token PrevPrevTok;
 
 public:
-  PrintPPOutputPPCallbacks(Preprocessor , raw_ostream , bool lineMarkers,
+  PrintPPOutputPPCallbacks(Preprocessor , raw_ostream *os, bool lineMarkers,
bool defines, bool DumpIncludeDirectives,
bool UseLineDirectives, bool MinimizeWhitespace,
bool DirectivesOnly)
@@ -235,23 +235,23 @@ void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned 
LineNo,
 
   // Emit #line directives or GNU line markers depending on what mode we're in.
   if (UseLineDirectives) {
-OS << "#line" << ' ' << LineNo << ' ' << '"';
-OS.write_escaped(CurFilename);
-OS << '"';
+*OS << "#line" << ' ' << LineNo << ' ' << '"';
+OS->write_escaped(CurFilename);
+*OS << '"';
   } else {
-OS << '#' << ' ' << LineNo << ' ' << '"';
-OS.write_escaped(CurFilename);
-OS << '"';
+*OS << '#' << ' ' << LineNo << ' ' << '"';
+OS->write_escaped(CurFilename);
+*OS << '"';
 
 if (ExtraLen)
-  OS.write(Extra, ExtraLen);
+  OS->write(Extra, ExtraLen);
 
 if (FileType == SrcMgr::C_System)
-  OS.write(" 3", 2);
+  OS->write(" 3", 2);
 else if (FileType == SrcMgr::C_ExternCSystem)
-  OS.write(" 3 4", 4);
+  OS->write(" 3 4", 4);
   }
-  OS << '\n';
+  *OS << '\n';
 }
 
 /// MoveToLine - Move the output to the source line specified by the location
@@ -266,7 +266,7 @@ bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo,
   bool StartedNewLine = false;
   if ((RequireStartOfLine && EmittedTokensOnThisLine) ||
   EmittedDirectiveOnThisLine) {
-OS << '\n';
+*OS << '\n';
 StartedNewLine = true;
 CurLine += 1;
 EmittedTokensOnThisLine = false;
@@ -283,12 +283,12 @@ bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo,
 // Printing a single line has priority over printing a #line directive, 
even
 // when minimizing whitespace which otherwise would print #line directives
 // for every single line.
-OS << '\n';
+*OS << '\n';
 StartedNewLine = true;
   } else if (!DisableLineMarkers) {
 if (LineNo - CurLine <= 8) {
   const char *NewLines = "\n\n\n\n\n\n\n\n";
-  OS.write(NewLines, LineNo - CurLine);
+  OS->write(NewLines, LineNo - CurLine);
 } else {
   // Emit a #line or line marker.
   

[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-09-28 Thread Jessica Clarke via cfe-commits

jrtc27 wrote:

Its limitations, i.e. how in various cases it can break or change correct code, 
need to be documented somewhere alongside the option (ideally a disclaimer in 
--help's output and a longer explanation in the rendered documentation).

https://github.com/llvm/llvm-project/pull/67684
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-09-28 Thread via cfe-commits

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 1b7285bf3ee73975fa63acfc5301a9b57a53fca2 
a71131f734c90aeaff9c3fb0cdc57c0f81dd8971 -- 
clang/test/Frontend/Inputs/dashE/dashE.h 
clang/test/Frontend/Inputs/dashE/sys/a.h 
clang/test/Frontend/Inputs/dashE/sys/b.h 
clang/test/Frontend/dashE-sysincludes.cpp 
clang/include/clang/Frontend/PreprocessorOutputOptions.h 
clang/lib/Driver/ToolChains/Clang.cpp 
clang/lib/Frontend/PrintPreprocessedOutput.cpp 
clang/test/Preprocessor/minimize-whitespace-messages.c
``





View the diff from clang-format here.


``diff
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 1101add97b65..f4c1b90ff5e7 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -6716,8 +6716,7 @@ void Clang::ConstructJob(Compilation , const JobAction 
,
 types::ID InputType = Inputs[0].getType();
 if (!isDerivedFromC(InputType))
   D.Diag(diag::err_drv_opt_unsupported_input_type)
-  << "-fminimize-whitespace"
-  << types::getTypeName(InputType);
+  << "-fminimize-whitespace" << types::getTypeName(InputType);
 CmdArgs.push_back("-fminimize-whitespace");
   }
 
@@ -6727,8 +6726,7 @@ void Clang::ConstructJob(Compilation , const JobAction 
,
 types::ID InputType = Inputs[0].getType();
 if (!isDerivedFromC(InputType))
   D.Diag(diag::err_drv_opt_unsupported_input_type)
-  << "-fkeep-system-includes"
-  << types::getTypeName(InputType);
+  << "-fkeep-system-includes" << types::getTypeName(InputType);
 CmdArgs.push_back("-fkeep-system-includes");
   }
 
diff --git a/clang/lib/Frontend/PrintPreprocessedOutput.cpp 
b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
index 4056649e85c2..81ee1654af51 100644
--- a/clang/lib/Frontend/PrintPreprocessedOutput.cpp
+++ b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
@@ -52,7 +52,7 @@ static void PrintMacroDefinition(const IdentifierInfo , 
const MacroInfo ,
 }
 
 if (MI.isGNUVarargs())
-  *OS << "...";  // #define foo(x...)
+  *OS << "..."; // #define foo(x...)
 
 *OS << ')';
   }
@@ -82,6 +82,7 @@ class PrintPPOutputPPCallbacks : public PPCallbacks {
   TokenConcatenation ConcatInfo;
 public:
   raw_ostream *OS;
+
 private:
   unsigned CurLine;
 
@@ -409,11 +410,9 @@ void PrintPPOutputPPCallbacks::InclusionDirective(
 MoveToLine(HashLoc, /*RequireStartOfLine=*/true);
 const std::string TokenText = PP.getSpelling(IncludeTok);
 assert(!TokenText.empty());
-*OS << "#" << TokenText << " "
-<< (IsAngled ? '<' : '"') << FileName << (IsAngled ? '>' : '"')
-<< " /* clang -E "
-<< (DumpIncludeDirectives ? "-dI" : "-fkeep-system-includes")
-<< " */";
+*OS << "#" << TokenText << " " << (IsAngled ? '<' : '"') << FileName
+<< (IsAngled ? '>' : '"') << " /* clang -E "
+<< (DumpIncludeDirectives ? "-dI" : "-fkeep-system-includes") << " */";
 setEmittedDirectiveOnThisLine();
   }
 
@@ -424,12 +423,10 @@ void PrintPPOutputPPCallbacks::InclusionDirective(
 case tok::pp_import:
 case tok::pp_include_next:
   MoveToLine(HashLoc, /*RequireStartOfLine=*/true);
-  *OS << "#pragma clang module import "
-  << Imported->getFullModuleName(true)
+  *OS << "#pragma clang module import " << 
Imported->getFullModuleName(true)
   << " /* clang -E: implicit import for "
-  << "#" << PP.getSpelling(IncludeTok) << " "
-  << (IsAngled ? '<' : '"') << FileName << (IsAngled ? '>' : '"')
-  << " */";
+  << "#" << PP.getSpelling(IncludeTok) << " " << (IsAngled ? '<' : '"')
+  << FileName << (IsAngled ? '>' : '"') << " */";
   setEmittedDirectiveOnThisLine();
   break;
 
@@ -513,10 +510,8 @@ static void outputPrintable(raw_ostream *OS, StringRef 
Str) {
 if (isPrintable(Char) && Char != '\\' && Char != '"')
   *OS << (char)Char;
 else // Output anything hard as an octal escape.
-  *OS << '\\'
-  << (char)('0' + ((Char >> 6) & 7))
-  << (char)('0' + ((Char >> 3) & 7))
-  << (char)('0' + ((Char >> 0) & 7));
+  *OS << '\\' << (char)('0' + ((Char >> 6) & 7))
+  << (char)('0' + ((Char >> 3) & 7)) << (char)('0' + ((Char >> 0) & 
7));
   }
 }
 
@@ -605,15 +600,33 @@ void 
PrintPPOutputPPCallbacks::PragmaWarning(SourceLocation Loc,
 
   *OS << "#pragma warning(";
   switch(WarningSpec) {
-case PWS_Default:  *OS << "default"; break;
-case PWS_Disable:  *OS << "disable"; break;
-case PWS_Error:*OS << "error"; break;
-case PWS_Once: *OS << "once"; break;
-case PWS_Suppress: *OS << "suppress"; break;
-case PWS_Level1:   *OS << '1'; break;
-case PWS_Level2:   *OS << '2'; break;
-case PWS_Level3:   *OS << '3'; break;
-

[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-09-28 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang


Changes

This option will cause -E to preserve the #include directives
for system headers, rather than expanding them into the output.
This can greatly reduce the volume of preprocessed source text 
in a test case, making test case reduction simpler.

Note that -fkeep-system-includes is not always appropriate. For
example, if the problem you want to reproduce is induced by a
system header file, it's better to expand those headers fully.
If your source defines symbols that influence the content of a
system header (e.g., _POSIX_SOURCE) then -E will eliminate the
definition, potentially changing the meaning of the preprocessed
source. If you use -isystem to point to non-system headers, for
example to suppress warnings in third-party software, those will
not be expanded and might make the preprocessed source less useful
as a test case.


---

Patch is 30.09 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/67684.diff


10 Files Affected:

- (modified) clang/include/clang/Basic/DiagnosticDriverKinds.td (+2-2) 
- (modified) clang/include/clang/Driver/Options.td (+7) 
- (modified) clang/include/clang/Frontend/PreprocessorOutputOptions.h (+2) 
- (modified) clang/lib/Driver/ToolChains/Clang.cpp (+16-2) 
- (modified) clang/lib/Frontend/PrintPreprocessedOutput.cpp (+123-110) 
- (added) clang/test/Frontend/Inputs/dashE/dashE.h (+3) 
- (added) clang/test/Frontend/Inputs/dashE/sys/a.h (+3) 
- (added) clang/test/Frontend/Inputs/dashE/sys/b.h (+1) 
- (added) clang/test/Frontend/dashE-sysincludes.cpp (+23) 
- (modified) clang/test/Preprocessor/minimize-whitespace-messages.c (+9-6) 


``diff
diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td 
b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 2a48c063e243ee0..446a610f56f0379 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -172,8 +172,8 @@ def err_drv_invalid_Xopenmp_target_with_args : Error<
   "invalid -Xopenmp-target argument: '%0', options requiring arguments are 
unsupported">;
 def err_drv_argument_only_allowed_with : Error<
   "invalid argument '%0' only allowed with '%1'">;
-def err_drv_minws_unsupported_input_type : Error<
-  "'-fminimize-whitespace' invalid for input of type %0">;
+def err_drv_opt_unsupported_input_type : Error<
+  "'%0' invalid for input of type %1">;
 def err_drv_amdgpu_ieee_without_no_honor_nans : Error<
   "invalid argument '-mno-amdgpu-ieee' only allowed with relaxed NaN 
handling">;
 def err_drv_argument_not_allowed_with : Error<
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index f8143651cd3c151..4a64aee64d3f06e 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2483,6 +2483,13 @@ defm minimize_whitespace : 
BoolFOption<"minimize-whitespace",
   "whitespace such that two files with only formatting changes are "
   "equal.\n\nOnly valid with -E on C-like inputs and incompatible "
   "with -traditional-cpp.">, NegFlag>;
+defm keep_system_includes : BoolFOption<"keep-system-includes",
+  PreprocessorOutputOpts<"KeepSystemIncludes">, DefaultFalse,
+  PosFlag,
+  NegFlag>;
 
 def ffreestanding : Flag<["-"], "ffreestanding">, Group,
   Visibility<[ClangOption, CC1Option]>,
diff --git a/clang/include/clang/Frontend/PreprocessorOutputOptions.h 
b/clang/include/clang/Frontend/PreprocessorOutputOptions.h
index d542032431b14c8..db2ec9f2ae20698 100644
--- a/clang/include/clang/Frontend/PreprocessorOutputOptions.h
+++ b/clang/include/clang/Frontend/PreprocessorOutputOptions.h
@@ -26,6 +26,7 @@ class PreprocessorOutputOptions {
   unsigned RewriteImports  : 1;///< Include contents of 
transitively-imported modules.
   unsigned MinimizeWhitespace : 1; ///< Ignore whitespace from input.
   unsigned DirectivesOnly : 1; ///< Process directives but do not expand 
macros.
+  unsigned KeepSystemIncludes : 1; ///< Do not expand system headers.
 
 public:
   PreprocessorOutputOptions() {
@@ -40,6 +41,7 @@ class PreprocessorOutputOptions {
 RewriteImports = 0;
 MinimizeWhitespace = 0;
 DirectivesOnly = 0;
+KeepSystemIncludes = 0;
   }
 };
 
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index dda6aef641904aa..1101add97b65e85 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -68,7 +68,9 @@ using namespace llvm::opt;
 static void CheckPreprocessingOptions(const Driver , const ArgList ) {
   if (Arg *A = Args.getLastArg(clang::driver::options::OPT_C, options::OPT_CC,
options::OPT_fminimize_whitespace,
-   options::OPT_fno_minimize_whitespace)) {
+   options::OPT_fno_minimize_whitespace,
+   options::OPT_fkeep_system_includes,
+

[clang] Add -fkeep-system-includes modifier for -E (PR #67684)

2023-09-28 Thread Paul T Robinson via cfe-commits

https://github.com/pogo59 created 
https://github.com/llvm/llvm-project/pull/67684

This option will cause -E to preserve the #include directives
for system headers, rather than expanding them into the output.
This can greatly reduce the volume of preprocessed source text 
in a test case, making test case reduction simpler.

Note that -fkeep-system-includes is not always appropriate. For
example, if the problem you want to reproduce is induced by a
system header file, it's better to expand those headers fully.
If your source defines symbols that influence the content of a
system header (e.g., _POSIX_SOURCE) then -E will eliminate the
definition, potentially changing the meaning of the preprocessed
source. If you use -isystem to point to non-system headers, for
example to suppress warnings in third-party software, those will
not be expanded and might make the preprocessed source less useful
as a test case.


>From 2a0dd575dbbcf80d1cd264d6dbc8de8fa897 Mon Sep 17 00:00:00 2001
From: Paul Robinson 
Date: Wed, 27 Sep 2023 15:30:52 -0700
Subject: [PATCH 1/2] [NFC] Change a reference member to pointer

This will allow the raw_ostream to be redirected in a subsequent commit.
---
 .../lib/Frontend/PrintPreprocessedOutput.cpp  | 208 +-
 1 file changed, 104 insertions(+), 104 deletions(-)

diff --git a/clang/lib/Frontend/PrintPreprocessedOutput.cpp 
b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
index 1b262d9e6f7cb3b..f86ba08d36223be 100644
--- a/clang/lib/Frontend/PrintPreprocessedOutput.cpp
+++ b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
@@ -32,42 +32,42 @@ using namespace clang;
 /// PrintMacroDefinition - Print a macro definition in a form that will be
 /// properly accepted back as a definition.
 static void PrintMacroDefinition(const IdentifierInfo , const MacroInfo ,
- Preprocessor , raw_ostream ) {
-  OS << "#define " << II.getName();
+ Preprocessor , raw_ostream *OS) {
+  *OS << "#define " << II.getName();
 
   if (MI.isFunctionLike()) {
-OS << '(';
+*OS << '(';
 if (!MI.param_empty()) {
   MacroInfo::param_iterator AI = MI.param_begin(), E = MI.param_end();
   for (; AI+1 != E; ++AI) {
-OS << (*AI)->getName();
-OS << ',';
+*OS << (*AI)->getName();
+*OS << ',';
   }
 
   // Last argument.
   if ((*AI)->getName() == "__VA_ARGS__")
-OS << "...";
+*OS << "...";
   else
-OS << (*AI)->getName();
+*OS << (*AI)->getName();
 }
 
 if (MI.isGNUVarargs())
-  OS << "...";  // #define foo(x...)
+  *OS << "...";  // #define foo(x...)
 
-OS << ')';
+*OS << ')';
   }
 
   // GCC always emits a space, even if the macro body is empty.  However, do 
not
   // want to emit two spaces if the first token has a leading space.
   if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace())
-OS << ' ';
+*OS << ' ';
 
   SmallString<128> SpellingBuffer;
   for (const auto  : MI.tokens()) {
 if (T.hasLeadingSpace())
-  OS << ' ';
+  *OS << ' ';
 
-OS << PP.getSpelling(T, SpellingBuffer);
+*OS << PP.getSpelling(T, SpellingBuffer);
   }
 }
 
@@ -81,7 +81,7 @@ class PrintPPOutputPPCallbacks : public PPCallbacks {
   SourceManager 
   TokenConcatenation ConcatInfo;
 public:
-  raw_ostream 
+  raw_ostream *OS;
 private:
   unsigned CurLine;
 
@@ -102,7 +102,7 @@ class PrintPPOutputPPCallbacks : public PPCallbacks {
   Token PrevPrevTok;
 
 public:
-  PrintPPOutputPPCallbacks(Preprocessor , raw_ostream , bool lineMarkers,
+  PrintPPOutputPPCallbacks(Preprocessor , raw_ostream *os, bool lineMarkers,
bool defines, bool DumpIncludeDirectives,
bool UseLineDirectives, bool MinimizeWhitespace,
bool DirectivesOnly)
@@ -235,23 +235,23 @@ void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned 
LineNo,
 
   // Emit #line directives or GNU line markers depending on what mode we're in.
   if (UseLineDirectives) {
-OS << "#line" << ' ' << LineNo << ' ' << '"';
-OS.write_escaped(CurFilename);
-OS << '"';
+*OS << "#line" << ' ' << LineNo << ' ' << '"';
+OS->write_escaped(CurFilename);
+*OS << '"';
   } else {
-OS << '#' << ' ' << LineNo << ' ' << '"';
-OS.write_escaped(CurFilename);
-OS << '"';
+*OS << '#' << ' ' << LineNo << ' ' << '"';
+OS->write_escaped(CurFilename);
+*OS << '"';
 
 if (ExtraLen)
-  OS.write(Extra, ExtraLen);
+  OS->write(Extra, ExtraLen);
 
 if (FileType == SrcMgr::C_System)
-  OS.write(" 3", 2);
+  OS->write(" 3", 2);
 else if (FileType == SrcMgr::C_ExternCSystem)
-  OS.write(" 3 4", 4);
+  OS->write(" 3 4", 4);
   }
-  OS << '\n';
+  *OS << '\n';
 }
 
 /// MoveToLine - Move the output to the source line specified by the location
@@ -266,7 +266,7 @@ bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo,
   bool