-
Notifications
You must be signed in to change notification settings - Fork 0
/
cppcheck.py
59 lines (42 loc) · 1.37 KB
/
cppcheck.py
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
import json
import multiprocessing
import shutil
import subprocess
import sys
import os
build_dir = sys.argv[1]
compile_commands_json = os.path.join(build_dir, "compile_commands.json")
if not os.path.exists(compile_commands_json):
sys.exit(f'error: compile_commands.json not found in {build_dir}')
print('Performing Cppcheck checks in directory:', build_dir)
cppcheck_cmd = shutil.which('cppcheck')
if not cppcheck_cmd:
sys.exit('error: unable to find cppcheck')
print('Found cppcheck:', cppcheck_cmd)
with open(compile_commands_json) as f:
json_doc = json.load(f)
# Remove entries that are irrelevant for checking.
def is_valid_entry(obj):
key = None
if 'output' in obj:
key = obj['output']
elif 'file' in obj:
key = obj['file']
if key is None:
return True
return ('_deps' not in key and
'cerlibTests.dir' not in key and
'embedded_files' not in key and
'stb.c' not in key)
json_doc = [obj for obj in json_doc if is_valid_entry(obj)]
output_filename = os.path.join(build_dir, "compile_commands_cppcheck.json")
with open(output_filename, "w") as f:
f.write(json.dumps(json_doc, indent=2))
num_jobs = multiprocessing.cpu_count()
subprocess.check_call([
cppcheck_cmd,
f'--project={output_filename}',
'--enable=warning',
'--std=c++20',
'-j', str(num_jobs)
], shell=False)