-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathcheck-code.py
More file actions
384 lines (297 loc) · 11 KB
/
check-code.py
File metadata and controls
384 lines (297 loc) · 11 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/python3
# SPDX-License-Identifier: Apache-2.0
# Copyright (C) ifm electronic gmbh
#
# THE PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
# Provides functions to format C/C++ files using clang-format
import argparse
from collections.abc import Generator
import functools
import json
import multiprocessing
import os
import re
import shutil
import subprocess
import sys
from typing import Any
assert sys.version_info > (3, 5), "python 3.5 or more required"
MAX_PARALLEL_PROCESSES = 10
SUPPORTED_TOOL_VERSIONS = {
"clang_format": 14,
"clang_tidy": 20,
"cppcheck": 2,
}
ENABLED_MODULES_LINT = [
"BUILD_MODULE_PCICCLIENT",
"BUILD_TESTS",
]
# Extension and folders to be excludes from formatting
APPLY_EXTENSIONS = (".cxx", ".cpp", ".c", ".hxx", ".hh", ".cc", ".hpp", ".h")
EXCLUDES = list([re.compile(p) for p in [r".*/_deps/.*"]])
VALID_ROOTS = [os.path.abspath(os.getcwd())]
def run_clang_format(file: str, args: dict[str, Any]) -> bool:
cmd = [args["clang_format"], "-i", "-style=file"]
if not args["fix"]:
cmd.append("--dry-run")
cmd.append("--Werror")
cmd.append(file)
return subprocess.call(cmd) == 0
def run_cppcheck(args: dict[str, Any]) -> bool:
cmd = ["cppcheck"]
if "build_dir" in args:
cmd.append(f"--project={args['build_dir']}/compile_commands.json")
cmd.append(f"--cppcheck-build-dir={args['build_dir']}/cppcheck")
cmd.append(f"--suppress=*:{args['build_dir']}/*")
cmd.append(f"--output-file={args['build_dir']}/cppcheck.xml")
os.makedirs(os.path.join(args["build_dir"], "cppcheck"), exist_ok=True)
cmd.append(f"--suppress=*:/usr/*")
cmd.extend(
[
"--std=c++17",
"--xml",
"--error-exitcode=1",
"-j",
str(MAX_PARALLEL_PROCESSES),
f"-D{"_WIN32" if sys.platform == "nt" else "__unix__"}",
]
)
return subprocess.call(cmd) == 0
def include_file(file: str) -> bool:
if not any([os.path.abspath(file).startswith(root) for root in VALID_ROOTS]):
return False
# check for file extension
if not file.endswith(APPLY_EXTENSIONS):
return False
# Check for excludes
if any([exclude.match(file) for exclude in EXCLUDES]):
return False
return True
def glob_files(path: str) -> list[str]:
filelist = set()
for root, _, files in os.walk(os.path.relpath(path, os.getcwd())):
for file in files:
path = os.path.join(root, file)
if include_file(file):
filelist.add(path)
return list(filelist)
def get_compile_db_files(compile_db: str) -> list[str]:
with open(compile_db, "r") as f:
compile_db = json.loads(f.read())
filelist = set()
for entry in compile_db:
file_path: str = entry["file"]
rel_path: str = os.path.relpath(file_path, os.getcwd())
if include_file(rel_path):
filelist.add(rel_path)
return list(filelist)
def filter_files(
files: list[str], files_to_check: list[str]
) -> Generator[str, None, None]:
if not files_to_check:
for f in files:
yield f
for f1 in files_to_check:
for f2 in files:
if os.path.abspath(f1) == os.path.abspath(f2):
yield f2
break
def process_files(files: list[str], func: str, args: dict[str, Any]) -> bool:
with multiprocessing.Pool(MAX_PARALLEL_PROCESSES) as p:
try:
ok = True
for i, result in enumerate(
p.imap(functools.partial(globals()[func], args=args), files)
):
ok = ok and result
print(
f"[{i+1}/{len(files)}] {files[i]}: {'\x1b[6;32mOK\x1b[0m' if result else '\x1b[6;31mFAIL\x1b[0m'}",
flush=True,
file=sys.stderr,
)
return ok
except Exception as e:
print("Error while processing files", e, flush=True, file=sys.stderr)
return False
def check_clang_version(tool_path: str, supported_version: int) -> None:
"""
Function checks the version of a tool
"""
try:
if not shutil.which(tool_path):
print("Unable to find tool executable")
ret = os.popen("{} --version".format(tool_path)).read()
match = re.search(
r"(?P<application>.*?) version (?P<version_major>[0-9]+)(?:\.(?P<version_minor>[0-9]+))?(?:\.(?P<version_patch>[0-9]+))?(?P<suffix>.*)",
ret,
)
if not match:
print("Unable to check tool version")
exit(1)
version_major = int(match.group("version_major"))
if version_major != supported_version:
print(
f"Unsupported tool version: {version_major} expected {supported_version}"
)
exit(1)
except Exception as e:
print("Error while checking tool version", e)
exit(1)
def check_cppcheck_version(tool_path: str, supported_version: int) -> None:
"""
Function checks the version of cppcheck
"""
try:
if not shutil.which(tool_path):
print("Unable to find tool executable")
ret = os.popen("{} --version".format(tool_path)).read()
match = re.search(
r"Cppcheck (?P<version_major>[0-9]+)(?:\.(?P<version_minor>[0-9]+))?(?:\.(?P<version_patch>[0-9]+))?(?P<suffix>.*)",
ret,
)
if not match:
print("Unable to check tool version")
exit(1)
version_major = int(match.group("version_major"))
if version_major != supported_version:
print(
f"Unsupported tool version: {version_major} expected {supported_version}"
)
exit(1)
except Exception as e:
print("Error while checking tool version", e)
exit(1)
def check_compile_db(build_dir) -> bool:
compile_db = os.path.join(build_dir, "compile_commands.json")
if not os.path.exists(compile_db):
print(
f"Unable to find compile_commands.json in {build_dir}. Please make sure to run cmake before calling this script."
)
exit(1)
def command_clang_tidy(args: dict[str, Any]) -> bool:
check_clang_version(args["clang_tidy"], SUPPORTED_TOOL_VERSIONS["clang_tidy"])
source_path = os.getcwd()
build_path = os.path.realpath(args["build_dir"])
clang_tidy_cmd = ["ctcache"] if not args["no_cache"] else []
clang_tidy_cmd.append(args["clang_tidy"])
clang_tidy_cmd.append("--allow-no-checks")
clang_tidy_cmd.append(f"--exclude-header-filter=^{build_path}/_deps/.*")
clang_tidy_cmd.append("--quiet")
clang_tidy_cmd.append("--use-color")
configure_cmd = [
"cmake",
"-S",
source_path,
"-B",
build_path,
f"-DCMAKE_CXX_CLANG_TIDY={';'.join(clang_tidy_cmd)}",
"-DVERIFY_INTERFACE_HEADER_SETS=ON",
] + [f"-D{mod}=ON" for mod in ENABLED_MODULES_LINT]
clean_cmd = ["cmake", "--build", build_path, "--target", "clean"]
lint_cmd = ["cmake", "--build", build_path, "-j", str(MAX_PARALLEL_PROCESSES)]
print("Running: ", " ".join(configure_cmd))
p = subprocess.run(configure_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
if p.returncode != 0:
print(p.stdout.decode("utf-8"))
return False
if not args["no_clean"]:
print("Running: ", " ".join(clean_cmd))
p = subprocess.run(clean_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
if p.returncode != 0:
print(p.stdout.decode("utf-8"))
return False
print("Running: ", " ".join(lint_cmd))
p = subprocess.run(
lint_cmd, env={"CTCACHE_STRIP": f"{build_path}:{source_path}", **os.environ}
)
if p.returncode != 0:
return False
return True
def command_clang_format(args: dict[str, Any]) -> bool:
check_clang_version(args["clang_format"], SUPPORTED_TOOL_VERSIONS["clang_format"])
files = glob_files(args["path"])
files = list(filter_files(files, [x[0] for x in args["file"]]))
return process_files(files, "run_clang_format", args)
def command_cppcheck(args: dict[str, Any]) -> bool:
check_cppcheck_version(args["cppcheck"], SUPPORTED_TOOL_VERSIONS["cppcheck"])
check_compile_db(args["build_dir"])
return run_cppcheck(args)
# entry point
if __name__ == "__main__":
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="command", help="Available commands")
cmd_clang_tidy = subparsers.add_parser(
"clang-tidy", help="Analyze the code using clang-tidy"
)
cmd_clang_tidy.add_argument(
"--clang-tidy",
help="Path to clang-tidy executable",
default=f"clang-tidy-{SUPPORTED_TOOL_VERSIONS['clang_tidy']}",
)
cmd_clang_tidy.add_argument(
"--build-dir", help="Path to the build directory", default="build_lint"
)
cmd_clang_tidy.add_argument(
"--no-clean",
help="Don't clean the build directory before checking, can be used to speed up rechecking while working on fixes",
action="store_true",
)
cmd_clang_tidy.add_argument(
"--no-cache",
help="Disables ctcache",
action="store_true",
)
cmd_cppcheck = subparsers.add_parser(
"cppcheck", help="Analyze the code using cppcheck"
)
cmd_cppcheck.add_argument(
"--cppcheck", help="Path to cppcheck executable", default="cppcheck"
)
cmd_cppcheck.add_argument(
"--build-dir", help="Path to the build directory", default="build"
)
cmd_clang_tidy.add_argument(
"--file",
help="Path to a single file to check, can be repeated for multiple files",
default=[],
action="append",
nargs="*",
)
cmd_clang_format = subparsers.add_parser(
"clang-format", help="Check formatting using clang-format"
)
cmd_clang_format.add_argument(
"--clang-format",
help="Path to clang-format executable",
default=f"clang-format-{SUPPORTED_TOOL_VERSIONS['clang_format']}",
)
cmd_clang_format.add_argument(
"--path", help="Path to the source code", default="modules"
)
cmd_clang_format.add_argument(
"--fix", help="Apply formatting to the code", action="store_true"
)
cmd_clang_format.add_argument(
"--file",
help="Path to a single file to check, can be repeated for multiple files",
default=[],
action="append",
nargs="*",
)
args = vars(parser.parse_args())
if "build_dir" in args:
VALID_ROOTS.append(os.path.abspath(args["build_dir"]))
if args["command"] is None:
parser.print_help()
quit(0)
cmd = f"command_{args["command"].replace("-", "_")}"
if cmd not in globals():
print(f"Unknown command: {args['command']}")
parser.print_help()
quit(1)
ok = globals()[f"command_{args["command"].replace("-", "_")}"](args)
if ok:
print("\x1b[6;32m" + "All OK!" + "\x1b[0m")
else:
print("\x1b[6;31m" + "FAILED!" + "\x1b[0m")
quit(0 if ok else 1)