Skip to content

Commit 9687154

Browse files
committed
[clang] Use a formatted_raw_ostream in TextDiagnostic
Let's see about the CI.
1 parent 750a583 commit 9687154

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

clang/include/clang/Frontend/TextDiagnostic.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
#define LLVM_CLANG_FRONTEND_TEXTDIAGNOSTIC_H
1717

1818
#include "clang/Frontend/DiagnosticRenderer.h"
19-
#include "llvm/Support/raw_ostream.h"
19+
#include "llvm/Support/FormattedStream.h"
2020

2121
namespace clang {
2222

23+
using llvm::formatted_raw_ostream;
24+
2325
/// Class to encapsulate the logic for formatting and printing a textual
2426
/// diagnostic message.
2527
///
@@ -33,7 +35,7 @@ namespace clang {
3335
/// DiagnosticClient is implemented through this class as is diagnostic
3436
/// printing coming out of libclang.
3537
class TextDiagnostic : public DiagnosticRenderer {
36-
raw_ostream &OS;
38+
formatted_raw_ostream OS;
3739
const Preprocessor *PP;
3840

3941
public:
@@ -45,12 +47,12 @@ class TextDiagnostic : public DiagnosticRenderer {
4547
struct StyleRange {
4648
unsigned Start;
4749
unsigned End;
48-
enum llvm::raw_ostream::Colors Color;
49-
StyleRange(unsigned S, unsigned E, enum llvm::raw_ostream::Colors C)
50-
: Start(S), End(E), Color(C){};
50+
enum raw_ostream::Colors Color;
51+
StyleRange(unsigned S, unsigned E, enum raw_ostream::Colors C)
52+
: Start(S), End(E), Color(C) {};
5153
};
5254

53-
/// Print the diagonstic level to a raw_ostream.
55+
/// Print the diagonstic level to a formatted_raw_ostream.
5456
///
5557
/// This is a static helper that handles colorizing the level and formatting
5658
/// it into an arbitrary output stream. This is used internally by the
@@ -61,7 +63,7 @@ class TextDiagnostic : public DiagnosticRenderer {
6163
DiagnosticsEngine::Level Level,
6264
bool ShowColors);
6365

64-
/// Pretty-print a diagnostic message to a raw_ostream.
66+
/// Pretty-print a diagnostic message to a formatted_raw_ostream.
6567
///
6668
/// This is a static helper to handle the line wrapping, colorizing, and
6769
/// rendering of a diagnostic message to a particular ostream. It is

clang/lib/Frontend/TextDiagnostic.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "llvm/Support/ConvertUTF.h"
1818
#include "llvm/Support/ErrorHandling.h"
1919
#include "llvm/Support/Locale.h"
20-
#include "llvm/Support/raw_ostream.h"
2120
#include <algorithm>
2221
#include <optional>
2322

@@ -662,7 +661,7 @@ void TextDiagnostic::emitDiagnosticMessage(
662661
FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level,
663662
StringRef Message, ArrayRef<clang::CharSourceRange> Ranges,
664663
DiagOrStoredDiag D) {
665-
uint64_t StartOfLocationInfo = OS.tell();
664+
uint64_t StartOfLocationInfo = OS.getColumn();
666665

667666
// Emit the location of this particular diagnostic.
668667
if (Loc.isValid())
@@ -675,7 +674,7 @@ void TextDiagnostic::emitDiagnosticMessage(
675674
printDiagnosticLevel(OS, Level, DiagOpts.ShowColors);
676675
printDiagnosticMessage(OS,
677676
/*IsSupplemental*/ Level == DiagnosticsEngine::Note,
678-
Message, OS.tell() - StartOfLocationInfo,
677+
Message, OS.getColumn() - StartOfLocationInfo,
679678
DiagOpts.MessageLength, DiagOpts.ShowColors);
680679
}
681680

@@ -688,11 +687,21 @@ TextDiagnostic::printDiagnosticLevel(raw_ostream &OS,
688687
switch (Level) {
689688
case DiagnosticsEngine::Ignored:
690689
llvm_unreachable("Invalid diagnostic type");
691-
case DiagnosticsEngine::Note: OS.changeColor(noteColor, true); break;
692-
case DiagnosticsEngine::Remark: OS.changeColor(remarkColor, true); break;
693-
case DiagnosticsEngine::Warning: OS.changeColor(warningColor, true); break;
694-
case DiagnosticsEngine::Error: OS.changeColor(errorColor, true); break;
695-
case DiagnosticsEngine::Fatal: OS.changeColor(fatalColor, true); break;
690+
case DiagnosticsEngine::Note:
691+
OS.changeColor(noteColor, true);
692+
break;
693+
case DiagnosticsEngine::Remark:
694+
OS.changeColor(remarkColor, true);
695+
break;
696+
case DiagnosticsEngine::Warning:
697+
OS.changeColor(warningColor, true);
698+
break;
699+
case DiagnosticsEngine::Error:
700+
OS.changeColor(errorColor, true);
701+
break;
702+
case DiagnosticsEngine::Fatal:
703+
OS.changeColor(fatalColor, true);
704+
break;
696705
}
697706
}
698707

@@ -1459,7 +1468,7 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine,
14591468

14601469
// Print the source line one character at a time.
14611470
bool PrintReversed = false;
1462-
std::optional<llvm::raw_ostream::Colors> CurrentColor;
1471+
std::optional<raw_ostream::Colors> CurrentColor;
14631472
size_t I = 0;
14641473
while (I < SourceLine.size()) {
14651474
auto [Str, WasPrintable] =
@@ -1485,7 +1494,7 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine,
14851494
if (CharStyle != Styles.end()) {
14861495
if (!CurrentColor ||
14871496
(CurrentColor && *CurrentColor != CharStyle->Color)) {
1488-
OS.changeColor(CharStyle->Color, false);
1497+
OS.changeColor(CharStyle->Color);
14891498
CurrentColor = CharStyle->Color;
14901499
}
14911500
} else if (CurrentColor) {

llvm/include/llvm/Support/FormattedStream.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ class LLVM_ABI formatted_raw_ostream : public raw_ostream {
180180
return *this;
181181
}
182182

183-
raw_ostream &changeColor(enum Colors Color, bool Bold, bool BG) override {
183+
raw_ostream &changeColor(enum Colors Color, bool Bold = false,
184+
bool BG = false) override {
184185
if (colors_enabled()) {
185186
DisableScanScope S(this);
186187
raw_ostream::changeColor(Color, Bold, BG);

0 commit comments

Comments
 (0)