1)Implement another declaration of 'Token::eraseTokens' with parameters: the starting token and the number of times needed to call 'deleteNext'.
2)Use the overloaded 'eraseTokens' where needed instead of using consecutively 'deleteNext'. 3)Tokenizer: general optimizations about the deletion of some tokens. 4)Run astyle.
This commit is contained in:
parent
b9224439c1
commit
612b3c59ca
|
@ -1551,7 +1551,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
|
||||||
--innerIndentlevel;
|
--innerIndentlevel;
|
||||||
}
|
}
|
||||||
while (innerIndentlevel == 0 && Token::Match(tok3, "[{};] if|ifv|else { continue ; }")) {
|
while (innerIndentlevel == 0 && Token::Match(tok3, "[{};] if|ifv|else { continue ; }")) {
|
||||||
Token::eraseTokens(tok3, tok3->tokAt(6));
|
Token::eraseTokens(tok3, 5);
|
||||||
if (Token::simpleMatch(tok3->next(), "else"))
|
if (Token::simpleMatch(tok3->next(), "else"))
|
||||||
tok3->deleteNext();
|
tok3->deleteNext();
|
||||||
}
|
}
|
||||||
|
@ -1559,12 +1559,8 @@ 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(";");
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 3);
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2->tokAt(4), 2);
|
||||||
tok2->deleteNext();
|
|
||||||
Token *t = tok2->tokAt(4);
|
|
||||||
t->deleteNext();
|
|
||||||
t->deleteNext();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1579,8 +1575,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
|
||||||
|
|
||||||
// Replace "{ }" with ";"
|
// Replace "{ }" with ";"
|
||||||
if (Token::simpleMatch(tok2->next(), "{ }")) {
|
if (Token::simpleMatch(tok2->next(), "{ }")) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 2);
|
||||||
tok2->deleteNext();
|
|
||||||
tok2->insertToken(";");
|
tok2->insertToken(";");
|
||||||
done = false;
|
done = false;
|
||||||
}
|
}
|
||||||
|
@ -1605,8 +1600,7 @@ 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->deleteNext();
|
Token::eraseTokens(tok2, 2);
|
||||||
tok2->deleteNext();
|
|
||||||
tok2->tokAt(4)->deleteNext();
|
tok2->tokAt(4)->deleteNext();
|
||||||
done = false;
|
done = false;
|
||||||
}
|
}
|
||||||
|
@ -1621,26 +1615,19 @@ 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% ;") &&
|
||||||
tok2->strAt(2) == tok2->strAt(5)) {
|
tok2->strAt(2) == tok2->strAt(5)) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 4);
|
||||||
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 ;")) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 3);
|
||||||
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 ;")) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 3);
|
||||||
tok2->deleteNext();
|
|
||||||
tok2->deleteNext();
|
|
||||||
done = false;
|
done = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1649,44 +1636,31 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
|
||||||
// Remove "if"
|
// Remove "if"
|
||||||
tok2->deleteNext();
|
tok2->deleteNext();
|
||||||
// Remove "; else return"
|
// Remove "; else return"
|
||||||
Token *t = tok2->next();
|
Token::eraseTokens(tok2->next(), 3);
|
||||||
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 *t = tok2->next();
|
Token::eraseTokens(tok2->next(), 2);
|
||||||
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 *t = tok2->next();
|
Token::eraseTokens(tok2->next(), 2);
|
||||||
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 *t = tok2->tokAt(3);
|
Token::eraseTokens(tok2->tokAt(3), 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 *t = tok2->next();
|
Token::eraseTokens(tok2->next(), 3);
|
||||||
t->deleteNext();
|
|
||||||
t->deleteNext();
|
|
||||||
t->deleteNext();
|
|
||||||
done = false;
|
done = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1699,7 +1673,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
|
||||||
// Delete "if { dealloc|assign|use ; return ; }"
|
// Delete "if { dealloc|assign|use ; return ; }"
|
||||||
else if (Token::Match(tok2, "[;{}] if { dealloc|assign|use ; return ; }") &&
|
else if (Token::Match(tok2, "[;{}] if { dealloc|assign|use ; return ; }") &&
|
||||||
!Token::findmatch(tok, "if {| alloc ;")) {
|
!Token::findmatch(tok, "if {| alloc ;")) {
|
||||||
Token::eraseTokens(tok2, tok2->tokAt(8));
|
Token::eraseTokens(tok2, 7);
|
||||||
if (Token::simpleMatch(tok2->next(), "else"))
|
if (Token::simpleMatch(tok2->next(), "else"))
|
||||||
tok2->deleteNext();
|
tok2->deleteNext();
|
||||||
done = false;
|
done = false;
|
||||||
|
@ -1708,7 +1682,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
|
||||||
// Remove "if { dealloc ; callfunc ; } !!else|return"
|
// Remove "if { dealloc ; callfunc ; } !!else|return"
|
||||||
else if (Token::Match(tok2->next(), "if { dealloc|assign ; callfunc ; }") &&
|
else if (Token::Match(tok2->next(), "if { dealloc|assign ; callfunc ; }") &&
|
||||||
!Token::Match(tok2->tokAt(8), "else|return")) {
|
!Token::Match(tok2->tokAt(8), "else|return")) {
|
||||||
Token::eraseTokens(tok2, tok2->tokAt(8));
|
Token::eraseTokens(tok2, 7);
|
||||||
done = false;
|
done = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1717,9 +1691,7 @@ 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 ;")) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 3);
|
||||||
tok2->deleteNext();
|
|
||||||
tok2->deleteNext();
|
|
||||||
done = false;
|
done = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1750,8 +1722,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
|
||||||
|
|
||||||
// Reduce "if(!var) exit ;" => ";"
|
// Reduce "if(!var) exit ;" => ";"
|
||||||
if (Token::simpleMatch(tok2, "; if(!var) exit ;")) {
|
if (Token::simpleMatch(tok2, "; if(!var) exit ;")) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 2);
|
||||||
tok2->deleteNext();
|
|
||||||
done = false;
|
done = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1766,8 +1737,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
|
||||||
tok2->str("if(var)");
|
tok2->str("if(var)");
|
||||||
|
|
||||||
// remove the "; else"
|
// remove the "; else"
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 2);
|
||||||
tok2->deleteNext();
|
|
||||||
} else {
|
} else {
|
||||||
// remove the "if*"
|
// remove the "if*"
|
||||||
tok2->deleteNext();
|
tok2->deleteNext();
|
||||||
|
@ -1792,17 +1762,13 @@ 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(";");
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 2);
|
||||||
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")) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 4);
|
||||||
tok2->deleteNext();
|
|
||||||
tok2->deleteNext();
|
|
||||||
tok2->deleteNext();
|
|
||||||
done = false;
|
done = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1827,39 +1793,33 @@ 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);
|
Token::eraseTokens(tok2->tokAt(3), 2);
|
||||||
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 ;")) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 3);
|
||||||
tok2->deleteNext();
|
|
||||||
tok2->deleteNext();
|
|
||||||
done = false;
|
done = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reduce "do { dealloc ; alloc ; } while(var) ;" => ";"
|
// Reduce "do { dealloc ; alloc ; } while(var) ;" => ";"
|
||||||
if (Token::simpleMatch(tok2->next(), "do { dealloc ; alloc ; } while(var) ;")) {
|
if (Token::simpleMatch(tok2->next(), "do { dealloc ; alloc ; } while(var) ;")) {
|
||||||
Token::eraseTokens(tok2, tok2->tokAt(9));
|
Token::eraseTokens(tok2, 8);
|
||||||
done = false;
|
done = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 ; }")) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 2);
|
||||||
tok2->deleteNext();
|
|
||||||
tok2->tokAt(2)->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 ;")) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 2);
|
||||||
tok2->deleteNext();
|
|
||||||
done = false;
|
done = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1871,25 +1831,19 @@ 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")) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 3);
|
||||||
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 *t = tok2->next();
|
Token::eraseTokens(tok2->next(), 3);
|
||||||
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 ;")) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 2);
|
||||||
tok2->deleteNext();
|
|
||||||
done = false;
|
done = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1921,7 +1875,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
|
||||||
// loop { use ; callfunc ; } => use ;
|
// loop { use ; callfunc ; } => use ;
|
||||||
// assume that the "callfunc" is not noreturn
|
// assume that the "callfunc" is not noreturn
|
||||||
if (Token::simpleMatch(tok2, "loop { use ; callfunc ; }")) {
|
if (Token::simpleMatch(tok2, "loop { use ; callfunc ; }")) {
|
||||||
Token::eraseTokens(tok2, tok2->tokAt(7));
|
Token::eraseTokens(tok2, 6);
|
||||||
tok2->str("use");
|
tok2->str("use");
|
||||||
tok2->insertToken(";");
|
tok2->insertToken(";");
|
||||||
done = false;
|
done = false;
|
||||||
|
@ -1929,9 +1883,7 @@ 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 ;")) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 3);
|
||||||
tok2->deleteNext();
|
|
||||||
tok2->deleteNext();
|
|
||||||
done = false;
|
done = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1950,17 +1902,13 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
|
||||||
// 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 ;")) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 4);
|
||||||
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 ;")) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 2);
|
||||||
tok2->deleteNext();
|
|
||||||
done = false;
|
done = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1973,33 +1921,28 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
|
||||||
// use; if| use; => use;
|
// use; if| use; => use;
|
||||||
while (Token::Match(tok2, "[;{}] use ; if| use ;")) {
|
while (Token::Match(tok2, "[;{}] use ; if| use ;")) {
|
||||||
Token *t = tok2->tokAt(2);
|
Token *t = tok2->tokAt(2);
|
||||||
t->deleteNext();
|
Token::eraseTokens(t, 2);
|
||||||
t->deleteNext();
|
if (t->next() && t->next()->str() == ";")
|
||||||
if (t->strAt(1) == ";")
|
|
||||||
t->deleteNext();
|
t->deleteNext();
|
||||||
done = false;
|
done = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 ;")) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 2);
|
||||||
tok2->deleteNext();
|
|
||||||
done = false;
|
done = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// try/catch
|
// try/catch
|
||||||
if (Token::simpleMatch(tok2, "try ; catch exit ;")) {
|
if (Token::simpleMatch(tok2, "try ; catch exit ;")) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 3);
|
||||||
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 ;")) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 2);
|
||||||
tok2->deleteNext();
|
|
||||||
done = false;
|
done = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1341,9 +1341,7 @@ void Preprocessor::simplifyCondition(const std::map<std::string, std::string> &c
|
||||||
tok->str("0");
|
tok->str("0");
|
||||||
else
|
else
|
||||||
continue;
|
continue;
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 3);
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1416,8 +1414,7 @@ void Preprocessor::simplifyCondition(const std::map<std::string, std::string> &c
|
||||||
|
|
||||||
for (Token *tok = const_cast<Token *>(tokenizer.tokens()); tok; tok = tok->next()) {
|
for (Token *tok = const_cast<Token *>(tokenizer.tokens()); tok; tok = tok->next()) {
|
||||||
while (Token::Match(tok, "(|%oror% %any% %oror% 1")) {
|
while (Token::Match(tok, "(|%oror% %any% %oror% 1")) {
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
if (tok->tokAt(-3))
|
if (tok->tokAt(-3))
|
||||||
tok = tok->tokAt(-3);
|
tok = tok->tokAt(-3);
|
||||||
}
|
}
|
||||||
|
|
|
@ -866,6 +866,16 @@ void Token::eraseTokens(Token *begin, const Token *end)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Token::eraseTokens(Token *tok, unsigned long index)
|
||||||
|
{
|
||||||
|
if (!tok)
|
||||||
|
return;
|
||||||
|
|
||||||
|
while (tok->next() && index--) {
|
||||||
|
tok->deleteNext();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Token::createMutualLinks(Token *begin, Token *end)
|
void Token::createMutualLinks(Token *begin, Token *end)
|
||||||
{
|
{
|
||||||
assert(begin != NULL);
|
assert(begin != NULL);
|
||||||
|
|
10
lib/token.h
10
lib/token.h
|
@ -279,6 +279,16 @@ public:
|
||||||
*/
|
*/
|
||||||
static void eraseTokens(Token *begin, const Token *end);
|
static void eraseTokens(Token *begin, const Token *end);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete tokens after tok 'index' times. It's a replacement
|
||||||
|
* of the consecutive calls to 'deleteNext', more efficient
|
||||||
|
* than calling 'Token::eraseTokens(tok, tok->tokAt(index+1))'.
|
||||||
|
*
|
||||||
|
* @param tok Tokens after this will be erased.
|
||||||
|
* @param index How many times it should remove the next token.
|
||||||
|
*/
|
||||||
|
static void eraseTokens(Token *tok, unsigned long index);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert new token after this token. This function will handle
|
* Insert new token after this token. This function will handle
|
||||||
* relations between next and previous token also.
|
* relations between next and previous token also.
|
||||||
|
|
216
lib/tokenize.cpp
216
lib/tokenize.cpp
|
@ -936,9 +936,7 @@ void Tokenizer::simplifyTypedef()
|
||||||
bool undefinedStruct = false;
|
bool undefinedStruct = false;
|
||||||
if (Token::Match(tok, "typedef enum|struct %type% %type% ;") && tok->strAt(2) == tok->strAt(3)) {
|
if (Token::Match(tok, "typedef enum|struct %type% %type% ;") && tok->strAt(2) == tok->strAt(3)) {
|
||||||
if (tok->next()->str() == "enum") {
|
if (tok->next()->str() == "enum") {
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 3);
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteThis();
|
tok->deleteThis();
|
||||||
if (tok->next())
|
if (tok->next())
|
||||||
tok->deleteThis();
|
tok->deleteThis();
|
||||||
|
@ -1431,8 +1429,7 @@ void Tokenizer::simplifyTypedef()
|
||||||
}
|
}
|
||||||
|
|
||||||
for (std::size_t i = classLevel; i < spaceInfo.size(); ++i) {
|
for (std::size_t i = classLevel; i < spaceInfo.size(); ++i) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 2);
|
||||||
tok2->deleteNext();
|
|
||||||
}
|
}
|
||||||
simplifyType = true;
|
simplifyType = true;
|
||||||
} else if ((inScope && !exitThisScope) || inMemberFunc) {
|
} else if ((inScope && !exitThisScope) || inMemberFunc) {
|
||||||
|
@ -1440,8 +1437,7 @@ void Tokenizer::simplifyTypedef()
|
||||||
// Don't replace this typename if it's preceded by "::" unless it's a namespace
|
// Don't replace this typename if it's preceded by "::" unless it's a namespace
|
||||||
if (!spaceInfo.empty() && (tok2->strAt(-2) == spaceInfo[0].className) && spaceInfo[0].isNamespace) {
|
if (!spaceInfo.empty() && (tok2->strAt(-2) == spaceInfo[0].className) && spaceInfo[0].isNamespace) {
|
||||||
tok2 = tok2->tokAt(-3);
|
tok2 = tok2->tokAt(-3);
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 2);
|
||||||
tok2->deleteNext();
|
|
||||||
tok2 = tok2->next();
|
tok2 = tok2->next();
|
||||||
simplifyType = true;
|
simplifyType = true;
|
||||||
}
|
}
|
||||||
|
@ -1914,8 +1910,7 @@ bool Tokenizer::tokenize(std::istream &code,
|
||||||
(tok->isName() || tok->isNumber()) &&
|
(tok->isName() || tok->isNumber()) &&
|
||||||
(tok->tokAt(2)->isName() || tok->tokAt(2)->isNumber())) {
|
(tok->tokAt(2)->isName() || tok->tokAt(2)->isNumber())) {
|
||||||
tok->str(tok->str() + tok->strAt(2));
|
tok->str(tok->str() + tok->strAt(2));
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
if (tok->previous())
|
if (tok->previous())
|
||||||
tok = tok->previous();
|
tok = tok->previous();
|
||||||
}
|
}
|
||||||
|
@ -1948,8 +1943,7 @@ bool Tokenizer::tokenize(std::istream &code,
|
||||||
const bool isJava(_files[0].find(".java") != std::string::npos);
|
const bool isJava(_files[0].find(".java") != std::string::npos);
|
||||||
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
||||||
if (isJava && Token::Match(tok, ") throws %var% {")) {
|
if (isJava && Token::Match(tok, ") throws %var% {")) {
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
} else if (tok->str() == "private")
|
} else if (tok->str() == "private")
|
||||||
tok->str("private:");
|
tok->str("private:");
|
||||||
else if (tok->str() == "protected")
|
else if (tok->str() == "protected")
|
||||||
|
@ -2552,8 +2546,9 @@ void Tokenizer::arraySize()
|
||||||
{
|
{
|
||||||
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
||||||
if (Token::Match(tok, "%var% [ ] = { %str% }")) {
|
if (Token::Match(tok, "%var% [ ] = { %str% }")) {
|
||||||
tok->tokAt(3)->deleteNext();
|
Token *t = tok->tokAt(3);
|
||||||
tok->tokAt(4)->deleteNext();
|
t->deleteNext();
|
||||||
|
t->next()->deleteNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Token::Match(tok, "%var% [ ] = {")) {
|
if (Token::Match(tok, "%var% [ ] = {")) {
|
||||||
|
@ -2789,8 +2784,7 @@ std::set<std::string> Tokenizer::simplifyTemplatesExpandSpecialized()
|
||||||
tok2->str(name);
|
tok2->str(name);
|
||||||
|
|
||||||
// delete the "template < >"
|
// delete the "template < >"
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteThis();
|
tok->deleteThis();
|
||||||
|
|
||||||
// Use this special template in the code..
|
// Use this special template in the code..
|
||||||
|
@ -3100,10 +3094,7 @@ void Tokenizer::simplifyTemplatesInstantiate(const Token *tok,
|
||||||
if (Token::Match(tok2, "%var% < sizeof ( %type% ) >") && tok2->tokAt(4)->isStandardType()) {
|
if (Token::Match(tok2, "%var% < sizeof ( %type% ) >") && tok2->tokAt(4)->isStandardType()) {
|
||||||
Token * const tok3 = tok2->next();
|
Token * const tok3 = tok2->next();
|
||||||
const unsigned int sz = sizeOfType(tok3->tokAt(3));
|
const unsigned int sz = sizeOfType(tok3->tokAt(3));
|
||||||
tok3->deleteNext();
|
Token::eraseTokens(tok3, 4);
|
||||||
tok3->deleteNext();
|
|
||||||
tok3->deleteNext();
|
|
||||||
tok3->deleteNext();
|
|
||||||
tok3->insertToken(MathLib::toString<unsigned int>(sz));
|
tok3->insertToken(MathLib::toString<unsigned int>(sz));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3392,7 +3383,7 @@ void Tokenizer::simplifyTemplates2()
|
||||||
if (tok->str() == "(")
|
if (tok->str() == "(")
|
||||||
tok = tok->link();
|
tok = tok->link();
|
||||||
|
|
||||||
else if (Token::Match(tok, "%type% <") &&
|
else if (Token::Match(tok, "%type% <") &&
|
||||||
(!tok->previous() || tok->previous()->str() == ";")) {
|
(!tok->previous() || tok->previous()->str() == ";")) {
|
||||||
const Token *tok2 = tok->tokAt(2);
|
const Token *tok2 = tok->tokAt(2);
|
||||||
std::string type;
|
std::string type;
|
||||||
|
@ -4049,9 +4040,7 @@ void Tokenizer::simplifySizeof()
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (Token::simpleMatch(tok->next(), ". . .")) {
|
if (Token::simpleMatch(tok->next(), ". . .")) {
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 3);
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// sizeof 'x'
|
// sizeof 'x'
|
||||||
|
@ -4166,9 +4155,7 @@ void Tokenizer::simplifySizeof()
|
||||||
|
|
||||||
if (Token::simpleMatch(tok->next(), "( * )")) {
|
if (Token::simpleMatch(tok->next(), "( * )")) {
|
||||||
tok->str(MathLib::toString<unsigned long>(sizeOfType(tok->tokAt(2))));
|
tok->str(MathLib::toString<unsigned long>(sizeOfType(tok->tokAt(2))));
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 3);
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// sizeof( a )
|
// sizeof( a )
|
||||||
|
@ -4188,9 +4175,7 @@ void Tokenizer::simplifySizeof()
|
||||||
unsigned int size = sizeOfType(tok->tokAt(2));
|
unsigned int size = sizeOfType(tok->tokAt(2));
|
||||||
if (size > 0) {
|
if (size > 0) {
|
||||||
tok->str(MathLib::toString<unsigned int>(size));
|
tok->str(MathLib::toString<unsigned int>(size));
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 3);
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4272,9 +4257,7 @@ bool Tokenizer::simplifyTokenList()
|
||||||
// change array to pointer..
|
// change array to pointer..
|
||||||
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
||||||
if (Token::Match(tok, "%type% %var% [ ] [,;=]")) {
|
if (Token::Match(tok, "%type% %var% [ ] [,;=]")) {
|
||||||
Token *t = tok->next();
|
Token::eraseTokens(tok->next(), 2);
|
||||||
t->deleteNext();
|
|
||||||
t->deleteNext();
|
|
||||||
tok->insertToken("*");
|
tok->insertToken("*");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4319,8 +4302,7 @@ bool Tokenizer::simplifyTokenList()
|
||||||
&& (Token::Match(tok->next(), "* ( %var% + %num% )") ||
|
&& (Token::Match(tok->next(), "* ( %var% + %num% )") ||
|
||||||
Token::Match(tok->next(), "* ( %var% + %var% )"))) {
|
Token::Match(tok->next(), "* ( %var% + %var% )"))) {
|
||||||
// remove '* ('
|
// remove '* ('
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
|
|
||||||
tok = tok->tokAt(2);
|
tok = tok->tokAt(2);
|
||||||
// '+'->'['
|
// '+'->'['
|
||||||
|
@ -4369,16 +4351,14 @@ bool Tokenizer::simplifyTokenList()
|
||||||
const unsigned int varid(tok->next()->varId());
|
const unsigned int varid(tok->next()->varId());
|
||||||
|
|
||||||
// Delete the "%var% ="
|
// Delete the "%var% ="
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
|
|
||||||
// Change function name "realloc" to "free"
|
// Change function name "realloc" to "free"
|
||||||
tok->next()->str("free");
|
tok->next()->str("free");
|
||||||
|
|
||||||
// delete the ", 0"
|
// delete the ", 0"
|
||||||
tok = tok->tokAt(3);
|
tok = tok->tokAt(3);
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
|
|
||||||
// goto the ";"
|
// goto the ";"
|
||||||
tok = tok->tokAt(2);
|
tok = tok->tokAt(2);
|
||||||
|
@ -4397,8 +4377,7 @@ bool Tokenizer::simplifyTokenList()
|
||||||
tok->str("malloc");
|
tok->str("malloc");
|
||||||
|
|
||||||
// delete "0 ,"
|
// delete "0 ,"
|
||||||
tok->next()->deleteNext();
|
Token::eraseTokens(tok->next(), 2);
|
||||||
tok->next()->deleteNext();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4443,9 +4422,7 @@ bool Tokenizer::simplifyTokenList()
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
ostr << Token::getStrLength(tok->tokAt(2));
|
ostr << Token::getStrLength(tok->tokAt(2));
|
||||||
tok->str(ostr.str());
|
tok->str(ostr.str());
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 3);
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4558,19 +4535,11 @@ void Tokenizer::removeRedundantAssignment()
|
||||||
if (!localvars.empty()) {
|
if (!localvars.empty()) {
|
||||||
for (Token *tok2 = tok->next(); tok2 && tok2 != end;) {
|
for (Token *tok2 = tok->next(); tok2 && tok2 != end;) {
|
||||||
if (Token::Match(tok2, "[;{}] %type% %var% ;") && localvars.find(tok2->tokAt(2)->varId()) != localvars.end()) {
|
if (Token::Match(tok2, "[;{}] %type% %var% ;") && localvars.find(tok2->tokAt(2)->varId()) != localvars.end()) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 3);
|
||||||
tok2->deleteNext();
|
|
||||||
tok2->deleteNext();
|
|
||||||
} else if (Token::Match(tok2, "[;{}] %type% * %var% ;") && localvars.find(tok2->tokAt(3)->varId()) != localvars.end()) {
|
} else if (Token::Match(tok2, "[;{}] %type% * %var% ;") && localvars.find(tok2->tokAt(3)->varId()) != localvars.end()) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 4);
|
||||||
tok2->deleteNext();
|
|
||||||
tok2->deleteNext();
|
|
||||||
tok2->deleteNext();
|
|
||||||
} else if (Token::Match(tok2, "[;{}] %var% = %any% ;") && localvars.find(tok2->next()->varId()) != localvars.end()) {
|
} else if (Token::Match(tok2, "[;{}] %var% = %any% ;") && localvars.find(tok2->next()->varId()) != localvars.end()) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 4);
|
||||||
tok2->deleteNext();
|
|
||||||
tok2->deleteNext();
|
|
||||||
tok2->deleteNext();
|
|
||||||
} else
|
} else
|
||||||
tok2 = tok2->next();
|
tok2 = tok2->next();
|
||||||
}
|
}
|
||||||
|
@ -4701,10 +4670,7 @@ bool Tokenizer::removeRedundantConditions()
|
||||||
else
|
else
|
||||||
tok->str(";");
|
tok->str(";");
|
||||||
|
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 4);
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = true;
|
ret = true;
|
||||||
|
@ -4728,10 +4694,7 @@ bool Tokenizer::removeRedundantConditions()
|
||||||
else
|
else
|
||||||
tok->str(";");
|
tok->str(";");
|
||||||
|
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 4);
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = true;
|
ret = true;
|
||||||
|
@ -4790,8 +4753,7 @@ void Tokenizer::removeRedundantFor()
|
||||||
// Simplify loop if loop variable isn't written
|
// Simplify loop if loop variable isn't written
|
||||||
if (!write) {
|
if (!write) {
|
||||||
// remove "for ("
|
// remove "for ("
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
|
|
||||||
// If loop variable is read then keep assignment before
|
// If loop variable is read then keep assignment before
|
||||||
// loop body..
|
// loop body..
|
||||||
|
@ -4800,20 +4762,11 @@ void Tokenizer::removeRedundantFor()
|
||||||
tok = tok->tokAt(4);
|
tok = tok->tokAt(4);
|
||||||
} else {
|
} else {
|
||||||
// remove "x = 0 ;"
|
// remove "x = 0 ;"
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 4);
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove "x < 1 ; x ++ )"
|
// remove "x < 1 ; x ++ )"
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 7);
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
|
|
||||||
// Add assignment after the loop body so the loop variable
|
// Add assignment after the loop body so the loop variable
|
||||||
// get the correct end value
|
// get the correct end value
|
||||||
|
@ -4839,9 +4792,7 @@ void Tokenizer::removeRedundantSemicolons()
|
||||||
if (Token::simpleMatch(tok, "; ;")) {
|
if (Token::simpleMatch(tok, "; ;")) {
|
||||||
tok->deleteNext();
|
tok->deleteNext();
|
||||||
} else if (Token::simpleMatch(tok, "; { ; }")) {
|
} else if (Token::simpleMatch(tok, "; { ; }")) {
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 3);
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -5234,16 +5185,14 @@ bool Tokenizer::simplifyConditions()
|
||||||
if (Token::simpleMatch(tok, "( true &&") ||
|
if (Token::simpleMatch(tok, "( true &&") ||
|
||||||
Token::simpleMatch(tok, "&& true &&") ||
|
Token::simpleMatch(tok, "&& true &&") ||
|
||||||
Token::simpleMatch(tok->next(), "&& true )")) {
|
Token::simpleMatch(tok->next(), "&& true )")) {
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
ret = true;
|
ret = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (Token::simpleMatch(tok, "( false ||") ||
|
else if (Token::simpleMatch(tok, "( false ||") ||
|
||||||
Token::simpleMatch(tok, "|| false ||") ||
|
Token::simpleMatch(tok, "|| false ||") ||
|
||||||
Token::simpleMatch(tok->next(), "|| false )")) {
|
Token::simpleMatch(tok->next(), "|| false )")) {
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
ret = true;
|
ret = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5331,8 +5280,7 @@ bool Tokenizer::simplifyConditions()
|
||||||
|
|
||||||
if (! cmp.empty()) {
|
if (! cmp.empty()) {
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
|
|
||||||
tok->str(result ? "true" : "false");
|
tok->str(result ? "true" : "false");
|
||||||
ret = true;
|
ret = true;
|
||||||
|
@ -5404,8 +5352,7 @@ bool Tokenizer::simplifyQuestionMark()
|
||||||
|
|
||||||
// delete the condition token and the "?"
|
// delete the condition token and the "?"
|
||||||
tok = tok->tokAt(-2);
|
tok = tok->tokAt(-2);
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
|
|
||||||
// delete operator after the :
|
// delete operator after the :
|
||||||
if (end) {
|
if (end) {
|
||||||
|
@ -5701,8 +5648,7 @@ bool Tokenizer::simplifyFunctionReturn()
|
||||||
if (Token::Match(tok2, pattern.c_str())) {
|
if (Token::Match(tok2, pattern.c_str())) {
|
||||||
tok2 = tok2->next();
|
tok2 = tok2->next();
|
||||||
tok2->str(tok->strAt(5));
|
tok2->str(tok->strAt(5));
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 2);
|
||||||
tok2->deleteNext();
|
|
||||||
ret = true;
|
ret = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6351,32 +6297,28 @@ void Tokenizer::simplifyIfNot()
|
||||||
tok = tok->previous();
|
tok = tok->previous();
|
||||||
tok->insertToken("!");
|
tok->insertToken("!");
|
||||||
tok = tok->tokAt(4);
|
tok = tok->tokAt(4);
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (Token::Match(tok, "* %var% == 0|false")) {
|
else if (Token::Match(tok, "* %var% == 0|false")) {
|
||||||
tok = tok->previous();
|
tok = tok->previous();
|
||||||
tok->insertToken("!");
|
tok->insertToken("!");
|
||||||
tok = tok->tokAt(3);
|
tok = tok->tokAt(3);
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (tok->link() && Token::Match(tok, ") == 0|false")) {
|
else if (tok->link() && Token::Match(tok, ") == 0|false")) {
|
||||||
// if( foo(x) == 0 )
|
// if( foo(x) == 0 )
|
||||||
if (Token::Match(tok->link()->tokAt(-2), "( %var%")) {
|
if (Token::Match(tok->link()->tokAt(-2), "( %var%")) {
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
tok->link()->previous()->insertToken(tok->link()->previous()->str().c_str());
|
tok->link()->previous()->insertToken(tok->link()->previous()->str().c_str());
|
||||||
tok->link()->tokAt(-2)->str("!");
|
tok->link()->tokAt(-2)->str("!");
|
||||||
}
|
}
|
||||||
|
|
||||||
// if( (x) == 0 )
|
// if( (x) == 0 )
|
||||||
else if (Token::simpleMatch(tok->link()->previous(), "(")) {
|
else if (Token::simpleMatch(tok->link()->previous(), "(")) {
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
tok->link()->insertToken("(");
|
tok->link()->insertToken("(");
|
||||||
tok->link()->str("!");
|
tok->link()->str("!");
|
||||||
Token *temp = tok->link();
|
Token *temp = tok->link();
|
||||||
|
@ -7030,8 +6972,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!structname.empty()) {
|
if (!structname.empty()) {
|
||||||
tok3->deleteNext();
|
Token::eraseTokens(tok3, 2);
|
||||||
tok3->deleteNext();
|
|
||||||
}
|
}
|
||||||
if (Token::Match(valueToken, "& %var% ;")) {
|
if (Token::Match(valueToken, "& %var% ;")) {
|
||||||
tok3->insertToken("&");
|
tok3->insertToken("&");
|
||||||
|
@ -7096,8 +7037,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
|
||||||
// array usage
|
// array usage
|
||||||
if (Token::Match(tok3, ("[(,] " + structname + " %varid% [|%op%").c_str(), varid)) {
|
if (Token::Match(tok3, ("[(,] " + structname + " %varid% [|%op%").c_str(), varid)) {
|
||||||
if (!structname.empty()) {
|
if (!structname.empty()) {
|
||||||
tok3->deleteNext();
|
Token::eraseTokens(tok3, 2);
|
||||||
tok3->deleteNext();
|
|
||||||
}
|
}
|
||||||
tok3 = tok3->next();
|
tok3 = tok3->next();
|
||||||
tok3->str(value);
|
tok3->str(value);
|
||||||
|
@ -7114,8 +7054,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
|
||||||
if (value[0] == '\"')
|
if (value[0] == '\"')
|
||||||
break;
|
break;
|
||||||
if (!structname.empty()) {
|
if (!structname.empty()) {
|
||||||
tok3->deleteNext();
|
Token::eraseTokens(tok3, 2);
|
||||||
tok3->deleteNext();
|
|
||||||
}
|
}
|
||||||
tok3 = tok3->next();
|
tok3 = tok3->next();
|
||||||
tok3->str(value);
|
tok3->str(value);
|
||||||
|
@ -7176,9 +7115,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
|
||||||
if (indentlevel == indentlevel3 && Token::Match(tok3->next(), "%varid% ++|--", varid) && MathLib::isInt(value)) {
|
if (indentlevel == indentlevel3 && Token::Match(tok3->next(), "%varid% ++|--", varid) && MathLib::isInt(value)) {
|
||||||
const std::string op(tok3->strAt(2));
|
const std::string op(tok3->strAt(2));
|
||||||
if (Token::Match(tok3, "[{};] %any% %any% ;")) {
|
if (Token::Match(tok3, "[{};] %any% %any% ;")) {
|
||||||
tok3->deleteNext();
|
Token::eraseTokens(tok3, 3);
|
||||||
tok3->deleteNext();
|
|
||||||
tok3->deleteNext();
|
|
||||||
} else {
|
} else {
|
||||||
tok3 = tok3->next();
|
tok3 = tok3->next();
|
||||||
tok3->str(value);
|
tok3->str(value);
|
||||||
|
@ -7199,9 +7136,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
|
||||||
(*tok2)->tokAt(2)->str(value);
|
(*tok2)->tokAt(2)->str(value);
|
||||||
(*tok2)->tokAt(2)->varId(valueVarId);
|
(*tok2)->tokAt(2)->varId(valueVarId);
|
||||||
if (Token::Match(tok3, "[;{}] %any% %any% ;")) {
|
if (Token::Match(tok3, "[;{}] %any% %any% ;")) {
|
||||||
tok3->deleteNext();
|
Token::eraseTokens(tok3, 3);
|
||||||
tok3->deleteNext();
|
|
||||||
tok3->deleteNext();
|
|
||||||
} else {
|
} else {
|
||||||
tok3->deleteNext();
|
tok3->deleteNext();
|
||||||
tok3->next()->str(value);
|
tok3->next()->str(value);
|
||||||
|
@ -7343,8 +7278,8 @@ bool Tokenizer::simplifyRedundantParenthesis()
|
||||||
|
|
||||||
if (Token::Match(tok, "( ( %bool% )") ||
|
if (Token::Match(tok, "( ( %bool% )") ||
|
||||||
Token::Match(tok, "( ( %num% )")) {
|
Token::Match(tok, "( ( %num% )")) {
|
||||||
tok->tokAt(2)->deleteNext();
|
|
||||||
tok->deleteNext();
|
tok->deleteNext();
|
||||||
|
tok->next()->deleteNext();
|
||||||
ret = true;
|
ret = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7430,8 +7365,7 @@ bool Tokenizer::simplifyCalculations()
|
||||||
if (Token::Match(tok->tokAt(-4), "[;{}] %var% = %var% [+-|] 0 ;") &&
|
if (Token::Match(tok->tokAt(-4), "[;{}] %var% = %var% [+-|] 0 ;") &&
|
||||||
tok->strAt(-3) == tok->previous()->str()) {
|
tok->strAt(-3) == tok->previous()->str()) {
|
||||||
tok = tok->tokAt(-3);
|
tok = tok->tokAt(-3);
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteThis();
|
tok->deleteThis();
|
||||||
}
|
}
|
||||||
tok->deleteNext();
|
tok->deleteNext();
|
||||||
|
@ -7505,8 +7439,7 @@ bool Tokenizer::simplifyCalculations()
|
||||||
result = (op1 > op2) ? "1" : "0";
|
result = (op1 > op2) ? "1" : "0";
|
||||||
|
|
||||||
tok->str(result);
|
tok->str(result);
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
ret = true;
|
ret = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7525,8 +7458,7 @@ bool Tokenizer::simplifyCalculations()
|
||||||
ss << result;
|
ss << result;
|
||||||
|
|
||||||
tok->str(ss.str());
|
tok->str(ss.str());
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7561,8 +7493,7 @@ bool Tokenizer::simplifyCalculations()
|
||||||
if (!result.empty()) {
|
if (!result.empty()) {
|
||||||
ret = true;
|
ret = true;
|
||||||
tok->str(result);
|
tok->str(result);
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7679,8 +7610,7 @@ void Tokenizer::simplifyGoto()
|
||||||
|
|
||||||
const std::string name(tok->str());
|
const std::string name(tok->str());
|
||||||
|
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteThis();
|
tok->deleteThis();
|
||||||
|
|
||||||
// This label is at the end of the function.. replace all matching goto statements..
|
// This label is at the end of the function.. replace all matching goto statements..
|
||||||
|
@ -7689,9 +7619,7 @@ void Tokenizer::simplifyGoto()
|
||||||
if (token->next()->str() == name) {
|
if (token->next()->str() == name) {
|
||||||
// Delete the "goto name;"
|
// Delete the "goto name;"
|
||||||
token = token->previous();
|
token = token->previous();
|
||||||
token->deleteNext();
|
Token::eraseTokens(token, 3);
|
||||||
token->deleteNext();
|
|
||||||
token->deleteNext();
|
|
||||||
|
|
||||||
// Insert the statements..
|
// Insert the statements..
|
||||||
bool ret = false; // is there return
|
bool ret = false; // is there return
|
||||||
|
@ -8181,8 +8109,7 @@ void Tokenizer::simplifyEnum()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasClass) {
|
if (hasClass) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 2);
|
||||||
tok2->deleteNext();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
simplify = false;
|
simplify = false;
|
||||||
|
@ -8268,8 +8195,7 @@ void Tokenizer::simplifyEnum()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasClass) {
|
if (hasClass) {
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 2);
|
||||||
tok2->deleteNext();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
simplify = false;
|
simplify = false;
|
||||||
|
@ -8473,11 +8399,7 @@ void Tokenizer::eraseDeadCode(Token *begin, const Token *end)
|
||||||
if (indentlevel == indentcase)
|
if (indentlevel == indentcase)
|
||||||
++indentlevel;
|
++indentlevel;
|
||||||
if (!checklabel || !indentswitch) {
|
if (!checklabel || !indentswitch) {
|
||||||
if (tok->next()->str() == "case")
|
Token::eraseTokens(tok, 3 + (tok->next()->str() == "case"));
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
} else {
|
} else {
|
||||||
tok = tok->tokAt(3 + (tok->next()->str() == "case"));
|
tok = tok->tokAt(3 + (tok->next()->str() == "case"));
|
||||||
}
|
}
|
||||||
|
@ -8499,11 +8421,7 @@ void Tokenizer::eraseDeadCode(Token *begin, const Token *end)
|
||||||
if (Token::Match(tok2->next(), "{|[|(")) {
|
if (Token::Match(tok2->next(), "{|[|(")) {
|
||||||
tok2 = tok2->next()->link();
|
tok2 = tok2->next()->link();
|
||||||
} else if (Token::Match(tok2, "[{};] case %any% : ;") || Token::Match(tok2, "[{};] default : ;")) {
|
} else if (Token::Match(tok2, "[{};] case %any% : ;") || Token::Match(tok2, "[{};] default : ;")) {
|
||||||
if (tok2->next()->str() == "case")
|
Token::eraseTokens(tok2, 3 + (tok2->next()->str() == "case"));
|
||||||
tok2->deleteNext();
|
|
||||||
tok2->deleteNext();
|
|
||||||
tok2->deleteNext();
|
|
||||||
tok2->deleteNext();
|
|
||||||
} else if (tok2->next()->str() == "}") {
|
} else if (tok2->next()->str() == "}") {
|
||||||
--indentlevel2;
|
--indentlevel2;
|
||||||
if (indentlevel2 <= indentcase)
|
if (indentlevel2 <= indentcase)
|
||||||
|
@ -9079,10 +8997,7 @@ void Tokenizer::simplifyWhile0()
|
||||||
|
|
||||||
// delete "(}) while ( 0 ) (;)"
|
// delete "(}) while ( 0 ) (;)"
|
||||||
tok = tok->previous();
|
tok = tok->previous();
|
||||||
tok->deleteNext(); // while
|
Token::eraseTokens(tok, 4); // while ( 0 )
|
||||||
tok->deleteNext(); // (
|
|
||||||
tok->deleteNext(); // 0
|
|
||||||
tok->deleteNext(); // )
|
|
||||||
if (tok->next() && tok->next()->str() == ";")
|
if (tok->next() && tok->next()->str() == ";")
|
||||||
tok->deleteNext(); // ;
|
tok->deleteNext(); // ;
|
||||||
if (!flowmatch)
|
if (!flowmatch)
|
||||||
|
@ -9444,10 +9359,10 @@ void Tokenizer::simplifyBitfields()
|
||||||
}
|
}
|
||||||
} else if (Token::Match(tok, ";|{|}|public:|protected:|private: const| %type% : %any% ;") &&
|
} else if (Token::Match(tok, ";|{|}|public:|protected:|private: const| %type% : %any% ;") &&
|
||||||
tok->next()->str() != "default") {
|
tok->next()->str() != "default") {
|
||||||
unsigned char offset = (tok->next()->str() == "const");
|
bool offset = (tok->next()->str() == "const");
|
||||||
|
|
||||||
if (tok->strAt(3 + offset) != "{") {
|
if (tok->strAt(3 + offset) != "{") {
|
||||||
Token::eraseTokens(tok, tok->tokAt(5 + offset));
|
Token::eraseTokens(tok, 4+offset);
|
||||||
tok = tok->previous();
|
tok = tok->previous();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9492,11 +9407,10 @@ void Tokenizer::simplifyBuiltinExpect()
|
||||||
}
|
}
|
||||||
} else if (Token::Match(tok->next(), "likely|unlikely (")) {
|
} else if (Token::Match(tok->next(), "likely|unlikely (")) {
|
||||||
// remove closing ')'
|
// remove closing ')'
|
||||||
tok->linkAt(2)->previous()->deleteNext();
|
tok->linkAt(2)->deleteThis();
|
||||||
|
|
||||||
// remove "likely|unlikely ("
|
// remove "likely|unlikely ("
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 2);
|
||||||
tok->deleteNext();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9513,14 +9427,9 @@ void Tokenizer::simplifyMicrosoftMFC()
|
||||||
|
|
||||||
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
for (Token *tok = _tokens; tok; tok = tok->next()) {
|
||||||
if (Token::simpleMatch(tok->next(), "DECLARE_MESSAGE_MAP ( )")) {
|
if (Token::simpleMatch(tok->next(), "DECLARE_MESSAGE_MAP ( )")) {
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 3);
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
} else if (Token::Match(tok->next(), "DECLARE_DYNAMIC|DECLARE_DYNAMIC_CLASS|DECLARE_DYNCREATE ( %any% )")) {
|
} else if (Token::Match(tok->next(), "DECLARE_DYNAMIC|DECLARE_DYNAMIC_CLASS|DECLARE_DYNCREATE ( %any% )")) {
|
||||||
tok->deleteNext();
|
Token::eraseTokens(tok, 4);
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
tok->deleteNext();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9790,8 +9699,7 @@ void Tokenizer::simplifyQtSignalsSlots()
|
||||||
} else if (Token::Match(tok2->next(), "public|protected|private slots|Q_SLOTS :")) {
|
} else if (Token::Match(tok2->next(), "public|protected|private slots|Q_SLOTS :")) {
|
||||||
tok2 = tok2->next();
|
tok2 = tok2->next();
|
||||||
tok2->str(tok2->str() + ":");
|
tok2->str(tok2->str() + ":");
|
||||||
tok2->deleteNext();
|
Token::eraseTokens(tok2, 2);
|
||||||
tok2->deleteNext();
|
|
||||||
tok2 = tok2->previous();
|
tok2 = tok2->previous();
|
||||||
} else if (Token::Match(tok2->next(), "signals|Q_SIGNALS :")) {
|
} else if (Token::Match(tok2->next(), "signals|Q_SIGNALS :")) {
|
||||||
tok2 = tok2->next();
|
tok2 = tok2->next();
|
||||||
|
|
Loading…
Reference in New Issue