|
1 | 1 | import subprocess |
| 2 | +import sys |
2 | 3 | from argparse import ArgumentParser |
3 | | -from typing import Tuple |
| 4 | +from pathlib import Path |
| 5 | +from typing import Optional, Tuple |
4 | 6 |
|
5 | 7 | from cpp_linter_hooks.util import resolve_install, DEFAULT_CLANG_TIDY_VERSION |
6 | 8 |
|
| 9 | +COMPILE_DB_SEARCH_DIRS = ["build", "out", "cmake-build-debug", "_build"] |
7 | 10 |
|
8 | 11 | parser = ArgumentParser() |
9 | 12 | parser.add_argument("--version", default=DEFAULT_CLANG_TIDY_VERSION) |
| 13 | +parser.add_argument("--compile-commands", default=None, dest="compile_commands") |
| 14 | +parser.add_argument( |
| 15 | + "--no-compile-commands", action="store_true", dest="no_compile_commands" |
| 16 | +) |
| 17 | +parser.add_argument("-v", "--verbose", action="store_true") |
10 | 18 |
|
11 | 19 |
|
12 | | -def run_clang_tidy(args=None) -> Tuple[int, str]: |
13 | | - hook_args, other_args = parser.parse_known_args(args) |
14 | | - if hook_args.version: |
15 | | - resolve_install("clang-tidy", hook_args.version) |
16 | | - command = ["clang-tidy"] + other_args |
| 20 | +def _find_compile_commands() -> Optional[str]: |
| 21 | + for d in COMPILE_DB_SEARCH_DIRS: |
| 22 | + if (Path(d) / "compile_commands.json").exists(): |
| 23 | + return d |
| 24 | + return None |
| 25 | + |
| 26 | + |
| 27 | +def _resolve_compile_db( |
| 28 | + hook_args, other_args |
| 29 | +) -> Tuple[Optional[str], Optional[Tuple[int, str]]]: |
| 30 | + """Resolve the compile_commands.json directory to pass as -p to clang-tidy. |
| 31 | +
|
| 32 | + Returns (db_path, None) on success or (None, (retval, message)) on error. |
| 33 | + """ |
| 34 | + if hook_args.no_compile_commands: |
| 35 | + return None, None |
| 36 | + |
| 37 | + # Covers both "-p ./build" (two tokens) and "-p=./build" (one token) |
| 38 | + has_p = any(a == "-p" or a.startswith("-p=") for a in other_args) |
| 39 | + |
| 40 | + if hook_args.compile_commands: |
| 41 | + if has_p: |
| 42 | + print( |
| 43 | + "Warning: --compile-commands ignored; -p already in args", |
| 44 | + file=sys.stderr, |
| 45 | + ) |
| 46 | + return None, None |
| 47 | + p = Path(hook_args.compile_commands) |
| 48 | + if not p.is_dir() or not (p / "compile_commands.json").exists(): |
| 49 | + return None, ( |
| 50 | + 1, |
| 51 | + f"--compile-commands: no compile_commands.json" |
| 52 | + f" in '{hook_args.compile_commands}'", |
| 53 | + ) |
| 54 | + return hook_args.compile_commands, None |
| 55 | + |
| 56 | + if not has_p: |
| 57 | + return _find_compile_commands(), None |
| 58 | + |
| 59 | + return None, None |
17 | 60 |
|
18 | | - retval = 0 |
19 | | - output = "" |
| 61 | + |
| 62 | +def _exec_clang_tidy(command) -> Tuple[int, str]: |
| 63 | + """Run clang-tidy and return (retval, output).""" |
20 | 64 | try: |
21 | 65 | sp = subprocess.run( |
22 | 66 | command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8" |
23 | 67 | ) |
24 | | - retval = sp.returncode |
25 | 68 | output = (sp.stdout or "") + (sp.stderr or "") |
26 | | - if "warning:" in output or "error:" in output: |
27 | | - retval = 1 |
| 69 | + retval = ( |
| 70 | + 1 if sp.returncode != 0 or "warning:" in output or "error:" in output else 0 |
| 71 | + ) |
28 | 72 | return retval, output |
29 | | - except FileNotFoundError as stderr: |
30 | | - retval = 1 |
31 | | - return retval, str(stderr) |
| 73 | + except FileNotFoundError as e: |
| 74 | + return 1, str(e) |
| 75 | + |
| 76 | + |
| 77 | +def run_clang_tidy(args=None) -> Tuple[int, str]: |
| 78 | + hook_args, other_args = parser.parse_known_args(args) |
| 79 | + if hook_args.version: |
| 80 | + resolve_install("clang-tidy", hook_args.version) |
| 81 | + |
| 82 | + compile_db_path, error = _resolve_compile_db(hook_args, other_args) |
| 83 | + if error is not None: |
| 84 | + return error |
| 85 | + |
| 86 | + if compile_db_path: |
| 87 | + if hook_args.verbose: |
| 88 | + print( |
| 89 | + f"Using compile_commands.json from: {compile_db_path}", file=sys.stderr |
| 90 | + ) |
| 91 | + other_args = ["-p", compile_db_path] + other_args |
| 92 | + |
| 93 | + return _exec_clang_tidy(["clang-tidy"] + other_args) |
32 | 94 |
|
33 | 95 |
|
34 | 96 | def main() -> int: |
|
0 commit comments