forked from cppcheck-opensource/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhole-program_test.py
More file actions
297 lines (226 loc) · 8.81 KB
/
Copy pathwhole-program_test.py
File metadata and controls
297 lines (226 loc) · 8.81 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
import os
import pytest
import json
from testutils import cppcheck
__script_dir = os.path.dirname(os.path.abspath(__file__))
# TODO: use dedicated addon
# TODO: test CheckNullPointer
# TODO: test CheckUninitVar
# TODO: test CheckBufferOverrun
def __create_compile_commands(dir, entries):
j = []
for e in entries:
f = os.path.basename(e)
obj = {
'directory': os.path.dirname(os.path.abspath(e)),
'command': 'gcc -c {}'.format(f),
'file': f
}
j.append(obj)
compile_commands = os.path.join(dir, 'compile_commmands.json')
with open(compile_commands, 'wt') as f:
f.write(json.dumps(j))
return compile_commands
def __test_addon_suppress_inline(extra_args):
args = [
'-q',
'--addon=misra',
'--template=simple',
'--enable=information,style',
'--disable=missingInclude', # TODO: remove
'--inline-suppr',
'--error-exitcode=1',
'whole-program/whole1.c',
'whole-program/whole2.c'
]
args += extra_args
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
lines = stderr.splitlines()
assert lines == []
assert stdout == ''
assert ret == 0, stdout
def test_addon_suppress_inline():
__test_addon_suppress_inline(['-j1'])
# TODO: inline suppressions currently do not work with whole program analysis and addons - see #12835
# whole program analysis requires a build dir with -j
@pytest.mark.xfail(strict=True)
def test_addon_suppress_inline_j():
__test_addon_suppress_inline(['-j2'])
def test_addon_suppress_inline_builddir(tmpdir):
build_dir = os.path.join(tmpdir, 'b1')
os.mkdir(build_dir)
__test_addon_suppress_inline(['-j1', '--cppcheck-build-dir={}'.format(build_dir)])
# TODO: inline suppressions currently do not work with whole program analysis and addons - see #12835
@pytest.mark.xfail(strict=True)
def test_addon_suppress_inline_builddir_j(tmpdir):
build_dir = os.path.join(tmpdir, 'b1')
os.mkdir(build_dir)
__test_addon_suppress_inline(['-j2', '--cppcheck-build-dir={}'.format(build_dir)])
def __test_addon_suppress_inline_project(tmpdir, extra_args):
compile_db = __create_compile_commands(tmpdir, [
os.path.join(__script_dir, 'whole-program', 'whole1.c'),
os.path.join(__script_dir, 'whole-program', 'whole2.c')
])
args = [
'-q',
'--addon=misra',
'--template=simple',
'--enable=information,style',
'--disable=missingInclude', # TODO: remove
'--inline-suppr',
'--error-exitcode=1',
'--project={}'.format(compile_db)
]
args += extra_args
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
lines = stderr.splitlines()
assert lines == []
assert stdout == ''
assert ret == 0, stdout
def test_addon_suppress_inline_project(tmpdir):
__test_addon_suppress_inline_project(tmpdir, ['-j1'])
# TODO: inline suppressions currently do not work with whole program analysis and addons - see #12835
# whole program analysis requires a build dir with -j
@pytest.mark.xfail(strict=True)
def test_addon_suppress_inline_project_j(tmpdir):
__test_addon_suppress_inline_project(tmpdir, ['-j2'])
def test_addon_suppress_inline_project_builddir(tmpdir):
build_dir = os.path.join(tmpdir, 'b1')
os.mkdir(build_dir)
__test_addon_suppress_inline_project(tmpdir, ['-j1', '--cppcheck-build-dir={}'.format(build_dir)])
# TODO: inline suppressions currently do not work with whole program analysis and addons - see #12835
@pytest.mark.xfail(strict=True)
def test_addon_suppress_inline_project_builddir_j(tmpdir):
build_dir = os.path.join(tmpdir, 'b1')
os.mkdir(build_dir)
__test_addon_suppress_inline_project(tmpdir, ['-j2', '--cppcheck-build-dir={}'.format(build_dir)])
def __test_suppress_inline(extra_args):
args = [
'-q',
'--template=simple',
'--enable=information,style',
'--disable=missingInclude', # TODO: remove
'--inline-suppr',
'--error-exitcode=1',
'whole-program/odr1.cpp',
'whole-program/odr2.cpp'
]
args += extra_args
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
lines = stderr.splitlines()
assert lines == []
assert stdout == ''
assert ret == 0, stdout
def test_suppress_inline():
__test_suppress_inline(['-j1'])
# TODO: inline suppressions do not work with whole program analysis and -j
# whole program analysis requires a build dir with -j
@pytest.mark.xfail(strict=True)
def test_suppress_inline_j():
__test_suppress_inline(['-j2'])
def test_suppress_inline_builddir(tmpdir):
build_dir = os.path.join(tmpdir, 'b1')
os.mkdir(build_dir)
__test_suppress_inline(['-j1', '--cppcheck-build-dir={}'.format(build_dir)])
# TODO: inline suppressions do not work with whole program analysis and -j
@pytest.mark.xfail(strict=True)
def test_suppress_inline_builddir_j(tmpdir):
build_dir = os.path.join(tmpdir, 'b1')
os.mkdir(build_dir)
__test_suppress_inline(['-j2', '--cppcheck-build-dir={}'.format(build_dir)])
def __test_suppress_inline_project(tmpdir, extra_args):
compile_db = __create_compile_commands(tmpdir, [
os.path.join(__script_dir, 'whole-program', 'odr1.cpp'),
os.path.join(__script_dir, 'whole-program', 'odr2.cpp')
])
args = [
'-q',
'--template=simple',
'--enable=information,style',
'--disable=missingInclude', # TODO: remove
'--inline-suppr',
'--error-exitcode=1',
'--project={}'.format(compile_db)
]
args += extra_args
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
lines = stderr.splitlines()
assert lines == []
assert stdout == ''
assert ret == 0, stdout
def test_suppress_inline_project(tmpdir):
__test_suppress_inline_project(tmpdir, ['-j1'])
# whole program analysis requires a build dir with -j
@pytest.mark.xfail(strict=True)
def test_suppress_inline_project_j(tmpdir):
__test_suppress_inline_project(tmpdir, ['-j2'])
def test_suppress_inline_project_builddir(tmpdir):
build_dir = os.path.join(tmpdir, 'b1')
os.mkdir(build_dir)
__test_suppress_inline_project(tmpdir, ['-j1', '--cppcheck-build-dir={}'.format(build_dir)])
# TODO: inline suppressions do not work with whole program analysis and -j
@pytest.mark.xfail(strict=True)
def test_suppress_inline_project_builddir_j(tmpdir):
build_dir = os.path.join(tmpdir, 'b1')
os.mkdir(build_dir)
__test_suppress_inline_project(tmpdir, ['-j2', '--cppcheck-build-dir={}'.format(build_dir)])
def __test_checkclass(extra_args):
args = [
'-q',
'--template=simple',
'--enable=information,style',
'--disable=missingInclude', # TODO: remove
'--error-exitcode=1',
'whole-program/odr1.cpp',
'whole-program/odr2.cpp'
]
args += extra_args
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
lines = stderr.splitlines()
assert lines == [
"whole-program{}odr1.cpp:6:1: error: The one definition rule is violated, different classes/structs have the same name 'C' [ctuOneDefinitionRuleViolation]".format(os.path.sep)
]
assert stdout == ''
assert ret == 1, stdout
def test_checkclass():
__test_checkclass(['-j1'])
# whole program analysis requires a build dir with -j
@pytest.mark.xfail(strict=True)
def test_checkclass_j():
__test_checkclass(['-j2'])
def test_checkclass_builddir(tmpdir):
build_dir = os.path.join(tmpdir, 'b1')
os.mkdir(build_dir)
__test_checkclass(['--cppcheck-build-dir={}'.format(build_dir)])
def __test_checkclass_project(tmpdir, extra_args):
odr_file_1 = os.path.join(__script_dir, 'whole-program', 'odr1.cpp')
compile_db = __create_compile_commands(tmpdir, [
odr_file_1,
os.path.join(__script_dir, 'whole-program', 'odr2.cpp')
])
args = [
'-q',
'--template=simple',
'--enable=information,style',
'--disable=missingInclude', # TODO: remove
'--error-exitcode=1',
'--project={}'.format(compile_db)
]
args += extra_args
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
lines = stderr.splitlines()
assert lines == [
"{}:6:1: error: The one definition rule is violated, different classes/structs have the same name 'C' [ctuOneDefinitionRuleViolation]".format(odr_file_1)
]
assert stdout == ''
assert ret == 1, stdout
def test_checkclass_project(tmpdir):
__test_checkclass_project(tmpdir, ['-j1'])
# whole program analysis requires a build dir with -j
@pytest.mark.xfail(strict=True)
def test_checkclass_project_j(tmpdir):
__test_checkclass_project(tmpdir, ['-j2'])
def test_checkclass_project_builddir(tmpdir):
build_dir = os.path.join(tmpdir, 'b1')
os.mkdir(build_dir)
__test_checkclass_project(tmpdir, ['-j1', '--cppcheck-build-dir={}'.format(build_dir)])