Memory leak: handle allocation in deallocation list

This commit is contained in:
Daniel Marjamäki 2009-03-14 20:19:36 +01:00
parent 02df14ec37
commit 75d0e8687b
2 changed files with 19 additions and 7 deletions

View File

@ -104,7 +104,7 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType(const To
return gMalloc;
}
if (Token::Match(tok2, "new %type% [;(]"))
if (Token::Match(tok2, "new %type% [;()]"))
return New;
if (Token::Match(tok2, "new %type% ["))
@ -1467,6 +1467,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable(const char clas
while (functionToken)
{
int indent = 0;
bool initlist = false;
for (const Token *tok = functionToken; tok; tok = tok->next())
{
if (tok->str() == "{")
@ -1477,12 +1478,20 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable(const char clas
if (indent <= 0)
break;
}
else if (indent > 0)
else if (indent == 0 && Token::simpleMatch(tok, ") :"))
initlist = true;
else if (initlist || indent > 0)
{
// Allocate..
if (Token::Match(tok, (std::string(varname) + " =").c_str()))
if (indent == 0)
{
AllocType alloc = GetAllocationType(tok->tokAt(2));
if (!Token::Match(tok, (":|, " + std::string(varname) + " (").c_str()))
continue;
}
// Allocate..
if (indent == 0 || Token::Match(tok, (std::string(varname) + " =").c_str()))
{
AllocType alloc = GetAllocationType(tok->tokAt((indent>0) ? 2 : 3));
if (alloc != No)
{
if (Alloc != No && Alloc != alloc)
@ -1500,6 +1509,9 @@ void CheckMemoryLeakClass::CheckMemoryLeak_ClassMembers_Variable(const char clas
}
}
if (indent == 0)
continue;
// Deallocate..
const char *varnames[3] = { "var", 0, 0 };
varnames[0] = varname;

View File

@ -153,7 +153,7 @@ private:
TEST_CASE(class8);
TEST_CASE(class9);
TEST_CASE(class10);
// TODO TEST_CASE(class11);
TEST_CASE(class11);
TEST_CASE(throw1);
TEST_CASE(throw2);
@ -1538,7 +1538,7 @@ private:
"};\n"
"A::A() : p(new int[10])\n"
"{ }", true);
ASSERT_EQUALS("memory leak", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (all) Memory leak: A::p\n", errout.str());
}