-
Notifications
You must be signed in to change notification settings - Fork 0
207 lines (176 loc) · 8.55 KB
/
cppcheck.yml
File metadata and controls
207 lines (176 loc) · 8.55 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
name: check Workflow
on:
workflow_call:
inputs:
platform:
required: true
type: string
default: ubuntu
jobs:
check:
runs-on: ubuntu-22.04
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: set up python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Set up dependencies
run: |
wget -O- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo add-apt-repository "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-15 main"
sudo apt-get update
sudo apt purge clang-format-14 -y
sudo apt-get install cppcheck git clang-format-15 python3-autopep8 -y
sudo ln -sf $(which clang-format-15) /usr/local/bin/clang-format
- name: generate cppcheck.sh
run: |
echo "generate /tmp/cppcheck.sh ..."
cat << EOF >/tmp/cppcheck.sh
#!/bin/bash
echo "Running check.sh"
default_ignore=(
"build/"
".*_test.cc"
\${CPPCHECK_IGNORE[@]}
)
echo "default_ignore is : \${default_ignore[@]}"
# 构造忽略参数
ignore_args=""
for ignore in \${default_ignore[@]}; do
# 判断是否为目录
if [ -d "\$ignore" ]; then
ignore_args="-i\$ignore \$ignore_args"
else
files=\$(find . ! -path '*/build/*' -regex "\$ignore" -type f)
for file in \$files; do
ignore_args="-i\$file \$ignore_args"
done
fi
done
# 如果当前目录下没有cc或者cpp或者cxx或者c文件,则直接退出,不需要进行cppcheck检查
if [ -z "\$(find . -name "*.cc" -o -name "*.cpp" -o -name "*.cxx" -o -name "*.c")" ]; then
echo "no cc or cpp or cxx files, not need to cppcheck"
exit 0
fi
echo "check cmd is :"
echo "cppcheck . --enable=warning,style,performance,portability,missingInclude --xml \$ignore_args"
cppcheck . --enable=warning,style,performance,portability,missingInclude --xml \$ignore_args 2>/tmp/cppcheck.xml
# 如果cppcheck检查出错,直接退出
if [ \$? -ne 0 ]; then
echo "cppcheck failed"
exit -1
fi
- name: generate cppcheck_analyze.py
run: |
echo "generate /tmp/cppcheck_analyze.py ..."
cat << EOF >/tmp/cppcheck_analyze.py
#!/usr/bin/env python3
import os
import sys
import xml.dom.minidom as minidom
#cppcheck 所有的错误类型的严重性
severityList=['information', 'performance', 'style', 'portability', 'warning', 'error']
informationSeverityCount = 0
performanceSeverityCount = 0
styleSeverityCount = 0
portabilitySeverityCount = 0
warningSeverityCount = 0
errorSeverityCount = 0
# 如果 /tmp/cppcheck.xml 文件不存在,则直接退出
if not os.path.exists('/tmp/cppcheck.xml'):
print("/tmp/cppcheck.xml not exists")
sys.exit(0)
# 打开cppcheck输出文件
with open('/tmp/cppcheck.xml', 'r') as file:
# 解析XML文件
dom = minidom.parse(file)
# 获取所有的error元素
errors = dom.getElementsByTagName('error')
for error in errors:
# 分别统计每种严重性的错误数
for i in range(len(severityList)):
if error.getAttribute('severity') == severityList[i]:
if i == 0:
informationSeverityCount += 1
elif i == 1:
performanceSeverityCount += 1
elif i == 2:
styleSeverityCount += 1
elif i == 3:
portabilitySeverityCount += 1
elif i == 4:
warningSeverityCount += 1
elif i == 5:
# 如果是syntaxError则不计入errorSeverityCount
if error.getAttribute('id') == 'syntaxError':
continue
# 如果是internalAstError则不计入errorSeverityCount
if error.getAttribute('id') == 'internalAstError':
continue
# 如果是internalAstError则不计入errorSeverityCount
if error.getAttribute('id') == 'unknownMacro':
continue
# 如 果msg中包含"Syntax"则不计入errorSeverityCount
if error.getAttribute('msg').find("Syntax") != -1:
continue
errorSeverityCount += 1
print(error.getAttribute('msg'))
# 打印结果
# print(severityList)
print("informationSeverityCount: ", informationSeverityCount)
print("performanceSeverityCount: ", performanceSeverityCount)
print("styleSeverityCount: ", styleSeverityCount)
print("portabilitySeverityCount: ", portabilitySeverityCount)
print("warningSeverityCount: ", warningSeverityCount)
print("errorSeverityCount: ", errorSeverityCount)
# 获取环境变量,如果超过阈值则返回-1,否则返回0
# 获取环境变量
informationSeverityMax = os.environ.get('INFORMATION_SEVERITY_MAX')
performanceSeverityMax = os.environ.get('PERFORMANCE_SEVERITY_MAX')
styleSeverityMax = os.environ.get('STYLE_SEVERITY_MAX')
portabilitySeverityMax = os.environ.get('PORTABILITY_SEVERITY_MAX')
warningSeverityMax = os.environ.get('WARNING_SEVERITY_MAX')
errorSeverityMax = os.environ.get('ERROR_SEVERITY_MAX')
# 如果环境变量不存在则不做处理
if informationSeverityMax != None:
if informationSeverityCount >= int(informationSeverityMax):
print("informationSeverityCount[{}] >= informationSeverityMax[{}]".format(informationSeverityCount, informationSeverityMax))
sys.exit(-1)
if performanceSeverityMax != None:
if performanceSeverityCount >= int(performanceSeverityMax):
print("performanceSeverityCount[{}] >= performanceSeverityMax[{}]".format(performanceSeverityCount, performanceSeverityMax))
sys.exit(-1)
if styleSeverityMax != None:
if styleSeverityCount >= int(styleSeverityMax):
print("styleSeverityCount[{}] >= styleSeverityMax[{}]".format(styleSeverityCount, styleSeverityMax))
sys.exit(-1)
if portabilitySeverityMax != None:
if portabilitySeverityCount >= int(portabilitySeverityMax):
print("portabilitySeverityCount[{}] >= portabilitySeverityMax[{}]".format(portabilitySeverityCount, portabilitySeverityMax))
sys.exit(-1)
if warningSeverityMax != None:
if warningSeverityCount >= int(warningSeverityMax):
print("warningSeverityCount[{}] >= warningSeverityMax[{}]".format(warningSeverityCount, warningSeverityMax))
sys.exit(-1)
if errorSeverityMax != None:
if errorSeverityCount >= int(errorSeverityMax):
print("errorSeverityCount[{}] >= errorSeverityMax[{}]".format(errorSeverityCount, errorSeverityMax))
sys.exit(-1)
EOF
- name: run cppcheck
run: |
chmod +x /tmp/cppcheck.sh
chmod +x /tmp/cppcheck_analyze.py
/tmp/cppcheck.sh
/tmp/cppcheck_analyze.py
- name: run format
run: |
./format.sh
git config --global --add safe.directory '*'
git status
if [ -n "$(git status --porcelain)" ]; then
echo "check ${{ github.ref_name }} failed"
exit 1
fi