Files
lix/meson/clang-tidy/clean_compdb.py
T
Commentator2.0 f65b83df3b treefmt/ruff: extract ruff config from f2 to global state
So far, our ruff config was confined to the f2 package. This meant, that
when one added additional paths to the ruff formatter, those wouldn't
get the same rules applied as f2, resulting in inconsistent styling
thoughout the project.

Due to how configs are resolved, only the "closesed" pyproject toml is
considered. This means, we need to tell f2 to extend its configuration
with the base level one. Though no change is required for other parts of
the project, as long as they don't have their own pyproject.toml

Change-Id: I145c764e7b850194020b5560e1025f4aa80411ae
2025-12-05 13:45:47 +00:00

54 lines
1.8 KiB
Python
Executable File

#!/usr/bin/env python3
# Deletes the PCH arguments from a compilation database, to workaround nixpkgs
# stdenv having a cc-wrapper that is impossible to use for anything except cc
# itself, for example, clang-tidy.
import json
import shlex
import argparse
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"]:
# -fpch-preprocess as used with gcc, -fpch-instantiate-templates as used by clang
continue
if 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
if not eat_next:
out.append(arg)
eat_next = False
return out
def chomp(item: dict) -> dict:
item = item.copy()
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")
return [chomp(x) for x in compdb if cmdfilter(x)]
def main():
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")
args = ap.parse_args()
input_json = json.load(args.input)
json.dump(process_compdb(input_json), args.output, indent=2)
if __name__ == "__main__":
main()