[Lldb-commits] [lldb] r340747 - Disable use-color if the output stream is not a terminal with color support.

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Mon Aug 27 08:16:25 PDT 2018


Author: teemperor
Date: Mon Aug 27 08:16:25 2018
New Revision: 340747

URL: http://llvm.org/viewvc/llvm-project?rev=340747&view=rev
Log:
Disable use-color if the output stream is not a terminal with color support.

Summary:
LLDB currently only checks the output terminal for color support by looking
at the `TERM` environment variable and comparing it to `"dumb"`. This causes that
when running LLDB on a CI node, the syntax highlighter will not be deactivated by
LLDB and the output log is filled with color codes (unless the terminal emulator
actually exposes itself as dumb).

This patch now relies on the LLVM code for detecting color support which is more
reliable. We now also correctly actually initialize the `m_supports_colors` variable in `File`.
`m_supports_colors` was so far uninitialized, but the code path that uses `m_supports_colors`
was also dead so the sanitizers didn't sound an alarm.

The old check that compares `TERM` is not removed by this patch as the new LLVM code
doesn't seem to handle this case (and it's a good thing to check for "dumb" terminals).

Reviewers: aprantl, javed.absar

Reviewed By: aprantl

Subscribers: kristof.beyls, abidh, lldb-commits

Differential Revision: https://reviews.llvm.org/D51243

Added:
    lldb/trunk/lit/Settings/
    lldb/trunk/lit/Settings/TestDisableColor.test
    lldb/trunk/lit/Settings/lit.local.cfg
Modified:
    lldb/trunk/include/lldb/Host/File.h
    lldb/trunk/source/Core/Debugger.cpp

Modified: lldb/trunk/include/lldb/Host/File.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/File.h?rev=340747&r1=340746&r2=340747&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/File.h (original)
+++ lldb/trunk/include/lldb/Host/File.h Mon Aug 27 08:16:25 2018
@@ -54,13 +54,15 @@ public:
       : IOObject(eFDTypeFile, false), m_descriptor(kInvalidDescriptor),
         m_stream(kInvalidStream), m_options(0), m_own_stream(false),
         m_is_interactive(eLazyBoolCalculate),
-        m_is_real_terminal(eLazyBoolCalculate) {}
+        m_is_real_terminal(eLazyBoolCalculate),
+        m_supports_colors(eLazyBoolCalculate) {}
 
   File(FILE *fh, bool transfer_ownership)
       : IOObject(eFDTypeFile, false), m_descriptor(kInvalidDescriptor),
         m_stream(fh), m_options(0), m_own_stream(transfer_ownership),
         m_is_interactive(eLazyBoolCalculate),
-        m_is_real_terminal(eLazyBoolCalculate) {}
+        m_is_real_terminal(eLazyBoolCalculate),
+        m_supports_colors(eLazyBoolCalculate) {}
 
   //------------------------------------------------------------------
   /// Constructor with path.

Added: lldb/trunk/lit/Settings/TestDisableColor.test
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lit/Settings/TestDisableColor.test?rev=340747&view=auto
==============================================================================
--- lldb/trunk/lit/Settings/TestDisableColor.test (added)
+++ lldb/trunk/lit/Settings/TestDisableColor.test Mon Aug 27 08:16:25 2018
@@ -0,0 +1,7 @@
+# RUN: %lldb -x -b -s %s | FileCheck %s
+settings show use-color
+q
+# This tests that LLDB turns off use-color if the output file is not an
+# interactive terminal. In this example, use-color should be off because LLDB
+# is run just by the non-interactive lit test runner.
+# CHECK: use-color (boolean) = false

Added: lldb/trunk/lit/Settings/lit.local.cfg
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lit/Settings/lit.local.cfg?rev=340747&view=auto
==============================================================================
--- lldb/trunk/lit/Settings/lit.local.cfg (added)
+++ lldb/trunk/lit/Settings/lit.local.cfg Mon Aug 27 08:16:25 2018
@@ -0,0 +1 @@
+config.suffixes = ['.test']

Modified: lldb/trunk/source/Core/Debugger.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/Debugger.cpp?rev=340747&r1=340746&r2=340747&view=diff
==============================================================================
--- lldb/trunk/source/Core/Debugger.cpp (original)
+++ lldb/trunk/source/Core/Debugger.cpp Mon Aug 27 08:16:25 2018
@@ -804,6 +804,9 @@ Debugger::Debugger(lldb::LogOutputCallba
   const char *term = getenv("TERM");
   if (term && !strcmp(term, "dumb"))
     SetUseColor(false);
+  // Turn off use-color if we don't write to a terminal with color support.
+  if (!m_output_file_sp->GetFile().GetIsTerminalWithColors())
+    SetUseColor(false);
 }
 
 Debugger::~Debugger() { Clear(); }




More information about the lldb-commits mailing list