Author: Nick Desaulniers
Date: 2026-11-13T09:52:39-08:00
New Revision: 09a50d5cf1dc8b751c9091e550771d0ac9caba1c

URL: 
https://github.com/llvm/llvm-project/commit/09a50d5cf1dc8b751c9091e550771d0ac9caba1c
DIFF: 
https://github.com/llvm/llvm-project/commit/09a50d5cf1dc8b751c9091e550771d0ac9caba1c.diff

LOG: [diagtool] add --color={always,auto,never} (#242002)

I love `diagtool tree`, but find its output too long, so I frequently
pipe it into a pager, `less`. Because a pipe is not a TTY (which
supports color), raw_ostream doesn't know what's listening on the end of
the pipe it's writing to, so it conservatively does not emit ANSI color
sequences.

Many other unix tools have `--color` flags to always emit ANSI color
codes, and `less` has `-R` for interpreting these escape sequences.

    $ diagtool tree --color | less -R

Assisted-by: Gemini

Added: 
    clang/test/Misc/warning-flags-tree-color.c

Modified: 
    clang/docs/CommandGuide/diagtool.md
    clang/tools/diagtool/TreeView.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/CommandGuide/diagtool.md 
b/clang/docs/CommandGuide/diagtool.md
index 50729bddfc911..f3e21ebe9c349 100644
--- a/clang/docs/CommandGuide/diagtool.md
+++ b/clang/docs/CommandGuide/diagtool.md
@@ -41,4 +41,14 @@ the sections that follow.
 
 ### tree
 
-{program}`diagtool` tree \[*diagnostic-group*\]
+{program}`diagtool` tree \[*options*\] \[*diagnostic-group*\]
+
+:::{option} --internal
+Print individual diagnostic names in addition to diagnostic groups.
+:::
+
+:::{option} --color [=<always|auto|never>]
+Specify whether to use color in output. Defaults to `auto`, which detects
+whether standard output supports color. Specifying `--color` alone is
+equivalent to `--color=always`.
+:::

diff  --git a/clang/test/Misc/warning-flags-tree-color.c 
b/clang/test/Misc/warning-flags-tree-color.c
new file mode 100644
index 0000000000000..4fa9b6062e5b7
--- /dev/null
+++ b/clang/test/Misc/warning-flags-tree-color.c
@@ -0,0 +1,22 @@
+// REQUIRES: ansi-escape-sequences
+
+// RUN: diagtool tree --color -Wgnu | FileCheck -check-prefix CHECK-COLOR %s
+// RUN: diagtool tree --color=always -Wgnu | FileCheck -check-prefix 
CHECK-COLOR %s
+// RUN: diagtool tree --internal --color=always -Wgnu | FileCheck 
-check-prefix CHECK-COLOR %s
+// RUN: diagtool tree --color=always --internal -Wgnu | FileCheck 
-check-prefix CHECK-COLOR %s
+// RUN: diagtool tree --color=auto -Wgnu | FileCheck -check-prefix 
CHECK-NOCOLOR %s
+// RUN: diagtool tree --color=never -Wgnu | FileCheck -check-prefix 
CHECK-NOCOLOR %s
+// RUN: not diagtool tree --color=invalid -Wgnu 2>&1 | FileCheck -check-prefix 
CHECK-INVALID %s
+
+// CHECK-COLOR: {{.}}[0;32mGREEN{{.}}[0m = enabled by default
+// CHECK-COLOR: {{.}}[0;33mYELLOW{{.}}[0m = disabled by default
+// CHECK-COLOR: {{.}}[0;31mRED{{.}}[0m = unimplemented (accepted for GCC 
compatibility)
+// CHECK-COLOR: {{.}}[0;33m-Wgnu
+
+// CHECK-NOCOLOR: GREEN = enabled by default
+// CHECK-NOCOLOR: YELLOW = disabled by default
+// CHECK-NOCOLOR: RED = unimplemented (accepted for GCC compatibility)
+// CHECK-NOCOLOR: -Wgnu
+
+// CHECK-INVALID: Invalid value for --color: 'invalid'; expected 'always', 
'auto', or 'never'
+// CHECK-INVALID: Usage: diagtool tree [--internal] 
[--color[=<always|auto|never>]] [<diagnostic-group>]

diff  --git a/clang/tools/diagtool/TreeView.cpp 
b/clang/tools/diagtool/TreeView.cpp
index 13b80150e6041..f05a44d561959 100644
--- a/clang/tools/diagtool/TreeView.cpp
+++ b/clang/tools/diagtool/TreeView.cpp
@@ -12,8 +12,11 @@
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/DiagnosticOptions.h"
 #include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/StringSwitch.h"
 #include "llvm/Support/Format.h"
 #include "llvm/Support/Process.h"
+#include "llvm/Support/WithColor.h"
+#include <optional>
 
 DEF_DIAGTOOL("tree", "Show warning flags in a tree view", TreeView)
 
@@ -136,19 +139,40 @@ class TreePrinter {
 };
 
 static void printUsage() {
-  llvm::errs() << "Usage: diagtool tree [--internal] [<diagnostic-group>]\n";
+  llvm::errs()
+      << "Usage: diagtool tree [--internal] [--color[=<always|auto|never>]] "
+         "[<diagnostic-group>]\n";
 }
 
 int TreeView::run(unsigned int argc, char **argv, llvm::raw_ostream &out) {
-  // First check our one flag (--flags-only).
   bool Internal = false;
-  if (argc > 0) {
-    StringRef FirstArg(*argv);
-    if (FirstArg == "--internal") {
+  llvm::ColorMode Color = llvm::ColorMode::Auto;
+
+  while (argc > 0) {
+    StringRef Arg(*argv);
+    if (Arg == "--internal") {
       Internal = true;
-      --argc;
-      ++argv;
+    } else if (Arg == "--color") {
+      Color = llvm::ColorMode::Enable;
+    } else if (Arg.consume_front("--color=")) {
+      std::optional<llvm::ColorMode> ParsedColor =
+          llvm::StringSwitch<std::optional<llvm::ColorMode>>(Arg)
+              .Case("always", llvm::ColorMode::Enable)
+              .Case("auto", llvm::ColorMode::Auto)
+              .Case("never", llvm::ColorMode::Disable)
+              .Default(std::nullopt);
+      if (!ParsedColor) {
+        llvm::errs() << "Invalid value for --color: '" << Arg
+                     << "'; expected 'always', 'auto', or 'never'\n";
+        printUsage();
+        return -1;
+      }
+      Color = *ParsedColor;
+    } else {
+      break;
     }
+    --argc;
+    ++argv;
   }
 
   bool ShowAll = false;
@@ -170,7 +194,17 @@ int TreeView::run(unsigned int argc, char **argv, 
llvm::raw_ostream &out) {
     return -1;
   }
 
-  out.enable_colors(out.has_colors());
+  switch (Color) {
+  case llvm::ColorMode::Enable:
+    out.enable_colors(true);
+    break;
+  case llvm::ColorMode::Disable:
+    out.enable_colors(false);
+    break;
+  case llvm::ColorMode::Auto:
+    out.enable_colors(out.has_colors());
+    break;
+  }
 
   TreePrinter TP(out);
   TP.Internal = Internal;


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to