From c6bbc9d739900304899d3fc604d2b7f09e734a4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 2 Apr 2010 19:29:54 +0200 Subject: [PATCH 1/2] Fixed #1565 (False positive: Uninitialized variable 'B::j'. There are 2 classes with the name 'B') --- lib/tokenize.cpp | 5 +++-- test/testclass.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 47f710abf..2edae2696 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6180,7 +6180,7 @@ const Token * Tokenizer::findClassFunction(const Token *tok, const std::string & */ const std::string classPattern(std::string(isStruct ? "struct " : "class ") + classname + " :|{"); const std::string internalPattern(std::string("!!~ ") + funcname + " ("); - const std::string externalPattern(std::string(classname) + " :: " + funcname + " ("); + const std::string externalPattern(classname + " :: " + funcname + " ("); for (; tok; tok = tok->next()) { @@ -6234,7 +6234,8 @@ const Token * Tokenizer::findClassFunction(const Token *tok, const std::string & else if (indentlevel == 0 && Token::Match(tok, externalPattern.c_str())) { - return tok; + if (!Token::simpleMatch(tok->previous(), "::")) + return tok; } } diff --git a/test/testclass.cpp b/test/testclass.cpp index cf0253f6f..6bbaee42f 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -69,6 +69,7 @@ private: TEST_CASE(uninitOperator); // No FP about uninitialized 'operator[]' TEST_CASE(uninitFunction1); // No FP when initialized in function TEST_CASE(uninitFunction2); // No FP when initialized in function + TEST_CASE(uninitSameClassName); // No FP when two classes have the same name TEST_CASE(noConstructor1); TEST_CASE(noConstructor2); @@ -1574,6 +1575,33 @@ private: ASSERT_EQUALS("", errout.str()); } + void uninitSameClassName() + { + checkUninitVar("class B\n" + "{\n" + "public:\n" + " B();\n" + " int j;\n" + "};\n" + "\n" + "class A\n" + "{\n" + " class B\n" + " {\n" + " public:\n" + " B();\n" + " int i;\n" + " };\n" + "};\n" + "\n" + "A::B::B()\n" + "{\n" + " i = 0;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + + void checkNoConstructor(const char code[]) { // Tokenize.. From 1d1d79a1e51f9d990c9c69823c78870ec1c870f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 2 Apr 2010 20:25:13 +0200 Subject: [PATCH 2/2] Cleanup: Removed unused %name% cmd for patterns --- lib/token.cpp | 11 +---------- lib/token.h | 1 - 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/lib/token.cpp b/lib/token.cpp index 4d7b4dda5..97434473d 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -390,17 +390,8 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid) patternIdentified = true; } - // Any symbolname.. - if (firstWordEquals(p, "%name%") == 0) - { - if (!tok->isName()) - return false; - - patternIdentified = true; - } - // Type.. - if (firstWordEquals(p, "%type%") == 0) + else if (firstWordEquals(p, "%type%") == 0) { if (!tok->isName()) return false; diff --git a/lib/token.h b/lib/token.h index 963b3e4b1..39d8237a0 100644 --- a/lib/token.h +++ b/lib/token.h @@ -98,7 +98,6 @@ public: * Possible patterns * - "%any%" any token * - "%var%" any token which is a name or type e.g. "hello" or "int" - * - "%name%" any token which is a name or type e.g. "hello" or "int" * - "%type%" Anything that can be a variable type, e.g. "int", but not "delete". * - "%num%" Any numeric token, e.g. "23" * - "%bool%" true or false