@@ -24,56 +24,67 @@ def _find_compile_commands() -> Optional[str]:
2424 return None
2525
2626
27- def run_clang_tidy (args = None ) -> Tuple [int , str ]:
28- hook_args , other_args = parser .parse_known_args (args )
29- if hook_args .version :
30- resolve_install ("clang-tidy" , hook_args .version )
27+ def _resolve_compile_db (hook_args , other_args ) -> Tuple [Optional [str ], Optional [Tuple [int , str ]]]:
28+ """Resolve the compile_commands.json directory to pass as -p to clang-tidy.
29+
30+ Returns (db_path, None) on success or (None, (retval, message)) on error.
31+ """
32+ if hook_args .no_compile_commands :
33+ return None , None
3134
3235 # Covers both "-p ./build" (two tokens) and "-p=./build" (one token)
3336 has_p = any (a == "-p" or a .startswith ("-p=" ) for a in other_args )
3437
35- compile_db_path = None
36- if not hook_args .no_compile_commands :
37- if hook_args .compile_commands :
38- if has_p :
39- print (
40- "Warning: --compile-commands ignored; -p already in args" ,
41- file = sys .stderr ,
42- )
43- else :
44- p = Path (hook_args .compile_commands )
45- if not p .is_dir () or not (p / "compile_commands.json" ).exists ():
46- return 1 , (
47- f"--compile-commands: no compile_commands.json"
48- f" in '{ hook_args .compile_commands } '"
49- )
50- compile_db_path = hook_args .compile_commands
51- elif not has_p :
52- compile_db_path = _find_compile_commands ()
53-
54- if compile_db_path :
55- if hook_args .verbose :
38+ if hook_args .compile_commands :
39+ if has_p :
5640 print (
57- f"Using compile_commands.json from: { compile_db_path } " , file = sys .stderr
41+ "Warning: --compile-commands ignored; -p already in args" ,
42+ file = sys .stderr ,
5843 )
59- other_args = ["-p" , compile_db_path ] + other_args
44+ return None , None
45+ p = Path (hook_args .compile_commands )
46+ if not p .is_dir () or not (p / "compile_commands.json" ).exists ():
47+ return None , (
48+ 1 ,
49+ f"--compile-commands: no compile_commands.json"
50+ f" in '{ hook_args .compile_commands } '" ,
51+ )
52+ return hook_args .compile_commands , None
53+
54+ if not has_p :
55+ return _find_compile_commands (), None
56+
57+ return None , None
6058
61- command = ["clang-tidy" ] + other_args
6259
63- retval = 0
64- output = ""
60+ def _exec_clang_tidy ( command ) -> Tuple [ int , str ]:
61+ """Run clang-tidy and return (retval, output)." ""
6562 try :
6663 sp = subprocess .run (
6764 command , stdout = subprocess .PIPE , stderr = subprocess .PIPE , encoding = "utf-8"
6865 )
69- retval = sp .returncode
7066 output = (sp .stdout or "" ) + (sp .stderr or "" )
71- if "warning:" in output or "error:" in output :
72- retval = 1
67+ retval = 1 if sp .returncode != 0 or "warning:" in output or "error:" in output else 0
7368 return retval , output
74- except FileNotFoundError as stderr :
75- retval = 1
76- return retval , str (stderr )
69+ except FileNotFoundError as e :
70+ return 1 , str (e )
71+
72+
73+ def run_clang_tidy (args = None ) -> Tuple [int , str ]:
74+ hook_args , other_args = parser .parse_known_args (args )
75+ if hook_args .version :
76+ resolve_install ("clang-tidy" , hook_args .version )
77+
78+ compile_db_path , error = _resolve_compile_db (hook_args , other_args )
79+ if error is not None :
80+ return error
81+
82+ if compile_db_path :
83+ if hook_args .verbose :
84+ print (f"Using compile_commands.json from: { compile_db_path } " , file = sys .stderr )
85+ other_args = ["-p" , compile_db_path ] + other_args
86+
87+ return _exec_clang_tidy (["clang-tidy" ] + other_args )
7788
7889
7990def main () -> int :
0 commit comments