ci(clang-tidy): analyse each source file once (#29019)

* ci(clang-tidy): analyse each source file once

* fix(clang-tidy): print a failure command that still reproduces

The failure line printed -p with the temporary deduplicated database, which is deleted when the script exits, so pasting the command failed to load a compilation database. Print the build's own database instead.

Assisted-by: Claude:claude-opus-5-5

* fix(clang-tidy): remove the temporary directories on every exit

The deduplicated database directory and the -fix export directory were only removed on the normal path, so an exception such as an invalid -exclude regex or a missing clang-apply-replacements left them in $TMPDIR. Ctrl-C keeps its explicit removal because it ends with SIGKILL, which skips atexit.

Assisted-by: Claude:claude-opus-5-5

---------

Co-authored-by: Jacob Dahl <dahl.jakejacob@gmail.com>
This commit is contained in:
Anil KircalialiandJacob Dahl authored and GitHub committed 2026-10-11 14:34:07 -06:00
1 parent e2d5029265
commit 58bdfc324f
1 file changed
+33 -7
+33 -7
View File
@@ -36,6 +36,7 @@ http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
from __future__ import print_function
import argparse
import atexit
import json
import multiprocessing
import os
@@ -65,7 +66,7 @@ def find_compilation_database(path):
def get_tidy_invocation(f, clang_tidy_binary, checks, tmpdir, build_path,
header_filter, extra_arg, extra_arg_before):
header_filter, extra_arg, extra_arg_before, db_dir):
"""Gets a command line for clang-tidy."""
start = [clang_tidy_binary]
if header_filter is not None:
@@ -86,7 +87,7 @@ def get_tidy_invocation(f, clang_tidy_binary, checks, tmpdir, build_path,
start.append('-extra-arg=%s' % arg)
for arg in extra_arg_before:
start.append('-extra-arg-before=%s' % arg)
start.append('-p=' + build_path)
start.append('-p=' + db_dir)
start.append(f)
return start
@@ -101,18 +102,23 @@ def apply_fixes(args, tmpdir):
shutil.rmtree(tmpdir)
def run_tidy(args, tmpdir, build_path, queue):
def run_tidy(args, tmpdir, build_path, db_dir, queue):
"""Takes filenames out of queue and runs clang-tidy on them."""
while True:
name = queue.get()
invocation = get_tidy_invocation(name, args.clang_tidy_binary, args.checks,
tmpdir, build_path, args.header_filter,
args.extra_arg, args.extra_arg_before)
args.extra_arg, args.extra_arg_before,
db_dir)
try:
subprocess.check_call(invocation, stdin=None, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
except subprocess.CalledProcessError as e:
sys.stdout.write(f'failed on {name} --> {" ".join(invocation)}\n')
# db_dir is deleted on exit, so print a command that still reproduces
# the failure afterwards
repro = ['-p=' + build_path if arg == '-p=' + db_dir else arg
for arg in invocation]
sys.stdout.write(f'failed on {name} --> {" ".join(repro)}\n')
# Now run again to see the actual output
subprocess.call(invocation, stdin=None)
global tidy_failures
@@ -182,7 +188,24 @@ def main():
# Load the database and extract all files.
database = json.load(open(os.path.join(build_path, db_path)))
files = [entry['file'] for entry in database]
# Keep one compile command per source file. A file built into several
# targets (eg ekf2 sources: modules__ekf2 and the ecl_EKF library) is
# otherwise analysed once per target inside a single clang-tidy process.
seen = set()
unique_entries = []
for entry in database:
if entry['file'] not in seen:
seen.add(entry['file'])
unique_entries.append(entry)
files = [entry['file'] for entry in unique_entries]
# clang-tidy reads the database itself, so hand it the deduplicated copy
db_dir = tempfile.mkdtemp()
atexit.register(shutil.rmtree, db_dir, ignore_errors=True)
with open(os.path.join(db_dir, db_path), 'w') as db_file:
json.dump(unique_entries, db_file)
max_task = args.j
if max_task == 0:
@@ -191,6 +214,7 @@ def main():
tmpdir = None
if args.fix:
tmpdir = tempfile.mkdtemp()
atexit.register(shutil.rmtree, tmpdir, ignore_errors=True)
# Build up a big regexy filter from all command line arguments.
file_name_re = re.compile('(' + ')|('.join(args.files) + ')')
@@ -201,7 +225,7 @@ def main():
queue = Queue.Queue(max_task)
for _ in range(max_task):
t = threading.Thread(target=run_tidy,
args=(args, tmpdir, build_path, queue))
args=(args, tmpdir, build_path, db_dir, queue))
t.daemon = True
t.start()
@@ -219,8 +243,10 @@ def main():
# This is a sad hack. Unfortunately subprocess goes
# bonkers with ctrl-c and we start forking merrily.
print('\nCtrl-C detected, goodbye.')
# SIGKILL skips the atexit cleanup
if args.fix:
shutil.rmtree(tmpdir)
shutil.rmtree(db_dir)
os.kill(0, 9)
if args.fix: