Revert r367649: Improve raw_ostream so that you can "write" colors using operator<<

This reverts commit r367649 in an attempt to unbreak Windows bots.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@367658 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rui Ueyama 2019-08-02 07:22:34 +00:00
parent 12fe6a4285
commit ed560ae76a
12 changed files with 39 additions and 89 deletions

View File

@ -121,8 +121,8 @@ public:
return *this; return *this;
} }
raw_ostream &changeColor(Color C, bool Bold, bool BG) override { raw_ostream &changeColor(enum Colors Color, bool Bold, bool BG) override {
TheStream->changeColor(C, Bold, BG); TheStream->changeColor(Color, Bold, BG);
return *this; return *this;
} }

View File

@ -53,7 +53,8 @@ public:
/// @param BG If true, change the background, default: change foreground /// @param BG If true, change the background, default: change foreground
/// @param DisableColors Whether to ignore color changes regardless of -color /// @param DisableColors Whether to ignore color changes regardless of -color
/// and support in OS /// and support in OS
WithColor(raw_ostream &OS, raw_ostream::Color Color = raw_ostream::SAVEDCOLOR, WithColor(raw_ostream &OS,
raw_ostream::Colors Color = raw_ostream::SAVEDCOLOR,
bool Bold = false, bool BG = false, bool DisableColors = false) bool Bold = false, bool BG = false, bool DisableColors = false)
: OS(OS), DisableColors(DisableColors) { : OS(OS), DisableColors(DisableColors) {
changeColor(Color, Bold, BG); changeColor(Color, Bold, BG);
@ -101,7 +102,7 @@ public:
/// change only the bold attribute, and keep colors untouched /// change only the bold attribute, and keep colors untouched
/// @param Bold Bold/brighter text, default false /// @param Bold Bold/brighter text, default false
/// @param BG If true, change the background, default: change foreground /// @param BG If true, change the background, default: change foreground
WithColor &changeColor(raw_ostream::Color Color, bool Bold = false, WithColor &changeColor(raw_ostream::Colors Color, bool Bold = false,
bool BG = false); bool BG = false);
/// Reset the colors to terminal defaults. Call this when you are done /// Reset the colors to terminal defaults. Call this when you are done

View File

@ -72,7 +72,7 @@ private:
public: public:
// color order matches ANSI escape sequence, don't change // color order matches ANSI escape sequence, don't change
enum class Color { enum Colors {
BLACK = 0, BLACK = 0,
RED, RED,
GREEN, GREEN,
@ -81,21 +81,9 @@ public:
MAGENTA, MAGENTA,
CYAN, CYAN,
WHITE, WHITE,
SAVEDCOLOR, SAVEDCOLOR
RESET,
}; };
static const Color BLACK = Color::BLACK;
static const Color RED = Color::RED;
static const Color GREEN = Color::GREEN;
static const Color YELLOW = Color::YELLOW;
static const Color BLUE = Color::BLUE;
static const Color MAGENTA = Color::MAGENTA;
static const Color CYAN = Color::CYAN;
static const Color WHITE = Color::WHITE;
static const Color SAVEDCOLOR = Color::SAVEDCOLOR;
static const Color RESET = Color::RESET;
explicit raw_ostream(bool unbuffered = false) explicit raw_ostream(bool unbuffered = false)
: BufferMode(unbuffered ? Unbuffered : InternalBuffer) { : BufferMode(unbuffered ? Unbuffered : InternalBuffer) {
// Start out ready to flush. // Start out ready to flush.
@ -226,9 +214,6 @@ public:
/// Output \p N in hexadecimal, without any prefix or padding. /// Output \p N in hexadecimal, without any prefix or padding.
raw_ostream &write_hex(unsigned long long N); raw_ostream &write_hex(unsigned long long N);
// Change the foreground color of text.
raw_ostream &operator<<(Color C);
/// Output a formatted UUID with dash separators. /// Output a formatted UUID with dash separators.
using uuid_t = uint8_t[16]; using uuid_t = uint8_t[16];
raw_ostream &write_uuid(const uuid_t UUID); raw_ostream &write_uuid(const uuid_t UUID);
@ -263,14 +248,15 @@ public:
/// Changes the foreground color of text that will be output from this point /// Changes the foreground color of text that will be output from this point
/// forward. /// forward.
/// @param C ANSI color to use, the special SAVEDCOLOR can be used to /// @param Color ANSI color to use, the special SAVEDCOLOR can be used to
/// change only the bold attribute, and keep colors untouched /// change only the bold attribute, and keep colors untouched
/// @param Bold bold/brighter text, default false /// @param Bold bold/brighter text, default false
/// @param BG if true change the background, default: change foreground /// @param BG if true change the background, default: change foreground
/// @returns itself so it can be used within << invocations /// @returns itself so it can be used within << invocations
virtual raw_ostream &changeColor(Color C, bool Bold = false, virtual raw_ostream &changeColor(enum Colors Color,
bool Bold = false,
bool BG = false) { bool BG = false) {
(void)C; (void)Color;
(void)Bold; (void)Bold;
(void)BG; (void)BG;
return *this; return *this;
@ -291,11 +277,6 @@ public:
/// This function determines if this stream is displayed and supports colors. /// This function determines if this stream is displayed and supports colors.
virtual bool has_colors() const { return is_displayed(); } virtual bool has_colors() const { return is_displayed(); }
// Enable or disable colors. Once disable_colors() is called,
// changeColor() has no effect until enable_colors() is called.
virtual void enable_colors() {}
virtual void disable_colors() {}
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//
// Subclass Interface // Subclass Interface
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//
@ -387,8 +368,6 @@ class raw_fd_ostream : public raw_pwrite_stream {
bool SupportsSeeking; bool SupportsSeeking;
bool ColorEnabled;
#ifdef _WIN32 #ifdef _WIN32
/// True if this fd refers to a Windows console device. Mintty and other /// True if this fd refers to a Windows console device. Mintty and other
/// terminal emulators are TTYs, but they are not consoles. /// terminal emulators are TTYs, but they are not consoles.
@ -453,8 +432,8 @@ public:
/// to the offset specified from the beginning of the file. /// to the offset specified from the beginning of the file.
uint64_t seek(uint64_t off); uint64_t seek(uint64_t off);
raw_ostream &changeColor(Color C, bool bold = false, raw_ostream &changeColor(enum Colors colors, bool bold=false,
bool bg = false) override; bool bg=false) override;
raw_ostream &resetColor() override; raw_ostream &resetColor() override;
raw_ostream &reverseColor() override; raw_ostream &reverseColor() override;
@ -463,10 +442,6 @@ public:
bool has_colors() const override; bool has_colors() const override;
void enable_colors() override { ColorEnabled = true; }
void disable_colors() override { ColorEnabled = false; }
std::error_code error() const { return EC; } std::error_code error() const { return EC; }
/// Return the value of the flag in this raw_fd_ostream indicating whether an /// Return the value of the flag in this raw_fd_ostream indicating whether an

View File

@ -22,8 +22,6 @@ WithColor::WithColor(raw_ostream &OS, HighlightColor Color, bool DisableColors)
: OS(OS), DisableColors(DisableColors) { : OS(OS), DisableColors(DisableColors) {
// Detect color from terminal type unless the user passed the --color option. // Detect color from terminal type unless the user passed the --color option.
if (colorsEnabled()) { if (colorsEnabled()) {
OS.enable_colors();
switch (Color) { switch (Color) {
case HighlightColor::Address: case HighlightColor::Address:
OS.changeColor(raw_ostream::YELLOW); OS.changeColor(raw_ostream::YELLOW);
@ -106,9 +104,10 @@ bool WithColor::colorsEnabled() {
return UseColor == cl::BOU_TRUE; return UseColor == cl::BOU_TRUE;
} }
WithColor &WithColor::changeColor(raw_ostream::Color C, bool Bold, bool BG) { WithColor &WithColor::changeColor(raw_ostream::Colors Color, bool Bold,
bool BG) {
if (colorsEnabled()) if (colorsEnabled())
OS.changeColor(C, Bold, BG); OS.changeColor(Color, Bold, BG);
return *this; return *this;
} }

View File

@ -65,17 +65,6 @@
using namespace llvm; using namespace llvm;
const raw_ostream::Color raw_ostream::BLACK;
const raw_ostream::Color raw_ostream::RED;
const raw_ostream::Color raw_ostream::GREEN;
const raw_ostream::Color raw_ostream::YELLOW;
const raw_ostream::Color raw_ostream::BLUE;
const raw_ostream::Color raw_ostream::MAGENTA;
const raw_ostream::Color raw_ostream::CYAN;
const raw_ostream::Color raw_ostream::WHITE;
const raw_ostream::Color raw_ostream::SAVEDCOLOR;
const raw_ostream::Color raw_ostream::RESET;
raw_ostream::~raw_ostream() { raw_ostream::~raw_ostream() {
// raw_ostream's subclasses should take care to flush the buffer // raw_ostream's subclasses should take care to flush the buffer
// in their destructors. // in their destructors.
@ -144,14 +133,6 @@ raw_ostream &raw_ostream::write_hex(unsigned long long N) {
return *this; return *this;
} }
raw_ostream &raw_ostream::operator<<(Color C) {
if (C == Color::RESET)
resetColor();
else
changeColor(C);
return *this;
}
raw_ostream &raw_ostream::write_uuid(const uuid_t UUID) { raw_ostream &raw_ostream::write_uuid(const uuid_t UUID) {
for (int Idx = 0; Idx < 16; ++Idx) { for (int Idx = 0; Idx < 16; ++Idx) {
*this << format("%02" PRIX32, UUID[Idx]); *this << format("%02" PRIX32, UUID[Idx]);
@ -571,9 +552,8 @@ raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
/// FD is the file descriptor that this writes to. If ShouldClose is true, this /// FD is the file descriptor that this writes to. If ShouldClose is true, this
/// closes the file when the stream is destroyed. /// closes the file when the stream is destroyed.
raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered) raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered)
: raw_pwrite_stream(unbuffered), FD(fd), ShouldClose(shouldClose), : raw_pwrite_stream(unbuffered), FD(fd), ShouldClose(shouldClose) {
ColorEnabled(sys::Process::FileDescriptorHasColors(fd)) { if (FD < 0 ) {
if (FD < 0) {
ShouldClose = false; ShouldClose = false;
return; return;
} }
@ -802,16 +782,13 @@ size_t raw_fd_ostream::preferred_buffer_size() const {
#endif #endif
} }
raw_ostream &raw_fd_ostream::changeColor(Color color, bool bold, bool bg) { raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
if (!ColorEnabled) bool bg) {
return *this;
if (sys::Process::ColorNeedsFlush()) if (sys::Process::ColorNeedsFlush())
flush(); flush();
const char *colorcode = const char *colorcode =
(color == Color::SAVEDCOLOR) (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
? sys::Process::OutputBold(bg) : sys::Process::OutputColor(colors, bold, bg);
: sys::Process::OutputColor(static_cast<char>(color), bold, bg);
if (colorcode) { if (colorcode) {
size_t len = strlen(colorcode); size_t len = strlen(colorcode);
write(colorcode, len); write(colorcode, len);
@ -822,9 +799,6 @@ raw_ostream &raw_fd_ostream::changeColor(Color color, bool bold, bool bg) {
} }
raw_ostream &raw_fd_ostream::resetColor() { raw_ostream &raw_fd_ostream::resetColor() {
if (!ColorEnabled)
return *this;
if (sys::Process::ColorNeedsFlush()) if (sys::Process::ColorNeedsFlush())
flush(); flush();
const char *colorcode = sys::Process::ResetColor(); const char *colorcode = sys::Process::ResetColor();
@ -838,9 +812,6 @@ raw_ostream &raw_fd_ostream::resetColor() {
} }
raw_ostream &raw_fd_ostream::reverseColor() { raw_ostream &raw_fd_ostream::reverseColor() {
if (!ColorEnabled)
return *this;
if (sys::Process::ColorNeedsFlush()) if (sys::Process::ColorNeedsFlush())
flush(); flush();
const char *colorcode = sys::Process::OutputReverse(); const char *colorcode = sys::Process::OutputReverse();

View File

@ -111,7 +111,7 @@ void renderDivider(ArrayRef<size_t> ColumnWidths, raw_ostream &OS) {
/// Return the color which correponds to the coverage percentage of a /// Return the color which correponds to the coverage percentage of a
/// certain metric. /// certain metric.
template <typename T> template <typename T>
raw_ostream::Color determineCoveragePercentageColor(const T &Info) { raw_ostream::Colors determineCoveragePercentageColor(const T &Info) {
if (Info.isFullyCovered()) if (Info.isFullyCovered())
return raw_ostream::GREEN; return raw_ostream::GREEN;
return Info.getPercentCovered() >= 80.0 ? raw_ostream::YELLOW return Info.getPercentCovered() >= 80.0 ? raw_ostream::YELLOW

View File

@ -46,7 +46,7 @@ struct CoverageViewOptions {
/// Change the output's stream color if the colors are enabled. /// Change the output's stream color if the colors are enabled.
ColoredRawOstream colored_ostream(raw_ostream &OS, ColoredRawOstream colored_ostream(raw_ostream &OS,
raw_ostream::Color Color) const { raw_ostream::Colors Color) const {
return llvm::colored_ostream(OS, Color, Colors); return llvm::colored_ostream(OS, Color, Colors);
} }

View File

@ -47,7 +47,7 @@ inline raw_ostream &operator<<(const ColoredRawOstream &OS, T &&Value) {
/// Change the color of the output stream if the `IsColorUsed` flag /// Change the color of the output stream if the `IsColorUsed` flag
/// is true. Returns an object that resets the color when destroyed. /// is true. Returns an object that resets the color when destroyed.
inline ColoredRawOstream colored_ostream(raw_ostream &OS, inline ColoredRawOstream colored_ostream(raw_ostream &OS,
raw_ostream::Color Color, raw_ostream::Colors Color,
bool IsColorUsed = true, bool IsColorUsed = true,
bool Bold = false, bool BG = false) { bool Bold = false, bool BG = false) {
if (IsColorUsed) if (IsColorUsed)

View File

@ -101,7 +101,7 @@ void SourceCoverageViewText::renderLine(raw_ostream &OS, LineRef L,
auto *WrappedSegment = LCS.getWrappedSegment(); auto *WrappedSegment = LCS.getWrappedSegment();
CoverageSegmentArray Segments = LCS.getLineSegments(); CoverageSegmentArray Segments = LCS.getLineSegments();
Optional<raw_ostream::Color> Highlight; Optional<raw_ostream::Colors> Highlight;
SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges; SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
// The first segment overlaps from a previous line, so we treat it specially. // The first segment overlaps from a previous line, so we treat it specially.

View File

@ -83,10 +83,13 @@ int main(int argc, const char **argv) {
} }
} }
if (argc > 1) if (argc > 1) {
errs() << raw_ostream::RED << "Unrecognized command: " << argv[1] << ".\n\n" if (sys::Process::StandardErrHasColors())
<< raw_ostream::RESET; errs().changeColor(raw_ostream::RED);
errs() << "Unrecognized command: " << argv[1] << ".\n\n";
if (sys::Process::StandardErrHasColors())
errs().resetColor();
}
helpMain(argc, argv); helpMain(argc, argv);
return 1; return 1;
} }

View File

@ -103,8 +103,8 @@ void TimelineView::onEvent(const HWInstructionEvent &Event) {
LastCycle = std::max(LastCycle, CurrentCycle); LastCycle = std::max(LastCycle, CurrentCycle);
} }
static raw_ostream::Color chooseColor(unsigned CumulativeCycles, static raw_ostream::Colors chooseColor(unsigned CumulativeCycles,
unsigned Executions, int BufferSize) { unsigned Executions, int BufferSize) {
if (CumulativeCycles && BufferSize < 0) if (CumulativeCycles && BufferSize < 0)
return raw_ostream::MAGENTA; return raw_ostream::MAGENTA;
unsigned Size = static_cast<unsigned>(BufferSize); unsigned Size = static_cast<unsigned>(BufferSize);
@ -120,7 +120,7 @@ static void tryChangeColor(raw_ostream &OS, unsigned Cycles,
if (!OS.has_colors()) if (!OS.has_colors())
return; return;
raw_ostream::Color Color = chooseColor(Cycles, Executions, BufferSize); raw_ostream::Colors Color = chooseColor(Cycles, Executions, BufferSize);
if (Color == raw_ostream::SAVEDCOLOR) { if (Color == raw_ostream::SAVEDCOLOR) {
OS.resetColor(); OS.resetColor();
return; return;

View File

@ -138,11 +138,12 @@ struct MarkerStyle {
/// The starting char (before tildes) for marking the line. /// The starting char (before tildes) for marking the line.
char Lead; char Lead;
/// What color to use for this annotation. /// What color to use for this annotation.
raw_ostream::Color Color; raw_ostream::Colors Color;
/// A note to follow the marker, or empty string if none. /// A note to follow the marker, or empty string if none.
std::string Note; std::string Note;
MarkerStyle() {} MarkerStyle() {}
MarkerStyle(char Lead, raw_ostream::Color Color, const std::string &Note = "") MarkerStyle(char Lead, raw_ostream::Colors Color,
const std::string &Note = "")
: Lead(Lead), Color(Color), Note(Note) {} : Lead(Lead), Color(Color), Note(Note) {}
}; };