[llvm] [FileCheck] Use default colors in input dumps (PR #204936)

Joel E. Denny via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 20 10:01:00 PDT 2026


https://github.com/jdenny-ornl created https://github.com/llvm/llvm-project/pull/204936

This patch makes two improvements to colors used in FileCheck input dumps:

1. Without this patch, input line numbers and ellipses have a foreground color of black, which is hard to see in a terminal with a dark color theme.  This patch changes that to the terminal's default color.
2. Without this patch, the input text is accidentally set to bold when neither `-v` or `-vv` is specified.  Perhaps I never noticed because I tend to always use `-vv`.  This patch changes that to use the terminal's default color.

Case 2 exposes a problem with LLVM's color implementation.  Without this patch, the call to `WithColor`'s constructor actually specifies bold as `false`, but `WithColor` ignores that when the color is `SAVEDCOLOR`.  While it seems like that should be fixed, I am concerned about the impact of such a fix on other tools that might have come to inadvertently depend on the old behavior.  For now, this patch adds fixme comments to the color APIs.

I never found a nice way to write tests checking that FileCheck emits expected color codes.  Do I really want to hardcode the exact codes into tests?  The best I came up with in the past was merely checking for extra characters, as in `llvm/test/FileCheck/opt-color.txt`, but that does not really help here as we already expect extra characters for colors.  Maybe we need a script that translates color codes into some kind of plain text format with symbolic names instead of codes. That seems like too much work for this fix, so this patch has no test changes.

>From 2df672a9185516005556fadd0156d94083b60a84 Mon Sep 17 00:00:00 2001
From: "Joel E. Denny" <jdenny.ornl at gmail.com>
Date: Sat, 20 Jun 2026 12:47:44 -0400
Subject: [PATCH] [FileCheck] Use default colors in input dumps

This patch makes two improvements to colors used in FileCheck input
dumps:

1. Without this patch, input line numbers and ellipses have a
   foreground color of black, which is hard to see in a terminal with
   a dark color theme.  This patch changes that to the terminal's
   default color.
2. Without this patch, the input text is accidentally set to bold when
   neither `-v` or `-vv` is specified.  Perhaps I never noticed
   because I tend to always use `-vv`.  This patch changes that to use
   the terminal's default color.

Case 2 exposes a problem with LLVM's color implementation.  Without
this patch, the call to `WithColor`'s constructor actually specifies
bold as `false`, but `WithColor` ignores that when the color is
`SAVEDCOLOR`.  While it seems like that should be fixed, I am
concerned about the impact of such a fix on other tools that might
have come to inadvertently depend on the old behavior.  For now, this
patch adds fixme comments to the color APIs.

I never found a nice way to write tests checking that FileCheck emits
expected color codes.  Do I really want to hardcode the exact codes
into tests?  The best I came up with in the past was merely checking
for extra characters, as in `llvm/test/FileCheck/opt-color.txt`, but
that does not really help here as we already expect extra characters
for colors.  Maybe we need a script that translates color codes into
some kind of plain text format with symbolic names instead of codes.
That seems like too much work for this fix, so this patch has no test
changes.
---
 llvm/include/llvm/Support/WithColor.h   |  4 ++++
 llvm/include/llvm/Support/raw_ostream.h |  2 ++
 llvm/utils/FileCheck/FileCheck.cpp      | 18 ++++++++++++++----
 3 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/Support/WithColor.h b/llvm/include/llvm/Support/WithColor.h
index 2835e179e6195..febf211c3a391 100644
--- a/llvm/include/llvm/Support/WithColor.h
+++ b/llvm/include/llvm/Support/WithColor.h
@@ -68,6 +68,8 @@ class WithColor {
   /// @param Bold Bold/brighter text, default false
   /// @param BG If true, change the background, default: change foreground
   /// @param Mode Enable, disable or compute whether to use colors.
+  ///
+  /// FIXME: If Color == SAVEDCOLOR, Bold == false is currently ignored.
   LLVM_CTOR_NODISCARD WithColor(
       raw_ostream &OS, raw_ostream::Colors Color = raw_ostream::SAVEDCOLOR,
       bool Bold = false, bool BG = false, ColorMode Mode = ColorMode::Auto)
@@ -117,6 +119,8 @@ class WithColor {
   /// change only the bold attribute, and keep colors untouched
   /// @param Bold Bold/brighter text, default false
   /// @param BG If true, change the background, default: change foreground
+  ///
+  /// FIXME: If Color == SAVEDCOLOR, Bold == false is currently ignored.
   LLVM_ABI WithColor &changeColor(raw_ostream::Colors Color, bool Bold = false,
                                   bool BG = false);
 
diff --git a/llvm/include/llvm/Support/raw_ostream.h b/llvm/include/llvm/Support/raw_ostream.h
index 70916d8e4adb0..1e66052c849b7 100644
--- a/llvm/include/llvm/Support/raw_ostream.h
+++ b/llvm/include/llvm/Support/raw_ostream.h
@@ -332,6 +332,8 @@ class LLVM_ABI raw_ostream {
   /// @param Bold bold/brighter text, default false
   /// @param BG if true change the background, default: change foreground
   /// @returns itself so it can be used within << invocations
+  ///
+  /// FIXME: If Color == SAVEDCOLOR, Bold == false is currently ignored.
   virtual raw_ostream &changeColor(enum Colors Color, bool Bold = false,
                                    bool BG = false);
 
diff --git a/llvm/utils/FileCheck/FileCheck.cpp b/llvm/utils/FileCheck/FileCheck.cpp
index 8c760db50a375..5d389102af0c3 100644
--- a/llvm/utils/FileCheck/FileCheck.cpp
+++ b/llvm/utils/FileCheck/FileCheck.cpp
@@ -686,7 +686,7 @@ static void DumpEllipsisOrElidedLines(raw_ostream &OS, std::string &ElidedLines,
   unsigned EllipsisLines = 3;
   if (EllipsisLines < StringRef(ElidedLines).count('\n')) {
     for (unsigned i = 0; i < EllipsisLines; ++i) {
-      WithColor(OS, raw_ostream::BLACK, /*Bold=*/true)
+      WithColor(OS, raw_ostream::SAVEDCOLOR, /*Bold=*/true)
           << right_justify(".", LabelWidthGlobal);
       OS << '\n';
     }
@@ -810,7 +810,7 @@ static void DumpAnnotatedInput(raw_ostream &OS, const FileCheckRequest &Req,
     }
 
     // Print right-aligned line number.
-    WithColor(*LineOS, raw_ostream::BLACK, /*Bold=*/true, /*BF=*/false,
+    WithColor(*LineOS, raw_ostream::SAVEDCOLOR, /*Bold=*/true, /*BG=*/false,
               TheColorMode)
         << format_decimal(Line, LabelWidthGlobal) << ": ";
 
@@ -833,8 +833,16 @@ static void DumpAnnotatedInput(raw_ostream &OS, const FileCheckRequest &Req,
       WithColor COS(*LineOS, raw_ostream::SAVEDCOLOR, /*Bold=*/false,
                     /*BG=*/false, TheColorMode);
       bool InMatch = false;
-      if (Req.Verbose)
-        COS.changeColor(raw_ostream::CYAN, true, true);
+      if (Req.Verbose) {
+        COS.changeColor(raw_ostream::CYAN, /*Bold=*/true, /*BG=*/true);
+      } else {
+        // Our goal is to use the output streams's default color so that input
+        // text is legibile in both light and dark themes.  SAVEDCOLOR above
+        // currently ignores the Bold=false there, so we override it with
+        // resetColor here, which ensures consistent colors with the resetColor
+        // below anyway.
+        COS.resetColor();
+      }
       for (unsigned Col = 1; InputFilePtr != InputFileEnd && !Newline; ++Col) {
         bool WasInMatch = InMatch;
         InMatch = false;
@@ -844,6 +852,8 @@ static void DumpAnnotatedInput(raw_ostream &OS, const FileCheckRequest &Req,
             break;
           }
         }
+        // If !Req.Verbose, FoundAndExpectedMatches is empty, so InMatch and
+        // WasInMatch remain false, so these color transitions never happen.
         if (!WasInMatch && InMatch)
           COS.resetColor();
         else if (WasInMatch && !InMatch)



More information about the llvm-commits mailing list