[clang-tools-extra] [clang-tidy] Allow disabling color in clang-tidy-diff (PR #245996)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 25 05:12:53 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy
Author: Aarav Khanal (Aaravkhanal)
<details>
<summary>Changes</summary>
Accept an optional boolean for -use-color, matching run-clang-tidy.py. Forward an explicit false so callers can override UseColor in their configuration.
Fixes #<!-- -->237055
Assisted-by: OpenAI Codex
---
Full diff: https://github.com/llvm/llvm-project/pull/245996.diff
2 Files Affected:
- (modified) clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py (+23-3)
- (modified) clang-tools-extra/test/clang-tidy/infrastructure/clang-tidy-diff.cpp (+9)
``````````diff
diff --git a/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py b/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py
index 059b2a2e8f476f..0b899159faa4fa 100755
--- a/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py
+++ b/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py
@@ -44,6 +44,17 @@
yaml = None
+def strtobool(val: str) -> bool:
+ """Parse a boolean using the same values as clang-tidy."""
+
+ value = val.lower()
+ if value in ("", "true", "1"):
+ return True
+ if value in ("false", "0"):
+ return False
+ raise argparse.ArgumentTypeError(f"invalid boolean value: {val!r}")
+
+
def run_tidy(task_queue, lock, timeout, failed_files):
watchdog = None
while True:
@@ -192,7 +203,14 @@ def main():
help="Specify the path of .clang-tidy or custom config file",
default="",
)
- parser.add_argument("-use-color", action="store_true", help="Use colors in output")
+ parser.add_argument(
+ "-use-color",
+ type=strtobool,
+ nargs="?",
+ const=True,
+ help="Use colors in diagnostics, overriding clang-tidy's default behavior "
+ "and the 'UseColor' option in .clang-tidy, if any.",
+ )
parser.add_argument(
"-path", dest="build_path", help="Path used to read a compile command database."
)
@@ -378,8 +396,10 @@ def main():
common_clang_tidy_args.append("-quiet")
if args.build_path is not None:
common_clang_tidy_args.append("-p=%s" % args.build_path)
- if args.use_color:
- common_clang_tidy_args.append("--use-color")
+ if args.use_color is not None:
+ common_clang_tidy_args.append(
+ "--use-color" if args.use_color else "--use-color=false"
+ )
if args.allow_no_checks:
common_clang_tidy_args.append("--allow-no-checks")
for arg in args.extra_arg:
diff --git a/clang-tools-extra/test/clang-tidy/infrastructure/clang-tidy-diff.cpp b/clang-tools-extra/test/clang-tidy/infrastructure/clang-tidy-diff.cpp
index 3064eca9354aba..67a765f1e5f869 100644
--- a/clang-tools-extra/test/clang-tidy/infrastructure/clang-tidy-diff.cpp
+++ b/clang-tools-extra/test/clang-tidy/infrastructure/clang-tidy-diff.cpp
@@ -17,6 +17,15 @@
// RUN: cat %t.clean.diff %t.diff | %clang_tidy_diff -checks=-*,modernize-use-override -j 1 -- -std=c++11 2>&1 | FileCheck %s --check-prefix=CHECK-NOBLANK
// CHECK-NOBLANK: Running clang-tidy in 1 threads...
// CHECK-NOBLANK-NEXT: :8: warning: annotate this
+
+// RUN: echo 'UseColor: true' > %t.dir/color.yaml
+// RUN: %clang_tidy_diff -checks=-*,modernize-use-override -config-file=%t.dir/color.yaml -- -std=c++11 < %t.diff | FileCheck %s --check-prefix=CHECK-COLOR
+// RUN: %clang_tidy_diff -checks=-*,modernize-use-override -config-file=%t.dir/color.yaml -use-color=false -- -std=c++11 < %t.diff | FileCheck %s --check-prefix=CHECK-NO-COLOR
+// RUN: echo 'UseColor: false' > %t.dir/color.yaml
+// RUN: %clang_tidy_diff -checks=-*,modernize-use-override -config-file=%t.dir/color.yaml -use-color -- -std=c++11 < %t.diff | FileCheck %s --check-prefix=CHECK-COLOR
+// CHECK-COLOR: {{.\[0;1;35m}}warning: {{.\[0m}}{{.\[1m}}annotate this
+// CHECK-NO-COLOR: :8: warning: annotate this
+
struct A {
virtual void f() {}
virtual void g() {}
``````````
</details>
https://github.com/llvm/llvm-project/pull/245996
More information about the cfe-commits
mailing list