Skip to content

Commit 4fb735e

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

File tree

5 files changed

+55
-35
lines changed

5 files changed

+55
-35
lines changed

clang/include/clang/Frontend/TextDiagnostic.h

Lines changed: 16 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 formatted_raw_ostream = llvm::formatted_raw_ostream;
24+
2325
/// Class to encapsulate the logic for formatting and printing a textual
2426
/// diagnostic message.
2527
///
@@ -33,35 +35,36 @@ 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 llvm::formatted_raw_ostream::Colors Color;
51+
StyleRange(unsigned S, unsigned E,
52+
enum llvm::formatted_raw_ostream::Colors C)
53+
: Start(S), End(E), Color(C) {};
5154
};
5255

53-
/// Print the diagonstic level to a raw_ostream.
56+
/// Print the diagonstic level to a formatted_raw_ostream.
5457
///
5558
/// This is a static helper that handles colorizing the level and formatting
5659
/// it into an arbitrary output stream. This is used internally by the
5760
/// TextDiagnostic emission code, but it can also be used directly by
5861
/// consumers that don't have a source manager or other state that the full
5962
/// TextDiagnostic logic requires.
60-
static void printDiagnosticLevel(raw_ostream &OS,
63+
static void printDiagnosticLevel(formatted_raw_ostream &OS,
6164
DiagnosticsEngine::Level Level,
6265
bool ShowColors);
6366

64-
/// Pretty-print a diagnostic message to a raw_ostream.
67+
/// Pretty-print a diagnostic message to a formatted_raw_ostream.
6568
///
6669
/// This is a static helper to handle the line wrapping, colorizing, and
6770
/// rendering of a diagnostic message to a particular ostream. It is
@@ -77,9 +80,10 @@ class TextDiagnostic : public DiagnosticRenderer {
7780
/// \param Columns The number of columns to use in line-wrapping, 0 disables
7881
/// all line-wrapping.
7982
/// \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);
83+
static void printDiagnosticMessage(formatted_raw_ostream &OS,
84+
bool IsSupplemental, StringRef Message,
85+
unsigned CurrentColumn, unsigned Columns,
86+
bool ShowColors);
8387

8488
protected:
8589
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/Driver/ToolChains/Clang.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6486,6 +6486,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
64866486
// terminal and, if so, implicitly define -fmessage-length appropriately.
64876487
MessageLength = llvm::sys::Process::StandardErrColumns();
64886488
}
6489+
// llvm::errs() << "Message length: " << MessageLength << '\n';
64896490
if (MessageLength != 0)
64906491
CmdArgs.push_back(
64916492
Args.MakeArgString("-fmessage-length=" + Twine(MessageLength)));

clang/lib/Frontend/TextDiagnostic.cpp

Lines changed: 35 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,9 +602,12 @@ 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) {
607+
// llvm::errs() << "Columns: " << Columns << '\n';
608+
// llvm::errs() << "Column: " << Column << '\n';
608609
const unsigned Length = std::min(Str.find('\n'), Str.size());
610+
// llvm::errs() << "textLength: " << Length << '\n';
609611
bool TextNormal = true;
610612

611613
bool Wrapped = false;
@@ -651,7 +653,8 @@ static bool printWordWrapped(raw_ostream &OS, StringRef Str, unsigned Columns,
651653
return Wrapped;
652654
}
653655

654-
TextDiagnostic::TextDiagnostic(raw_ostream &OS, const LangOptions &LangOpts,
656+
TextDiagnostic::TextDiagnostic(formatted_raw_ostream &OS,
657+
const LangOptions &LangOpts,
655658
DiagnosticOptions &DiagOpts,
656659
const Preprocessor *PP)
657660
: DiagnosticRenderer(LangOpts, DiagOpts), OS(OS), PP(PP) {}
@@ -662,7 +665,7 @@ void TextDiagnostic::emitDiagnosticMessage(
662665
FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level,
663666
StringRef Message, ArrayRef<clang::CharSourceRange> Ranges,
664667
DiagOrStoredDiag D) {
665-
uint64_t StartOfLocationInfo = OS.tell();
668+
uint64_t StartOfLocationInfo = OS.getColumn();
666669

667670
// Emit the location of this particular diagnostic.
668671
if (Loc.isValid())
@@ -675,24 +678,34 @@ void TextDiagnostic::emitDiagnosticMessage(
675678
printDiagnosticLevel(OS, Level, DiagOpts.ShowColors);
676679
printDiagnosticMessage(OS,
677680
/*IsSupplemental*/ Level == DiagnosticsEngine::Note,
678-
Message, OS.tell() - StartOfLocationInfo,
681+
Message, OS.getColumn() - StartOfLocationInfo,
679682
DiagOpts.MessageLength, DiagOpts.ShowColors);
680683
}
681684

682685
/*static*/ void
683-
TextDiagnostic::printDiagnosticLevel(raw_ostream &OS,
686+
TextDiagnostic::printDiagnosticLevel(formatted_raw_ostream &OS,
684687
DiagnosticsEngine::Level Level,
685688
bool ShowColors) {
686689
if (ShowColors) {
687690
// Print diagnostic category in bold and color
688691
switch (Level) {
689692
case DiagnosticsEngine::Ignored:
690693
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;
694+
case DiagnosticsEngine::Note:
695+
OS.changeColor(noteColor, true, false);
696+
break;
697+
case DiagnosticsEngine::Remark:
698+
OS.changeColor(remarkColor, true, false);
699+
break;
700+
case DiagnosticsEngine::Warning:
701+
OS.changeColor(warningColor, true, false);
702+
break;
703+
case DiagnosticsEngine::Error:
704+
OS.changeColor(errorColor, true, false);
705+
break;
706+
case DiagnosticsEngine::Fatal:
707+
OS.changeColor(fatalColor, true, false);
708+
break;
696709
}
697710
}
698711

@@ -711,7 +724,7 @@ TextDiagnostic::printDiagnosticLevel(raw_ostream &OS,
711724
}
712725

713726
/*static*/
714-
void TextDiagnostic::printDiagnosticMessage(raw_ostream &OS,
727+
void TextDiagnostic::printDiagnosticMessage(formatted_raw_ostream &OS,
715728
bool IsSupplemental,
716729
StringRef Message,
717730
unsigned CurrentColumn,
@@ -720,7 +733,7 @@ void TextDiagnostic::printDiagnosticMessage(raw_ostream &OS,
720733
if (ShowColors && !IsSupplemental) {
721734
// Print primary diagnostic messages in bold and without color, to visually
722735
// indicate the transition from continuation notes and other output.
723-
OS.changeColor(savedColor, true);
736+
OS.changeColor(savedColor, true, false);
724737
Bold = true;
725738
}
726739

@@ -798,7 +811,7 @@ void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
798811
return;
799812

800813
if (DiagOpts.ShowColors)
801-
OS.changeColor(savedColor, true);
814+
OS.changeColor(savedColor, true, false);
802815

803816
emitFilename(PLoc.getFilename(), Loc.getManager());
804817
switch (DiagOpts.getFormat()) {
@@ -1423,7 +1436,7 @@ void TextDiagnostic::emitSnippetAndCaret(
14231436
if (!CaretLine.empty()) {
14241437
indentForLineNumbers();
14251438
if (DiagOpts.ShowColors)
1426-
OS.changeColor(caretColor, true);
1439+
OS.changeColor(caretColor, true, false);
14271440
OS << CaretLine << '\n';
14281441
if (DiagOpts.ShowColors)
14291442
OS.resetColor();
@@ -1433,7 +1446,7 @@ void TextDiagnostic::emitSnippetAndCaret(
14331446
indentForLineNumbers();
14341447
if (DiagOpts.ShowColors)
14351448
// Print fixit line in color
1436-
OS.changeColor(fixitColor, false);
1449+
OS.changeColor(fixitColor, false, false);
14371450
if (DiagOpts.ShowSourceRanges)
14381451
OS << ' ';
14391452
OS << FixItInsertionLine << '\n';
@@ -1459,7 +1472,7 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine,
14591472

14601473
// Print the source line one character at a time.
14611474
bool PrintReversed = false;
1462-
std::optional<llvm::raw_ostream::Colors> CurrentColor;
1475+
std::optional<llvm::formatted_raw_ostream::Colors> CurrentColor;
14631476
size_t I = 0;
14641477
while (I < SourceLine.size()) {
14651478
auto [Str, WasPrintable] =
@@ -1485,7 +1498,7 @@ void TextDiagnostic::emitSnippet(StringRef SourceLine,
14851498
if (CharStyle != Styles.end()) {
14861499
if (!CurrentColor ||
14871500
(CurrentColor && *CurrentColor != CharStyle->Color)) {
1488-
OS.changeColor(CharStyle->Color, false);
1501+
OS.changeColor(CharStyle->Color, false, false);
14891502
CurrentColor = CharStyle->Color;
14901503
}
14911504
} else if (CurrentColor) {

clang/lib/Frontend/TextDiagnosticPrinter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "clang/Frontend/TextDiagnostic.h"
1717
#include "clang/Lex/Lexer.h"
1818
#include "llvm/Support/ErrorHandling.h"
19+
#include "llvm/Support/FormattedStream.h"
1920
#include "llvm/Support/raw_ostream.h"
2021
using namespace clang;
2122

0 commit comments

Comments
 (0)