Skip to content

Commit dc81dcf

Browse files
vbvictorlocalspook
andauthored
[clang-tidy] Add new '-hide-progress' option to tidy-scripts for suppressing progress information (#154416)
Progress information bloated output, now `run-clang-tidy` emits **only** actual diagnostics, which makes reading its output significantly easier. --------- Co-authored-by: Victor Chernyakin <[email protected]>
1 parent c062fa6 commit dc81dcf

File tree

4 files changed

+55
-13
lines changed

4 files changed

+55
-13
lines changed

clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ def main():
258258
help="Upgrades clang-tidy warnings to errors. Same format as '-checks'.",
259259
default="",
260260
)
261+
parser.add_argument(
262+
"-hide-progress",
263+
action="store_true",
264+
help="Hide progress",
265+
)
261266

262267
clang_tidy_args = []
263268
argv = sys.argv[1:]
@@ -312,7 +317,8 @@ def main():
312317
if max_task_count == 0:
313318
max_task_count = multiprocessing.cpu_count()
314319
max_task_count = min(len(lines_by_file), max_task_count)
315-
print(f"Running clang-tidy in {max_task_count} threads...")
320+
if not args.hide_progress:
321+
print(f"Running clang-tidy in {max_task_count} threads...")
316322

317323
combine_fixes = False
318324
export_fixes_dir = None
@@ -408,7 +414,8 @@ def main():
408414
return_code = 1
409415

410416
if combine_fixes:
411-
print("Writing fixes to " + args.export_fixes + " ...")
417+
if not args.hide_progress:
418+
print(f"Writing fixes to {args.export_fixes} ...")
412419
try:
413420
merge_replacement_files(export_fixes_dir, args.export_fixes)
414421
except:

clang-tools-extra/clang-tidy/tool/run-clang-tidy.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,11 @@ async def main() -> None:
576576
action="store_true",
577577
help="Enable per-check timing profiles, and print a report",
578578
)
579+
parser.add_argument(
580+
"-hide-progress",
581+
action="store_true",
582+
help="Hide progress",
583+
)
579584
args = parser.parse_args()
580585

581586
db_path = "compile_commands.json"
@@ -681,13 +686,11 @@ async def main() -> None:
681686
file_name_re = re.compile("|".join(args.files))
682687
files = {f for f in files if file_name_re.search(f)}
683688

684-
print(
685-
f"Running clang-tidy in {max_task} threads for",
686-
len(files),
687-
"files out of",
688-
number_files_in_database,
689-
"in compilation database ...",
690-
)
689+
if not args.hide_progress:
690+
print(
691+
f"Running clang-tidy in {max_task} threads for {len(files)} files "
692+
f"out of {number_files_in_database} in compilation database ..."
693+
)
691694

692695
returncode = 0
693696
semaphore = asyncio.Semaphore(max_task)
@@ -716,13 +719,15 @@ async def main() -> None:
716719
result.stderr += f"{result.filename}: terminated by signal {-result.returncode}\n"
717720
progress = f"[{i + 1: >{len(f'{len(files)}')}}/{len(files)}]"
718721
runtime = f"[{result.elapsed:.1f}s]"
719-
print(f"{progress}{runtime} {' '.join(result.invocation)}")
722+
if not args.hide_progress:
723+
print(f"{progress}{runtime} {' '.join(result.invocation)}")
720724
if result.stdout:
721725
print(result.stdout, end=("" if result.stderr else "\n"))
722726
if result.stderr:
723727
print(result.stderr)
724728
except asyncio.CancelledError:
725-
print("\nCtrl-C detected, goodbye.")
729+
if not args.hide_progress:
730+
print("\nCtrl-C detected, goodbye.")
726731
for task in tasks:
727732
task.cancel()
728733
if delete_fixes_dir:
@@ -742,7 +747,8 @@ async def main() -> None:
742747
print("No profiling data found.")
743748

744749
if combine_fixes:
745-
print(f"Writing fixes to {args.export_fixes} ...")
750+
if not args.hide_progress:
751+
print(f"Writing fixes to {args.export_fixes} ...")
746752
try:
747753
assert export_fixes_dir
748754
merge_replacement_files(export_fixes_dir, args.export_fixes)
@@ -752,7 +758,8 @@ async def main() -> None:
752758
returncode = 1
753759

754760
if args.fix:
755-
print("Applying fixes ...")
761+
if not args.hide_progress:
762+
print("Applying fixes ...")
756763
try:
757764
assert export_fixes_dir
758765
apply_fixes(args, clang_apply_replacements_binary, export_fixes_dir)

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ Improvements to clang-tidy
135135
:program:`clang-tidy-20`. Users should use the check-specific options of the
136136
same name instead.
137137

138+
- Improved :program:`run-clang-tidy.py` and :program:`clang-tidy-diff.py`
139+
scripts by adding the `-hide-progress` option to suppress progress and
140+
informational messages.
141+
138142
New checks
139143
^^^^^^^^^^
140144

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir %t
3+
// RUN: echo "[{\"directory\":\".\",\"command\":\"clang++ -c %/t/test.cpp\",\"file\":\"%/t/test.cpp\"}]" | sed -e 's/\\/\\\\/g' > %t/compile_commands.json
4+
// RUN: echo "Checks: '-*,readability-magic-numbers'" > %t/.clang-tidy
5+
// RUN: cp "%s" "%t/test.cpp"
6+
// RUN: cd "%t"
7+
8+
// RUN: %run_clang_tidy -quiet -hide-progress "test.cpp" 2>&1 | FileCheck %s --check-prefix=CHECK-RUN-QUIET
9+
// CHECK-RUN-QUIET-NOT: Running clang-tidy in {{[1-9][0-9]*}} threads for
10+
// CHECK-RUN-QUIET-NOT: {{[0-9]+}} warning{{s?}} generated
11+
// CHECK-RUN-QUIET-NOT: [1/1]
12+
// CHECK-RUN-QUIET: 42 is a magic number;
13+
14+
// REQUIRES: shell
15+
// RUN: sed 's/42/99/' %s > %t-diff.cpp
16+
17+
// RUN: not diff -U0 %s %t-diff.cpp | %clang_tidy_diff -checks=-*,readability-magic-numbers -quiet -hide-progress -- -std=c++11 2>&1 | FileCheck %s --check-prefix=CHECK-DIFF-QUIET
18+
// CHECK-DIFF-QUIET-NOT: Running clang-tidy in {{[1-9][0-9]*}} threads...
19+
// CHECK-DIFF-QUIET-NOT: {{[0-9]+}} warning{{s?}} generated
20+
// CHECK-DIFF-QUIET: 99 is a magic number;
21+
22+
int main() {
23+
int x = 42;
24+
}

0 commit comments

Comments
 (0)