- Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathtool_clang_tidy.py
executable file
·96 lines (80 loc) · 3.81 KB
/
tool_clang_tidy.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
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
# Copyright (c) 2024, 2025, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is designed to work with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have either included with
# the program or referenced in the documentation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
importos
importsys
frommultiprocessing.poolimportThreadPool
fromstatic_analysis_incimporthelpers
# Performance tweaking
# How many source code files to pass to a single clang-tidy process.
# Large batch with large number of instances can end up using
# a lot of CPU and RAM.
TIDY_BATCH=2
TIDY_BIN=''
BUILD_PATH=''
defrun_tidy(files):
"""Execute clang-tidy on a batch of source code files, update progress"""
arg_files=' '.join(files)
# Advantages using clang-tidy directly vs using run-clang-tidy wrapper:
# - progress tracking
# - job control (how many parallel processes to use)
# - timeout control (kill stuck subprocesses)
# - better control of what files we process (filtering)
# - speedup: process more than one file with a single clang-tidy process (batching)
cmd=f"{TIDY_BIN} -extra-arg='-ferror-limit=0' -p {BUILD_PATH}{arg_files}"
# do not use --quiet mode to detect "no checks enabled"
out, err, rc=helpers.shell_execute(cmd)
ifnoterrornot'Error: no checks enabled'inerr:
print(out);
# try to avoid printing not so useful info like:
# 352679 warnings generated.
ifrc!=0anderrandnotoutandnot'Error: no checks enabled'inerr:
print(err, file=sys.stderr)
helpers.progress_update(len(files))
helpers.progress_report()
defscan_tree(clang_tidy_binary, repo_root, jobs_count, build_path, scan_root):
helpers.verbose_log(f"Scan source root folder: \'{repo_root}\'")
globalBUILD_PATH
BUILD_PATH=build_path
globalTIDY_BIN
TIDY_BIN=clang_tidy_binary
git_branch=helpers.git_branch_name()
helpers.verbose_log(f"Detected git branch: \'{git_branch}\'")
target_files=helpers.list_sources(build_path, scan_root)
helpers.progress_start(len(target_files))
helpers.progress_report()
# run static analysis with a thread pool
withThreadPool(jobs_count) aspool:
try:
pool.map(run_tidy, helpers.batched(target_files, TIDY_BATCH))
exceptKeyboardInterrupt:
print("\nScript aborted.", file=sys.stderr)
helpers.progress_done()
sys.exit(1)
helpers.progress_done()
defscan_commit(clang_tidy_diff_script, commit, jobs_count, build_path):
helpers.verbose_log(f"Scan commit: \'{commit}\'")
cmd=f"git diff-tree -p {commit} -U0 -- '*.cc' '*.cpp' '*.c++' '*.cxx' '*.c' '*.cl' '*.h' '*.hpp' '*.hh' '*.i' '*.ic' ':!*/extra/*' | python3 {clang_tidy_diff_script} -timeout 600 -path {build_path} -j={jobs_count} -p1 -extra-arg='-ferror-limit=0'"
out, err, rc=helpers.shell_execute(cmd)
ifrc!=0anderr:
print(err, file=sys.stderr)
print(out)