Memory leak : Optimising the 'CheckMemoryLeakClass::simplifycode'

This commit is contained in:
Daniel Marjamäki 2008-12-29 08:07:25 +00:00
parent c3954afdef
commit 7e43f9f7f9
1 changed files with 111 additions and 97 deletions

View File

@ -691,7 +691,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
} }
// Delete "if ; else ;" // Delete "if ; else ;"
else if ( TOKEN::Match(tok2->next(), "if ; else ;") ) else if ( TOKEN::simpleMatch(tok2->next(), "if ; else ;") )
{ {
erase( tok2, tok2->tokAt(4) ); erase( tok2, tok2->tokAt(4) );
done = false; done = false;
@ -726,7 +726,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
} }
// Reduce "if if" => "if" // Reduce "if if" => "if"
else if ( TOKEN::Match(tok2, "if if") ) else if ( TOKEN::simpleMatch(tok2, "if if") )
{ {
erase(tok2, tok2->tokAt(2)); erase(tok2, tok2->tokAt(2));
done = false; done = false;
@ -755,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::Match(tok2->next(), "if ; else return use ;") ) else if ( TOKEN::simpleMatch(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::Match(tok2->next(), "if return ; if return ;") ) else if ( TOKEN::simpleMatch(tok2->next(), "if return ; if return ;") )
{ {
erase( tok2, tok2->tokAt(4) ); erase( tok2, tok2->tokAt(4) );
done = false; done = false;
@ -828,7 +828,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
} }
// Reduce "else ;" => ";" // Reduce "else ;" => ";"
if ( TOKEN::Match(tok2->next(), "else ;") ) if ( TOKEN::simpleMatch(tok2->next(), "else ;") )
{ {
erase(tok2, tok2->tokAt(2)); erase(tok2, tok2->tokAt(2));
done = false; done = false;
@ -843,7 +843,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
// Replace "dealloc use ;" with "dealloc ;" // Replace "dealloc use ;" with "dealloc ;"
if ( TOKEN::Match(tok2, "dealloc use ;") ) if ( TOKEN::simpleMatch(tok2, "dealloc use ;") )
{ {
erase(tok2, tok2->tokAt(2)); erase(tok2, tok2->tokAt(2));
done = false; done = false;
@ -859,31 +859,13 @@ 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::Match(tok2->next(), "do { alloc ; }") ) if ( TOKEN::simpleMatch(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;
} }
// Reduce "loop if break ; => ";"
if ( TOKEN::Match( tok2->next(), "loop 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;
}
// Reduce "if(true) X ;" => "X ;" // Reduce "if(true) X ;" => "X ;"
if (TOKEN::Match(tok2->next(), "if(true) %var% ; !!else") ) if (TOKEN::Match(tok2->next(), "if(true) %var% ; !!else") )
{ {
@ -891,33 +873,56 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
done = false; done = false;
} }
// Replace "loop { X ; break ; }" with "X ;" if ( TOKEN::simpleMatch(tok2->next(), "loop") )
if ( TOKEN::Match(tok2->next(), "loop { %var% ; break ; }") )
{ {
erase(tok2, tok2->tokAt(3)); // Replace "loop ;" with ";"
erase(tok2->next()->next(), tok2->tokAt(6)); if ( TOKEN::simpleMatch(tok2->tokAt(2), ";") )
done = false; {
} erase(tok2, tok2->tokAt(2));
done = false;
}
// Replace "loop ;" with ";" // Reduce "loop if break ; => ";"
if ( TOKEN::Match(tok2->next(), "loop ;") ) if ( TOKEN::Match( tok2->tokAt(2), "if break|continue ; !!else") )
{ {
erase(tok2, tok2->tokAt(2)); erase( tok2, tok2->tokAt(4) );
done = false; done = false;
} }
// Replace "loop !var ;" with ";" // Replace "loop !var ;" with ";"
if ( TOKEN::Match(tok2->next(), "loop !var ;") ) else if ( TOKEN::simpleMatch(tok2->tokAt(2), "!var ;") )
{ {
erase(tok2, tok2->tokAt(4)); erase(tok2, tok2->tokAt(3));
done = false; done = false;
} }
// Replace "loop !var alloc ;" with " alloc ;" // Replace "loop !var alloc ;" with " alloc ;"
if ( TOKEN::Match(tok2->next(), "loop !var alloc ;") ) else if ( TOKEN::simpleMatch(tok2->tokAt(2), "!var alloc ;") )
{ {
erase(tok2, tok2->tokAt(3)); erase(tok2, tok2->tokAt(3));
done = false; 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;
}
// 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;
}
continue;
} }
// Delete if block in "alloc ; if(!var) return ;" // Delete if block in "alloc ; if(!var) return ;"
@ -934,20 +939,6 @@ 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") )
{ {
@ -962,51 +953,71 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
done = false; done = false;
} }
// Reduce "[;{}] alloc ; dealloc ;" => "[;{}]" if ( TOKEN::Match(tok2, "[;{}]") )
if ( TOKEN::Match(tok2, "[;{}] alloc ; dealloc ;") )
{ {
erase( tok2, tok2->tokAt(5) );
done = false; // 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;
} }
// Reduce "if* alloc ; dealloc ;" => ";" // Reduce "if* alloc ; dealloc ;" => ";"
if ( TOKEN::Match(tok2->tokAt(2), "alloc ; dealloc ;") && if ( TOKEN::simpleMatch(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::Match(tok2, "case ; case ;")) while (TOKEN::simpleMatch(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::Match(tok2, "switch {")) if (TOKEN::simpleMatch(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;
@ -1031,11 +1042,14 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
else if (_tok->str() == "loop") else if (_tok->str() == "loop")
break; break;
else if (incase && TOKEN::Match(_tok,"case")) else if (incase && _tok->str()=="case")
break; break;
incase |= TOKEN::Match(_tok,"case"); else if (_tok->str() == "case")
incase &= !TOKEN::Match(_tok,"break"); incase = true;
else if (_tok->str() == "break")
incase = false;
} }
if ( !incase && valid ) if ( !incase && valid )
@ -1045,9 +1059,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") || TOKEN::Match(tok2,"default")) while (TOKEN::Match(tok2, "case|default"))
{ {
bool def = TOKEN::Match(tok2, "default"); bool def = bool(tok2->str() == "default");
tok2->setstr(first ? "if" : "}"); tok2->setstr(first ? "if" : "}");
if ( first ) if ( first )
{ {
@ -1062,9 +1076,9 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
instoken( tok2, "if" ); instoken( tok2, "if" );
instoken( tok2, "else" ); instoken( tok2, "else" );
} }
while ( tok2 && tok2->str() != "}" && ! TOKEN::Match(tok2,"break ;") ) while ( tok2 && tok2->str() != "}" && ! TOKEN::simpleMatch(tok2,"break ;") )
tok2 = tok2->next(); tok2 = tok2->next();
if (TOKEN::Match(tok2,"break ;")) if (TOKEN::simpleMatch(tok2,"break ;"))
{ {
tok2->setstr(";"); tok2->setstr(";");
tok2 = tok2->next()->next(); tok2 = tok2->next()->next();