llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-driver

Author: Joseph Huber (jhuber6)

<details>
<summary>Changes</summary>

Summary:
This PR tries to consolidate the color output handling in Clang. The
motivation was noticing that `-Xclang -ast-dump` would not behave like
`-fcolor-diagnostics` and would output ANSI codes to a file when I tried
to pipe it.

This PR primarily turns the handling into a tri-state enum keyed off of
`-f[no]-color-diagnostics`. The default/auto case will be if the target
stream supports colors. Getting this to work required a lot of seemingly
unrelated plumbing.


---

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


14 Files Affected:

- (modified) clang-tools-extra/clang-tidy/ClangTidy.cpp (+5-2) 
- (modified) clang-tools-extra/clangd/Compiler.cpp (+1-1) 
- (modified) clang/include/clang/Basic/DiagnosticOptions.def (+1-1) 
- (modified) clang/include/clang/Basic/DiagnosticOptions.h (+20) 
- (modified) clang/include/clang/Options/Options.td (+1-1) 
- (modified) clang/lib/AST/ASTDumper.cpp (+17-11) 
- (modified) clang/lib/Basic/Warnings.cpp (+3-1) 
- (modified) clang/lib/Driver/ToolChains/CommonArgs.cpp (+9-1) 
- (modified) clang/lib/Frontend/CompilerInvocation.cpp (+20-17) 
- (modified) clang/lib/Frontend/TextDiagnostic.cpp (+12-11) 
- (modified) clang/lib/Frontend/TextDiagnosticPrinter.cpp (+3-2) 
- (modified) clang/unittests/Tooling/ToolingTest.cpp (+4-3) 
- (modified) flang/lib/Frontend/CompilerInvocation.cpp (+5-3) 
- (modified) flang/lib/Frontend/TextDiagnosticPrinter.cpp (+4-4) 


``````````diff
diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp 
b/clang-tools-extra/clang-tidy/ClangTidy.cpp
index 05c8fd02fe86a..e72e36134f141 100644
--- a/clang-tools-extra/clang-tidy/ClangTidy.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp
@@ -106,9 +106,12 @@ class ErrorReporter {
         Diags(DiagnosticIDs::create(), DiagOpts, DiagPrinter),
         SourceMgr(Diags, Files), Context(Context), ApplyFixes(ApplyFixes) {
     DiagOpts.ShowColors = Context.getOptions().UseColor.value_or(
-        llvm::sys::Process::StandardOutHasColors());
+                              llvm::sys::Process::StandardOutHasColors())
+                              ? DiagOpts.ShowColors = Colors_On
+                              : DiagOpts.ShowColors = Colors_Off;
     DiagPrinter->BeginSourceFile(LangOpts);
-    if (DiagOpts.ShowColors && !llvm::sys::Process::StandardOutIsDisplayed())
+    if (DiagOpts.showColors(llvm::sys::Process::StandardOutHasColors()) &&
+        !llvm::sys::Process::StandardOutIsDisplayed())
       llvm::sys::Process::UseANSIEscapeCodes(true);
   }
 
