https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/202441
>From 72fa669b742d8fdf64d46528bb31f305dd122089 Mon Sep 17 00:00:00 2001 From: Joseph Huber <[email protected]> Date: Mon, 8 Jun 2026 16:39:36 -0500 Subject: [PATCH] [Clang] Refactor and consolidate color diagnostic handling 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. --- clang-tools-extra/clang-tidy/ClangTidy.cpp | 9 +++-- clang-tools-extra/clangd/Compiler.cpp | 2 +- .../include/clang/Basic/DiagnosticOptions.def | 2 +- clang/include/clang/Basic/DiagnosticOptions.h | 20 ++++++++++ clang/include/clang/Options/Options.td | 2 +- clang/lib/AST/ASTDumper.cpp | 28 ++++++++------ clang/lib/Basic/Warnings.cpp | 4 +- clang/lib/Driver/ToolChains/CommonArgs.cpp | 10 ++++- clang/lib/Frontend/CompilerInvocation.cpp | 37 ++++++++++--------- clang/lib/Frontend/TextDiagnostic.cpp | 23 ++++++------ clang/lib/Frontend/TextDiagnosticPrinter.cpp | 5 ++- clang/unittests/Tooling/ToolingTest.cpp | 7 ++-- flang/lib/Frontend/CompilerInvocation.cpp | 8 ++-- flang/lib/Frontend/TextDiagnosticPrinter.cpp | 8 ++-- flang/test/Driver/color-diagnostics.f90 | 3 +- .../TypeSystem/Clang/TypeSystemClang.cpp | 13 +++++-- 16 files changed, 116 insertions(+), 65 deletions(-) diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp b/clang-tools-extra/clang-tidy/ClangTidy.cpp index 05c8fd02fe86a..aa20abc2e6d94 100644 --- a/clang-tools-extra/clang-tidy/ClangTidy.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp @@ -105,10 +105,13 @@ class ErrorReporter { DiagPrinter(new TextDiagnosticPrinter(llvm::outs(), DiagOpts)), Diags(DiagnosticIDs::create(), DiagOpts, DiagPrinter), SourceMgr(Diags, Files), Context(Context), ApplyFixes(ApplyFixes) { - DiagOpts.ShowColors = Context.getOptions().UseColor.value_or( - llvm::sys::Process::StandardOutHasColors()); + DiagOpts.setShowColors(Context.getOptions().UseColor.value_or( + llvm::sys::Process::StandardOutHasColors()) + ? Colors_On + : 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..498e5331b5ea5 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, FC1Option]>, 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/CompilerInvocation.cpp +++ b/flang/lib/Frontend/CompilerInvocation.cpp @@ -122,7 +122,8 @@ static unsigned getOptimizationLevel(llvm::opt::ArgList &args, bool Fortran::frontend::parseDiagnosticArgs(clang::DiagnosticOptions &opts, llvm::opt::ArgList &args) { - opts.ShowColors = parseShowColorsArgs(args); + opts.setShowColors(parseShowColorsArgs(args) ? clang::Colors_On + : clang::Colors_Off); return true; } @@ -1097,8 +1098,9 @@ static bool parseDiagArgs(CompilerInvocation &res, llvm::opt::ArgList &args, // Default to off for `flang -fc1`. bool showColors{parseShowColorsArgs(args, false)}; - diags.getDiagnosticOptions().ShowColors = showColors; - res.getDiagnosticOpts().ShowColors = showColors; + auto colorsMode = showColors ? clang::Colors_On : clang::Colors_Off; + diags.getDiagnosticOptions().setShowColors(colorsMode); + res.getDiagnosticOpts().setShowColors(colorsMode); res.getFrontendOpts().showColors = showColors; return !diags.hasUncompilableErrorOccurred(); } diff --git a/flang/lib/Frontend/TextDiagnosticPrinter.cpp b/flang/lib/Frontend/TextDiagnosticPrinter.cpp index 911b78a109e2e..33d54d9ad0b9c 100644 --- a/flang/lib/Frontend/TextDiagnosticPrinter.cpp +++ b/flang/lib/Frontend/TextDiagnosticPrinter.cpp @@ -81,7 +81,7 @@ void TextDiagnosticPrinter::printLocForRemarks( llvm::sys::path::make_preferred(absPath); // Used for changing only the bold attribute - if (diagOpts.ShowColors) + if (diagOpts.showColors(os.has_colors())) os.changeColor(llvm::raw_ostream::SAVEDCOLOR, true); // Print path, file name, line and column @@ -112,12 +112,12 @@ void TextDiagnosticPrinter::HandleDiagnostic( llvm::StringRef diagMsg; printLocForRemarks(diagMessageStream, diagMsg); - Fortran::frontend::TextDiagnostic::printDiagnosticLevel(os, level, - diagOpts.ShowColors); + Fortran::frontend::TextDiagnostic::printDiagnosticLevel( + os, level, diagOpts.showColors(os.has_colors())); Fortran::frontend::TextDiagnostic::printDiagnosticMessage( os, /*IsSupplemental=*/level == clang::DiagnosticsEngine::Note, diagMsg, - diagOpts.ShowColors); + diagOpts.showColors(os.has_colors())); os.flush(); } diff --git a/flang/test/Driver/color-diagnostics.f90 b/flang/test/Driver/color-diagnostics.f90 index 7c471e39f923f..0fb4531fb4967 100644 --- a/flang/test/Driver/color-diagnostics.f90 +++ b/flang/test/Driver/color-diagnostics.f90 @@ -9,7 +9,7 @@ ! RUN: not %flang_fc1 %s -fcolor-diagnostics 2>&1 \ ! RUN: | FileCheck %s --check-prefix=CHECK_CD ! RUN: not %flang_fc1 %s -fno-color-diagnostics 2>&1 \ -! RUN: | FileCheck %s --check-prefix=UNSUPPORTED_COLOR_DIAGS +! RUN: | FileCheck %s --check-prefix=CHECK_NCD ! RUN: not %flang %s -fdiagnostics-color 2>&1 \ ! RUN: | FileCheck %s --check-prefix=CHECK_CD @@ -31,7 +31,6 @@ ! CHECK_NCD: Semantic errors in {{.*}}color-diagnostics.f90 -! UNSUPPORTED_COLOR_DIAGS: error: unknown argument: '-fno-color-diagnostics' ! UNSUPPORTED_DIAGS_COLOR: error: unknown argument: '-fdiagnostics-color' ! UNSUPPORTED_NO_DIAGS_COLOR: error: unknown argument: '-fno-diagnostics-color' diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp index 22ba62af26f3a..b216bc2c8bb7b 100644 --- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp +++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp @@ -8521,14 +8521,19 @@ TypeSystemClang::dump(lldb::opaque_compiler_type_t type) const { namespace { struct ScopedASTColor { ScopedASTColor(clang::ASTContext &ast, bool show_colors) - : ast(ast), old_show_colors(ast.getDiagnostics().getShowColors()) { - ast.getDiagnostics().setShowColors(show_colors); + : ast(ast), + old_show_colors( + ast.getDiagnostics().getDiagnosticOptions().getShowColors()) { + ast.getDiagnostics().getDiagnosticOptions().setShowColors( + show_colors ? clang::Colors_On : clang::Colors_Off); } - ~ScopedASTColor() { ast.getDiagnostics().setShowColors(old_show_colors); } + ~ScopedASTColor() { + ast.getDiagnostics().getDiagnosticOptions().setShowColors(old_show_colors); + } clang::ASTContext * - const bool old_show_colors; + const clang::ShowColorsKind old_show_colors; }; } // namespace _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
