From 83f890abc78b7d42edbbd986ccbb517c80d50653 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Fri, 14 Jun 2024 12:51:52 +0200 Subject: [PATCH] refs #12835/#12839 - added Python tests for known issues with whole program analysis (#6521) added Python tests showing inline suppressions not working with whole program analysis and -j2 as well as tests for missing whole program analysis execution for addons --- test/cli/whole-program/whole1.c | 3 ++ test/cli/whole-program/whole2.c | 2 + test/cli/whole-program_test.py | 88 +++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 test/cli/whole-program/whole1.c create mode 100644 test/cli/whole-program/whole2.c create mode 100644 test/cli/whole-program_test.py diff --git a/test/cli/whole-program/whole1.c b/test/cli/whole-program/whole1.c new file mode 100644 index 00000000000..0917e545c06 --- /dev/null +++ b/test/cli/whole-program/whole1.c @@ -0,0 +1,3 @@ +// cppcheck-suppress misra-c2012-8.4 +// cppcheck-suppress misra-c2012-5.8 +int misra_5_8_var1; diff --git a/test/cli/whole-program/whole2.c b/test/cli/whole-program/whole2.c new file mode 100644 index 00000000000..62546753586 --- /dev/null +++ b/test/cli/whole-program/whole2.c @@ -0,0 +1,2 @@ +// cppcheck-suppress misra-c2012-5.8 +static int misra_5_8_var1; diff --git a/test/cli/whole-program_test.py b/test/cli/whole-program_test.py new file mode 100644 index 00000000000..6f137ec77a9 --- /dev/null +++ b/test/cli/whole-program_test.py @@ -0,0 +1,88 @@ +import os +import pytest +import json +from testutils import cppcheck + +__script_dir = os.path.dirname(os.path.abspath(__file__)) + +# TODO: use dedicated addon + + +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 + } + print(obj) + 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']) + + +@pytest.mark.xfail(strict=True) +def test_addon_suppress_inline_j(): + __test_addon_suppress_inline(['-j2']) + + +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 + + +@pytest.mark.xfail(strict=True) +def test_addon_suppress_inline_project(tmpdir): + __test_addon_suppress_inline_project(tmpdir, ['-j1']) + + +@pytest.mark.xfail(strict=True) +def test_addon_suppress_inline_project_j(tmpdir): + __test_addon_suppress_inline_project(tmpdir, ['-j2'])