Memory leaks: Made the checking a lot more sensitive

This commit is contained in:
Daniel Marjamäki 2008-11-26 18:13:36 +00:00
parent 2e22c7cb1d
commit 997f4e6165
2 changed files with 48 additions and 12 deletions

View File

@ -23,6 +23,7 @@
#include <algorithm>
#include <iostream>
#include <sstream>
#ifdef __BORLANDC__
@ -220,13 +221,30 @@ const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::list<const
ftok = ftok->next;
TOKEN *func = getcode( ftok->tokAt(1), callstack, parname, alloctype, dealloctype );
simplifycode( func );
const TOKEN *func_ = func;
while ( func_ && func_->str() == ";" )
func_ = func_->next;
/*
for (const TOKEN *t = func_; t; t = t->next)
{
std::cout << t->str() << "\n";
}
*/
const char *ret = 0;
if (TOKEN::findmatch(func, "goto"))
ret = "dealloc"; // TODO : "goto" isn't handled well
else if (TOKEN::findmatch(func, "use"))
ret = "use";
else if (TOKEN::findmatch(func, "dealloc"))
if (TOKEN::findmatch(func_, "goto"))
{
// TODO : "goto" isn't handled well
if ( TOKEN::findmatch(func_, "dealloc") )
ret = "dealloc";
else if ( TOKEN::findmatch(func_, "use") )
ret = "use";
}
else if (TOKEN::Match(func_, "dealloc"))
ret = "dealloc";
else if (TOKEN::Match(func_, "use"))
ret = "use";
Tokenizer::deleteTokens(func);
return ret;
}
@ -614,6 +632,22 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
}
}
// Delete "if ; else ;"
if ( TOKEN::Match(tok2->next, "if ; else ;") )
{
erase( tok2, tok2->tokAt(4) );
done = false;
}
// TODO Make this more generic. Delete "if ; else use ; use"
if ( TOKEN::Match(tok2, "; if ; else use ; use") ||
TOKEN::Match(tok2, "; if use ; else ; use") )
{
erase( tok2, tok2->tokAt(4) );
done = false;
}
// Delete "if dealloc ;" and "if use ;" that is not followed by an else..
// This may cause false positives
if (_settings._showAll &&

View File

@ -24,6 +24,7 @@
#include "CheckMemoryLeak.h"
#include "testsuite.h"
#include <iostream>
#include <sstream>
extern std::ostringstream errout;
@ -111,7 +112,7 @@ private:
TEST_CASE( func4 );
TEST_CASE( func5 );
TEST_CASE( func6 );
// TODO TEST_CASE( func7 );
TEST_CASE( func7 );
TEST_CASE( class1 );
TEST_CASE( class2 );
@ -779,7 +780,7 @@ private:
" foo(p);\n"
"}\n" );
std::string err( errout.str() );
ASSERT_EQUALS( std::string(""), err );
ASSERT_EQUALS( std::string("[test.cpp:10]: Memory leak: p\n"), err );
}
@ -798,7 +799,8 @@ private:
" foo(p);\n"
"}\n" );
std::string err( errout.str() );
ASSERT_EQUALS( std::string("[test.cpp:4]: Memory leak: p\n"), err );
std::cout << err << "\n";
ASSERT_EQUALS( std::string("[test.cpp:11]: Memory leak: p\n"), err );
}