Skip to content

Commit

Permalink
Compute files to run clang-tidy on from compile_commands file
Browse files Browse the repository at this point in the history
  • Loading branch information
jbcoe committed Feb 14, 2025
1 parent 10bc211 commit 1264827
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/clang-tidy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ jobs:
- name: clang-tidy
run: |
python scripts/reduce_compile_commands.py build/compile_commands.json > compile_commands.json
clang-tidy -warnings-as-errors="*" -p compile_commands.json \
indirect.cc \
indirect_test.cc \
polymorphic.cc \
polymorphic_test.cc
PROJECT_SOURCE_FILES="$(python scripts/reduce_compile_commands.py build/compile_commands.json \
--names-only \
--exclude_dirs build exploration compile_checks)"
clang-tidy -warnings-as-errors="*" -p compile_commands.json "$PROJECT_SOURCE_FILES"
26 changes: 23 additions & 3 deletions scripts/reduce_compile_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

import argparse
import json
import os
import re

_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def main(argv: list[str] | None = None) -> None:
Expand All @@ -18,22 +22,38 @@ def main(argv: list[str] | None = None) -> None:
"input",
help="Input compile_commands.json file",
)
parser.add_argument(
"--names-only",
help="Only output a list of unique source files",
action="store_true",
)
parser.add_argument(
"--exclude_dirs",
help="Exclude files in any of the listed directories (relative to the project root)",
nargs="+",
)

args = parser.parse_args(argv)
args.exclude_dirs = args.exclude_dirs or []
args.exclude_dirs = [os.path.join(_ROOT, d) for d in args.exclude_dirs]

with open(args.input, "r") as f:
compile_commands = json.load(f)

compile_commands_by_file = {}
for compile_command in compile_commands:
file = compile_command["file"]
if "/_deps/" in file:
file: str = compile_command["file"]
if any(file.startswith(d) for d in args.exclude_dirs):
continue
if file in compile_commands:
continue
compile_commands_by_file[file] = compile_command

print(json.dumps(list(compile_commands_by_file.values()), indent=2))
if args.names_only:
paths = [os.path.relpath(p, _ROOT) for p in compile_commands_by_file.keys()]
print(" ".join(paths))
else:
print(json.dumps(list(compile_commands_by_file.values()), indent=2))


if __name__ == "__main__":
Expand Down

0 comments on commit 1264827

Please sign in to comment.