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
393 lines (309 loc) · 12 KB
/
whole-program_test.py
File metadata and controls
393 lines (309 loc) · 12 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
385
386
387
388
389
390
391
392
393
import glob
import os
import pytest
import json
import shutil
from testutils import cppcheck
import xml.etree.ElementTree as ET
__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',
'--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',
'--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',
'--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',
'--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)])
@pytest.mark.parametrize("builddir", (False,True))
def test_addon_rerun(tmp_path, builddir):
"""Rerun analysis and ensure that misra CTU works; with and without build dir"""
args = [
'--addon=misra',
'--enable=style',
'--template={id}',
'whole-program']
# do not use the injection because the directory needs to survive runs
if builddir:
args.append('--cppcheck-build-dir=' + str(tmp_path))
else:
args.append('--no-cppcheck-build-dir')
_, _, stderr = cppcheck(args, cwd=__script_dir)
assert 'misra-c2012-5.8' in stderr
_, _, stderr = cppcheck(args, cwd=__script_dir)
assert 'misra-c2012-5.8' in stderr
def test_addon_builddir_use_ctuinfo(tmp_path):
"""Test that ctu-info files are used when builddir is used"""
args = [
'--cppcheck-build-dir=' + str(tmp_path),
'--addon=misra',
'--enable=style',
'--template={id}',
'whole-program']
_, _, stderr = cppcheck(args, cwd=__script_dir)
assert 'misra-c2012-5.8' in stderr
with open(tmp_path / 'whole1.a1.ctu-info', 'wt'):
pass
with open(tmp_path / 'whole2.a1.ctu-info', 'wt'):
pass
_, _, stderr = cppcheck(args, cwd=__script_dir)
assert 'misra-c2012-5.8' not in stderr
@pytest.mark.parametrize("builddir", (False,True))
def test_addon_no_artifacts(tmp_path, builddir):
"""Test that there are no artifacts left after analysis"""
shutil.copyfile(os.path.join(__script_dir, 'whole-program', 'whole1.c'), tmp_path / 'whole1.c')
shutil.copyfile(os.path.join(__script_dir, 'whole-program', 'whole2.c'), tmp_path / 'whole2.c')
build_dir = str(tmp_path / 'b1')
os.mkdir(build_dir)
args = [
'--addon=misra',
'--enable=style',
'--template={id}',
str(tmp_path)]
if builddir:
args.append('--cppcheck-build-dir=' + build_dir)
_, _, stderr = cppcheck(args, cwd=__script_dir)
assert 'misra-c2012-5.8' in stderr
files = []
for f in glob.glob(str(tmp_path / '*')):
if os.path.isfile(f):
files.append(os.path.basename(f))
files.sort()
assert ' '.join(files) == 'whole1.c whole2.c'
def __test_checkclass(extra_args):
args = [
'-q',
'--template=simple',
'--enable=information,style',
'--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'])
@pytest.mark.xfail(strict=True) # TODO: check error
def test_checkclass_j():
__test_checkclass(['-j2', '--no-cppcheck-build-dir'])
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_builddir_j(tmpdir):
build_dir = os.path.join(tmpdir, 'b1')
os.mkdir(build_dir)
__test_checkclass(['-j2', '--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',
'--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'])
@pytest.mark.xfail(strict=True) # TODO: check error
def test_checkclass_project_j(tmpdir):
__test_checkclass_project(tmpdir, ['-j2', '--no-cppcheck-build-dir'])
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)])
def test_checkclass_project_builddir_j(tmpdir):
build_dir = os.path.join(tmpdir, 'b1')
os.mkdir(build_dir)
__test_checkclass_project(tmpdir, ['-j2', '--cppcheck-build-dir={}'.format(build_dir)])
def __test_nullpointer_file0(extra_args):
args = [
'-q',
'--xml',
'--error-exitcode=1',
'whole-program/nullpointer1.cpp'
]
args += extra_args
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
results = ET.fromstring(stderr)
file0 = ''
for e in results.findall('errors/error[@id="ctunullpointer"]'):
file0 = e.attrib['file0']
assert ret == 1, stdout if stdout else stderr
assert stdout == ''
assert file0 == 'whole-program/nullpointer1.cpp', stderr
def test_nullpointer_file0():
__test_nullpointer_file0(['-j1'])
@pytest.mark.xfail(strict=True) # no CTU without builddir
def test_nullpointer_file0_j():
__test_nullpointer_file0(['-j2', '--no-cppcheck-build-dir'])
def test_nullpointer_file0_builddir_j(tmpdir):
build_dir = os.path.join(tmpdir, 'b1')
os.mkdir(build_dir)
__test_nullpointer_file0(['-j2', '--cppcheck-build-dir={}'.format(build_dir)])