fix 2838 (Token::Match called with varid 0 on auto_ptr check) cleanup code and better check varid

This commit is contained in:
seb777 2011-06-17 21:09:27 +02:00
parent 8b70046a42
commit 20de3f90ef
2 changed files with 18 additions and 7 deletions

View File

@ -1113,7 +1113,6 @@ void CheckStl::string_c_strError(const Token *tok)
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void CheckStl::checkAutoPointer() void CheckStl::checkAutoPointer()
{ {
std::set<int> autoPtrVarId; std::set<int> autoPtrVarId;
std::set<int>::const_iterator iter; std::set<int>::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"; 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::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); autoPointerContainerError(tok);
} }
@ -1131,21 +1130,21 @@ void CheckStl::checkAutoPointer()
const Token *tok2 = tok->next()->next(); const Token *tok2 = tok->next()->next();
while (tok2) while (tok2)
{ {
if (Token::Match(tok2, "> %var%")) if (Token::Match(tok2, "> %var%"))
{ {
const Token *tok3 = tok2->next()->next(); const Token *tok3 = tok2->next()->next();
while (tok3 && ! Token::simpleMatch(tok3, ";")) while (tok3 && tok3->str() != ";")
{ {
tok3 = tok3->next(); tok3 = tok3->next();
} }
if (Token::Match(tok3->previous()->previous(),"] )")) tok3 = tok3->previous()->previous();
if (Token::Match(tok3, "] )"))
{ {
autoPointerArrayError(tok2->next()); 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) if (decltok)
{ {
autoPointerArrayError(tok2->next()); autoPointerArrayError(tok2->next());

View File

@ -1321,6 +1321,18 @@ private:
void autoPointer() void autoPointer()
{ {
// ticket 2839
check("template <class MUTEX_TYPE>\n"
"class Guarded\n"
"{\n"
" typedef std::auto_ptr<MUTEX_TYPE > WriteGuardType;\n"
" virtual WriteGuardType getWriteGuard(bool enabledGuard = true);\n"
"};\n"
"class SafeSharedMemory : public Guarded<int>\n"
"{\n"
"};\n");
ASSERT_EQUALS("", errout.str());
check("void foo()\n" check("void foo()\n"
"{\n" "{\n"
" auto_ptr< ns1:::MyClass > y;\n" " auto_ptr< ns1:::MyClass > y;\n"