Skip to content

Commit c7be967

Browse files
IOBYTERobert Reif
andauthored
fix #10295 (false negatives by inconsistent 'void' in argument list (declaration vs definition)) (cppcheck-opensource#3274)
Co-authored-by: Robert Reif <reif@FX6840>
1 parent d3bb84c commit c7be967

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

lib/symboldatabase.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,6 +2429,11 @@ bool Function::argsMatch(const Scope *scope, const Token *first, const Token *se
24292429
int offset = 0;
24302430
int openParen = 0;
24312431

2432+
// check for () == (void) and (void) == ()
2433+
if ((Token::simpleMatch(first, "( )") && Token::simpleMatch(second, "( void )")) ||
2434+
(Token::simpleMatch(first, "( void )") && Token::simpleMatch(second, "( )")))
2435+
return true;
2436+
24322437
while (first->str() == second->str() &&
24332438
first->isLong() == second->isLong() &&
24342439
first->isUnsigned() == second->isUnsigned()) {

test/testconstructors.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ class TestConstructors : public TestFixture {
148148
TEST_CASE(uninitVar30); // ticket #6417
149149
TEST_CASE(uninitVar31); // ticket #8271
150150
TEST_CASE(uninitVar32); // ticket #8835
151+
TEST_CASE(uninitVar33); // ticket #10295
151152
TEST_CASE(uninitVarEnum1);
152153
TEST_CASE(uninitVarEnum2); // ticket #8146
153154
TEST_CASE(uninitVarStream);
@@ -2524,6 +2525,18 @@ class TestConstructors : public TestFixture {
25242525
ASSERT_EQUALS("[test.cpp:5]: (warning) Member variable 'Foo::member' is not initialized in the constructor.\n", errout.str());
25252526
}
25262527

2528+
void uninitVar33() { // ticket #10295
2529+
check("namespace app {\n"
2530+
" class B {\n"
2531+
" public:\n"
2532+
" B(void);\n"
2533+
" int x;\n"
2534+
" };\n"
2535+
"};\n"
2536+
"app::B::B(void){}");
2537+
ASSERT_EQUALS("[test.cpp:8]: (warning) Member variable 'B::x' is not initialized in the constructor.\n", errout.str());
2538+
}
2539+
25272540
void uninitVarArray1() {
25282541
check("class John\n"
25292542
"{\n"

test/testsymboldatabase.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ class TestSymbolDatabase: public TestFixture {
347347
TEST_CASE(symboldatabase92); // daca crash
348348
TEST_CASE(symboldatabase93); // alignas attribute
349349
TEST_CASE(symboldatabase94); // structured bindings
350+
TEST_CASE(symboldatabase95); // #10295
350351

351352
TEST_CASE(createSymbolDatabaseFindAllScopes1);
352353

@@ -4720,6 +4721,24 @@ class TestSymbolDatabase: public TestFixture {
47204721
ASSERT(db->getVariableFromVarId(2) != nullptr);
47214722
}
47224723

4724+
void symboldatabase95() { // #10295
4725+
GET_SYMBOL_DB("struct B {\n"
4726+
" void foo1(void);\n"
4727+
" void foo2();\n"
4728+
"};\n"
4729+
"void B::foo1() {}\n"
4730+
"void B::foo2(void) {}\n");
4731+
ASSERT_EQUALS("", errout.str());
4732+
const Token *functok = Token::findsimplematch(tokenizer.tokens(), "foo1 ( ) { }");
4733+
ASSERT(functok);
4734+
ASSERT(functok->function());
4735+
ASSERT(functok->function()->name() == "foo1");
4736+
functok = Token::findsimplematch(tokenizer.tokens(), "foo2 ( void ) { }");
4737+
ASSERT(functok);
4738+
ASSERT(functok->function());
4739+
ASSERT(functok->function()->name() == "foo2");
4740+
}
4741+
47234742
void createSymbolDatabaseFindAllScopes1() {
47244743
GET_SYMBOL_DB("void f() { union {int x; char *p;} a={0}; }");
47254744
ASSERT(db->scopeList.size() == 3);

0 commit comments

Comments
 (0)