From 68bb197bcbf2167f1cfcdf20fc15a5b618458436 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Mon, 12 Jan 2015 06:11:22 +0100 Subject: [PATCH] Destructor detected as constructor resulting in false variable not initialized warnings --- lib/symboldatabase.cpp | 5 ++++- test/testconstructors.cpp | 17 +++++++++++++++++ test/testsymboldatabase.cpp | 21 +++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 470dca2b8..62664ae13 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -3451,10 +3451,13 @@ const Scope * SymbolDatabase::findNamespace(const Token * tok, const Scope * sco Function * SymbolDatabase::findFunctionInScope(const Token *func, const Scope *ns) { const Function * function = nullptr; + const bool destructor = func->strAt(-1) == "~"; for (std::multimap::const_iterator it = ns->functionMap.find(func->str()); it != ns->functionMap.end() && it->first == func->str(); ++it) { - if (Function::argsMatch(ns, func->tokAt(2), it->second->argDef->next(), "", 0)) { + + if (Function::argsMatch(ns, func->tokAt(2), it->second->argDef->next(), "", 0) && + it->second->isDestructor() == destructor) { function = it->second; break; } diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index 9982faeea..051098a01 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -137,6 +137,7 @@ private: TEST_CASE(uninitVar27); // ticket #5170 - rtl::math::setNan(&d) TEST_CASE(uninitVar28); // ticket #6258 TEST_CASE(uninitVar29); + TEST_CASE(uninitVar30); // ticket #6417 TEST_CASE(uninitVarEnum); TEST_CASE(uninitVarStream); TEST_CASE(uninitVarTypedef); @@ -2160,6 +2161,22 @@ private: "[test.cpp:25]: (warning) Member variable 'D::i' is not initialized in the constructor.\n", errout.str()); } + void uninitVar30() { // ticket #6417 + check("namespace NS {\n" + " class MyClass {\n" + " public:\n" + " MyClass();\n" + " ~MyClass();\n" + " private:\n" + " bool SomeVar;\n" + " };\n" + "}\n" + "using namespace NS;\n" + "MyClass::~MyClass() { }\n" + "MyClass::MyClass() : SomeVar(false) { }\n"); + ASSERT_EQUALS("", errout.str()); + } + void uninitVarArray1() { check("class John\n" "{\n" diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index e167b9557..70eff45ea 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -221,6 +221,7 @@ private: TEST_CASE(symboldatabase45); // #6125 TEST_CASE(symboldatabase46); // #6171 (anonymous namespace) TEST_CASE(symboldatabase47); // #6308 + TEST_CASE(symboldatabase48); // #6417 TEST_CASE(isImplicitlyVirtual); @@ -2044,6 +2045,26 @@ private: ASSERT(db && !db->functionScopes.empty() && db->functionScopes.front()->function && db->functionScopes.front()->function->functionScope == db->functionScopes.front()); } + void symboldatabase48() { // #6417 + GET_SYMBOL_DB("namespace NS {\n" + " class MyClass {\n" + " MyClass();\n" + " ~MyClass();\n" + " };\n" + "}\n" + "using namespace NS;\n" + "MyClass::~MyClass() { }\n" + "MyClass::MyClass() { }\n"); + ASSERT(db && !db->functionScopes.empty() && db->functionScopes.front()->function && db->functionScopes.front()->function->functionScope == db->functionScopes.front()); + + const Token *f = Token::findsimplematch(tokenizer.tokens(), "MyClass ( ) ;"); + ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 3 && f->function()->token->linenr() == 9); + + f = Token::findsimplematch(tokenizer.tokens(), "~ MyClass ( ) ;"); + f = f->next(); + ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 4 && f->function()->token->linenr() == 8); + } + void isImplicitlyVirtual() { { GET_SYMBOL_DB("class Base {\n"