Memory leak: Refactoring and improvements of simplifycode etc

This commit is contained in:
Daniel Marjamäki 2008-12-01 18:52:40 +00:00
parent bbd21613f7
commit a8bf4d7e77
2 changed files with 65 additions and 23 deletions

View File

@ -141,14 +141,6 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType( const T
CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetDeallocationType( const TOKEN *tok, const char *varnames[] ) CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetDeallocationType( const TOKEN *tok, const char *varnames[] )
{ {
// Redundant condition..
if ( TOKEN::Match(tok, "if ( %var1% )", varnames) )
{
tok = tok->tokAt(4);
if (tok->str() =="{")
tok = tok->next;
}
if ( TOKEN::Match(tok, "delete %var1% ;", varnames) ) if ( TOKEN::Match(tok, "delete %var1% ;", varnames) )
return New; return New;
@ -336,9 +328,9 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
} }
else if ( tok->aaaa0() == '}' ) else if ( tok->aaaa0() == '}' )
{ {
addtoken( "}" );
if ( indentlevel <= 0 ) if ( indentlevel <= 0 )
break; break;
addtoken( "}" );
indentlevel--; indentlevel--;
} }
@ -579,7 +571,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
erase( tok2->next->next, tok2->tokAt(4) ); erase( tok2->next->next, tok2->tokAt(4) );
done = false; done = false;
} }
if ( TOKEN::Match(tok2->next, "{ return use ; }") ) if ( TOKEN::Match(tok2->next, "{ %var% %var% ; }") )
{ {
erase( tok2, tok2->tokAt(2) ); erase( tok2, tok2->tokAt(2) );
erase( tok2->next->next->next, tok2->tokAt(5) ); erase( tok2->next->next->next, tok2->tokAt(5) );
@ -624,6 +616,28 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
done = false; done = false;
} }
// Reduce "if(var) dealloc ;" and "if(var) use ;" that is not followed by an else..
if ((TOKEN::Match(tok2, "[;{}] if(var) dealloc ;") || TOKEN::Match(tok2, "[;{}] if(var) use ;")) &&
!TOKEN::Match(tok2->tokAt(4), "else"))
{
erase(tok2, tok2->tokAt(2));
done = false;
}
// Reduce "if if" => "if"
if ( TOKEN::Match(tok2, "if if") )
{
erase(tok2, tok2->tokAt(2));
done = false;
}
// Reduce "else ;" => ";"
if ( TOKEN::Match(tok2->next, "else ;") )
{
erase(tok2, tok2->tokAt(2));
done = false;
}
// Delete if block: "alloc; if return use ;" // Delete if block: "alloc; if return use ;"
if (TOKEN::Match(tok2,"alloc ; if return use ;") && !TOKEN::Match(tok2->tokAt(6),"else")) if (TOKEN::Match(tok2,"alloc ; if return use ;") && !TOKEN::Match(tok2->tokAt(6),"else"))
{ {
@ -631,6 +645,14 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
done = false; done = false;
} }
// Reduce "if return ; alloc ;" => "alloc ;"
if (TOKEN::Match(tok2, "[;{}] if return ; alloc ;"))
{
erase(tok2, tok2->tokAt(4));
done = false;
}
// "[;{}] if alloc ; else return ;" => "[;{}] alloc ;" // "[;{}] if alloc ; else return ;" => "[;{}] alloc ;"
if (TOKEN::Match(tok2,"[;{}] if alloc ; else return ;")) if (TOKEN::Match(tok2,"[;{}] if alloc ; else return ;"))
{ {
@ -714,6 +736,19 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
// Delete if block in "alloc ; if(!var) return ;" // Delete if block in "alloc ; if(!var) return ;"
if ( TOKEN::Match(tok2, "alloc ; if(!var) return ;") ) if ( TOKEN::Match(tok2, "alloc ; if(!var) return ;") )
{
erase(tok2, tok2->tokAt(4));
done = false;
}
// Delete if block: "alloc; if return use ;"
if (TOKEN::Match(tok2,"alloc ; if return use ;") && !TOKEN::Match(tok2->tokAt(6),"else"))
{
erase(tok2, tok2->tokAt(5));
done = false;
}
// Reduce "if return ; if return ;" => "if return ;"
if ( TOKEN::Match(tok2->next, "if return ; if return ;") )
{ {
erase( tok2, tok2->tokAt(4) ); erase( tok2, tok2->tokAt(4) );
done = false; done = false;
@ -733,6 +768,14 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
done = false; done = false;
} }
// Reduce "if* alloc ; dealloc ;" => ";"
if ( TOKEN::Match(tok2->tokAt(2), "alloc ; dealloc ;") &&
tok2->next->str().find("if") == 0 )
{
erase( tok2, tok2->tokAt(5) );
done = false;
}
// Delete second use in "use ; use ;" // Delete second use in "use ; use ;"
while (TOKEN::Match(tok2, "[;{}] use ; use ;")) while (TOKEN::Match(tok2, "[;{}] use ; use ;"))
{ {
@ -837,6 +880,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
AllocType dealloctype = No; AllocType dealloctype = No;
TOKEN *tok = getcode( Tok1, callstack, varname, alloctype, dealloctype ); TOKEN *tok = getcode( Tok1, callstack, varname, alloctype, dealloctype );
simplifycode( tok );
// If the variable is not allocated at all => no memory leak // If the variable is not allocated at all => no memory leak
if (TOKEN::findmatch(tok, "alloc") == 0) if (TOKEN::findmatch(tok, "alloc") == 0)
@ -852,8 +896,6 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
return; return;
} }
simplifycode( tok );
if ( TOKEN::findmatch(tok, "loop alloc ;") ) if ( TOKEN::findmatch(tok, "loop alloc ;") )
{ {
MemoryLeak(TOKEN::findmatch(tok, "loop alloc ;"), varname); MemoryLeak(TOKEN::findmatch(tok, "loop alloc ;"), varname);
@ -923,6 +965,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
noerr |= TOKEN::Match( first, "if alloc ; dealloc ;" ); noerr |= TOKEN::Match( first, "if alloc ; dealloc ;" );
noerr |= TOKEN::Match( first, "if alloc ; return use ;" ); noerr |= TOKEN::Match( first, "if alloc ; return use ;" );
noerr |= TOKEN::Match( first, "if alloc ; use ;" ); noerr |= TOKEN::Match( first, "if alloc ; use ;" );
noerr |= TOKEN::Match( first, "alloc ; ifv return ; dealloc ;" );
// Unhandled case.. // Unhandled case..
if ( ! noerr ) if ( ! noerr )

View File

@ -58,7 +58,7 @@ private:
void run() void run()
{ {
// TODO TEST_CASE( simple1 ); TEST_CASE( simple1 );
TEST_CASE( simple2 ); TEST_CASE( simple2 );
TEST_CASE( simple3 ); TEST_CASE( simple3 );
TEST_CASE( simple4 ); TEST_CASE( simple4 );
@ -72,7 +72,7 @@ private:
TEST_CASE( ifelse1 ); TEST_CASE( ifelse1 );
TEST_CASE( ifelse2 ); TEST_CASE( ifelse2 );
// TODO TEST_CASE( ifelse3 ); TEST_CASE( ifelse3 );
TEST_CASE( ifelse4 ); TEST_CASE( ifelse4 );
TEST_CASE( ifelse5 ); TEST_CASE( ifelse5 );
TEST_CASE( ifelse6 ); TEST_CASE( ifelse6 );
@ -93,13 +93,13 @@ private:
TEST_CASE( forwhile3 ); TEST_CASE( forwhile3 );
TEST_CASE( forwhile4 ); TEST_CASE( forwhile4 );
TEST_CASE( forwhile5 ); TEST_CASE( forwhile5 );
// TODO TEST_CASE( forwhile6 ); TEST_CASE( forwhile6 );
TEST_CASE( forwhile7 ); TEST_CASE( forwhile7 );
TEST_CASE( dowhile1 ); TEST_CASE( dowhile1 );
TEST_CASE( switch1 ); TEST_CASE( switch1 );
// TODO TEST_CASE( switch2 ); TEST_CASE( switch2 );
TEST_CASE( ret1 ); TEST_CASE( ret1 );
TEST_CASE( ret2 ); TEST_CASE( ret2 );
@ -108,10 +108,10 @@ private:
TEST_CASE( func1 ); TEST_CASE( func1 );
TEST_CASE( func2 ); TEST_CASE( func2 );
// TODO TEST_CASE( func3 ); TEST_CASE( func3 );
TEST_CASE( func4 ); TEST_CASE( func4 );
TEST_CASE( func5 ); TEST_CASE( func5 );
// TODO TEST_CASE( func6 ); TEST_CASE( func6 );
// TODO TEST_CASE( func7 ); // TODO TEST_CASE( func7 );
TEST_CASE( func8 ); // Using callback TEST_CASE( func8 ); // Using callback
@ -440,8 +440,7 @@ private:
" if (b)\n" " if (b)\n"
" free(s);\n" " free(s);\n"
"}\n" ); "}\n" );
std::string err( errout.str() ); ASSERT_EQUALS( std::string(""), errout.str() );
ASSERT_EQUALS( std::string(""), err );
} }
void if5() void if5()