treefmt: Add meson/clang-tidy to the list of files checked by ruff

Change-Id: I3141286069844588a721cd4e523a284ef3a359c8
This commit is contained in:
Tom Hubrecht
2025-11-22 13:19:40 +01:00
parent b707403a30
commit 94ca4fd7b4
4 changed files with 101 additions and 73 deletions
+34 -15
View File
@@ -1,30 +1,49 @@
#!/usr/bin/env python3
import subprocess
def get_targets_of_rule(build_root: str, rule_name: str) -> list[str]:
return subprocess.check_output(['ninja', '-C', build_root, '-t', 'targets', 'rule', rule_name]).decode().strip().splitlines()
return (
subprocess.check_output(
["ninja", "-C", build_root, "-t", "targets", "rule", rule_name]
)
.decode()
.strip()
.splitlines()
)
def ninja_build(build_root: str, targets: list[str]):
subprocess.check_call(['ninja', '-C', build_root, '--', *targets])
subprocess.check_call(["ninja", "-C", build_root, "--", *targets])
def main():
import argparse
ap = argparse.ArgumentParser(description='Builds required targets for clang-tidy')
ap.add_argument('build_root', help='Ninja build root', type=str)
ap = argparse.ArgumentParser(description="Builds required targets for clang-tidy")
ap.add_argument("build_root", help="Ninja build root", type=str)
args = ap.parse_args()
targets = [
t for t in get_targets_of_rule(args.build_root, 'CUSTOM_COMMAND')
if t.endswith('.gen.hh')
] + [
t for t in get_targets_of_rule(args.build_root, 'CUSTOM_COMMAND_DEP')
if t.endswith('.capnp.h')
] + [
t for t in get_targets_of_rule(args.build_root, 'CUSTOM_COMMAND')
if t.endswith('.gen.inc')
]
targets = (
[
t
for t in get_targets_of_rule(args.build_root, "CUSTOM_COMMAND")
if t.endswith(".gen.hh")
]
+ [
t
for t in get_targets_of_rule(args.build_root, "CUSTOM_COMMAND_DEP")
if t.endswith(".capnp.h")
]
+ [
t
for t in get_targets_of_rule(args.build_root, "CUSTOM_COMMAND")
if t.endswith(".gen.inc")
]
)
ninja_build(args.build_root, targets)
if __name__ == '__main__':
if __name__ == "__main__":
main()
+52 -39
View File
@@ -12,40 +12,47 @@ because the run-clang-tidy UX is so questionable.
# https://github.com/mesonbuild/meson/issues/1564
import multiprocessing
import subprocess
import os
import sys
from pathlib import Path
def default_concurrency():
return min(multiprocessing.cpu_count(),
int(os.environ.get("NIX_BUILD_CORES", "16")))
return min(
multiprocessing.cpu_count(), int(os.environ.get("NIX_BUILD_CORES", "16"))
)
def go(exe: str, plugin_path: Path, compile_commands_json_dir: Path, jobs: int,
paths: list[Path], werror: bool, fix: bool):
def go(
exe: str,
plugin_path: Path,
compile_commands_json_dir: Path,
jobs: int,
paths: list[Path],
werror: bool,
fix: bool,
):
args = [
# XXX: This explicitly invokes it with python because of a nixpkgs bug
# where clang-unwrapped does not patch interpreters in run-clang-tidy.
# However, making clang-unwrapped depend on python is also silly, so idk.
sys.executable,
exe,
'-quiet',
'-load',
"-quiet",
"-load",
plugin_path,
'-p',
"-p",
compile_commands_json_dir,
'-j',
"-j",
str(jobs),
'-header-filter',
r'lix/[^/]+/.*\.hh'
"-header-filter",
r"lix/[^/]+/.*\.hh",
]
if werror:
args += ['-warnings-as-errors', '*']
args += ["-warnings-as-errors", "*"]
if fix:
args += ['-fix']
args += ['--']
args += ["-fix"]
args += ["--"]
args += paths
os.execvp(sys.executable, args)
@@ -53,37 +60,43 @@ def go(exe: str, plugin_path: Path, compile_commands_json_dir: Path, jobs: int,
def main():
import argparse
ap = argparse.ArgumentParser(description='Runs run-clang-tidy for you')
ap.add_argument('--jobs',
'-j',
type=int,
default=default_concurrency(),
help='Parallel linting jobs to run')
ap.add_argument('--plugin-path',
type=Path,
help='Path to the Lix clang-tidy plugin')
ap = argparse.ArgumentParser(description="Runs run-clang-tidy for you")
ap.add_argument(
"--jobs",
"-j",
type=int,
default=default_concurrency(),
help="Parallel linting jobs to run",
)
ap.add_argument(
"--plugin-path", type=Path, help="Path to the Lix clang-tidy plugin"
)
# FIXME: maybe we should integrate this so it just fixes the compdb for you and throws it in a tempdir?
ap.add_argument(
'--compdb-path',
"--compdb-path",
type=Path,
help=
'Path to the directory containing the fixed-up compilation database from clean_compdb'
help="Path to the directory containing the fixed-up compilation database from clean_compdb",
)
ap.add_argument('--werror',
action='store_true',
help='Warnings get turned into errors')
ap.add_argument('--fix',
action='store_true',
help='Apply fixes for warnings')
ap.add_argument('--run-clang-tidy-path',
default='run-clang-tidy',
help='Path to run-clang-tidy')
ap.add_argument('paths', nargs='*', help='Source paths to check')
ap.add_argument(
"--werror", action="store_true", help="Warnings get turned into errors"
)
ap.add_argument("--fix", action="store_true", help="Apply fixes for warnings")
ap.add_argument(
"--run-clang-tidy-path", default="run-clang-tidy", help="Path to run-clang-tidy"
)
ap.add_argument("paths", nargs="*", help="Source paths to check")
args = ap.parse_args()
go(args.run_clang_tidy_path, args.plugin_path, args.compdb_path, args.jobs,
args.paths, args.werror, args.fix)
go(
args.run_clang_tidy_path,
args.plugin_path,
args.compdb_path,
args.jobs,
args.paths,
args.werror,
args.fix,
)
if __name__ == '__main__':
if __name__ == "__main__":
main()
+13 -17
View File
@@ -8,15 +8,16 @@ import shlex
def process_compdb(compdb: list[dict]) -> list[dict]:
def munch_command(args: list[str]) -> list[str]:
out = []
eat_next = False
for i, arg in enumerate(args):
if arg in ['-fpch-preprocess', '-fpch-instantiate-templates']:
if arg in ["-fpch-preprocess", "-fpch-instantiate-templates"]:
# -fpch-preprocess as used with gcc, -fpch-instantiate-templates as used by clang
continue
elif arg == '-include-pch' or (arg == '-include' and args[i + 1] == 'precompiled-headers.hh'):
elif arg == "-include-pch" or (
arg == "-include" and args[i + 1] == "precompiled-headers.hh"
):
# -include-pch some-pch (clang), or -include some-pch (gcc)
eat_next = True
continue
@@ -27,34 +28,29 @@ def process_compdb(compdb: list[dict]) -> list[dict]:
def chomp(item: dict) -> dict:
item = item.copy()
item['command'] = shlex.join(munch_command(shlex.split(item['command'])))
item["command"] = shlex.join(munch_command(shlex.split(item["command"])))
return item
def cmdfilter(item: dict) -> bool:
file = item['file']
return (
not file.endswith('precompiled-headers.hh')
and not file.endswith('.rs')
)
file = item["file"]
return not file.endswith("precompiled-headers.hh") and not file.endswith(".rs")
return [chomp(x) for x in compdb if cmdfilter(x)]
def main():
import argparse
ap = argparse.ArgumentParser(
description='Delete pch arguments from compilation database')
ap.add_argument('input',
type=argparse.FileType('r'),
help='Input json file')
ap.add_argument('output',
type=argparse.FileType('w'),
help='Output json file')
description="Delete pch arguments from compilation database"
)
ap.add_argument("input", type=argparse.FileType("r"), help="Input json file")
ap.add_argument("output", type=argparse.FileType("w"), help="Output json file")
args = ap.parse_args()
input_json = json.load(args.input)
json.dump(process_compdb(input_json), args.output, indent=2)
if __name__ == '__main__':
if __name__ == "__main__":
main()
+2 -2
View File
@@ -6,11 +6,11 @@ excludes = ["tests/**"]
[formatter.ruff-format]
command = "ruff"
options = ["format"]
includes = ["tests/functional2/**/*.py"]
includes = ["tests/functional2/**/*.py", "meson/clang-tidy/*.py"]
priority = 0
[formatter.ruff]
command = "ruff"
options = ["check"]
includes = ["tests/functional2/**/*.py"]
includes = ["tests/functional2/**/*.py", "meson/clang-tidy/*.py"]
priority = 1