Index: llvm/include/llvm/Support/WithColor.h =================================================================== --- llvm/include/llvm/Support/WithColor.h +++ llvm/include/llvm/Support/WithColor.h @@ -37,6 +37,7 @@ class WithColor { raw_ostream &OS; bool DisableColors; + raw_ostream::ColorState PrevColor; public: /// To be used like this: WithColor(OS, HighlightColor::String) << "text"; Index: llvm/include/llvm/Support/raw_ostream.h =================================================================== --- llvm/include/llvm/Support/raw_ostream.h +++ llvm/include/llvm/Support/raw_ostream.h @@ -85,6 +85,14 @@ RESET, }; + /// Color settings. + struct ColorState { + Colors Color = Colors::RESET; + bool Bold = false; + /// If true, the background color is changed instead of the foreground. + bool Background = false; + }; + static const Colors BLACK = Colors::BLACK; static const Colors RED = Colors::RED; static const Colors GREEN = Colors::GREEN; @@ -296,6 +304,10 @@ // changeColor() has no effect until enable_colors() is called. virtual void enable_colors(bool /*enable*/) {} + /// Returns the current color set through the raw_ostream interface + /// (changeColor, etc). + virtual ColorState getColor() { return {}; } + //===--------------------------------------------------------------------===// // Subclass Interface //===--------------------------------------------------------------------===// @@ -386,6 +398,7 @@ bool ShouldClose; bool SupportsSeeking; bool ColorEnabled = true; + ColorState CurrentColor; #ifdef _WIN32 /// True if this fd refers to a Windows console device. Mintty and other @@ -414,6 +427,10 @@ void anchor() override; + void saveColor(Colors Color, bool Bold, bool BG) { + CurrentColor = {Color, Bold, BG}; + } + public: /// Open the specified file for writing. If an error occurs, information /// about the error is put into EC, and the stream should be immediately @@ -457,6 +474,8 @@ raw_ostream &reverseColor() override; + ColorState getColor() override { return CurrentColor; } + bool is_displayed() const override; bool has_colors() const override; Index: llvm/lib/Support/WithColor.cpp =================================================================== --- llvm/lib/Support/WithColor.cpp +++ llvm/lib/Support/WithColor.cpp @@ -24,34 +24,34 @@ if (colorsEnabled()) { switch (Color) { case HighlightColor::Address: - OS.changeColor(raw_ostream::YELLOW); + changeColor(raw_ostream::YELLOW); break; case HighlightColor::String: - OS.changeColor(raw_ostream::GREEN); + changeColor(raw_ostream::GREEN); break; case HighlightColor::Tag: - OS.changeColor(raw_ostream::BLUE); + changeColor(raw_ostream::BLUE); break; case HighlightColor::Attribute: - OS.changeColor(raw_ostream::CYAN); + changeColor(raw_ostream::CYAN); break; case HighlightColor::Enumerator: - OS.changeColor(raw_ostream::MAGENTA); + changeColor(raw_ostream::MAGENTA); break; case HighlightColor::Macro: - OS.changeColor(raw_ostream::RED); + changeColor(raw_ostream::RED); break; case HighlightColor::Error: - OS.changeColor(raw_ostream::RED, true); + changeColor(raw_ostream::RED, true); break; case HighlightColor::Warning: - OS.changeColor(raw_ostream::MAGENTA, true); + changeColor(raw_ostream::MAGENTA, true); break; case HighlightColor::Note: - OS.changeColor(raw_ostream::BLACK, true); + changeColor(raw_ostream::BLACK, true); break; case HighlightColor::Remark: - OS.changeColor(raw_ostream::BLUE, true); + changeColor(raw_ostream::BLUE, true); break; } } @@ -69,8 +69,7 @@ bool DisableColors) { if (!Prefix.empty()) OS << Prefix << ": "; - return WithColor(OS, HighlightColor::Error, DisableColors).get() - << "error: "; + return WithColor(OS, HighlightColor::Error, DisableColors).get() << "error: "; } raw_ostream &WithColor::warning(raw_ostream &OS, StringRef Prefix, @@ -106,8 +105,11 @@ WithColor &WithColor::changeColor(raw_ostream::Colors Color, bool Bold, bool BG) { - if (colorsEnabled()) + if (colorsEnabled()) { + PrevColor = OS.getColor(); OS.changeColor(Color, Bold, BG); + } + return *this; } @@ -117,4 +119,11 @@ return *this; } -WithColor::~WithColor() { resetColor(); } +WithColor::~WithColor() { + if (colorsEnabled()) { + if (PrevColor.Color == raw_ostream::Colors::RESET) + OS.resetColor(); + else + OS.changeColor(PrevColor.Color, PrevColor.Bold, PrevColor.Background); + } +} Index: llvm/lib/Support/raw_ostream.cpp =================================================================== --- llvm/lib/Support/raw_ostream.cpp +++ llvm/lib/Support/raw_ostream.cpp @@ -818,6 +818,8 @@ // don't account colors towards output characters pos -= len; } + + saveColor(colors, bold, bg); return *this; } @@ -834,6 +836,8 @@ // don't account colors towards output characters pos -= len; } + + saveColor(Colors::RESET, false, false); return *this; }