Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refs #12842 - added more Python tests for whole program analysis #6548

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions test/cli/whole-program/odr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Base {
public:
virtual void f() = 0;
};

extern Base *c1_create();
extern Base *c2_create();
15 changes: 15 additions & 0 deletions test/cli/whole-program/odr1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "odr.h"

#include <iostream>

// cppcheck-suppress ctuOneDefinitionRuleViolation
class C : public Base
{
public:
void f() override { std::cout << "1"; }
};

Base *c1_create()
{
return new C();
}
14 changes: 14 additions & 0 deletions test/cli/whole-program/odr2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "odr.h"

#include <iostream>

class C : public Base
{
public:
void f() override { std::cout << "2"; }
};

Base *c2_create()
{
return new C();
}
116 changes: 115 additions & 1 deletion test/cli/whole-program_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
__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):
Expand All @@ -17,7 +20,6 @@ def __create_compile_commands(dir, entries):
'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:
Expand Down Expand Up @@ -85,3 +87,115 @@ def test_addon_suppress_inline_project(tmpdir):
@pytest.mark.xfail(strict=True)
def test_addon_suppress_inline_project_j(tmpdir):
__test_addon_suppress_inline_project(tmpdir, ['-j2'])


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'])


@pytest.mark.xfail(strict=True)
def test_suppress_inline_j():
__test_suppress_inline(['-j2'])


@pytest.mark.xfail(strict=True)
def test_suppress_inline_project(tmpdir):
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)
]

ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
lines = stderr.splitlines()
assert lines == []
assert stdout == ''
assert ret == 0, stdout


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'])


@pytest.mark.xfail(strict=True)
def test_checkclass_j():
__test_checkclass(['-j2'])


@pytest.mark.xfail(strict=True)
def test_checkclass_project(tmpdir):
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)
]

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
Loading