CheckUnusedVar: Code cleanup

This commit is contained in:
Daniel Marjamäki 2018-12-17 17:27:45 +01:00
parent 9290122c2f
commit cf09fd6274
1 changed files with 3 additions and 58 deletions

View File

@ -793,21 +793,10 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
else else
tok = scope->classDef->next(); tok = scope->classDef->next();
for (; tok && tok != scope->bodyEnd; tok = tok->next()) { for (; tok && tok != scope->bodyEnd; tok = tok->next()) {
if (tok->str() == "for" || tok->str() == "while" || tok->str() == "do") {
for (std::list<Scope*>::const_iterator i = scope->nestedList.begin(); i != scope->nestedList.end(); ++i) {
if ((*i)->classDef == tok) { // Find associated scope
checkFunctionVariableUsage_iterateScopes(*i, variables, true); // Scan child scope
tok = (*i)->bodyStart->link();
break;
}
}
if (!tok)
break;
}
if (tok->str() == "{" && tok != scope->bodyStart && !tok->previous()->varId()) { if (tok->str() == "{" && tok != scope->bodyStart && !tok->previous()->varId()) {
for (std::list<Scope*>::const_iterator i = scope->nestedList.begin(); i != scope->nestedList.end(); ++i) { for (const Scope *i : scope->nestedList) {
if ((*i)->bodyStart == tok) { // Find associated scope if (i->bodyStart == tok) { // Find associated scope
checkFunctionVariableUsage_iterateScopes(*i, variables, false); // Scan child scope checkFunctionVariableUsage_iterateScopes(i, variables, false); // Scan child scope
tok = tok->link(); tok = tok->link();
break; break;
} }
@ -820,10 +809,6 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
variables.clear(); variables.clear();
break; break;
} }
if (Token::Match(tok, "goto|break")) { // #4447
variables.clear();
break;
}
// templates // templates
if (tok->isName() && endsWith(tok->str(), '>')) { if (tok->isName() && endsWith(tok->str(), '>')) {
@ -833,46 +818,6 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
variables.clear(); variables.clear();
} }
// bailout when for_each is used
if (Token::Match(tok, "%name% (") && Token::simpleMatch(tok->linkAt(1), ") {") && !Token::Match(tok, "if|for|while|switch")) {
// does the name contain "for_each" or "foreach"?
std::string nameTok;
nameTok.resize(tok->str().size());
std::transform(tok->str().begin(), tok->str().end(), nameTok.begin(), ::tolower);
if (nameTok.find("foreach") != std::string::npos || nameTok.find("for_each") != std::string::npos) {
// bailout all variables in the body that are used more than once.
// TODO: there is no need to bailout if variable is only read or only written
std::set<unsigned int> varid;
const Token * const endTok = tok->linkAt(1)->linkAt(1);
for (const Token *tok2 = endTok->link(); tok2 && tok2 != endTok; tok2 = tok2->next()) {
if (tok2->varId()) {
if (varid.find(tok2->varId()) == varid.end())
varid.insert(tok2->varId());
else
variables.erase(tok2->varId());
}
}
}
}
// C++11 std::for_each
// No warning should be written if a variable is first read and
// then written in the body.
else if (mTokenizer->isCPP() && Token::simpleMatch(tok, "for_each (") && Token::simpleMatch(tok->linkAt(1), ") ;")) {
const Token *end = tok->linkAt(1);
if (end->previous()->str() == "}") {
std::set<unsigned int> readvar;
for (const Token *body = end->linkAt(-1); body != end; body = body->next()) {
if (body->varId() == 0U)
continue;
if (!Token::simpleMatch(body->next(),"="))
readvar.insert(body->varId());
else if (readvar.find(body->varId()) != readvar.end())
variables.erase(body->varId());
}
}
}
else if (Token::Match(tok->previous(), "[;{}]")) { else if (Token::Match(tok->previous(), "[;{}]")) {
for (const Token* tok2 = tok->next(); tok2; tok2 = tok2->next()) { for (const Token* tok2 = tok->next(); tok2; tok2 = tok2->next()) {
if (tok2->varId()) { if (tok2->varId()) {