Refactoring: Use range for loops

This commit is contained in:
Daniel Marjamäki 2018-10-24 11:29:15 +02:00
parent 2e4f317c0b
commit be1ff268c0
1 changed files with 45 additions and 50 deletions

View File

@ -65,39 +65,39 @@ void CheckUninitVar::check()
} }
// check every executable scope // check every executable scope
for (std::list<Scope>::const_iterator scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) { for (const Scope &scope : symbolDatabase->scopeList) {
if (scope->isExecutable()) { if (scope.isExecutable()) {
checkScope(&*scope, arrayTypeDefs); checkScope(&scope, arrayTypeDefs);
} }
} }
} }
void CheckUninitVar::checkScope(const Scope* scope, const std::set<std::string> &arrayTypeDefs) void CheckUninitVar::checkScope(const Scope* scope, const std::set<std::string> &arrayTypeDefs)
{ {
for (std::list<Variable>::const_iterator i = scope->varlist.begin(); i != scope->varlist.end(); ++i) { for (const Variable &var : scope->varlist) {
if ((mTokenizer->isCPP() && i->type() && !i->isPointer() && i->type()->needInitialization != Type::True) || if ((mTokenizer->isCPP() && var.type() && !var.isPointer() && var.type()->needInitialization != Type::True) ||
i->isStatic() || i->isExtern() || i->isReference()) var.isStatic() || var.isExtern() || var.isReference())
continue; continue;
// don't warn for try/catch exception variable // don't warn for try/catch exception variable
if (i->isThrow()) if (var.isThrow())
continue; continue;
if (Token::Match(i->nameToken()->next(), "[({:]")) if (Token::Match(var.nameToken()->next(), "[({:]"))
continue; continue;
if (Token::Match(i->nameToken(), "%name% =")) { // Variable is initialized, but Rhs might be not if (Token::Match(var.nameToken(), "%name% =")) { // Variable is initialized, but Rhs might be not
checkRhs(i->nameToken(), *i, NO_ALLOC, 0U, emptyString); checkRhs(var.nameToken(), var, NO_ALLOC, 0U, emptyString);
continue; continue;
} }
if (Token::Match(i->nameToken(), "%name% ) (") && Token::simpleMatch(i->nameToken()->linkAt(2), ") =")) { // Function pointer is initialized, but Rhs might be not if (Token::Match(var.nameToken(), "%name% ) (") && Token::simpleMatch(var.nameToken()->linkAt(2), ") =")) { // Function pointer is initialized, but Rhs might be not
checkRhs(i->nameToken()->linkAt(2)->next(), *i, NO_ALLOC, 0U, emptyString); checkRhs(var.nameToken()->linkAt(2)->next(), var, NO_ALLOC, 0U, emptyString);
continue; continue;
} }
if (i->isArray() || i->isPointerToArray()) { if (var.isArray() || var.isPointerToArray()) {
const Token *tok = i->nameToken()->next(); const Token *tok = var.nameToken()->next();
if (i->isPointerToArray()) if (var.isPointerToArray())
tok = tok->next(); tok = tok->next();
while (Token::simpleMatch(tok->link(), "] [")) while (Token::simpleMatch(tok->link(), "] ["))
tok = tok->link()->next(); tok = tok->link()->next();
@ -105,13 +105,13 @@ void CheckUninitVar::checkScope(const Scope* scope, const std::set<std::string>
continue; continue;
} }
bool stdtype = mTokenizer->isC() && arrayTypeDefs.find(i->typeStartToken()->str()) == arrayTypeDefs.end(); bool stdtype = mTokenizer->isC() && arrayTypeDefs.find(var.typeStartToken()->str()) == arrayTypeDefs.end();
const Token* tok = i->typeStartToken(); const Token* tok = var.typeStartToken();
for (; tok != i->nameToken() && tok->str() != "<"; tok = tok->next()) { for (; tok != var.nameToken() && tok->str() != "<"; tok = tok->next()) {
if (tok->isStandardType() || tok->isEnumType()) if (tok->isStandardType() || tok->isEnumType())
stdtype = true; stdtype = true;
} }
if (i->isArray() && !stdtype) if (var.isArray() && !stdtype)
continue; continue;
while (tok && tok->str() != ";") while (tok && tok->str() != ";")
@ -120,38 +120,37 @@ void CheckUninitVar::checkScope(const Scope* scope, const std::set<std::string>
continue; continue;
if (tok->astParent() && Token::simpleMatch(tok->astParent()->previous(), "for (") && if (tok->astParent() && Token::simpleMatch(tok->astParent()->previous(), "for (") &&
checkLoopBody(tok->astParent()->link()->next(), *i, i->isArray() ? ARRAY : NO_ALLOC, emptyString, true)) checkLoopBody(tok->astParent()->link()->next(), var, var.isArray() ? ARRAY : NO_ALLOC, emptyString, true))
continue; continue;
if (i->isArray()) { if (var.isArray()) {
Alloc alloc = ARRAY; Alloc alloc = ARRAY;
const std::map<unsigned int, VariableValue> variableValue; const std::map<unsigned int, VariableValue> variableValue;
checkScopeForVariable(tok, *i, nullptr, nullptr, &alloc, emptyString, variableValue); checkScopeForVariable(tok, var, nullptr, nullptr, &alloc, emptyString, variableValue);
continue; continue;
} }
if (stdtype || i->isPointer()) { if (stdtype || var.isPointer()) {
Alloc alloc = NO_ALLOC; Alloc alloc = NO_ALLOC;
const std::map<unsigned int, VariableValue> variableValue; const std::map<unsigned int, VariableValue> variableValue;
checkScopeForVariable(tok, *i, nullptr, nullptr, &alloc, emptyString, variableValue); checkScopeForVariable(tok, var, nullptr, nullptr, &alloc, emptyString, variableValue);
} }
if (i->type()) if (var.type())
checkStruct(tok, *i); checkStruct(tok, var);
} }
if (scope->function) { if (scope->function) {
for (unsigned int i = 0; i < scope->function->argCount(); i++) { for (const Variable &arg : scope->function->argumentList) {
const Variable *arg = scope->function->getArgumentVar(i); if (arg.declarationId() && Token::Match(arg.typeStartToken(), "%type% * %name% [,)]")) {
if (arg && arg->declarationId() && Token::Match(arg->typeStartToken(), "%type% * %name% [,)]")) {
// Treat the pointer as initialized until it is assigned by malloc // Treat the pointer as initialized until it is assigned by malloc
for (const Token *tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) { for (const Token *tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
if (Token::Match(tok, "[;{}] %varid% = %name% (", arg->declarationId()) && if (Token::Match(tok, "[;{}] %varid% = %name% (", arg.declarationId()) &&
mSettings->library.returnuninitdata.count(tok->strAt(3)) == 1U) { mSettings->library.returnuninitdata.count(tok->strAt(3)) == 1U) {
if (arg->typeStartToken()->strAt(-1) == "struct" || (arg->type() && arg->type()->isStructType())) if (arg.typeStartToken()->strAt(-1) == "struct" || (arg.type() && arg.type()->isStructType()))
checkStruct(tok, *arg); checkStruct(tok, arg);
else if (arg->typeStartToken()->isStandardType() || arg->typeStartToken()->isEnumType()) { else if (arg.typeStartToken()->isStandardType() || arg.typeStartToken()->isEnumType()) {
Alloc alloc = NO_ALLOC; Alloc alloc = NO_ALLOC;
const std::map<unsigned int, VariableValue> variableValue; const std::map<unsigned int, VariableValue> variableValue;
checkScopeForVariable(tok->next(), *arg, nullptr, nullptr, &alloc, emptyString, variableValue); checkScopeForVariable(tok->next(), arg, nullptr, nullptr, &alloc, emptyString, variableValue);
} }
} }
} }
@ -164,23 +163,19 @@ void CheckUninitVar::checkStruct(const Token *tok, const Variable &structvar)
{ {
const Token *typeToken = structvar.typeStartToken(); const Token *typeToken = structvar.typeStartToken();
const SymbolDatabase * symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase * symbolDatabase = mTokenizer->getSymbolDatabase();
for (std::size_t j = 0U; j < symbolDatabase->classAndStructScopes.size(); ++j) { for (const Scope *scope2 : symbolDatabase->classAndStructScopes) {
const Scope *scope2 = symbolDatabase->classAndStructScopes[j];
if (scope2->className == typeToken->str() && scope2->numConstructors == 0U) { if (scope2->className == typeToken->str() && scope2->numConstructors == 0U) {
for (std::list<Variable>::const_iterator it = scope2->varlist.begin(); it != scope2->varlist.end(); ++it) { for (const Variable &var : scope2->varlist) {
const Variable &var = *it;
if (var.isStatic() || var.hasDefault() || var.isArray() || if (var.isStatic() || var.hasDefault() || var.isArray() ||
(!mTokenizer->isC() && var.isClass() && (!var.type() || var.type()->needInitialization != Type::True))) (!mTokenizer->isC() && var.isClass() && (!var.type() || var.type()->needInitialization != Type::True)))
continue; continue;
// is the variable declared in a inner union? // is the variable declared in a inner union?
bool innerunion = false; bool innerunion = false;
for (auto it2 = scope2->nestedList.cbegin(); it2 != scope2->nestedList.cend(); ++it2) { for (const Scope *innerScope : scope2->nestedList) {
const Scope &innerScope = **it2; if (innerScope->type == Scope::eUnion) {
if (innerScope.type == Scope::eUnion) { if (var.typeStartToken()->linenr() >= innerScope->bodyStart->linenr() &&
if (var.typeStartToken()->linenr() >= innerScope.bodyStart->linenr() && var.typeStartToken()->linenr() <= innerScope->bodyEnd->linenr()) {
var.typeStartToken()->linenr() <= innerScope.bodyEnd->linenr()) {
innerunion = true; innerunion = true;
break; break;
} }
@ -1231,10 +1226,10 @@ void CheckUninitVar::valueFlowUninit()
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
// check every executable scope // check every executable scope
for (std::list<Scope>::const_iterator scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) { for (const Scope &scope : symbolDatabase->scopeList) {
if (!scope->isExecutable()) if (!scope.isExecutable())
continue; continue;
for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) { for (const Token* tok = scope.bodyStart; tok != scope.bodyEnd; tok = tok->next()) {
if (Token::simpleMatch(tok, "sizeof (")) { if (Token::simpleMatch(tok, "sizeof (")) {
tok = tok->linkAt(1); tok = tok->linkAt(1);
continue; continue;
@ -1256,11 +1251,11 @@ void CheckUninitVar::deadPointer()
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
// check every executable scope // check every executable scope
for (std::list<Scope>::const_iterator scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) { for (const Scope &scope : symbolDatabase->scopeList) {
if (!scope->isExecutable()) if (!scope.isExecutable())
continue; continue;
// Dead pointers.. // Dead pointers..
for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) { for (const Token* tok = scope.bodyStart; tok != scope.bodyEnd; tok = tok->next()) {
if (tok->variable() && if (tok->variable() &&
tok->variable()->isPointer() && tok->variable()->isPointer() &&
isVariableUsage(tok, true, NO_ALLOC)) { isVariableUsage(tok, true, NO_ALLOC)) {