From 20de3f90efff939a4584ee401553f3b73b4cd438 Mon Sep 17 00:00:00 2001 From: seb777 Date: Fri, 17 Jun 2011 21:09:27 +0200 Subject: [PATCH] fix 2838 (Token::Match called with varid 0 on auto_ptr check) cleanup code and better check varid --- lib/checkstl.cpp | 13 ++++++------- test/teststl.cpp | 12 ++++++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 5e5e68d7e..916bc0564 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -1113,7 +1113,6 @@ void CheckStl::string_c_strError(const Token *tok) //--------------------------------------------------------------------------- void CheckStl::checkAutoPointer() { - std::set autoPtrVarId; std::set::const_iterator iter; static const char STL_CONTAINER_LIST[] = "bitset|deque|list|map|multimap|multiset|priority_queue|queue|set|stack|hash_map|hash_multimap|hash_set|vector"; @@ -1122,7 +1121,7 @@ void CheckStl::checkAutoPointer() { if (Token::simpleMatch(tok, "auto_ptr <")) { - if ((Token::Match(tok->tokAt(-1), "< auto_ptr") && Token::Match(tok->tokAt(-2), STL_CONTAINER_LIST)) || (Token::Match(tok->tokAt(-3), "< std :: auto_ptr") && Token::Match(tok->tokAt(-4), STL_CONTAINER_LIST))) + if ((tok->previous()->str() == "<" && Token::Match(tok->tokAt(-2), STL_CONTAINER_LIST)) || (Token::Match(tok->tokAt(-3), "< std :: auto_ptr") && Token::Match(tok->tokAt(-4), STL_CONTAINER_LIST))) { autoPointerContainerError(tok); } @@ -1131,21 +1130,21 @@ void CheckStl::checkAutoPointer() const Token *tok2 = tok->next()->next(); while (tok2) { - if (Token::Match(tok2, "> %var%")) { const Token *tok3 = tok2->next()->next(); - while (tok3 && ! Token::simpleMatch(tok3, ";")) + while (tok3 && tok3->str() != ";") { tok3 = tok3->next(); } - if (Token::Match(tok3->previous()->previous(),"] )")) + tok3 = tok3->previous()->previous(); + if (Token::Match(tok3, "] )")) { autoPointerArrayError(tok2->next()); } - else if (Token::Match(tok3->previous()->previous(),"%var% )")) + else if (tok3->varId()) { - const Token *decltok = Token::findmatch(_tokenizer->tokens(), "%varid% = new %type% [", tok3->previous()->previous()->varId()); + const Token *decltok = Token::findmatch(_tokenizer->tokens(), "%varid% = new %type% [", tok3->varId()); if (decltok) { autoPointerArrayError(tok2->next()); diff --git a/test/teststl.cpp b/test/teststl.cpp index 09c0ebb3c..3ecdfadf0 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -1321,6 +1321,18 @@ private: void autoPointer() { + // ticket 2839 + check("template \n" + "class Guarded\n" + "{\n" + " typedef std::auto_ptr WriteGuardType;\n" + " virtual WriteGuardType getWriteGuard(bool enabledGuard = true);\n" + "};\n" + "class SafeSharedMemory : public Guarded\n" + "{\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); + check("void foo()\n" "{\n" " auto_ptr< ns1:::MyClass > y;\n"