Skip to content

Commit f1c184b

Browse files
committed
[clang] Use a formatted_raw_ostream in TextDiagnostic
1 parent f8b004d commit f1c184b

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

clang/include/clang/Frontend/TextDiagnostic.h

Lines changed: 5 additions & 3 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:
@@ -47,7 +49,7 @@ class TextDiagnostic : public DiagnosticRenderer {
4749
unsigned End;
4850
enum llvm::raw_ostream::Colors Color;
4951
StyleRange(unsigned S, unsigned E, enum llvm::raw_ostream::Colors C)
50-
: Start(S), End(E), Color(C){};
52+
: Start(S), End(E), Color(C) {};
5153
};
5254

5355
/// Print the diagonstic level to a raw_ostream.

clang/lib/Frontend/TextDiagnostic.cpp

Lines changed: 21 additions & 9 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,8 +674,11 @@ 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);
679+
// We use a formatted ostream, which does its own buffering. Flush here
680+
// so we keek the proper order of output.
681+
OS.flush();
680682
}
681683

682684
/*static*/ void
@@ -688,11 +690,21 @@ TextDiagnostic::printDiagnosticLevel(raw_ostream &OS,
688690
switch (Level) {
689691
case DiagnosticsEngine::Ignored:
690692
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;
693+
case DiagnosticsEngine::Note:
694+
OS.changeColor(noteColor, true);
695+
break;
696+
case DiagnosticsEngine::Remark:
697+
OS.changeColor(remarkColor, true);
698+
break;
699+
case DiagnosticsEngine::Warning:
700+
OS.changeColor(warningColor, true);
701+
break;
702+
case DiagnosticsEngine::Error:
703+
OS.changeColor(errorColor, true);
704+
break;
705+
case DiagnosticsEngine::Fatal:
706+
OS.changeColor(fatalColor, true);
707+
break;
696708
}
697709
}
698710

@@ -1485,7 +1497,7 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine,
14851497
if (CharStyle != Styles.end()) {
14861498
if (!CurrentColor ||
14871499
(CurrentColor && *CurrentColor != CharStyle->Color)) {
1488-
OS.changeColor(CharStyle->Color, false);
1500+
OS.changeColor(CharStyle->Color);
14891501
CurrentColor = CharStyle->Color;
14901502
}
14911503
} 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)