summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support
diff options
context:
space:
mode:
authorRui Ueyama <ruiu@google.com>2019-08-02 04:48:30 +0000
committerRui Ueyama <ruiu@google.com>2019-08-02 04:48:30 +0000
commita52f982f1cd98ebf94abb5deb5244f460ddad2d1 (patch)
tree61799d68dd515bda1a6bd6d7b70812e96dbe9ded /llvm/lib/Support
parent9131e925fd6b5010a9e797342a09141306183ed6 (diff)
downloadbcm5719-llvm-a52f982f1cd98ebf94abb5deb5244f460ddad2d1.tar.gz
bcm5719-llvm-a52f982f1cd98ebf94abb5deb5244f460ddad2d1.zip
Improve raw_ostream so that you can "write" colors using operator<<
1. raw_ostream supports ANSI colors so that you can write messages to the termina with colors. Previously, in order to change and reset color, you had to call `changeColor` and `resetColor` functions, respectively. So, if you print out "error: " in red, for example, you had to do something like this: OS.changeColor(raw_ostream::RED); OS << "error: "; OS.resetColor(); With this patch, you can write the same code as follows: OS << raw_ostream::RED << "error: " << raw_ostream::RESET; 2. Add a boolean flag to raw_ostream so that you can disable colored output. If you disable colors, changeColor, operator<<(Color), resetColor and other color-related functions have no effect. Most LLVM tools automatically prints out messages using colors, and you can disable it by passing a flag such as `--disable-colors`. This new flag makes it easy to write code that works that way. Differential Revision: https://reviews.llvm.org/D65564 llvm-svn: 367649
Diffstat (limited to 'llvm/lib/Support')
-rw-r--r--llvm/lib/Support/WithColor.cpp7
-rw-r--r--llvm/lib/Support/raw_ostream.cpp41
2 files changed, 39 insertions, 9 deletions
diff --git a/llvm/lib/Support/WithColor.cpp b/llvm/lib/Support/WithColor.cpp
index 345dd9cf394..1c380c65902 100644
--- a/llvm/lib/Support/WithColor.cpp
+++ b/llvm/lib/Support/WithColor.cpp
@@ -22,6 +22,8 @@ WithColor::WithColor(raw_ostream &OS, HighlightColor Color, bool DisableColors)
: OS(OS), DisableColors(DisableColors) {
// Detect color from terminal type unless the user passed the --color option.
if (colorsEnabled()) {
+ OS.enable_colors();
+
switch (Color) {
case HighlightColor::Address:
OS.changeColor(raw_ostream::YELLOW);
@@ -104,10 +106,9 @@ bool WithColor::colorsEnabled() {
return UseColor == cl::BOU_TRUE;
}
-WithColor &WithColor::changeColor(raw_ostream::Colors Color, bool Bold,
- bool BG) {
+WithColor &WithColor::changeColor(raw_ostream::Color C, bool Bold, bool BG) {
if (colorsEnabled())
- OS.changeColor(Color, Bold, BG);
+ OS.changeColor(C, Bold, BG);
return *this;
}
diff --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp
index 2baccaa0cbd..da24fe29471 100644
--- a/llvm/lib/Support/raw_ostream.cpp
+++ b/llvm/lib/Support/raw_ostream.cpp
@@ -65,6 +65,17 @@
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's subclasses should take care to flush the buffer
// in their destructors.
@@ -133,6 +144,14 @@ raw_ostream &raw_ostream::write_hex(unsigned long long N) {
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) {
for (int Idx = 0; Idx < 16; ++Idx) {
*this << format("%02" PRIX32, UUID[Idx]);
@@ -552,8 +571,9 @@ 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
/// closes the file when the stream is destroyed.
raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered)
- : raw_pwrite_stream(unbuffered), FD(fd), ShouldClose(shouldClose) {
- if (FD < 0 ) {
+ : raw_pwrite_stream(unbuffered), FD(fd), ShouldClose(shouldClose),
+ ColorEnabled(sys::Process::FileDescriptorHasColors(fd)) {
+ if (FD < 0) {
ShouldClose = false;
return;
}
@@ -782,13 +802,16 @@ size_t raw_fd_ostream::preferred_buffer_size() const {
#endif
}
-raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
- bool bg) {
+raw_ostream &raw_fd_ostream::changeColor(Color color, bool bold, bool bg) {
+ if (!ColorEnabled)
+ return *this;
+
if (sys::Process::ColorNeedsFlush())
flush();
const char *colorcode =
- (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
- : sys::Process::OutputColor(colors, bold, bg);
+ (color == Color::SAVEDCOLOR)
+ ? sys::Process::OutputBold(bg)
+ : sys::Process::OutputColor(static_cast<char>(color), bold, bg);
if (colorcode) {
size_t len = strlen(colorcode);
write(colorcode, len);
@@ -799,6 +822,9 @@ raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
}
raw_ostream &raw_fd_ostream::resetColor() {
+ if (!ColorEnabled)
+ return *this;
+
if (sys::Process::ColorNeedsFlush())
flush();
const char *colorcode = sys::Process::ResetColor();
@@ -812,6 +838,9 @@ raw_ostream &raw_fd_ostream::resetColor() {
}
raw_ostream &raw_fd_ostream::reverseColor() {
+ if (!ColorEnabled)
+ return *this;
+
if (sys::Process::ColorNeedsFlush())
flush();
const char *colorcode = sys::Process::OutputReverse();
OpenPOWER on IntegriCloud