Memory leak : Updated the multipass checking a little

This commit is contained in:
Daniel Marjamäki 2008-12-20 21:52:40 +00:00
parent 2888db49a9
commit a0cee01d35
3 changed files with 51 additions and 25 deletions

View File

@ -1348,8 +1348,45 @@ void CheckMemoryLeakClass::CheckMemoryLeak()
// Non-recursive function analysis
//---------------------------------------------------------------------------
TOKEN * CheckMemoryLeakClass::functionCode(const char funcname[])
TOKEN * CheckMemoryLeakClass::functionParameterCode(const TOKEN *ftok, int parameter)
{
int param = 1; // First parameter has index 1
// Extract the code for specified parameter...
for ( ; ftok; ftok = ftok->next() )
{
if ( ftok->str() == ")" )
break;
if ( ftok->str() == "," )
{
++param;
if ( param > parameter )
break;
}
if ( param != parameter )
continue;
if ( ! TOKEN::Match(ftok, "* %var% [,)]") )
continue;
// Extract and return the code for this parameter..
const char *parname = ftok->strAt(1);
// Goto function implementation..
while ( ftok && ftok->str() != "{" )
ftok = ftok->next();
ftok = ftok ? ftok->next() : NULL;
// Return the code..
AllocType alloc=No, dealloc=No;
std::list<const TOKEN *> callstack;
TOKEN *code = getcode( ftok, callstack, parname, alloc, dealloc );
simplifycode( code );
return code;
}
return NULL;
}

View File

@ -107,7 +107,7 @@ private:
#ifdef UNIT_TESTING
public:
#endif
TOKEN * functionCode(const char funcname[]);
TOKEN *functionParameterCode(const TOKEN *ftok, int parameter);
};
//---------------------------------------------------------------------------

View File

@ -36,12 +36,16 @@ private:
void run()
{
// TODO TEST_CASE( alloc1 );
TEST_CASE( param1 );
}
// Check that base classes have virtual destructors
std::string functionCode(const char code[], const char funcname[])
void param1()
{
const char code[] = "void f(char *s)\n"
"{\n"
" ;\n"
"}\n";
// Tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
@ -54,28 +58,13 @@ private:
// Check..
Settings settings;
CheckMemoryLeakClass checkMemoryLeak( &tokenizer, settings, this );
TOKEN *tok = checkMemoryLeak.functionCode(funcname);
TOKEN *tok = checkMemoryLeak.functionParameterCode(tokenizer.tokens(), 1);
// Return tokens..
std::ostringstream ret;
// Compare tokens..
std::string s;
for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next() )
ret << tok2->str() << " ";
while ( tok )
{
TOKEN *tok_ = tok;
tok = tok->next();
delete tok_;
}
return ret.str();
}
void alloc1()
{
const char code[] = "char *f()\n"
"{\n"
" return malloc(100);\n"
"}\n";
ASSERT_EQUALS( "alloc ;", functionCode(code, "f") );
s += tok2->str() + " ";
ASSERT_EQUALS( "; } ", s );
}
};