Skip to content

Commit 40b30fc

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

File tree

4 files changed

+51
-36
lines changed

4 files changed

+51
-36
lines changed

clang/include/clang/Frontend/TextDiagnostic.h

Lines changed: 15 additions & 12 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,35 +35,35 @@ 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:
40-
TextDiagnostic(raw_ostream &OS, const LangOptions &LangOpts,
42+
TextDiagnostic(formatted_raw_ostream &OS, const LangOptions &LangOpts,
4143
DiagnosticOptions &DiagOpts, const Preprocessor *PP = nullptr);
4244

4345
~TextDiagnostic() override;
4446

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
5759
/// TextDiagnostic emission code, but it can also be used directly by
5860
/// consumers that don't have a source manager or other state that the full
5961
/// TextDiagnostic logic requires.
60-
static void printDiagnosticLevel(raw_ostream &OS,
62+
static void printDiagnosticLevel(formatted_raw_ostream &OS,
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
@@ -77,9 +79,10 @@ class TextDiagnostic : public DiagnosticRenderer {
7779
/// \param Columns The number of columns to use in line-wrapping, 0 disables
7880
/// all line-wrapping.
7981
/// \param ShowColors Enable colorizing of the message.
80-
static void printDiagnosticMessage(raw_ostream &OS, bool IsSupplemental,
81-
StringRef Message, unsigned CurrentColumn,
82-
unsigned Columns, bool ShowColors);
82+
static void printDiagnosticMessage(formatted_raw_ostream &OS,
83+
bool IsSupplemental, StringRef Message,
84+
unsigned CurrentColumn, unsigned Columns,
85+
bool ShowColors);
8386

8487
protected:
8588
void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,

clang/include/clang/Frontend/TextDiagnosticPrinter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "clang/Basic/Diagnostic.h"
1818
#include "clang/Basic/LLVM.h"
1919
#include "llvm/ADT/IntrusiveRefCntPtr.h"
20+
#include "llvm/Support/FormattedStream.h"
2021
#include <memory>
2122

2223
namespace clang {
@@ -25,7 +26,7 @@ class LangOptions;
2526
class TextDiagnostic;
2627

2728
class TextDiagnosticPrinter : public DiagnosticConsumer {
28-
raw_ostream &OS;
29+
llvm::formatted_raw_ostream OS;
2930
DiagnosticOptions &DiagOpts;
3031

3132
/// Handle to the currently active text diagnostic emitter.

clang/lib/Frontend/TextDiagnostic.cpp

Lines changed: 32 additions & 22 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

@@ -49,7 +48,7 @@ static constexpr raw_ostream::Colors LiteralColor = raw_ostream::GREEN;
4948
static constexpr raw_ostream::Colors KeywordColor = raw_ostream::BLUE;
5049

5150
/// Add highlights to differences in template strings.
52-
static void applyTemplateHighlighting(raw_ostream &OS, StringRef Str,
51+
static void applyTemplateHighlighting(formatted_raw_ostream &OS, StringRef Str,
5352
bool &Normal, bool Bold) {
5453
while (true) {
5554
size_t Pos = Str.find(ToggleHighlight);
@@ -59,11 +58,11 @@ static void applyTemplateHighlighting(raw_ostream &OS, StringRef Str,
5958

6059
Str = Str.substr(Pos + 1);
6160
if (Normal)
62-
OS.changeColor(templateColor, true);
61+
OS.changeColor(templateColor, true, false);
6362
else {
6463
OS.resetColor();
6564
if (Bold)
66-
OS.changeColor(savedColor, true);
65+
OS.changeColor(savedColor, true, false);
6766
}
6867
Normal = !Normal;
6968
}
@@ -603,8 +602,8 @@ static unsigned findEndOfWord(unsigned Start, StringRef Str,
603602
/// \param Bold if the current text should be bold
604603
/// \returns true if word-wrapping was required, or false if the
605604
/// string fit on the first line.
606-
static bool printWordWrapped(raw_ostream &OS, StringRef Str, unsigned Columns,
607-
unsigned Column, bool Bold) {
605+
static bool printWordWrapped(formatted_raw_ostream &OS, StringRef Str,
606+
unsigned Columns, unsigned Column, bool Bold) {
608607
const unsigned Length = std::min(Str.find('\n'), Str.size());
609608
bool TextNormal = true;
610609

@@ -651,7 +650,8 @@ static bool printWordWrapped(raw_ostream &OS, StringRef Str, unsigned Columns,
651650
return Wrapped;
652651
}
653652

654-
TextDiagnostic::TextDiagnostic(raw_ostream &OS, const LangOptions &LangOpts,
653+
TextDiagnostic::TextDiagnostic(formatted_raw_ostream &OS,
654+
const LangOptions &LangOpts,
655655
DiagnosticOptions &DiagOpts,
656656
const Preprocessor *PP)
657657
: DiagnosticRenderer(LangOpts, DiagOpts), OS(OS), PP(PP) {}
@@ -662,7 +662,7 @@ void TextDiagnostic::emitDiagnosticMessage(
662662
FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level,
663663
StringRef Message, ArrayRef<clang::CharSourceRange> Ranges,
664664
DiagOrStoredDiag D) {
665-
uint64_t StartOfLocationInfo = OS.tell();
665+
uint64_t StartOfLocationInfo = OS.getColumn();
666666

667667
// Emit the location of this particular diagnostic.
668668
if (Loc.isValid())
@@ -675,24 +675,34 @@ void TextDiagnostic::emitDiagnosticMessage(
675675
printDiagnosticLevel(OS, Level, DiagOpts.ShowColors);
676676
printDiagnosticMessage(OS,
677677
/*IsSupplemental*/ Level == DiagnosticsEngine::Note,
678-
Message, OS.tell() - StartOfLocationInfo,
678+
Message, OS.getColumn() - StartOfLocationInfo,
679679
DiagOpts.MessageLength, DiagOpts.ShowColors);
680680
}
681681

682682
/*static*/ void
683-
TextDiagnostic::printDiagnosticLevel(raw_ostream &OS,
683+
TextDiagnostic::printDiagnosticLevel(formatted_raw_ostream &OS,
684684
DiagnosticsEngine::Level Level,
685685
bool ShowColors) {
686686
if (ShowColors) {
687687
// Print diagnostic category in bold and color
688688
switch (Level) {
689689
case DiagnosticsEngine::Ignored:
690690
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;
691+
case DiagnosticsEngine::Note:
692+
OS.changeColor(noteColor, true, false);
693+
break;
694+
case DiagnosticsEngine::Remark:
695+
OS.changeColor(remarkColor, true, false);
696+
break;
697+
case DiagnosticsEngine::Warning:
698+
OS.changeColor(warningColor, true, false);
699+
break;
700+
case DiagnosticsEngine::Error:
701+
OS.changeColor(errorColor, true, false);
702+
break;
703+
case DiagnosticsEngine::Fatal:
704+
OS.changeColor(fatalColor, true, false);
705+
break;
696706
}
697707
}
698708

@@ -711,7 +721,7 @@ TextDiagnostic::printDiagnosticLevel(raw_ostream &OS,
711721
}
712722

713723
/*static*/
714-
void TextDiagnostic::printDiagnosticMessage(raw_ostream &OS,
724+
void TextDiagnostic::printDiagnosticMessage(formatted_raw_ostream &OS,
715725
bool IsSupplemental,
716726
StringRef Message,
717727
unsigned CurrentColumn,
@@ -720,7 +730,7 @@ void TextDiagnostic::printDiagnosticMessage(raw_ostream &OS,
720730
if (ShowColors && !IsSupplemental) {
721731
// Print primary diagnostic messages in bold and without color, to visually
722732
// indicate the transition from continuation notes and other output.
723-
OS.changeColor(savedColor, true);
733+
OS.changeColor(savedColor, true, false);
724734
Bold = true;
725735
}
726736

@@ -798,7 +808,7 @@ void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
798808
return;
799809

800810
if (DiagOpts.ShowColors)
801-
OS.changeColor(savedColor, true);
811+
OS.changeColor(savedColor, true, false);
802812

803813
emitFilename(PLoc.getFilename(), Loc.getManager());
804814
switch (DiagOpts.getFormat()) {
@@ -1423,7 +1433,7 @@ void TextDiagnostic::emitSnippetAndCaret(
14231433
if (!CaretLine.empty()) {
14241434
indentForLineNumbers();
14251435
if (DiagOpts.ShowColors)
1426-
OS.changeColor(caretColor, true);
1436+
OS.changeColor(caretColor, true, false);
14271437
OS << CaretLine << '\n';
14281438
if (DiagOpts.ShowColors)
14291439
OS.resetColor();
@@ -1433,7 +1443,7 @@ void TextDiagnostic::emitSnippetAndCaret(
14331443
indentForLineNumbers();
14341444
if (DiagOpts.ShowColors)
14351445
// Print fixit line in color
1436-
OS.changeColor(fixitColor, false);
1446+
OS.changeColor(fixitColor, false, false);
14371447
if (DiagOpts.ShowSourceRanges)
14381448
OS << ' ';
14391449
OS << FixItInsertionLine << '\n';
@@ -1459,7 +1469,7 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine,
14591469

14601470
// Print the source line one character at a time.
14611471
bool PrintReversed = false;
1462-
std::optional<llvm::raw_ostream::Colors> CurrentColor;
1472+
std::optional<raw_ostream::Colors> CurrentColor;
14631473
size_t I = 0;
14641474
while (I < SourceLine.size()) {
14651475
auto [Str, WasPrintable] =
@@ -1485,7 +1495,7 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine,
14851495
if (CharStyle != Styles.end()) {
14861496
if (!CurrentColor ||
14871497
(CurrentColor && *CurrentColor != CharStyle->Color)) {
1488-
OS.changeColor(CharStyle->Color, false);
1498+
OS.changeColor(CharStyle->Color, false, false);
14891499
CurrentColor = CharStyle->Color;
14901500
}
14911501
} 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)