From fbd3d92aa95ab1238548c8a7e4a45b112be388ab Mon Sep 17 00:00:00 2001 From: Pete Johns Date: Wed, 13 Oct 2010 20:31:41 +1100 Subject: [PATCH] Fixed #2084 (False positive: object destroyed immediately (struct and function with same name)) Tightened up class definition matching so as not to match C-style struct tags. --- lib/checkother.cpp | 7 ++++--- test/testother.cpp | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 346270f33..3aa5e6f6d 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3884,8 +3884,8 @@ bool CheckOther::isIdentifierObjectType(const Token * const tok) return found->second; } - const std::string classDef = std::string("class|struct ") + identifier; - const bool result = Token::findmatch(_tokenizer->tokens(), classDef.c_str()); + const std::string classDefnOrDecl = std::string("class|struct ") + identifier + " [{:;]"; + const bool result = Token::findmatch(_tokenizer->tokens(), classDefnOrDecl.c_str()); isClassResults.insert(std::make_pair(identifier, result)); return result; } @@ -3893,6 +3893,7 @@ bool CheckOther::isIdentifierObjectType(const Token * const tok) void CheckOther::checkMisusedScopedObject() { + const char localClassDefinition[] = "class|struct %var% [{:]"; bool withinFunction = false; unsigned int depth = 0; std::string className = ""; @@ -3917,7 +3918,7 @@ void CheckOther::checkMisusedScopedObject() className.clear(); } } - else if (Token::Match(tok, "class|struct")) + else if (Token::Match(tok, localClassDefinition)) { className = tok->strAt(1); isClassResults.insert(std::make_pair(className, false)); diff --git a/test/testother.cpp b/test/testother.cpp index 931ddc247..1e4b12bd0 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -115,6 +115,7 @@ private: TEST_CASE(testMisusedScopeObjectDoesNotPickLocalClassConstructors); TEST_CASE(testMisusedScopeObjectDoesNotPickUsedObject); TEST_CASE(trac2071); + TEST_CASE(trac2084); TEST_CASE(assignmentInAssert); } @@ -3160,6 +3161,21 @@ private: ASSERT_EQUALS("", errout.str()); } + void trac2084() + { + check("#include \n" + "\n" + "void f()\n" + "{\n" + " struct sigaction sa;\n" + "\n" + " { sigaction(SIGHUP, &sa, 0); };\n" + " { sigaction(SIGINT, &sa, 0); };\n" + "}\n" + ); + ASSERT_EQUALS("", errout.str()); + } + void trac2071() { check("void f() {\n"