https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/164935
>From 42c6d0829ef7c486126883e3db50af2608b165bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]> Date: Fri, 24 Oct 2025 08:32:28 +0200 Subject: [PATCH] [clang] Use a formatted_raw_ostream in TextDiagnostic Let's see about the CI. --- clang/include/clang/Frontend/TextDiagnostic.h | 27 ++++++------ .../clang/Frontend/TextDiagnosticPrinter.h | 3 +- clang/lib/Frontend/TextDiagnostic.cpp | 42 ++++++++++++------- llvm/include/llvm/Support/FormattedStream.h | 3 +- 4 files changed, 45 insertions(+), 30 deletions(-) diff --git a/clang/include/clang/Frontend/TextDiagnostic.h b/clang/include/clang/Frontend/TextDiagnostic.h index e2e88d4d648a2..3e96c05911d67 100644 --- a/clang/include/clang/Frontend/TextDiagnostic.h +++ b/clang/include/clang/Frontend/TextDiagnostic.h @@ -16,10 +16,12 @@ #define LLVM_CLANG_FRONTEND_TEXTDIAGNOSTIC_H #include "clang/Frontend/DiagnosticRenderer.h" -#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/FormattedStream.h" namespace clang { +using llvm::formatted_raw_ostream; + /// Class to encapsulate the logic for formatting and printing a textual /// diagnostic message. /// @@ -33,11 +35,11 @@ namespace clang { /// DiagnosticClient is implemented through this class as is diagnostic /// printing coming out of libclang. class TextDiagnostic : public DiagnosticRenderer { - raw_ostream &OS; + formatted_raw_ostream &OS; const Preprocessor *PP; public: - TextDiagnostic(raw_ostream &OS, const LangOptions &LangOpts, + TextDiagnostic(formatted_raw_ostream &OS, const LangOptions &LangOpts, DiagnosticOptions &DiagOpts, const Preprocessor *PP = nullptr); ~TextDiagnostic() override; @@ -45,23 +47,23 @@ class TextDiagnostic : public DiagnosticRenderer { struct StyleRange { unsigned Start; unsigned End; - enum llvm::raw_ostream::Colors Color; - StyleRange(unsigned S, unsigned E, enum llvm::raw_ostream::Colors C) - : Start(S), End(E), Color(C){}; + enum raw_ostream::Colors Color; + StyleRange(unsigned S, unsigned E, enum raw_ostream::Colors C) + : Start(S), End(E), Color(C) {}; }; - /// Print the diagonstic level to a raw_ostream. + /// Print the diagonstic level to a formatted_raw_ostream. /// /// This is a static helper that handles colorizing the level and formatting /// it into an arbitrary output stream. This is used internally by the /// TextDiagnostic emission code, but it can also be used directly by /// consumers that don't have a source manager or other state that the full /// TextDiagnostic logic requires. - static void printDiagnosticLevel(raw_ostream &OS, + static void printDiagnosticLevel(formatted_raw_ostream &OS, DiagnosticsEngine::Level Level, bool ShowColors); - /// Pretty-print a diagnostic message to a raw_ostream. + /// Pretty-print a diagnostic message to a formatted_raw_ostream. /// /// This is a static helper to handle the line wrapping, colorizing, and /// rendering of a diagnostic message to a particular ostream. It is @@ -77,9 +79,10 @@ class TextDiagnostic : public DiagnosticRenderer { /// \param Columns The number of columns to use in line-wrapping, 0 disables /// all line-wrapping. /// \param ShowColors Enable colorizing of the message. - static void printDiagnosticMessage(raw_ostream &OS, bool IsSupplemental, - StringRef Message, unsigned CurrentColumn, - unsigned Columns, bool ShowColors); + static void printDiagnosticMessage(formatted_raw_ostream &OS, + bool IsSupplemental, StringRef Message, + unsigned CurrentColumn, unsigned Columns, + bool ShowColors); protected: void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc, diff --git a/clang/include/clang/Frontend/TextDiagnosticPrinter.h b/clang/include/clang/Frontend/TextDiagnosticPrinter.h index dd1ca6b2248b7..d7d892f2b80e7 100644 --- a/clang/include/clang/Frontend/TextDiagnosticPrinter.h +++ b/clang/include/clang/Frontend/TextDiagnosticPrinter.h @@ -17,6 +17,7 @@ #include "clang/Basic/Diagnostic.h" #include "clang/Basic/LLVM.h" #include "llvm/ADT/IntrusiveRefCntPtr.h" +#include "llvm/Support/FormattedStream.h" #include <memory> namespace clang { @@ -25,7 +26,7 @@ class LangOptions; class TextDiagnostic; class TextDiagnosticPrinter : public DiagnosticConsumer { - raw_ostream &OS; + llvm::formatted_raw_ostream OS; DiagnosticOptions &DiagOpts; /// Handle to the currently active text diagnostic emitter. diff --git a/clang/lib/Frontend/TextDiagnostic.cpp b/clang/lib/Frontend/TextDiagnostic.cpp index 58885712fbdcc..589c76776a6e7 100644 --- a/clang/lib/Frontend/TextDiagnostic.cpp +++ b/clang/lib/Frontend/TextDiagnostic.cpp @@ -17,7 +17,6 @@ #include "llvm/Support/ConvertUTF.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/Locale.h" -#include "llvm/Support/raw_ostream.h" #include <algorithm> #include <optional> @@ -49,7 +48,7 @@ static constexpr raw_ostream::Colors LiteralColor = raw_ostream::GREEN; static constexpr raw_ostream::Colors KeywordColor = raw_ostream::BLUE; /// Add highlights to differences in template strings. -static void applyTemplateHighlighting(raw_ostream &OS, StringRef Str, +static void applyTemplateHighlighting(formatted_raw_ostream &OS, StringRef Str, bool &Normal, bool Bold) { while (true) { size_t Pos = Str.find(ToggleHighlight); @@ -603,8 +602,8 @@ static unsigned findEndOfWord(unsigned Start, StringRef Str, /// \param Bold if the current text should be bold /// \returns true if word-wrapping was required, or false if the /// string fit on the first line. -static bool printWordWrapped(raw_ostream &OS, StringRef Str, unsigned Columns, - unsigned Column, bool Bold) { +static bool printWordWrapped(formatted_raw_ostream &OS, StringRef Str, + unsigned Columns, unsigned Column, bool Bold) { const unsigned Length = std::min(Str.find('\n'), Str.size()); bool TextNormal = true; @@ -651,7 +650,8 @@ static bool printWordWrapped(raw_ostream &OS, StringRef Str, unsigned Columns, return Wrapped; } -TextDiagnostic::TextDiagnostic(raw_ostream &OS, const LangOptions &LangOpts, +TextDiagnostic::TextDiagnostic(formatted_raw_ostream &OS, + const LangOptions &LangOpts, DiagnosticOptions &DiagOpts, const Preprocessor *PP) : DiagnosticRenderer(LangOpts, DiagOpts), OS(OS), PP(PP) {} @@ -662,7 +662,7 @@ void TextDiagnostic::emitDiagnosticMessage( FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level, StringRef Message, ArrayRef<clang::CharSourceRange> Ranges, DiagOrStoredDiag D) { - uint64_t StartOfLocationInfo = OS.tell(); + uint64_t StartOfLocationInfo = OS.getColumn(); // Emit the location of this particular diagnostic. if (Loc.isValid()) @@ -675,12 +675,12 @@ void TextDiagnostic::emitDiagnosticMessage( printDiagnosticLevel(OS, Level, DiagOpts.ShowColors); printDiagnosticMessage(OS, /*IsSupplemental*/ Level == DiagnosticsEngine::Note, - Message, OS.tell() - StartOfLocationInfo, + Message, OS.getColumn() - StartOfLocationInfo, DiagOpts.MessageLength, DiagOpts.ShowColors); } /*static*/ void -TextDiagnostic::printDiagnosticLevel(raw_ostream &OS, +TextDiagnostic::printDiagnosticLevel(formatted_raw_ostream &OS, DiagnosticsEngine::Level Level, bool ShowColors) { if (ShowColors) { @@ -688,11 +688,21 @@ TextDiagnostic::printDiagnosticLevel(raw_ostream &OS, switch (Level) { case DiagnosticsEngine::Ignored: llvm_unreachable("Invalid diagnostic type"); - case DiagnosticsEngine::Note: OS.changeColor(noteColor, true); break; - case DiagnosticsEngine::Remark: OS.changeColor(remarkColor, true); break; - case DiagnosticsEngine::Warning: OS.changeColor(warningColor, true); break; - case DiagnosticsEngine::Error: OS.changeColor(errorColor, true); break; - case DiagnosticsEngine::Fatal: OS.changeColor(fatalColor, true); break; + case DiagnosticsEngine::Note: + OS.changeColor(noteColor, true); + break; + case DiagnosticsEngine::Remark: + OS.changeColor(remarkColor, true); + break; + case DiagnosticsEngine::Warning: + OS.changeColor(warningColor, true); + break; + case DiagnosticsEngine::Error: + OS.changeColor(errorColor, true); + break; + case DiagnosticsEngine::Fatal: + OS.changeColor(fatalColor, true); + break; } } @@ -711,7 +721,7 @@ TextDiagnostic::printDiagnosticLevel(raw_ostream &OS, } /*static*/ -void TextDiagnostic::printDiagnosticMessage(raw_ostream &OS, +void TextDiagnostic::printDiagnosticMessage(formatted_raw_ostream &OS, bool IsSupplemental, StringRef Message, unsigned CurrentColumn, @@ -1459,7 +1469,7 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine, // Print the source line one character at a time. bool PrintReversed = false; - std::optional<llvm::raw_ostream::Colors> CurrentColor; + std::optional<raw_ostream::Colors> CurrentColor; size_t I = 0; while (I < SourceLine.size()) { auto [Str, WasPrintable] = @@ -1485,7 +1495,7 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine, if (CharStyle != Styles.end()) { if (!CurrentColor || (CurrentColor && *CurrentColor != CharStyle->Color)) { - OS.changeColor(CharStyle->Color, false); + OS.changeColor(CharStyle->Color); CurrentColor = CharStyle->Color; } } else if (CurrentColor) { diff --git a/llvm/include/llvm/Support/FormattedStream.h b/llvm/include/llvm/Support/FormattedStream.h index 011a6aea238e3..402cd3e3235dc 100644 --- a/llvm/include/llvm/Support/FormattedStream.h +++ b/llvm/include/llvm/Support/FormattedStream.h @@ -180,7 +180,8 @@ class LLVM_ABI formatted_raw_ostream : public raw_ostream { return *this; } - raw_ostream &changeColor(enum Colors Color, bool Bold, bool BG) override { + raw_ostream &changeColor(enum Colors Color, bool Bold = false, + bool BG = false) override { if (colors_enabled()) { DisableScanScope S(this); raw_ostream::changeColor(Color, Bold, BG); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
