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 // 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; return NULL;
} }

View File

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

View File

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