Memory leak : Reverted 796 and 797. I didn't see definite improvements

This commit is contained in:
Daniel Marjamäki 2008-12-29 11:31:19 +00:00
parent 6a83e24c90
commit 3947d8e797
1 changed files with 110 additions and 109 deletions

View File

@ -467,6 +467,14 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
{
addtoken("if(!var)");
}
else if ( TOKEN::Match(tok, "if ( true )") )
{
addtoken("if(true)");
}
else if ( TOKEN::Match(tok, "if ( false )") )
{
addtoken("if(false)");
}
else if ( TOKEN::Match(tok, "if") )
{
// Check if the condition depends on var somehow..
@ -683,7 +691,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
}
// Delete "if ; else ;"
else if ( TOKEN::simpleMatch(tok2->next(), "if ; else ;") )
else if ( TOKEN::Match(tok2->next(), "if ; else ;") )
{
erase( tok2, tok2->tokAt(4) );
done = false;
@ -718,7 +726,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
}
// Reduce "if if" => "if"
else if ( TOKEN::simpleMatch(tok2, "if if") )
else if ( TOKEN::Match(tok2, "if if") )
{
erase(tok2, tok2->tokAt(2));
done = false;
@ -747,14 +755,14 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
}
// Reduce "if ; else return use ;" => "if return use ;"
else if ( TOKEN::simpleMatch(tok2->next(), "if ; else return use ;") )
else if ( TOKEN::Match(tok2->next(), "if ; else return use ;") )
{
erase( tok2->next(), tok2->tokAt(4) );
done = false;
}
// Reduce "if return ; if return ;" => "if return ;"
else if ( TOKEN::simpleMatch(tok2->next(), "if return ; if return ;") )
else if ( TOKEN::Match(tok2->next(), "if return ; if return ;") )
{
erase( tok2, tok2->tokAt(4) );
done = false;
@ -813,14 +821,14 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
}
// Reduce "if* ;" that is not followed by an else..
if (TOKEN::Match(tok2->next(), "if(var)|if(!var)|ifv ; !!else") )
if (TOKEN::Match(tok2->next(), "if(var)|if(!var)|if(true)|if(false)|ifv ; !!else") )
{
erase(tok2, tok2->tokAt(2));
done = false;
}
// Reduce "else ;" => ";"
if ( TOKEN::simpleMatch(tok2->next(), "else ;") )
if ( TOKEN::Match(tok2->next(), "else ;") )
{
erase(tok2, tok2->tokAt(2));
done = false;
@ -835,7 +843,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
// Replace "dealloc use ;" with "dealloc ;"
if ( TOKEN::simpleMatch(tok2, "dealloc use ;") )
if ( TOKEN::Match(tok2, "dealloc use ;") )
{
erase(tok2, tok2->tokAt(2));
done = false;
@ -851,63 +859,65 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
// Reduce "do { alloc ; } " => "alloc ;"
// TODO: If the loop can be executed twice reduce to "loop alloc ;" instead
if ( TOKEN::simpleMatch(tok2->next(), "do { alloc ; }") )
if ( TOKEN::Match(tok2->next(), "do { alloc ; }") )
{
erase(tok2, tok2->tokAt(3));
erase(tok2->next()->next(), tok2->tokAt(4));
done = false;
}
if ( TOKEN::simpleMatch(tok2->next(), "loop") )
// Reduce "loop if break ; => ";"
if ( TOKEN::Match( tok2->next(), "loop if break|continue ; !!else") )
{
// Replace "loop ;" with ";"
if ( TOKEN::simpleMatch(tok2->tokAt(2), ";") )
{
erase(tok2, tok2->tokAt(2));
done = false;
}
erase( tok2, tok2->tokAt(4) );
done = false;
}
// Reduce "loop if break ; => ";"
if ( TOKEN::Match( tok2->tokAt(2), "if break|continue ; !!else") )
{
erase( tok2, tok2->tokAt(4) );
done = false;
}
// Reduce "loop { assign|dealloc|use ; alloc ; if break ; }" to "assign|dealloc|use ; alloc ;"
if ( TOKEN::Match( tok2->next(), "loop { assign|dealloc|use ; alloc ; if break|continue ; }" ) )
{
// erase "loop {"
erase( tok2, tok2->tokAt(3) );
// erase "if break|continue ; }"
tok2 = tok2->next()->next()->next()->next();
erase( tok2, tok2->tokAt(5) );
done = false;
}
// Replace "loop !var ;" with ";"
else if ( TOKEN::simpleMatch(tok2->tokAt(2), "!var ;") )
{
erase(tok2, tok2->tokAt(3));
done = false;
}
// Reduce "if(true) X ;" => "X ;"
if (TOKEN::Match(tok2->next(), "if(true) %var% ; !!else") )
{
erase( tok2, tok2->tokAt(2) );
done = false;
}
// Replace "loop !var alloc ;" with " alloc ;"
else if ( TOKEN::simpleMatch(tok2->tokAt(2), "!var alloc ;") )
{
erase(tok2, tok2->tokAt(3));
done = false;
}
// Replace "loop { X ; break ; }" with "X ;"
if ( TOKEN::Match(tok2->next(), "loop { %var% ; break ; }") )
{
erase(tok2, tok2->tokAt(3));
erase(tok2->next()->next(), tok2->tokAt(6));
done = false;
}
// Replace "loop { X ; break ; }" with "X ;"
else if ( TOKEN::Match(tok2->tokAt(2), "{ %var% ; break ; }") )
{
erase(tok2, tok2->tokAt(3));
erase(tok2->next()->next(), tok2->tokAt(6));
done = false;
}
// Replace "loop ;" with ";"
if ( TOKEN::Match(tok2->next(), "loop ;") )
{
erase(tok2, tok2->tokAt(2));
done = false;
}
// Reduce "loop { assign|dealloc|use ; alloc ; if break ; }" to "assign|dealloc|use ; alloc ;"
if ( TOKEN::Match( tok2->tokAt(2), "{ assign|dealloc|use ; alloc ; if break|continue ; }" ) )
{
// erase "loop {"
erase( tok2, tok2->tokAt(3) );
// erase "if break|continue ; }"
tok2 = tok2->next()->next()->next()->next();
erase( tok2, tok2->tokAt(5) );
done = false;
}
// Replace "loop !var ;" with ";"
if ( TOKEN::Match(tok2->next(), "loop !var ;") )
{
erase(tok2, tok2->tokAt(4));
done = false;
}
continue;
// Replace "loop !var alloc ;" with " alloc ;"
if ( TOKEN::Match(tok2->next(), "loop !var alloc ;") )
{
erase(tok2, tok2->tokAt(3));
done = false;
}
// Delete if block in "alloc ; if(!var) return ;"
@ -924,6 +934,20 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
done = false;
}
// Reduce "[;{}] return ; %var%" => "[;{}] return ;"
if ( TOKEN::Match(tok2, "[;{}] return ; %var%") )
{
erase( tok2->next()->next(), tok2->tokAt(4) );
done = false;
}
// Reduce "[;{}] return use ; %var%" => "[;{}] return use ;"
if ( TOKEN::Match(tok2, "[;{}] return use ; %var%") )
{
erase( tok2->next()->next()->next(), tok2->tokAt(5) );
done = false;
}
// Reduce "if(var) return use ;" => "return use ;"
if ( TOKEN::Match(tok2->next(), "if(var) return use ; !!else") )
{
@ -938,71 +962,51 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
done = false;
}
if ( TOKEN::Match(tok2, "[;{}]") )
// Reduce "[;{}] alloc ; dealloc ;" => "[;{}]"
if ( TOKEN::Match(tok2, "[;{}] alloc ; dealloc ;") )
{
// Reduce "[;{}] return ; %var%" => "[;{}] return ;"
while ( TOKEN::Match(tok2->next(), "return ; %var%") )
{
erase( tok2->next()->next(), tok2->tokAt(4) );
done = false;
}
// Reduce "[;{}] return use ; %var%" => "[;{}] return use ;"
while ( TOKEN::Match(tok2->next(), "return use ; %var%") )
{
erase( tok2->next()->next()->next(), tok2->tokAt(5) );
done = false;
}
// Reduce "[;{}] alloc ; dealloc ;" => "[;{}]"
if ( TOKEN::simpleMatch(tok2->next(), "alloc ; dealloc ;") )
{
erase( tok2, tok2->tokAt(5) );
done = false;
}
// Delete second use in "use ; use ;"
while (TOKEN::simpleMatch(tok2->next(), "use ; use ;"))
{
erase(tok2, tok2->tokAt(3));
done = false;
}
// Delete first part in "use ; dealloc ;"
if (TOKEN::simpleMatch(tok2->next(), "use ; dealloc ;"))
{
erase(tok2, tok2->tokAt(3));
done = false;
}
// Delete first part in "use ; return use ;"
if (TOKEN::simpleMatch(tok2, "use ; return use ;"))
{
erase(tok2, tok2->tokAt(2));
done = false;
}
continue;
erase( tok2, tok2->tokAt(5) );
done = false;
}
// Reduce "if* alloc ; dealloc ;" => ";"
if ( TOKEN::simpleMatch(tok2->tokAt(2), "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 ;"
while (TOKEN::Match(tok2, "[;{}] use ; use ;"))
{
erase(tok2, tok2->tokAt(3));
done = false;
}
// Delete first part in "use ; dealloc ;"
if (TOKEN::Match(tok2, "[;{}] use ; dealloc ;"))
{
erase(tok2, tok2->tokAt(3));
done = false;
}
// Delete first part in "use ; return use ;"
if (TOKEN::Match(tok2, "[;{}] use ; return use ;"))
{
erase(tok2, tok2->tokAt(2));
done = false;
}
// Delete second case in "case ; case ;"
while (TOKEN::simpleMatch(tok2, "case ; case ;"))
while (TOKEN::Match(tok2, "case ; case ;"))
{
erase(tok2, tok2->tokAt(3));
done = false;
}
// Replace switch with if (if not complicated)
if (TOKEN::simpleMatch(tok2, "switch {"))
if (TOKEN::Match(tok2, "switch {"))
{
// Right now, I just handle if there are a few case and perhaps a default.
bool valid = false;
@ -1027,14 +1031,11 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
else if (_tok->str() == "loop")
break;
else if (incase && _tok->str()=="case")
else if (incase && TOKEN::Match(_tok,"case"))
break;
else if (_tok->str() == "case")
incase = true;
else if (_tok->str() == "break")
incase = false;
incase |= TOKEN::Match(_tok,"case");
incase &= !TOKEN::Match(_tok,"break");
}
if ( !incase && valid )
@ -1044,9 +1045,9 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
erase( tok2, tok2->tokAt(2) );
tok2 = tok2->next();
bool first = true;
while (TOKEN::Match(tok2, "case|default"))
while (TOKEN::Match(tok2,"case") || TOKEN::Match(tok2,"default"))
{
bool def = bool(tok2->str() == "default");
bool def = TOKEN::Match(tok2, "default");
tok2->setstr(first ? "if" : "}");
if ( first )
{
@ -1061,9 +1062,9 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
instoken( tok2, "if" );
instoken( tok2, "else" );
}
while ( tok2 && tok2->str() != "}" && ! TOKEN::simpleMatch(tok2,"break ;") )
while ( tok2 && tok2->str() != "}" && ! TOKEN::Match(tok2,"break ;") )
tok2 = tok2->next();
if (TOKEN::simpleMatch(tok2,"break ;"))
if (TOKEN::Match(tok2,"break ;"))
{
tok2->setstr(";");
tok2 = tok2->next()->next();