diff --git a/clang-tools-extra/clangd/Compiler.cpp 
b/clang-tools-extra/clangd/Compiler.cpp
index 9ea7df139382a..aea03d28da53f 100644
--- a/clang-tools-extra/clangd/Compiler.cpp
+++ b/clang-tools-extra/clangd/Compiler.cpp
@@ -49,7 +49,7 @@ void disableUnsupportedOptions(CompilerInvocation &CI) {
   // our compiler invocation set-up doesn't seem to work with it (leading
   // assertions in VerifyDiagnosticConsumer).
   CI.getDiagnosticOpts().VerifyDiagnostics = false;
-  CI.getDiagnosticOpts().ShowColors = false;
+  CI.getDiagnosticOpts().setShowColors(Colors_Off);
 
   // Disable any dependency outputting, we don't want to generate files or 
write
   // to stdout/stderr.
diff --git a/clang/include/clang/Basic/DiagnosticOptions.def 
b/clang/include/clang/Basic/DiagnosticOptions.def
index 17d518c2b7fdd..64e524c249277 100644
--- a/clang/include/clang/Basic/DiagnosticOptions.def
+++ b/clang/include/clang/Basic/DiagnosticOptions.def
@@ -65,7 +65,7 @@ VALUE_DIAGOPT(ShowCategories, 2, 0) /// Show categories: 0 -> 
none, 1 -> Number,
 
 ENUM_DIAGOPT(Format, TextDiagnosticFormat, 2, Clang) /// Format for 
diagnostics:
 
-DIAGOPT(ShowColors, 1, 0)       /// Show diagnostics with ANSI color sequences.
+ENUM_DIAGOPT(ShowColors, ShowColorsKind, 2, Colors_Auto)
 DIAGOPT(UseANSIEscapeCodes, 1, 0)
 ENUM_DIAGOPT(ShowOverloads, OverloadsShown, 1,
              Ovl_All)    /// Overload candidates to show.
diff --git a/clang/include/clang/Basic/DiagnosticOptions.h 
b/clang/include/clang/Basic/DiagnosticOptions.h
index a230022224de5..b2ce9cf3ae362 100644
--- a/clang/include/clang/Basic/DiagnosticOptions.h
+++ b/clang/include/clang/Basic/DiagnosticOptions.h
@@ -23,6 +23,13 @@ class ArgList;
 namespace clang {
 class DiagnosticsEngine;
 
+/// Controls whether to show colors in output.
+enum ShowColorsKind : unsigned {
+  Colors_Auto,
+  Colors_On,
+  Colors_Off,
+};
+
 /// Specifies which overload candidates to display when overload
 /// resolution fails.
 enum OverloadsShown : unsigned {
@@ -143,6 +150,19 @@ class DiagnosticOptions {
 #define ENUM_DIAGOPT(Name, Type, Bits, Default) set##Name(Default);
 #include "clang/Basic/DiagnosticOptions.def"
   }
+
+  /// Resolve the color mode against a stream's capability.
+  bool showColors(bool StreamHasColors) const {
+    switch (getShowColors()) {
+    case Colors_On:
+      return true;
+    case Colors_Off:
+      return false;
+    case Colors_Auto:
+      return StreamHasColors;
+    }
+    return false;
+  }
 };
 
 using TextDiagnosticFormat = DiagnosticOptions::TextDiagnosticFormat;
diff --git a/clang/include/clang/Options/Options.td 
b/clang/include/clang/Options/Options.td
index 4fd892e58df86..3fd6a75216ea9 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -2065,7 +2065,7 @@ def fcolor_diagnostics : Flag<["-"], 
"fcolor-diagnostics">, Group<f_Group>,
   Visibility<[ClangOption, CLOption, DXCOption, CC1Option, FlangOption, 
FC1Option]>,
   HelpText<"Enable colors in diagnostics">;
 def fno_color_diagnostics : Flag<["-"], "fno-color-diagnostics">, 
Group<f_Group>,
-  Visibility<[ClangOption, CLOption, DXCOption, FlangOption]>,
+  Visibility<[ClangOption, CLOption, DXCOption, CC1Option, FlangOption]>,
   HelpText<"Disable colors in diagnostics">;
 def : Flag<["-"], "fdiagnostics-color">, Group<f_Group>,
   Visibility<[ClangOption, CLOption, DXCOption, FlangOption]>,
diff --git a/clang/lib/AST/ASTDumper.cpp b/clang/lib/AST/ASTDumper.cpp
index 00c1140b538ec..5c11336f20aa2 100644
--- a/clang/lib/AST/ASTDumper.cpp
+++ b/clang/lib/AST/ASTDumper.cpp
@@ -16,12 +16,18 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/DeclLookups.h"
 #include "clang/AST/JSONNodeDumper.h"
+#include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/SourceManager.h"
 #include "llvm/Support/raw_ostream.h"
 
 using namespace clang;
 using namespace clang::comments;
 
+static bool showColorsForStream(const ASTContext &Ctx, raw_ostream &OS) {
+  return Ctx.getDiagnostics().getDiagnosticOptions().showColors(
+      OS.has_colors());
+}
+
 void ASTDumper::dumpInvalidDeclContext(const DeclContext *DC) {
   NodeDumper.AddChild([=] {
     if (!DC) {
@@ -190,7 +196,7 @@ LLVM_DUMP_METHOD void QualType::dump() const {
 
 LLVM_DUMP_METHOD void QualType::dump(llvm::raw_ostream &OS,
                                      const ASTContext &Context) const {
-  ASTDumper Dumper(OS, Context, Context.getDiagnostics().getShowColors());
+  ASTDumper Dumper(OS, Context, showColorsForStream(Context, OS));
   Dumper.Visit(*this);
 }
 
@@ -211,7 +217,7 @@ LLVM_DUMP_METHOD void TypeLoc::dump() const {
 
 LLVM_DUMP_METHOD void TypeLoc::dump(llvm::raw_ostream &OS,
                                     const ASTContext &Context) const {
-  ASTDumper(OS, Context, 
Context.getDiagnostics().getShowColors()).Visit(*this);
+  ASTDumper(OS, Context, showColorsForStream(Context, OS)).Visit(*this);
 }
 
 
//===----------------------------------------------------------------------===//
@@ -231,7 +237,7 @@ LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS, bool 
Deserialize,
     (void)Deserialize; // FIXME?
     P.Visit(this);
   } else {
-    ASTDumper P(OS, Ctx, Ctx.getDiagnostics().getShowColors());
+    ASTDumper P(OS, Ctx, showColorsForStream(Ctx, OS));
     P.setDeserialize(Deserialize);
     P.Visit(this);
   }
@@ -262,7 +268,7 @@ LLVM_DUMP_METHOD void DeclContext::dumpAsDecl(const 
ASTContext *Ctx) const {
     // If an ASTContext is not available, a less capable ASTDumper is
     // constructed for which color diagnostics are, regrettably, disabled.
     ASTDumper P = Ctx ? ASTDumper(llvm::errs(), *Ctx,
-                                  Ctx->getDiagnostics().getShowColors())
+                                  showColorsForStream(*Ctx, llvm::errs()))
                       : ASTDumper(llvm::errs(), /*ShowColors*/ false);
     P.dumpInvalidDeclContext(this);
   }
@@ -279,7 +285,7 @@ LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream 
&OS,
   while (!DC->isTranslationUnit())
     DC = DC->getParent();
   const ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
-  ASTDumper P(OS, Ctx, Ctx.getDiagnostics().getShowColors());
+  ASTDumper P(OS, Ctx, showColorsForStream(Ctx, OS));
   P.setDeserialize(Deserialize);
   P.dumpLookups(this, DumpDecls);
 }
@@ -295,7 +301,7 @@ LLVM_DUMP_METHOD void Stmt::dump() const {
 
 LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS,
                                  const ASTContext &Context) const {
-  ASTDumper P(OS, Context, Context.getDiagnostics().getShowColors());
+  ASTDumper P(OS, Context, showColorsForStream(Context, OS));
   P.Visit(this);
 }
 
@@ -321,7 +327,7 @@ LLVM_DUMP_METHOD void Comment::dump(raw_ostream &OS,
   const auto *FC = dyn_cast<FullComment>(this);
   if (!FC)
     return;
-  ASTDumper Dumper(OS, Context, Context.getDiagnostics().getShowColors());
+  ASTDumper Dumper(OS, Context, showColorsForStream(Context, OS));
   Dumper.Visit(FC, FC);
 }
 
@@ -344,7 +350,7 @@ LLVM_DUMP_METHOD void APValue::dump() const {
 
 LLVM_DUMP_METHOD void APValue::dump(raw_ostream &OS,
                                     const ASTContext &Context) const {
-  ASTDumper Dumper(OS, Context, Context.getDiagnostics().getShowColors());
+  ASTDumper Dumper(OS, Context, showColorsForStream(Context, OS));
   Dumper.Visit(*this, /*Ty=*/Context.getPointerType(Context.CharTy));
 }
 
@@ -358,7 +364,7 @@ LLVM_DUMP_METHOD void ConceptReference::dump() const {
 
 LLVM_DUMP_METHOD void ConceptReference::dump(raw_ostream &OS) const {
   auto &Ctx = getNamedConcept()->getASTContext();
-  ASTDumper P(OS, Ctx, Ctx.getDiagnostics().getShowColors());
+  ASTDumper P(OS, Ctx, showColorsForStream(Ctx, OS));
   P.Visit(this);
 }
 
@@ -377,7 +383,7 @@ LLVM_DUMP_METHOD void TemplateName::dump() const {
 
 LLVM_DUMP_METHOD void TemplateName::dump(llvm::raw_ostream &OS,
                                          const ASTContext &Context) const {
-  ASTDumper Dumper(OS, Context, Context.getDiagnostics().getShowColors());
+  ASTDumper Dumper(OS, Context, showColorsForStream(Context, OS));
   Dumper.Visit(*this);
 }
 
@@ -392,6 +398,6 @@ LLVM_DUMP_METHOD void TemplateArgument::dump() const {
 
 LLVM_DUMP_METHOD void TemplateArgument::dump(llvm::raw_ostream &OS,
                                              const ASTContext &Context) const {
-  ASTDumper Dumper(OS, Context, Context.getDiagnostics().getShowColors());
+  ASTDumper Dumper(OS, Context, showColorsForStream(Context, OS));
   Dumper.Visit(*this);
 }
diff --git a/clang/lib/Basic/Warnings.cpp b/clang/lib/Basic/Warnings.cpp
index 5f48e0ec81554..3d0a948baf690 100644
--- a/clang/lib/Basic/Warnings.cpp
+++ b/clang/lib/Basic/Warnings.cpp
@@ -27,6 +27,7 @@
 #include "clang/Basic/DiagnosticIDs.h"
 #include "clang/Basic/DiagnosticOptions.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Process.h"
 #include "llvm/Support/VirtualFileSystem.h"
 #include <cstring>
 using namespace clang;
@@ -53,7 +54,8 @@ void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
 
   Diags.setElideType(Opts.ElideType);
   Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
-  Diags.setShowColors(Opts.ShowColors);
+  Diags.setShowColors(
+      Opts.showColors(llvm::sys::Process::StandardErrHasColors()));
 
   // Handle -ferror-limit
   if (Opts.ErrorLimit)
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 2267d74ee7d58..08db4ae52f112 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -3307,8 +3307,16 @@ void tools::handleColorDiagnosticsArgs(const Driver &D, 
const ArgList &Args,
           << Value << A->getOption().getName();
   }
 
-  if (D.getDiags().getDiagnosticOptions().ShowColors)
+  switch (D.getDiags().getDiagnosticOptions().getShowColors()) {
+  case Colors_On:
     CmdArgs.push_back("-fcolor-diagnostics");
+    break;
+  case Colors_Off:
+    CmdArgs.push_back("-fno-color-diagnostics");
+    break;
+  case Colors_Auto:
+    break;
+  }
 }
 
 void tools::escapeSpacesAndBackslashes(const char *Arg,
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index 9fc695a74a3c7..4bf0c04796f68 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -2437,35 +2437,30 @@ static bool 
ParseDependencyOutputArgs(DependencyOutputOptions &Opts,
   return Diags.getNumErrors() == NumErrorsBefore;
 }
 
-static bool parseShowColorsArgs(const ArgList &Args, bool DefaultColor) {
+static ShowColorsKind parseShowColorsMode(const ArgList &Args,
+                                          bool DefaultColor) {
   // Color diagnostics default to auto ("on" if terminal supports) in the 
driver
   // but default to off in cc1, needing an explicit OPT_fdiagnostics_color.
   // Support both clang's -f[no-]color-diagnostics and gcc's
   // -f[no-]diagnostics-colors[=never|always|auto].
-  enum {
-    Colors_On,
-    Colors_Off,
-    Colors_Auto
-  } ShowColors = DefaultColor ? Colors_Auto : Colors_Off;
+  ShowColorsKind Mode = DefaultColor ? Colors_Auto : Colors_Off;
   for (auto *A : Args) {
     const Option &O = A->getOption();
     if (O.matches(options::OPT_fcolor_diagnostics)) {
-      ShowColors = Colors_On;
+      Mode = Colors_On;
     } else if (O.matches(options::OPT_fno_color_diagnostics)) {
-      ShowColors = Colors_Off;
+      Mode = Colors_Off;
     } else if (O.matches(options::OPT_fdiagnostics_color_EQ)) {
       StringRef Value(A->getValue());
       if (Value == "always")
-        ShowColors = Colors_On;
+        Mode = Colors_On;
       else if (Value == "never")
-        ShowColors = Colors_Off;
+        Mode = Colors_Off;
       else if (Value == "auto")
-        ShowColors = Colors_Auto;
+        Mode = Colors_Auto;
     }
   }
-  return ShowColors == Colors_On ||
-         (ShowColors == Colors_Auto &&
-          llvm::sys::Process::StandardErrHasColors());
+  return Mode;
 }
 
 static bool checkVerifyPrefixes(const std::vector<std::string> &VerifyPrefixes,
@@ -2546,8 +2541,16 @@ void CompilerInvocationBase::GenerateDiagnosticArgs(
     GenerateArg(Consumer, OPT_diagnostic_serialized_file,
                 Opts.DiagnosticSerializationFile);
 
-  if (Opts.ShowColors)
+  switch (Opts.getShowColors()) {
+  case Colors_On:
     GenerateArg(Consumer, OPT_fcolor_diagnostics);
+    break;
+  case Colors_Off:
+    GenerateArg(Consumer, OPT_fno_color_diagnostics);
+    break;
+  case Colors_Auto:
+    break;
+  }
 
   if (Opts.VerifyDiagnostics &&
       llvm::is_contained(Opts.VerifyPrefixes, "expected"))
@@ -2656,7 +2659,7 @@ bool clang::ParseDiagnosticArgs(DiagnosticOptions &Opts, 
ArgList &Args,
   if (Arg *A =
           Args.getLastArg(OPT_diagnostic_serialized_file, 
OPT__serialize_diags))
     Opts.DiagnosticSerializationFile = A->getValue();
-  Opts.ShowColors = parseShowColorsArgs(Args, DefaultDiagColor);
+  Opts.setShowColors(parseShowColorsMode(Args, DefaultDiagColor));
 
   Opts.VerifyDiagnostics = Args.hasArg(OPT_verify) || 
Args.hasArg(OPT_verify_EQ);
   Opts.VerifyDirectives = Args.hasArg(OPT_verify_directives);
@@ -5074,7 +5077,7 @@ bool CompilerInvocation::CreateFromArgsImpl(
   ParseMigratorArgs(Res.getMigratorOpts(), Args, Diags);
   ParseAnalyzerArgs(Res.getAnalyzerOpts(), Args, Diags);
   ParseDiagnosticArgs(Res.getDiagnosticOpts(), Args, &Diags,
-                      /*DefaultDiagColor=*/false);
+                      /*DefaultDiagColor=*/true);
   ParseFrontendArgs(Res.getFrontendOpts(), Args, Diags, LangOpts.IsHeaderFile);
   // FIXME: We shouldn't have to pass the DashX option around here
   InputKind DashX = Res.getFrontendOpts().DashX;
diff --git a/clang/lib/Frontend/TextDiagnostic.cpp 
b/clang/lib/Frontend/TextDiagnostic.cpp
index 3f30709b0447e..01a4d2f6392d3 100644
--- a/clang/lib/Frontend/TextDiagnostic.cpp
+++ b/clang/lib/Frontend/TextDiagnostic.cpp
@@ -730,15 +730,16 @@ void TextDiagnostic::emitDiagnosticMessage(
   if (Loc.isValid())
     emitDiagnosticLoc(Loc, PLoc, Level, Ranges);
 
-  if (DiagOpts.ShowColors)
+  if (DiagOpts.showColors(OS.has_colors()))
     OS.resetColor();
 
   if (DiagOpts.ShowLevel)
-    printDiagnosticLevel(OS, Level, DiagOpts.ShowColors);
+    printDiagnosticLevel(OS, Level, DiagOpts.showColors(OS.has_colors()));
   printDiagnosticMessage(OS,
                          /*IsSupplemental*/ Level == DiagnosticsEngine::Note,
                          Message, OS.getColumn() - StartOfLocationInfo,
-                         DiagOpts.MessageLength, DiagOpts.ShowColors);
+                         DiagOpts.MessageLength,
+                         DiagOpts.showColors(OS.has_colors()));
   // We use a formatted ostream, which does its own buffering. Flush here
   // so we keep the proper order of output.
   OS.flush();
@@ -872,7 +873,7 @@ void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, 
PresumedLoc PLoc,
   if (!DiagOpts.ShowLocation)
     return;
 
-  if (DiagOpts.ShowColors)
+  if (DiagOpts.showColors(OS.has_colors()))
     OS.changeColor(SavedColor, true);
 
   emitFilename(PLoc.getFilename(), Loc.getManager());
@@ -1429,7 +1430,7 @@ void TextDiagnostic::emitSnippetAndCaret(
   // emit, starting from the first line.
   std::unique_ptr<SmallVector<StyleRange>[]> SourceStyles =
       highlightLines(BufData, Lines.first, Lines.second, PP, LangOpts,
-                     DiagOpts.ShowColors, FID, SM);
+                     DiagOpts.showColors(OS.has_colors()), FID, SM);
 
   SmallVector<LineRange> LineRanges =
       prepareAndFilterRanges(Ranges, SM, Lines, FID, LangOpts);
@@ -1508,22 +1509,22 @@ void TextDiagnostic::emitSnippetAndCaret(
 
     if (!CaretLine.empty()) {
       indentForLineNumbers();
-      if (DiagOpts.ShowColors)
+      if (DiagOpts.showColors(OS.has_colors()))
         OS.changeColor(CaretColor, true);
       OS << CaretLine << '\n';
-      if (DiagOpts.ShowColors)
+      if (DiagOpts.showColors(OS.has_colors()))
         OS.resetColor();
     }
 
     if (!FixItInsertionLine.empty()) {
       indentForLineNumbers();
-      if (DiagOpts.ShowColors)
+      if (DiagOpts.showColors(OS.has_colors()))
         // Print fixit line in color
         OS.changeColor(FixitColor, false);
       if (DiagOpts.ShowSourceRanges)
         OS << ' ';
       OS << FixItInsertionLine << '\n';
-      if (DiagOpts.ShowColors)
+      if (DiagOpts.showColors(OS.has_colors()))
         OS.resetColor();
     }
   }
@@ -1552,7 +1553,7 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine,
         printableTextForNextCharacter(SourceLine, &I, DiagOpts.TabStop);
 
     // Toggle inverted colors on or off for this character.
-    if (DiagOpts.ShowColors) {
+    if (DiagOpts.showColors(OS.has_colors())) {
       if (WasPrintable == PrintReversed) {
         PrintReversed = !PrintReversed;
         if (PrintReversed)
@@ -1583,7 +1584,7 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine,
     OS << Str;
   }
 
-  if (DiagOpts.ShowColors)
+  if (DiagOpts.showColors(OS.has_colors()))
     OS.resetColor();
 
   OS << '\n';
diff --git a/clang/lib/Frontend/TextDiagnosticPrinter.cpp 
b/clang/lib/Frontend/TextDiagnosticPrinter.cpp
index 83fd70e5f99f9..cbc8ae2cb2d24 100644
--- a/clang/lib/Frontend/TextDiagnosticPrinter.cpp
+++ b/clang/lib/Frontend/TextDiagnosticPrinter.cpp
@@ -133,11 +133,12 @@ void 
TextDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
   // diagnostics in a context that lacks language options, a source manager, or
   // other infrastructure necessary when emitting more rich diagnostics.
   if (!Info.getLocation().isValid()) {
-    TextDiagnostic::printDiagnosticLevel(OS, Level, DiagOpts.ShowColors);
+    TextDiagnostic::printDiagnosticLevel(OS, Level,
+                                         DiagOpts.showColors(OS.has_colors()));
     TextDiagnostic::printDiagnosticMessage(
         OS, /*IsSupplemental=*/Level == DiagnosticsEngine::Note,
         DiagMessageStream.str(), OS.tell() - StartOfLocationInfo,
-        DiagOpts.MessageLength, DiagOpts.ShowColors);
+        DiagOpts.MessageLength, DiagOpts.showColors(OS.has_colors()));
     OS.flush();
     return;
   }
diff --git a/clang/unittests/Tooling/ToolingTest.cpp 
b/clang/unittests/Tooling/ToolingTest.cpp
index c3b8ffa00924e..da8dcea872324 100644
--- a/clang/unittests/Tooling/ToolingTest.cpp
+++ b/clang/unittests/Tooling/ToolingTest.cpp
@@ -651,11 +651,12 @@ struct CheckColoredDiagnosticsAction : public 
clang::ASTFrontendAction {
       : ShouldShowColor(ShouldShowColor) {}
   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
                                                  StringRef) override {
-    if (Compiler.getDiagnosticOpts().ShowColors != ShouldShowColor)
+    bool HasColors = Compiler.getDiagnosticOpts().getShowColors() ==
+                     (ShouldShowColor ? Colors_On : Colors_Off);
+    if (!HasColors)
       Compiler.getDiagnostics().Report(
           Compiler.getDiagnostics().getCustomDiagID(
-              DiagnosticsEngine::Fatal,
-              "getDiagnosticOpts().ShowColors != ShouldShowColor"));
+              DiagnosticsEngine::Fatal, "getShowColors() != expected"));
     return std::make_unique<ASTConsumer>();
   }
 
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp 
b/flang/lib/Frontend/CompilerInvocation.cpp
index a3335fc9a250f..10ce8fd191f02 100644
--- a/flang/lib/Frontend/CompilerI...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/202441
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to