[clang] [llvm] [clang] Use a formatted_raw_ostream in TextDiagnostic (PR #164935)
Timm Baeder via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 24 04:28:07 PDT 2025
Timm =?utf-8?q?Bäder?= <tbaeder at redhat.com>,
Timm =?utf-8?q?Bäder?= <tbaeder at redhat.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/164935 at github.com>
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/164935
>From 9687154ddd5fb184ccf4c8ae2d85dd2f47d40fb2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 24 Oct 2025 08:32:28 +0200
Subject: [PATCH 1/3] [clang] Use a formatted_raw_ostream in TextDiagnostic
Let's see about the CI.
---
clang/include/clang/Frontend/TextDiagnostic.h | 16 +++++-----
clang/lib/Frontend/TextDiagnostic.cpp | 29 ++++++++++++-------
llvm/include/llvm/Support/FormattedStream.h | 3 +-
3 files changed, 30 insertions(+), 18 deletions(-)
diff --git a/clang/include/clang/Frontend/TextDiagnostic.h b/clang/include/clang/Frontend/TextDiagnostic.h
index e2e88d4d648a2..ccae81a90ac22 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,7 +35,7 @@ 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:
@@ -45,12 +47,12 @@ 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
@@ -61,7 +63,7 @@ class TextDiagnostic : public DiagnosticRenderer {
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
diff --git a/clang/lib/Frontend/TextDiagnostic.cpp b/clang/lib/Frontend/TextDiagnostic.cpp
index 58885712fbdcc..f458c23a39503 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>
@@ -662,7 +661,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,7 +674,7 @@ 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);
}
@@ -688,11 +687,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;
}
}
@@ -1459,7 +1468,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 +1494,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);
>From c0083d401a677e97cd3c2b39f68e73fc4bf398a5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 24 Oct 2025 13:26:18 +0200
Subject: [PATCH 2/3] Revert comment changes
---
clang/include/clang/Frontend/TextDiagnostic.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/Frontend/TextDiagnostic.h b/clang/include/clang/Frontend/TextDiagnostic.h
index ccae81a90ac22..1760a004ad86f 100644
--- a/clang/include/clang/Frontend/TextDiagnostic.h
+++ b/clang/include/clang/Frontend/TextDiagnostic.h
@@ -52,7 +52,7 @@ class TextDiagnostic : public DiagnosticRenderer {
: Start(S), End(E), Color(C) {};
};
- /// Print the diagonstic level to a formatted_raw_ostream.
+ /// Print the diagonstic level to a 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
@@ -63,7 +63,7 @@ class TextDiagnostic : public DiagnosticRenderer {
DiagnosticsEngine::Level Level,
bool ShowColors);
- /// Pretty-print a diagnostic message to a formatted_raw_ostream.
+ /// Pretty-print a diagnostic message to a 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
>From 73787b5bc667d3aa39caf1462bfb9f5944fba984 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 24 Oct 2025 13:27:47 +0200
Subject: [PATCH 3/3] Revert some unnecessary changes
---
clang/include/clang/Frontend/TextDiagnostic.h | 4 ++--
clang/lib/Frontend/TextDiagnostic.cpp | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/include/clang/Frontend/TextDiagnostic.h b/clang/include/clang/Frontend/TextDiagnostic.h
index 1760a004ad86f..10028186d27f3 100644
--- a/clang/include/clang/Frontend/TextDiagnostic.h
+++ b/clang/include/clang/Frontend/TextDiagnostic.h
@@ -47,8 +47,8 @@ class TextDiagnostic : public DiagnosticRenderer {
struct StyleRange {
unsigned Start;
unsigned End;
- enum raw_ostream::Colors Color;
- StyleRange(unsigned S, unsigned E, enum raw_ostream::Colors C)
+ enum llvm::raw_ostream::Colors Color;
+ StyleRange(unsigned S, unsigned E, enum llvm::raw_ostream::Colors C)
: Start(S), End(E), Color(C) {};
};
diff --git a/clang/lib/Frontend/TextDiagnostic.cpp b/clang/lib/Frontend/TextDiagnostic.cpp
index f458c23a39503..e44219c4d657a 100644
--- a/clang/lib/Frontend/TextDiagnostic.cpp
+++ b/clang/lib/Frontend/TextDiagnostic.cpp
@@ -1468,7 +1468,7 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine,
// Print the source line one character at a time.
bool PrintReversed = false;
- std::optional<raw_ostream::Colors> CurrentColor;
+ std::optional<llvm::raw_ostream::Colors> CurrentColor;
size_t I = 0;
while (I < SourceLine.size()) {
auto [Str, WasPrintable] =
More information about the llvm-commits
mailing list