Skip to content

Commit ff6371b

Browse files
committed
Add tests for rule 2.5 of misra.py
1 parent 95fea78 commit ff6371b

2 files changed

Lines changed: 293 additions & 0 deletions

File tree

‎addons/test/misra_test.py‎

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import os
99
import json
10+
from types import SimpleNamespace
1011

1112
from .util import dump_create, dump_remove, convert_json_output
1213
from addons.misra import C11_STDLIB_IDENTIFIERS, C99_STDLIB_IDENTIFIERS,C90_STDLIB_IDENTIFIERS, isStdLibId, isKeyword
@@ -206,6 +207,228 @@ def test_ctu_unused_declaration(checker, tmp_path, summary, rule, used_first):
206207
assert 'a.c-1-13-%d' % rule in checker.existing_violations
207208
assert 'b.c-1-13-%d' % rule not in checker.existing_violations
208209

210+
@pytest.mark.parametrize('summary,rule', [('MisraTypedefInfo', 506), ('MisraTagName', 507)])
211+
def test_ctu_declaration_conflict_missing_ctu_info(checker, tmp_path, summary, rule):
212+
# The same name declared in a.c and in b.c, then a ctu-info file that does not exist
213+
ctu_info = tmp_path / 'test.ctu-info'
214+
with open(ctu_info, 'wt') as f:
215+
for path in ['a.c', 'b.c']:
216+
data = [{'name': 'X', 'file': path, 'line': 1, 'column': 13, 'used': True}]
217+
f.write(json.dumps({'summary': summary, 'data': data}) + '\n')
218+
checker.analyse_ctu_info([str(ctu_info), str(tmp_path / 'missing.ctu-info')])
219+
assert checker.existing_violations == {'a.c-1-13-%d' % rule, 'b.c-1-13-%d' % rule}
220+
221+
def test_ctu_declaration_symlink(checker, tmp_path):
222+
# The same declaration in h.h, reached through a symbolic link
223+
header = tmp_path / 'h.h'
224+
header.write_text('')
225+
try:
226+
os.symlink(str(header), str(tmp_path / 'link.h'))
227+
except (OSError, NotImplementedError):
228+
pytest.skip('symbolic links are not available')
229+
unused = {'name': 'X', 'file': str(header), 'line': 1, 'column': 13, 'used': False}
230+
used = {'name': 'X', 'file': str(tmp_path / 'link.h'), 'line': 1, 'column': 13, 'used': True}
231+
ctu_info = tmp_path / 'test.ctu-info'
232+
with open(ctu_info, 'wt') as f:
233+
for declaration in [unused, used]:
234+
f.write(json.dumps({'summary': 'MisraMacro', 'data': [declaration]}) + '\n')
235+
checker.analyse_ctu_info([str(ctu_info)])
236+
assert not checker.existing_violations
237+
238+
def test_ctu_macro_several_definitions(checker, tmp_path):
239+
config_h = SimpleNamespace(str='#define BUFSIZE 2', file='config.h', linenr=1, column=1)
240+
defaults_h = SimpleNamespace(str='#define BUFSIZE 5', file='defaults.h', linenr=2, column=1)
241+
a = SimpleNamespace(macro_usage=[SimpleNamespace(name='BUFSIZE')], directives=[config_h, defaults_h], preprocessor_if_conditions=[])
242+
b = SimpleNamespace(macro_usage=[], directives=[config_h], preprocessor_if_conditions=[])
243+
checker.misra_2_5(str(tmp_path / 'a.c.dump'), a)
244+
checker.misra_2_5(str(tmp_path / 'b.c.dump'), b)
245+
checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info'), str(tmp_path / 'b.c.ctu-info')])
246+
assert not checker.existing_violations
247+
248+
def test_ctu_macro_unused(checker, tmp_path):
249+
unused = SimpleNamespace(str='#define UNUSED 1', file='h.h', linenr=3, column=9)
250+
cfg = SimpleNamespace(macro_usage=[], directives=[unused], preprocessor_if_conditions=[])
251+
checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg)
252+
checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')])
253+
assert checker.existing_violations == {'h.h-3-9-205'}
254+
255+
@pytest.mark.parametrize('exists', [True, False])
256+
def test_ctu_macro_undef_counts_as_use(checker, tmp_path, monkeypatch, exists):
257+
# The result is the same when h1.h and h2.h are not found from the current directory, for example with --relative-paths
258+
if exists:
259+
for name in ('h1.h', 'h2.h', 'a.c'):
260+
(tmp_path / name).write_text('')
261+
monkeypatch.chdir(str(tmp_path))
262+
defined_1 = SimpleNamespace(str='#define X 1', file='h1.h', linenr=1, column=9)
263+
defined_2 = SimpleNamespace(str='#define X 2', file='h2.h', linenr=1, column=9)
264+
undefined = SimpleNamespace(str='#undef X', file='a.c', linenr=3, column=8)
265+
cfg = SimpleNamespace(macro_usage=[], directives=[defined_1, defined_2, undefined], preprocessor_if_conditions=[])
266+
checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg)
267+
checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')])
268+
assert not checker.existing_violations
269+
270+
@pytest.mark.parametrize('exists', [True, False])
271+
def test_ctu_macro_undef_before_define(checker, tmp_path, monkeypatch, exists):
272+
# The directives of a.c can come before those of the headers it includes
273+
if exists:
274+
for name in ('a.c', 'h.h'):
275+
(tmp_path / name).write_text('')
276+
monkeypatch.chdir(str(tmp_path))
277+
undefined = SimpleNamespace(str='#undef X', file='a.c', linenr=2, column=8)
278+
defined = SimpleNamespace(str='#define X 1', file='h.h', linenr=1, column=9)
279+
cfg = SimpleNamespace(macro_usage=[], directives=[undefined, defined], preprocessor_if_conditions=[])
280+
checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg)
281+
checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')])
282+
assert not checker.existing_violations
283+
284+
@pytest.mark.parametrize('exists', [True, False])
285+
def test_ctu_macro_defined_again_after_undef(checker, tmp_path, monkeypatch, exists):
286+
# h.h not found from the current directory, for example with --relative-paths, gives the same finding
287+
if exists:
288+
(tmp_path / 'h.h').write_text('')
289+
monkeypatch.chdir(str(tmp_path))
290+
defined_1 = SimpleNamespace(str='#define X 1', file='h.h', linenr=1, column=9)
291+
undefined_1 = SimpleNamespace(str='#undef X', file='h.h', linenr=2, column=8)
292+
defined_2 = SimpleNamespace(str='#define X 2', file='h.h', linenr=3, column=9)
293+
undefined_2 = SimpleNamespace(str='#undef X', file='h.h', linenr=4, column=8)
294+
defined_3 = SimpleNamespace(str='#define X 3', file='h.h', linenr=5, column=9)
295+
cfg = SimpleNamespace(macro_usage=[], directives=[defined_1, undefined_1, defined_2, undefined_2, defined_3], preprocessor_if_conditions=[])
296+
checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg)
297+
checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')])
298+
assert checker.existing_violations == {'h.h-5-9-205'}
299+
300+
def test_ctu_macro_header_same_file_name_quote_and_angle(checker, tmp_path, monkeypatch):
301+
# "config.h" next to a.c is included in the inactive branch, <config.h> in the active one, both are checked
302+
(tmp_path / 'inc').mkdir()
303+
(tmp_path / 'config.h').write_text('')
304+
(tmp_path / 'inc' / 'config.h').write_text('')
305+
monkeypatch.chdir(str(tmp_path))
306+
directives = [SimpleNamespace(str='#ifdef X', file='a.c', linenr=1, column=1),
307+
SimpleNamespace(str='#include "config.h"', file='a.c', linenr=2, column=1),
308+
SimpleNamespace(str='#else', file='a.c', linenr=3, column=1),
309+
SimpleNamespace(str='#include <config.h>', file='a.c', linenr=4, column=1),
310+
SimpleNamespace(str='#endif', file='a.c', linenr=5, column=1),
311+
SimpleNamespace(str='#define M 1', file='config.h', linenr=1, column=9),
312+
SimpleNamespace(str='#define M 2', file=os.path.join('inc', 'config.h'), linenr=1, column=9)]
313+
conditions = [SimpleNamespace(file='a.c', linenr=1, result=0)]
314+
cfg = SimpleNamespace(macro_usage=[], directives=directives, preprocessor_if_conditions=conditions)
315+
checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg)
316+
checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')])
317+
assert '%s-1-9-205' % os.path.join('inc', 'config.h') in checker.existing_violations
318+
319+
@pytest.mark.parametrize('results', [[1, 0], [0, 1]])
320+
def test_ctu_macro_condition_evaluated_twice(checker, tmp_path, results):
321+
# h.h included twice, its include guard is true then false, or false then true
322+
directives = [SimpleNamespace(str='#ifndef H', file='h.h', linenr=1, column=1),
323+
SimpleNamespace(str='#define G 1', file='h.h', linenr=2, column=9),
324+
SimpleNamespace(str='#endif', file='h.h', linenr=3, column=1)]
325+
conditions = [SimpleNamespace(file='h.h', linenr=1, result=result) for result in results]
326+
cfg = SimpleNamespace(macro_usage=[], directives=directives, preprocessor_if_conditions=conditions)
327+
checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg)
328+
checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')])
329+
assert checker.existing_violations == {'h.h-2-9-205'}
330+
331+
def test_ctu_macro_source_file_name_in_inactive_include(checker, tmp_path):
332+
# the source file is active even when an inactive #include names it
333+
directives = [SimpleNamespace(str='#define M 1', file='a.c', linenr=1, column=9),
334+
SimpleNamespace(str='#if 0', file='a.c', linenr=2, column=1),
335+
SimpleNamespace(str='#include "a.c"', file='a.c', linenr=3, column=1),
336+
SimpleNamespace(str='#endif', file='a.c', linenr=4, column=1)]
337+
conditions = [SimpleNamespace(file='a.c', linenr=2, result=0)]
338+
cfg = SimpleNamespace(macro_usage=[], directives=directives, preprocessor_if_conditions=conditions)
339+
checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg)
340+
checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')])
341+
assert checker.existing_violations == {'a.c-1-9-205'}
342+
343+
def test_ctu_macro_header_included_by_macro(checker, tmp_path):
344+
# an #include that does not name its file can include any header
345+
directives = [SimpleNamespace(str='#if 0', file='a.c', linenr=1, column=1),
346+
SimpleNamespace(str='#include "h.h"', file='a.c', linenr=2, column=1),
347+
SimpleNamespace(str='#endif', file='a.c', linenr=3, column=1),
348+
SimpleNamespace(str='#define HDR "h.h"', file='a.c', linenr=4, column=9),
349+
SimpleNamespace(str='#include HDR', file='a.c', linenr=5, column=1),
350+
SimpleNamespace(str='#define M 1', file='h.h', linenr=1, column=9)]
351+
conditions = [SimpleNamespace(file='a.c', linenr=1, result=0)]
352+
cfg = SimpleNamespace(macro_usage=[SimpleNamespace(name='HDR')], directives=directives, preprocessor_if_conditions=conditions)
353+
checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg)
354+
checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')])
355+
assert checker.existing_violations == {'h.h-1-9-205'}
356+
357+
@pytest.mark.skipif(sys.platform != 'win32', reason='requires Windows')
358+
def test_ctu_macro_header_included_in_other_case(checker, tmp_path):
359+
directives = [SimpleNamespace(str='#if 0', file='a.c', linenr=1, column=1),
360+
SimpleNamespace(str='#include "h.h"', file='a.c', linenr=2, column=1),
361+
SimpleNamespace(str='#endif', file='a.c', linenr=3, column=1),
362+
SimpleNamespace(str='#include "H.h"', file='a.c', linenr=4, column=1),
363+
SimpleNamespace(str='#define M 1', file='h.h', linenr=1, column=9)]
364+
conditions = [SimpleNamespace(file='a.c', linenr=1, result=0)]
365+
cfg = SimpleNamespace(macro_usage=[], directives=directives, preprocessor_if_conditions=conditions)
366+
checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg)
367+
checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')])
368+
assert checker.existing_violations == {'h.h-1-9-205'}
369+
370+
def test_ctu_macro_header_included_in_branch_hard_link(checker, tmp_path, monkeypatch):
371+
# h.h is included as h2.h, a hard link, in an inactive branch first, the dump names it after this inclusion
372+
(tmp_path / 'h.h').write_text('')
373+
os.link(str(tmp_path / 'h.h'), str(tmp_path / 'h2.h'))
374+
monkeypatch.chdir(str(tmp_path))
375+
directives = [SimpleNamespace(str='#ifdef X', file='a.c', linenr=1, column=1),
376+
SimpleNamespace(str='#include "h2.h"', file='a.c', linenr=2, column=1),
377+
SimpleNamespace(str='#endif', file='a.c', linenr=3, column=1),
378+
SimpleNamespace(str='#include "h.h"', file='a.c', linenr=4, column=1),
379+
SimpleNamespace(str='#define H 1', file='h2.h', linenr=1, column=9)]
380+
conditions = [SimpleNamespace(file='a.c', linenr=1, result=0)]
381+
cfg = SimpleNamespace(macro_usage=[], directives=directives, preprocessor_if_conditions=conditions)
382+
checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg)
383+
checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')])
384+
assert checker.existing_violations == {'h2.h-1-9-205'}
385+
386+
def test_ctu_macro_header_included_in_branch_hard_link_in_subdirectory(checker, tmp_path, monkeypatch):
387+
# x/h2.h is a hard link of x/h.h, which the dump names after the inactive inclusion
388+
(tmp_path / 'x').mkdir()
389+
(tmp_path / 'x' / 'h.h').write_text('')
390+
os.link(str(tmp_path / 'x' / 'h.h'), str(tmp_path / 'x' / 'h2.h'))
391+
monkeypatch.chdir(str(tmp_path))
392+
directives = [SimpleNamespace(str='#ifdef X', file='a.c', linenr=1, column=1),
393+
SimpleNamespace(str='#include "x/h.h"', file='a.c', linenr=2, column=1),
394+
SimpleNamespace(str='#endif', file='a.c', linenr=3, column=1),
395+
SimpleNamespace(str='#include "x/h2.h"', file='a.c', linenr=4, column=1),
396+
SimpleNamespace(str='#define H 1', file=os.path.join('x', 'h.h'), linenr=1, column=9)]
397+
conditions = [SimpleNamespace(file='a.c', linenr=1, result=0)]
398+
cfg = SimpleNamespace(macro_usage=[], directives=directives, preprocessor_if_conditions=conditions)
399+
checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg)
400+
checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')])
401+
assert checker.existing_violations == {'%s-1-9-205' % os.path.join('x', 'h.h')}
402+
403+
def test_ctu_macro_header_included_through_link(checker, tmp_path):
404+
# y/config.h is first included in the inactive branch, then reached by the link l: no header has a path that ends like l/config.h
405+
directives = [SimpleNamespace(str='#if 0', file='a.c', linenr=1, column=1),
406+
SimpleNamespace(str='#include "y/config.h"', file='a.c', linenr=2, column=1),
407+
SimpleNamespace(str='#include "x/config.h"', file='a.c', linenr=3, column=1),
408+
SimpleNamespace(str='#endif', file='a.c', linenr=4, column=1),
409+
SimpleNamespace(str='#include "l/config.h"', file='a.c', linenr=5, column=1),
410+
SimpleNamespace(str='#define M 1', file='x/config.h', linenr=1, column=9),
411+
SimpleNamespace(str='#define N 1', file='y/config.h', linenr=1, column=9)]
412+
conditions = [SimpleNamespace(file='a.c', linenr=1, result=0)]
413+
cfg = SimpleNamespace(macro_usage=[], directives=directives, preprocessor_if_conditions=conditions)
414+
checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg)
415+
checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')])
416+
assert 'y/config.h-1-9-205' in checker.existing_violations
417+
418+
def test_ctu_macro_header_included_from_parent_directory(checker, tmp_path):
419+
# h.h next to a.c is included in an inactive branch, ../h.h in the active one
420+
directives = [SimpleNamespace(str='#if 0', file='a.c', linenr=1, column=1),
421+
SimpleNamespace(str='#include "h.h"', file='a.c', linenr=2, column=1),
422+
SimpleNamespace(str='#endif', file='a.c', linenr=3, column=1),
423+
SimpleNamespace(str='#include "../h.h"', file='a.c', linenr=4, column=1),
424+
SimpleNamespace(str='#define M 1', file='h.h', linenr=1, column=9),
425+
SimpleNamespace(str='#define M 2', file=os.path.join('..', 'h.h'), linenr=1, column=9)]
426+
conditions = [SimpleNamespace(file='a.c', linenr=1, result=0)]
427+
cfg = SimpleNamespace(macro_usage=[], directives=directives, preprocessor_if_conditions=conditions)
428+
checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg)
429+
checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')])
430+
assert checker.existing_violations == {'%s-1-9-205' % os.path.join('..', 'h.h')}
431+
209432
def test_platform(checker):
210433
test_file = os.path.join('addons','test','misra','misra-test.c')
211434
dump_create(test_file, "--language=c++")

