CheckMemoryLeakInFunction::simplifycode : use properly 'deleteNext' instead of 'deleteThis' or 'eraseTokens' and remove redundant conversion to 'std::string'.

This commit is contained in:
Edoardo Prezioso 2011-12-05 15:50:11 +01:00
parent 186ab3645e
commit c8bbb77776
1 changed files with 110 additions and 53 deletions

View File

@ -1530,7 +1530,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
// If the code starts with "if return ;" then remove it // If the code starts with "if return ;" then remove it
if (Token::Match(tok, ";| if return ;")) { if (Token::Match(tok, ";| if return ;")) {
tok->deleteThis(); tok->deleteNext();
tok->deleteThis(); tok->deleteThis();
if (tok->str() == "return") if (tok->str() == "return")
tok->deleteThis(); tok->deleteThis();
@ -1559,8 +1559,12 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
if (Token::simpleMatch(tok2, "while1 { if { dealloc ; return ; } }")) { if (Token::simpleMatch(tok2, "while1 { if { dealloc ; return ; } }")) {
tok2->str(";"); tok2->str(";");
Token::eraseTokens(tok2, tok2->tokAt(4)); tok2->deleteNext();
Token::eraseTokens(tok2->tokAt(4), tok2->tokAt(7)); tok2->deleteNext();
tok2->deleteNext();
Token *t = tok2->tokAt(4);
t->deleteNext();
t->deleteNext();
} }
} }
} }
@ -1575,7 +1579,8 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
// Replace "{ }" with ";" // Replace "{ }" with ";"
if (Token::simpleMatch(tok2->next(), "{ }")) { if (Token::simpleMatch(tok2->next(), "{ }")) {
tok2->eraseTokens(tok2, tok2->tokAt(3)); tok2->deleteNext();
tok2->deleteNext();
tok2->insertToken(";"); tok2->insertToken(";");
done = false; done = false;
} }
@ -1583,12 +1588,12 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
// Delete braces around a single instruction.. // Delete braces around a single instruction..
if (Token::Match(tok2->next(), "{ %var% ; }")) { if (Token::Match(tok2->next(), "{ %var% ; }")) {
tok2->deleteNext(); tok2->deleteNext();
Token::eraseTokens(tok2->tokAt(2), tok2->tokAt(4)); tok2->tokAt(2)->deleteNext();
done = false; done = false;
} }
if (Token::Match(tok2->next(), "{ %var% %var% ; }")) { if (Token::Match(tok2->next(), "{ %var% %var% ; }")) {
tok2->deleteNext(); tok2->deleteNext();
Token::eraseTokens(tok2->tokAt(3), tok2->tokAt(5)); tok2->tokAt(3)->deleteNext();
done = false; done = false;
} }
@ -1600,8 +1605,9 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
// outer/inner if blocks. Remove outer condition.. // outer/inner if blocks. Remove outer condition..
else if (Token::Match(tok2->next(), "if|if(var) { if return use ; }")) { else if (Token::Match(tok2->next(), "if|if(var) { if return use ; }")) {
tok2->tokAt(6)->deleteNext(); tok2->deleteNext();
Token::eraseTokens(tok2, tok2->tokAt(3)); tok2->deleteNext();
tok2->tokAt(4)->deleteNext();
done = false; done = false;
} }
@ -1614,57 +1620,79 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
// Reduce "if X ; else X ;" => "X ;" // Reduce "if X ; else X ;" => "X ;"
else if (Token::Match(tok2->next(), "if %var% ; else %var% ;") && else if (Token::Match(tok2->next(), "if %var% ; else %var% ;") &&
std::string(tok2->strAt(2)) == std::string(tok2->strAt(5))) { tok2->strAt(2) == tok2->strAt(5)) {
Token::eraseTokens(tok2, tok2->tokAt(5)); tok2->deleteNext();
tok2->deleteNext();
tok2->deleteNext();
tok2->deleteNext();
done = false; done = false;
} }
// Reduce "if continue ; if continue ;" => "if continue ;" // Reduce "if continue ; if continue ;" => "if continue ;"
else if (Token::simpleMatch(tok2->next(), "if continue ; if continue ;")) { else if (Token::simpleMatch(tok2->next(), "if continue ; if continue ;")) {
Token::eraseTokens(tok2, tok2->tokAt(4)); tok2->deleteNext();
tok2->deleteNext();
tok2->deleteNext();
done = false; done = false;
} }
// Reduce "if return ; alloc ;" => "alloc ;" // Reduce "if return ; alloc ;" => "alloc ;"
else if (Token::Match(tok2, "[;{}] if return ; alloc|return ;")) { else if (Token::Match(tok2, "[;{}] if return ; alloc|return ;")) {
Token::eraseTokens(tok2, tok2->tokAt(4)); tok2->deleteNext();
tok2->deleteNext();
tok2->deleteNext();
done = false; done = false;
} }
// "[;{}] if alloc ; else return ;" => "[;{}] alloc ;" // "[;{}] if alloc ; else return ;" => "[;{}] alloc ;"
else if (Token::Match(tok2, "[;{}] if alloc ; else return ;")) { else if (Token::Match(tok2, "[;{}] if alloc ; else return ;")) {
tok2->deleteNext(); // Remove "if" // Remove "if"
Token::eraseTokens(tok2->next(), tok2->tokAt(5)); // Remove "; else return" tok2->deleteNext();
// Remove "; else return"
Token *t = tok2->next();
t->deleteNext();
t->deleteNext();
t->deleteNext();
done = false; done = false;
} }
// Reduce "if ; else %var% ;" => "if %var% ;" // Reduce "if ; else %var% ;" => "if %var% ;"
else if (Token::Match(tok2->next(), "if ; else %var% ;")) { else if (Token::Match(tok2->next(), "if ; else %var% ;")) {
Token::eraseTokens(tok2->next(), tok2->tokAt(4)); Token *t = tok2->next();
t->deleteNext();
t->deleteNext();
done = false; done = false;
} }
// Reduce "if ; else" => "if" // Reduce "if ; else" => "if"
else if (Token::simpleMatch(tok2->next(), "if ; else")) { else if (Token::simpleMatch(tok2->next(), "if ; else")) {
Token::eraseTokens(tok2->next(), tok2->tokAt(4)); Token *t = tok2->next();
t->deleteNext();
t->deleteNext();
done = false; done = false;
} }
// Reduce "if return ; else|if return|continue ;" => "if return ;" // Reduce "if return ; else|if return|continue ;" => "if return ;"
else if (Token::Match(tok2->next(), "if return ; else|if return|continue|break ;")) { else if (Token::Match(tok2->next(), "if return ; else|if return|continue|break ;")) {
Token::eraseTokens(tok2->tokAt(3), tok2->tokAt(6)); Token *t = tok2->tokAt(3);
t->deleteNext();
t->deleteNext();
t->deleteNext();
done = false; done = false;
} }
// Reduce "if continue|break ; else|if return ;" => "if return ;" // Reduce "if continue|break ; else|if return ;" => "if return ;"
else if (Token::Match(tok2->next(), "if continue|break ; if|else return ;")) { else if (Token::Match(tok2->next(), "if continue|break ; if|else return ;")) {
Token::eraseTokens(tok2->next(), tok2->tokAt(5)); Token *t = tok2->next();
t->deleteNext();
t->deleteNext();
t->deleteNext();
done = false; done = false;
} }
// Remove "else" after "if continue|break|return" // Remove "else" after "if continue|break|return"
else if (Token::Match(tok2->next(), "if continue|break|return ; else")) { else if (Token::Match(tok2->next(), "if continue|break|return ; else")) {
tok2->tokAt(4)->deleteThis(); tok2->tokAt(3)->deleteNext();
done = false; done = false;
} }
@ -1689,7 +1717,9 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
// Reduce "alloc while(!var) alloc ;" => "alloc ;" // Reduce "alloc while(!var) alloc ;" => "alloc ;"
if (Token::Match(tok2, "[;{}] alloc ; while(!var) alloc ;")) { if (Token::Match(tok2, "[;{}] alloc ; while(!var) alloc ;")) {
Token::eraseTokens(tok2, tok2->tokAt(4)); tok2->deleteNext();
tok2->deleteNext();
tok2->deleteNext();
done = false; done = false;
} }
@ -1702,14 +1732,14 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
// Reduce "if(var) dealloc ;" and "if(var) use ;" that is not followed by an else.. // Reduce "if(var) dealloc ;" and "if(var) use ;" that is not followed by an else..
if (Token::Match(tok2, "[;{}] if(var) assign|dealloc|use ; !!else")) { if (Token::Match(tok2, "[;{}] if(var) assign|dealloc|use ; !!else")) {
Token::eraseTokens(tok2, tok2->tokAt(2)); tok2->deleteNext();
done = false; done = false;
} }
// Reduce "; if(!var) alloc ; !!else" => "; dealloc ; alloc ;" // Reduce "; if(!var) alloc ; !!else" => "; dealloc ; alloc ;"
if (Token::Match(tok2, "; if(!var) alloc ; !!else")) { if (Token::Match(tok2, "; if(!var) alloc ; !!else")) {
// Remove the "if(!var)" // Remove the "if(!var)"
Token::eraseTokens(tok2, tok2->tokAt(2)); tok2->deleteNext();
// Insert "dealloc ;" before the "alloc ;" // Insert "dealloc ;" before the "alloc ;"
tok2->insertToken(";"); tok2->insertToken(";");
@ -1718,9 +1748,10 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
done = false; done = false;
} }
// Reduce "; if(!var) exit ;" => ";" // Reduce "if(!var) exit ;" => ";"
if (Token::simpleMatch(tok2, "; if(!var) exit ;")) { if (Token::simpleMatch(tok2, "; if(!var) exit ;")) {
Token::eraseTokens(tok2, tok2->tokAt(3)); tok2->deleteNext();
tok2->deleteNext();
done = false; done = false;
} }
@ -1735,17 +1766,18 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
tok2->str("if(var)"); tok2->str("if(var)");
// remove the "; else" // remove the "; else"
Token::eraseTokens(tok2, tok2->tokAt(3)); tok2->deleteNext();
tok2->deleteNext();
} else { } else {
// remove the "if*" // remove the "if*"
Token::eraseTokens(tok2, tok2->tokAt(2)); tok2->deleteNext();
} }
done = false; done = false;
} }
// Reduce "else ;" => ";" // Reduce "else ;" => ";"
if (Token::simpleMatch(tok2->next(), "else ;")) { if (Token::simpleMatch(tok2->next(), "else ;")) {
Token::eraseTokens(tok2, tok2->tokAt(2)); tok2->deleteNext();
done = false; done = false;
} }
@ -1760,13 +1792,17 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
// Reduce "while1 if break ;" => ";" // Reduce "while1 if break ;" => ";"
if (Token::simpleMatch(tok2, "while1 if break ;")) { if (Token::simpleMatch(tok2, "while1 if break ;")) {
tok2->str(";"); tok2->str(";");
Token::eraseTokens(tok2, tok2->tokAt(3)); tok2->deleteNext();
tok2->deleteNext();
done = false; done = false;
} }
// Delete if block: "alloc; if return use ;" // Delete if block: "alloc; if return use ;"
if (Token::Match(tok2, "alloc ; if return use ; !!else")) { if (Token::Match(tok2, "alloc ; if return use ; !!else")) {
Token::eraseTokens(tok2, tok2->tokAt(5)); tok2->deleteNext();
tok2->deleteNext();
tok2->deleteNext();
tok2->deleteNext();
done = false; done = false;
} }
@ -1784,7 +1820,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
// Remove "if exit ;" // Remove "if exit ;"
if (Token::simpleMatch(tok2, "if exit ;")) { if (Token::simpleMatch(tok2, "if exit ;")) {
tok2->deleteThis(); tok2->deleteNext();
tok2->deleteThis(); tok2->deleteThis();
done = false; done = false;
} }
@ -1792,13 +1828,16 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
// Remove the "if break|continue ;" that follows "dealloc ; alloc ;" // Remove the "if break|continue ;" that follows "dealloc ; alloc ;"
if (! _settings->experimental && Token::Match(tok2, "dealloc ; alloc ; if break|continue ;")) { if (! _settings->experimental && Token::Match(tok2, "dealloc ; alloc ; if break|continue ;")) {
tok2 = tok2->tokAt(3); tok2 = tok2->tokAt(3);
Token::eraseTokens(tok2, tok2->tokAt(3)); tok2->deleteNext();
tok2->deleteNext();
done = false; done = false;
} }
// if break ; break ; => break ; // if break ; break ; => break ;
if (Token::Match(tok2->previous(), "[;{}] if break ; break ;")) { if (Token::Match(tok2->previous(), "[;{}] if break ; break ;")) {
Token::eraseTokens(tok2, tok2->tokAt(4)); tok2->deleteNext();
tok2->deleteNext();
tok2->deleteNext();
done = false; done = false;
} }
@ -1811,14 +1850,16 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
// Reduce "do { alloc ; } " => "alloc ;" // Reduce "do { alloc ; } " => "alloc ;"
/** @todo If the loop "do { alloc ; }" can be executed twice, reduce it to "loop alloc ;" */ /** @todo If the loop "do { alloc ; }" can be executed twice, reduce it to "loop alloc ;" */
if (Token::simpleMatch(tok2->next(), "do { alloc ; }")) { if (Token::simpleMatch(tok2->next(), "do { alloc ; }")) {
Token::eraseTokens(tok2, tok2->tokAt(3)); tok2->deleteNext();
Token::eraseTokens(tok2->tokAt(2), tok2->tokAt(4)); tok2->deleteNext();
tok2->tokAt(2)->deleteNext();
done = false; done = false;
} }
// Reduce "loop break ; => ";" // Reduce "loop break ; => ";"
if (Token::Match(tok2->next(), "loop break|continue ;")) { if (Token::Match(tok2->next(), "loop break|continue ;")) {
Token::eraseTokens(tok2, tok2->tokAt(3)); tok2->deleteNext();
tok2->deleteNext();
done = false; done = false;
} }
@ -1830,19 +1871,25 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
// Reduce "loop if break|continue ; !!else" => ";" // Reduce "loop if break|continue ; !!else" => ";"
if (Token::Match(tok2->next(), "loop if break|continue ; !!else")) { if (Token::Match(tok2->next(), "loop if break|continue ; !!else")) {
Token::eraseTokens(tok2, tok2->tokAt(4)); tok2->deleteNext();
tok2->deleteNext();
tok2->deleteNext();
done = false; done = false;
} }
// Reduce "loop { if break|continue ; !!else" => "loop {" // Reduce "loop { if break|continue ; !!else" => "loop {"
if (Token::Match(tok2, "loop { if break|continue ; !!else")) { if (Token::Match(tok2, "loop { if break|continue ; !!else")) {
Token::eraseTokens(tok2->next(), tok2->tokAt(5)); Token *t = tok2->next();
t->deleteNext();
t->deleteNext();
t->deleteNext();
done = false; done = false;
} }
// Replace "do ; loop ;" with ";" // Replace "do ; loop ;" with ";"
if (Token::simpleMatch(tok2, "; loop ;")) { if (Token::simpleMatch(tok2, "; loop ;")) {
Token::eraseTokens(tok2, tok2->tokAt(3)); tok2->deleteNext();
tok2->deleteNext();
done = false; done = false;
} }
@ -1854,19 +1901,19 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
// Replace "loop if return ;" with "if return ;" // Replace "loop if return ;" with "if return ;"
if (Token::simpleMatch(tok2->next(), "loop if return")) { if (Token::simpleMatch(tok2->next(), "loop if return")) {
Token::eraseTokens(tok2, tok2->tokAt(2)); tok2->deleteNext();
done = false; done = false;
} }
// Reduce "loop|while1 { dealloc ; alloc ; }" // Reduce "loop|while1 { dealloc ; alloc ; }"
if (Token::Match(tok2, "loop|while1 { dealloc ; alloc ; }")) { if (Token::Match(tok2, "loop|while1 { dealloc ; alloc ; }")) {
// delete "loop|while1"
tok2->deleteThis();
// delete "{" // delete "{"
tok2->deleteNext();
// delete "loop|while1"
tok2->deleteThis(); tok2->deleteThis();
// delete "}" // delete "}"
Token::eraseTokens(tok2->tokAt(3), tok2->tokAt(5)); tok2->tokAt(3)->deleteNext();
done = false; done = false;
} }
@ -1882,38 +1929,44 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
// Delete if block in "alloc ; if(!var) return ;" // Delete if block in "alloc ; if(!var) return ;"
if (Token::simpleMatch(tok2, "alloc ; if(!var) return ;")) { if (Token::simpleMatch(tok2, "alloc ; if(!var) return ;")) {
Token::eraseTokens(tok2, tok2->tokAt(4)); tok2->deleteNext();
tok2->deleteNext();
tok2->deleteNext();
done = false; done = false;
} }
// Reduce "[;{}] return use ; %var%" => "[;{}] return use ;" // Reduce "[;{}] return use ; %var%" => "[;{}] return use ;"
if (Token::Match(tok2, "[;{}] return use ; %var%")) { if (Token::Match(tok2, "[;{}] return use ; %var%")) {
Token::eraseTokens(tok2->tokAt(3), tok2->tokAt(5)); tok2->tokAt(3)->deleteNext();
done = false; 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")) {
Token::eraseTokens(tok2, tok2->tokAt(2)); tok2->deleteNext();
done = false; done = false;
} }
// malloc - realloc => alloc ; dealloc ; alloc ; // malloc - realloc => alloc ; dealloc ; alloc ;
// Reduce "[;{}] alloc ; dealloc ; alloc ;" => "[;{}] alloc ;" // Reduce "[;{}] alloc ; dealloc ; alloc ;" => "[;{}] alloc ;"
if (Token::Match(tok2, "[;{}] alloc ; dealloc ; alloc ;")) { if (Token::Match(tok2, "[;{}] alloc ; dealloc ; alloc ;")) {
Token::eraseTokens(tok2->next(), tok2->tokAt(6)); tok2->deleteNext();
tok2->deleteNext();
tok2->deleteNext();
tok2->deleteNext();
done = false; done = false;
} }
// use; dealloc; => dealloc; // use; dealloc; => dealloc;
if (Token::Match(tok2, "[;{}] use ; dealloc ;")) { if (Token::Match(tok2, "[;{}] use ; dealloc ;")) {
Token::eraseTokens(tok2, tok2->tokAt(3)); tok2->deleteNext();
tok2->deleteNext();
done = false; done = false;
} }
// use use => use // use use => use
if (Token::simpleMatch(tok2, "use use")) { if (Token::simpleMatch(tok2, "use use")) {
tok2->deleteThis(); tok2->deleteNext();
done = false; done = false;
} }
@ -1929,20 +1982,24 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
// Delete first part in "use ; return use ;" // Delete first part in "use ; return use ;"
if (Token::Match(tok2, "[;{}] use ; return use ;")) { if (Token::Match(tok2, "[;{}] use ; return use ;")) {
Token::eraseTokens(tok2, tok2->tokAt(3)); tok2->deleteNext();
tok2->deleteNext();
done = false; done = false;
} }
// try/catch // try/catch
if (Token::simpleMatch(tok2, "try ; catch exit ;")) { if (Token::simpleMatch(tok2, "try ; catch exit ;")) {
Token::eraseTokens(tok2, tok2->tokAt(4)); tok2->deleteNext();
tok2->deleteNext();
tok2->deleteNext();
tok2->deleteThis(); tok2->deleteThis();
done = false; done = false;
} }
// Delete second case in "case ; case ;" // Delete second case in "case ; case ;"
while (Token::simpleMatch(tok2, "case ; case ;")) { while (Token::simpleMatch(tok2, "case ; case ;")) {
Token::eraseTokens(tok2, tok2->tokAt(3)); tok2->deleteNext();
tok2->deleteNext();
done = false; done = false;
} }
@ -1982,7 +2039,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
if (!incase && valid) { if (!incase && valid) {
done = false; done = false;
tok2->str(";"); tok2->str(";");
Token::eraseTokens(tok2, tok2->tokAt(2)); tok2->deleteNext();
tok2 = tok2->next(); tok2 = tok2->next();
bool first = true; bool first = true;
while (Token::Match(tok2, "case|default")) { while (Token::Match(tok2, "case|default")) {
@ -2105,7 +2162,7 @@ void CheckMemoryLeakInFunction::checkScope(const Token *Tok1, const std::string
// Simplify the code and check if freed memory is used.. // Simplify the code and check if freed memory is used..
for (Token *tok2 = tok; tok2; tok2 = tok2->next()) { for (Token *tok2 = tok; tok2; tok2 = tok2->next()) {
while (Token::Match(tok2, "[;{}] ;")) while (Token::Match(tok2, "[;{}] ;"))
Token::eraseTokens(tok2, tok2->tokAt(2)); tok2->deleteNext();
} }
if ((result = Token::findmatch(tok, "[;{}] dealloc ; use_ ;")) != NULL) { if ((result = Token::findmatch(tok, "[;{}] dealloc ; use_ ;")) != NULL) {
deallocuseError(result->tokAt(3), varname); deallocuseError(result->tokAt(3), varname);