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