‎test/cli/whole-program_test.py‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,76 @@ def test_addon_no_artifacts(tmp_path, builddir):
247247
assert ' '.join(files) == 'whole1.c whole2.c'
248248

249249

250+
def test_addon_misra_2_5_fallback_definition(tmp_path):
251+
(tmp_path / 'config.h').write_text('#define BUFSIZE 2\n')
252+
(tmp_path / 'defaults.h').write_text('#ifndef BUFSIZE\n#define BUFSIZE 5\n#endif\n')
253+
(tmp_path / 'a.c').write_text('#include "config.h"\n#include "defaults.h"\nint x = BUFSIZE;\n')
254+
(tmp_path / 'b.c').write_text('#include "config.h"\nint b1(void);\n')
255+
args = [
256+
'--addon=misra',
257+
'--enable=style',
258+
'--template={id}',
259+
'a.c',
260+
'b.c']
261+
_, _, stderr = cppcheck(args, cwd=str(tmp_path))
262+
assert 'internalError' not in stderr
263+
assert 'misra-c2012-2.5' not in stderr
264+
265+
266+
def test_addon_misra_2_5_header_path_spelling(tmp_path):
267+
(tmp_path / 'inc').mkdir()
268+
(tmp_path / 'src').mkdir()
269+
(tmp_path / 'inc' / 'h.h').write_text('#define HM 3\n')
270+
(tmp_path / 'src' / 'a.c').write_text('#include "h.h"\nint a1(void);\n')
271+
(tmp_path / 'src' / 'b.c').write_text('#include "../inc/h.h"\nint b1 = HM;\n')
272+
args = [
273+
'--addon=misra',
274+
'--enable=style',
275+
'--template={id}',
276+
'-I' + str(tmp_path / 'inc'),
277+
'src/a.c',
278+
'src/b.c']
279+
_, _, stderr = cppcheck(args, cwd=str(tmp_path))
280+
assert 'internalError' not in stderr
281+
assert 'misra-c2012-2.5' not in stderr
282+
283+
284+
def test_addon_misra_2_5_header_relative_paths(tmp_path):
285+
# The header paths of the dump are relative to the -rp directory, not to the current one
286+
(tmp_path / 'inc').mkdir()
287+
(tmp_path / 'src').mkdir()
288+
(tmp_path / 'inc' / 'h.h').write_text('#define HM 3\n')
289+
(tmp_path / 'src' / 'a.c').write_text('#include "h.h"\nint a1(void);\n')
290+
(tmp_path / 'src' / 'b.c').write_text('#include "../inc/h.h"\nint b1 = HM;\n')
291+
args = [
292+
'--addon=misra',
293+
'--enable=style',
294+
'--template={id}',
295+
'-I' + str(tmp_path / 'inc'),
296+
'-rp=' + str(tmp_path),
297+
'a.c',
298+
'b.c']
299+
_, _, stderr = cppcheck(args, cwd=str(tmp_path / 'src'))
300+
assert 'internalError' not in stderr
301+
assert 'misra-c2012-2.5' not in stderr
302+
303+
304+
def test_addon_misra_2_5_header_relative_paths_defined_again(tmp_path):
305+
(tmp_path / 'inc').mkdir()
306+
(tmp_path / 'src').mkdir()
307+
(tmp_path / 'inc' / 'h.h').write_text('#define HM 1\n#undef HM\n#define HM 2\n')
308+
(tmp_path / 'src' / 'a.c').write_text('#include "h.h"\nint a1(void);\n')
309+
args = [
310+
'--addon=misra',
311+
'--enable=style',
312+
'--template={line}:{id}',
313+
'-I' + str(tmp_path / 'inc'),
314+
'-rp=' + str(tmp_path),
315+
'a.c']
316+
_, _, stderr = cppcheck(args, cwd=str(tmp_path / 'src'))
317+
assert [l for l in stderr.splitlines() if l.endswith('2.5')] == ['3:misra-c2012-2.5']
318+
319+
250320
def __test_checkclass(extra_args):
251321
args = [
252322
'-q',

0 commit comments

Comments
 (0)