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