[clang-tools-extra] [clang-tidy] Improve "-quiet" option in tidy-scripts by suppressing progress information (PR #154416)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 20 08:04:49 PDT 2025
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/154416
>From 3d87ccf38a518def2bd44dbddb661360a64158a2 Mon Sep 17 00:00:00 2001
From: Victor Baranov <bar.victor.2002 at gmail.com>
Date: Tue, 19 Aug 2025 22:39:19 +0300
Subject: [PATCH 1/2] [clang-tidy] Improve "-quiet" option in tidy-scripts by
suppressing progress information
---
.../clang-tidy/tool/clang-tidy-diff.py | 6 +++--
.../clang-tidy/tool/run-clang-tidy.py | 27 +++++++++++--------
clang-tools-extra/docs/ReleaseNotes.rst | 4 +++
.../infrastructure/quiet-flag-scripts.cpp | 24 +++++++++++++++++
4 files changed, 48 insertions(+), 13 deletions(-)
create mode 100644 clang-tools-extra/test/clang-tidy/infrastructure/quiet-flag-scripts.cpp
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 d7899e0a18d0c..bb012ceff442b 100755
--- a/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py
+++ b/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py
@@ -312,7 +312,8 @@ def main():
if max_task_count == 0:
max_task_count = multiprocessing.cpu_count()
max_task_count = min(len(lines_by_file), max_task_count)
- print(f"Running clang-tidy in {max_task_count} threads...")
+ if not args.quiet:
+ print(f"Running clang-tidy in {max_task_count} threads...")
combine_fixes = False
export_fixes_dir = None
@@ -408,7 +409,8 @@ def main():
return_code = 1
if combine_fixes:
- print("Writing fixes to " + args.export_fixes + " ...")
+ if not args.quiet:
+ print("Writing fixes to " + args.export_fixes + " ...")
try:
merge_replacement_files(export_fixes_dir, args.export_fixes)
except:
diff --git a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
index 670e0a2c7678a..69788b5b15b48 100755
--- a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
+++ b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
@@ -681,13 +681,14 @@ async def main() -> None:
file_name_re = re.compile("|".join(args.files))
files = {f for f in files if file_name_re.search(f)}
- print(
- f"Running clang-tidy in {max_task} threads for",
- len(files),
- "files out of",
- number_files_in_database,
- "in compilation database ...",
- )
+ if not args.quiet:
+ print(
+ f"Running clang-tidy in {max_task} threads for",
+ len(files),
+ "files out of",
+ number_files_in_database,
+ "in compilation database ...",
+ )
returncode = 0
semaphore = asyncio.Semaphore(max_task)
@@ -716,13 +717,15 @@ async def main() -> None:
result.stderr += f"{result.filename}: terminated by signal {-result.returncode}\n"
progress = f"[{i + 1: >{len(f'{len(files)}')}}/{len(files)}]"
runtime = f"[{result.elapsed:.1f}s]"
- print(f"{progress}{runtime} {' '.join(result.invocation)}")
+ if not args.quiet:
+ print(f"{progress}{runtime} {' '.join(result.invocation)}")
if result.stdout:
print(result.stdout, end=("" if result.stderr else "\n"))
if result.stderr:
print(result.stderr)
except asyncio.CancelledError:
- print("\nCtrl-C detected, goodbye.")
+ if not args.quiet:
+ print("\nCtrl-C detected, goodbye.")
for task in tasks:
task.cancel()
if delete_fixes_dir:
@@ -742,7 +745,8 @@ async def main() -> None:
print("No profiling data found.")
if combine_fixes:
- print(f"Writing fixes to {args.export_fixes} ...")
+ if not args.quiet:
+ print(f"Writing fixes to {args.export_fixes} ...")
try:
assert export_fixes_dir
merge_replacement_files(export_fixes_dir, args.export_fixes)
@@ -752,7 +756,8 @@ async def main() -> None:
returncode = 1
if args.fix:
- print("Applying fixes ...")
+ if not args.quiet:
+ print("Applying fixes ...")
try:
assert export_fixes_dir
apply_fixes(args, clang_apply_replacements_binary, export_fixes_dir)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 388979d9577ba..4473584e9fb26 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -122,6 +122,10 @@ Improvements to clang-tidy
- Improved :program:`clang-tidy` option `-quiet` by suppressing diagnostic
count messages.
+- Improved :program:`run-clang-tidy.py` and :program:`clang-tidy-diff.py`
+ scripts to respect the `-quiet` option by suppressing progress and
+ informational messages.
+
New checks
^^^^^^^^^^
diff --git a/clang-tools-extra/test/clang-tidy/infrastructure/quiet-flag-scripts.cpp b/clang-tools-extra/test/clang-tidy/infrastructure/quiet-flag-scripts.cpp
new file mode 100644
index 0000000000000..aec9356f3ad1f
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/infrastructure/quiet-flag-scripts.cpp
@@ -0,0 +1,24 @@
+// RUN: rm -rf %t
+// RUN: mkdir %t
+// RUN: echo "[{\"directory\":\".\",\"command\":\"clang++ -c %/t/test.cpp\",\"file\":\"%/t/test.cpp\"}]" | sed -e 's/\\/\\\\/g' > %t/compile_commands.json
+// RUN: echo "Checks: '-*,readability-magic-numbers'" > %t/.clang-tidy
+// RUN: cp "%s" "%t/test.cpp"
+// RUN: cd "%t"
+
+// RUN: %run_clang_tidy -quiet "test.cpp" 2>&1 | FileCheck %s --check-prefix=CHECK-RUN-QUIET
+// CHECK-RUN-QUIET-NOT: Running clang-tidy in {{[1-9][0-9]*}} threads for
+// CHECK-RUN-QUIET-NOT: {{[0-9]+}} warning{{s?}} generated
+// CHECK-RUN-QUIET-NOT: [1/1]
+// CHECK-RUN-QUIET: 42 is a magic number;
+
+// REQUIRES: shell
+// RUN: sed 's/42/99/' %s > %t-diff.cpp
+
+// RUN: not diff -U0 %s %t-diff.cpp | %clang_tidy_diff -checks=-*,readability-magic-numbers -quiet -- -std=c++11 2>&1 | FileCheck %s --check-prefix=CHECK-DIFF-QUIET
+// CHECK-DIFF-QUIET-NOT: Running clang-tidy in {{[1-9][0-9]*}} threads...
+// CHECK-DIFF-QUIET-NOT: {{[0-9]+}} warning{{s?}} generated
+// CHECK-DIFF-QUIET: 99 is a magic number;
+
+int main() {
+ int x = 42;
+}
>From 277f72995a4ea2fcb089dea660d9c8453489b704 Mon Sep 17 00:00:00 2001
From: Victor Baranov <bar.victor.2002 at gmail.com>
Date: Wed, 20 Aug 2025 18:04:37 +0300
Subject: [PATCH 2/2] use f-string for formatting
---
clang-tools-extra/clang-tidy/tool/run-clang-tidy.py | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
index 69788b5b15b48..9b7bf617b6eb9 100755
--- a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
+++ b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
@@ -683,11 +683,8 @@ async def main() -> None:
if not args.quiet:
print(
- f"Running clang-tidy in {max_task} threads for",
- len(files),
- "files out of",
- number_files_in_database,
- "in compilation database ...",
+ f"Running clang-tidy in {max_task} threads for {len(files)} files"
+ "out of {number_files_in_database} in compilation database ..."
)
returncode = 0
More information about the cfe-commits
mailing list