Skip to content

Commit 0926265

Browse files
authored
[clang] Use a formatted_raw_ostream in TextDiagnostic (#164935)
So we can use `getColumn()` to get the column without the bytes for color codes. Fixes #164933
1 parent 028bfa2 commit 0926265

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
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: 6 additions & 4 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 keep the proper order of output.
681+
OS.flush();
680682
}
681683

682684
/*static*/ void
@@ -1485,7 +1487,7 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine,
14851487
if (CharStyle != Styles.end()) {
14861488
if (!CurrentColor ||
14871489
(CurrentColor && *CurrentColor != CharStyle->Color)) {
1488-
OS.changeColor(CharStyle->Color, false);
1490+
OS.changeColor(CharStyle->Color);
14891491
CurrentColor = CharStyle->Color;
14901492
}
14911493
} else if (CurrentColor) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: not %clang_cc1 %s -fmessage-length=50 -fcolor-diagnostics -fno-show-source-location -o - 2>&1 | FileCheck %s
2+
3+
struct F {
4+
float a : 10;
5+
};
6+
// CHECK: bit-field 'a' has non-integral type 'float'

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)