[PATCH] D31246: Send ANSI color codes only to TTYs

Adrian McCarthy via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 22 10:00:59 PDT 2017


amccarth created this revision.

Color code changes were sent to the raw_ostream, even if the stream was piped to something other than a terminal (e.g., to FileCheck).  This caused a new test to fail on Linux because the output differed from the expected output by the ANSI codes.


https://reviews.llvm.org/D31246

Files:
  llvm/lib/Support/raw_ostream.cpp


Index: llvm/lib/Support/raw_ostream.cpp
===================================================================
--- llvm/lib/Support/raw_ostream.cpp
+++ llvm/lib/Support/raw_ostream.cpp
@@ -326,7 +326,7 @@
 }
 
 raw_ostream &raw_ostream::operator<<(const FormattedString &FS) {
-  unsigned Len = FS.Str.size(); 
+  unsigned Len = FS.Str.size();
   int PadAmount = FS.Width - Len;
   if (FS.RightJustify && (PadAmount > 0))
     this->indent(PadAmount);
@@ -640,6 +640,8 @@
 
 raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
                                          bool bg) {
+  if (!has_colors())
+    return *this;
   if (sys::Process::ColorNeedsFlush())
     flush();
   const char *colorcode =
@@ -655,6 +657,8 @@
 }
 
 raw_ostream &raw_fd_ostream::resetColor() {
+  if (!has_colors())
+    return *this;
   if (sys::Process::ColorNeedsFlush())
     flush();
   const char *colorcode = sys::Process::ResetColor();
@@ -668,6 +672,8 @@
 }
 
 raw_ostream &raw_fd_ostream::reverseColor() {
+  if (!has_colors())
+    return *this;
   if (sys::Process::ColorNeedsFlush())
     flush();
   const char *colorcode = sys::Process::OutputReverse();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31246.92651.patch
Type: text/x-patch
Size: 1164 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170322/147dc612/attachment.bin>


More information about the llvm-commits mailing list