From c7be967769242ede51dcf6fc41994514a032352b Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Mon, 24 May 2021 13:32:15 -0400 Subject: [PATCH] fix #10295 (false negatives by inconsistent 'void' in argument list (declaration vs definition)) (#3274) Co-authored-by: Robert Reif --- lib/symboldatabase.cpp | 5 +++++ test/testconstructors.cpp | 13 +++++++++++++ test/testsymboldatabase.cpp | 19 +++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 4334ba7c8..86cbc6498 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -2429,6 +2429,11 @@ bool Function::argsMatch(const Scope *scope, const Token *first, const Token *se int offset = 0; int openParen = 0; + // check for () == (void) and (void) == () + if ((Token::simpleMatch(first, "( )") && Token::simpleMatch(second, "( void )")) || + (Token::simpleMatch(first, "( void )") && Token::simpleMatch(second, "( )"))) + return true; + while (first->str() == second->str() && first->isLong() == second->isLong() && first->isUnsigned() == second->isUnsigned()) { diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index 26606103a..9d147c6c8 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -148,6 +148,7 @@ private: TEST_CASE(uninitVar30); // ticket #6417 TEST_CASE(uninitVar31); // ticket #8271 TEST_CASE(uninitVar32); // ticket #8835 + TEST_CASE(uninitVar33); // ticket #10295 TEST_CASE(uninitVarEnum1); TEST_CASE(uninitVarEnum2); // ticket #8146 TEST_CASE(uninitVarStream); @@ -2524,6 +2525,18 @@ private: ASSERT_EQUALS("[test.cpp:5]: (warning) Member variable 'Foo::member' is not initialized in the constructor.\n", errout.str()); } + void uninitVar33() { // ticket #10295 + check("namespace app {\n" + " class B {\n" + " public:\n" + " B(void);\n" + " int x;\n" + " };\n" + "};\n" + "app::B::B(void){}"); + ASSERT_EQUALS("[test.cpp:8]: (warning) Member variable 'B::x' is not initialized in the constructor.\n", errout.str()); + } + void uninitVarArray1() { check("class John\n" "{\n" diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 07df65e48..61fa1cc5c 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -347,6 +347,7 @@ private: TEST_CASE(symboldatabase92); // daca crash TEST_CASE(symboldatabase93); // alignas attribute TEST_CASE(symboldatabase94); // structured bindings + TEST_CASE(symboldatabase95); // #10295 TEST_CASE(createSymbolDatabaseFindAllScopes1); @@ -4720,6 +4721,24 @@ private: ASSERT(db->getVariableFromVarId(2) != nullptr); } + void symboldatabase95() { // #10295 + GET_SYMBOL_DB("struct B {\n" + " void foo1(void);\n" + " void foo2();\n" + "};\n" + "void B::foo1() {}\n" + "void B::foo2(void) {}\n"); + ASSERT_EQUALS("", errout.str()); + const Token *functok = Token::findsimplematch(tokenizer.tokens(), "foo1 ( ) { }"); + ASSERT(functok); + ASSERT(functok->function()); + ASSERT(functok->function()->name() == "foo1"); + functok = Token::findsimplematch(tokenizer.tokens(), "foo2 ( void ) { }"); + ASSERT(functok); + ASSERT(functok->function()); + ASSERT(functok->function()->name() == "foo2"); + } + void createSymbolDatabaseFindAllScopes1() { GET_SYMBOL_DB("void f() { union {int x; char *p;} a={0}; }"); ASSERT(db->scopeList.size() == 3);