From 7189b303ae1e3ab252a2b82bcfb2e8fffa7d5bd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Mon, 21 Sep 2020 19:30:47 +0200 Subject: [PATCH] fixed some modernize-loop-convert clang-tidy warnings (#2815) --- gui/erroritem.cpp | 4 +- gui/mainwindow.cpp | 18 +++--- gui/projectfiledialog.cpp | 4 +- gui/resultstree.cpp | 5 +- gui/threadhandler.cpp | 14 ++--- lib/checkinternal.cpp | 26 +++------ lib/checkio.cpp | 44 +++++++------- lib/checkother.cpp | 9 ++- lib/checkunusedvar.cpp | 4 +- lib/cppcheck.cpp | 10 ++-- lib/pathmatch.cpp | 4 +- lib/preprocessor.cpp | 14 ++--- lib/symboldatabase.cpp | 119 ++++++++++++++++++-------------------- lib/tokenize.cpp | 8 +-- lib/valueflow.cpp | 6 +- test/testexprengine.cpp | 6 +- 16 files changed, 138 insertions(+), 157 deletions(-) diff --git a/gui/erroritem.cpp b/gui/erroritem.cpp index 89a4fd7f9..b09e5e5b7 100644 --- a/gui/erroritem.cpp +++ b/gui/erroritem.cpp @@ -80,8 +80,8 @@ QString ErrorItem::toString() const str += GuiSeverity::toString(severity) +"\n"; str += summary + "\n"; str += message + "\n"; - for (int i = 0; i < errorPath.size(); i++) { - str += " " + errorPath[i].file + ": " + QString::number(errorPath[i].line) + "\n"; + for (const QErrorPathItem& i : errorPath) { + str += " " + i.file + ": " + QString::number(i.line) + "\n"; } return str; } diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 5608e82a7..d763d6eaf 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -1042,9 +1042,9 @@ void MainWindow::analysisDone() enableResultsButtons(); - for (int i = 0; i < MaxRecentProjects + 1; i++) { - if (mRecentProjectActs[i] != nullptr) - mRecentProjectActs[i]->setEnabled(true); + for (QAction* recentProjectAct : mRecentProjectActs) { + if (recentProjectAct != nullptr) + recentProjectAct->setEnabled(true); } // Notify user - if the window is not active - that check is ready @@ -1068,9 +1068,9 @@ void MainWindow::checkLockDownUI() if (mScratchPad) mScratchPad->setEnabled(false); - for (int i = 0; i < MaxRecentProjects + 1; i++) { - if (mRecentProjectActs[i] != nullptr) - mRecentProjectActs[i]->setEnabled(false); + for (QAction* recentProjectAct : mRecentProjectActs) { + if (recentProjectAct != nullptr) + recentProjectAct->setEnabled(false); } } @@ -1772,9 +1772,9 @@ void MainWindow::openRecentProject() void MainWindow::updateMRUMenuItems() { - for (int i = 0; i < MaxRecentProjects + 1; i++) { - if (mRecentProjectActs[i] != nullptr) - mUI.mMenuFile->removeAction(mRecentProjectActs[i]); + for (QAction* recentProjectAct : mRecentProjectActs) { + if (recentProjectAct != nullptr) + mUI.mMenuFile->removeAction(recentProjectAct); } QStringList projects = mSettings->value(SETTINGS_MRU_PROJECTS).toStringList(); diff --git a/gui/projectfiledialog.cpp b/gui/projectfiledialog.cpp index 6c43ecc2a..ac0e7b277 100644 --- a/gui/projectfiledialog.cpp +++ b/gui/projectfiledialog.cpp @@ -160,8 +160,8 @@ ProjectFileDialog::ProjectFileDialog(ProjectFile *projectFile, QWidget *parent) // Platforms.. Platforms platforms; - for (int i = 0; i < numberOfBuiltinPlatforms; i++) - mUI.mComboBoxPlatform->addItem(platforms.get(builtinPlatforms[i]).mTitle); + for (cppcheck::Platform::PlatformType builtinPlatform : builtinPlatforms) + mUI.mComboBoxPlatform->addItem(platforms.get(builtinPlatform).mTitle); QStringList platformFiles; foreach (QString sp, searchPaths) { if (sp.endsWith("/cfg")) diff --git a/gui/resultstree.cpp b/gui/resultstree.cpp index d7a4bf4a6..d3ee28f35 100644 --- a/gui/resultstree.cpp +++ b/gui/resultstree.cpp @@ -726,9 +726,8 @@ void ResultsTree::contextMenuEvent(QContextMenuEvent * e) mContextItem = mModel.itemFromIndex(index); if (mContextItem && mApplications->getApplicationCount() > 0 && mContextItem->parent()) { //Disconnect all signals - for (int i = 0; i < actions.size(); i++) { - - disconnect(actions[i], SIGNAL(triggered()), signalMapper, SLOT(map())); + for (QAction* action : actions) { + disconnect(action, SIGNAL(triggered()), signalMapper, SLOT(map())); } disconnect(signalMapper, SIGNAL(mapped(int)), diff --git a/gui/threadhandler.cpp b/gui/threadhandler.cpp index 457904b43..3e63702e0 100644 --- a/gui/threadhandler.cpp +++ b/gui/threadhandler.cpp @@ -136,13 +136,13 @@ void ThreadHandler::setThreadCount(const int count) void ThreadHandler::removeThreads() { - for (int i = 0; i < mThreads.size(); i++) { - mThreads[i]->terminate(); - disconnect(mThreads[i], &CheckThread::done, + for (CheckThread* thread : mThreads) { + thread->terminate(); + disconnect(thread, &CheckThread::done, this, &ThreadHandler::threadDone); - disconnect(mThreads[i], &CheckThread::fileChecked, + disconnect(thread, &CheckThread::fileChecked, &mResults, &ThreadResult::fileChecked); - delete mThreads[i]; + delete thread; } mThreads.clear(); @@ -175,8 +175,8 @@ void ThreadHandler::stop() { mCheckStartTime = QDateTime(); mAnalyseWholeProgram = false; - for (int i = 0; i < mThreads.size(); i++) { - mThreads[i]->stop(); + for (CheckThread* thread : mThreads) { + thread->stop(); } } diff --git a/lib/checkinternal.cpp b/lib/checkinternal.cpp index 14eb16cd9..9a854bf15 100644 --- a/lib/checkinternal.cpp +++ b/lib/checkinternal.cpp @@ -37,8 +37,7 @@ namespace { void CheckInternal::checkTokenMatchPatterns() { const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); - for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) { - const Scope * scope = symbolDatabase->functionScopes[i]; + for (const Scope *scope : symbolDatabase->functionScopes) { for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch (")) continue; @@ -130,8 +129,7 @@ void CheckInternal::checkRedundantTokCheckError(const Token* tok) void CheckInternal::checkTokenSimpleMatchPatterns() { const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); - for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) { - const Scope * scope = symbolDatabase->functionScopes[i]; + for (const Scope* scope : symbolDatabase->functionScopes) { for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { if (!Token::simpleMatch(tok, "Token :: simpleMatch (") && !Token::simpleMatch(tok, "Token :: findsimplematch (")) continue; @@ -151,9 +149,7 @@ void CheckInternal::checkTokenSimpleMatchPatterns() // Check for [xyz] usage - but exclude standalone square brackets unsigned int char_count = 0; - for (std::string::size_type pos = 0; pos < pattern.size(); ++pos) { - char c = pattern[pos]; - + for (char c : pattern) { if (c == ' ') { char_count = 0; } else if (c == ']') { @@ -168,9 +164,7 @@ void CheckInternal::checkTokenSimpleMatchPatterns() // Check | usage: Count characters before the symbol char_count = 0; - for (std::string::size_type pos = 0; pos < pattern.size(); ++pos) { - const char c = pattern[pos]; - + for (char c : pattern) { if (c == ' ') { char_count = 0; } else if (c == '|') { @@ -219,8 +213,7 @@ namespace { void CheckInternal::checkMissingPercentCharacter() { const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); - for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) { - const Scope * scope = symbolDatabase->functionScopes[i]; + for (const Scope* scope : symbolDatabase->functionScopes) { for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch (")) continue; @@ -262,8 +255,7 @@ void CheckInternal::checkMissingPercentCharacter() void CheckInternal::checkUnknownPattern() { const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); - for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) { - const Scope * scope = symbolDatabase->functionScopes[i]; + for (const Scope* scope : symbolDatabase->functionScopes) { for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch (")) continue; @@ -297,8 +289,7 @@ void CheckInternal::checkUnknownPattern() void CheckInternal::checkRedundantNextPrevious() { const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); - for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) { - const Scope * scope = symbolDatabase->functionScopes[i]; + for (const Scope* scope : symbolDatabase->functionScopes) { for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { if (tok->str() != ".") continue; @@ -329,8 +320,7 @@ void CheckInternal::checkRedundantNextPrevious() void CheckInternal::checkExtraWhitespace() { const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); - for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) { - const Scope * scope = symbolDatabase->functionScopes[i]; + for (const Scope* scope : symbolDatabase->functionScopes) { for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { if (!Token::Match(tok, "Token :: simpleMatch|findsimplematch|Match|findmatch (")) continue; diff --git a/lib/checkio.cpp b/lib/checkio.cpp index d892b14ab..8939fa859 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -146,22 +146,22 @@ void CheckIO::checkFileUsage() indent++; else if (tok->str() == "}") { indent--; - for (std::map::iterator i = filepointers.begin(); i != filepointers.end(); ++i) { - if (indent < i->second.mode_indent) { - i->second.mode_indent = 0; - i->second.mode = UNKNOWN_OM; + for (std::pair& filepointer : filepointers) { + if (indent < filepointer.second.mode_indent) { + filepointer.second.mode_indent = 0; + filepointer.second.mode = UNKNOWN_OM; } - if (indent < i->second.op_indent) { - i->second.op_indent = 0; - i->second.lastOperation = Filepointer::UNKNOWN_OP; + if (indent < filepointer.second.op_indent) { + filepointer.second.op_indent = 0; + filepointer.second.lastOperation = Filepointer::UNKNOWN_OP; } } } else if (tok->str() == "return" || tok->str() == "continue" || tok->str() == "break" || mSettings->library.isnoreturn(tok)) { // Reset upon return, continue or break - for (std::map::iterator i = filepointers.begin(); i != filepointers.end(); ++i) { - i->second.mode_indent = 0; - i->second.mode = UNKNOWN_OM; - i->second.op_indent = 0; - i->second.lastOperation = Filepointer::UNKNOWN_OP; + for (std::pair& filepointer : filepointers) { + filepointer.second.mode_indent = 0; + filepointer.second.mode = UNKNOWN_OM; + filepointer.second.op_indent = 0; + filepointer.second.lastOperation = Filepointer::UNKNOWN_OP; } } else if (Token::Match(tok, "%var% =") && (tok->strAt(2) != "fopen" && tok->strAt(2) != "freopen" && tok->strAt(2) != "tmpfile" && @@ -236,13 +236,13 @@ void CheckIO::checkFileUsage() const Token* const end2 = tok->linkAt(1); if (scope->functionOf && scope->functionOf->isClassOrStruct() && !scope->function->isStatic() && ((tok->strAt(-1) != "::" && tok->strAt(-1) != ".") || tok->strAt(-2) == "this")) { if (!tok->function() || (tok->function()->nestedIn && tok->function()->nestedIn->isClassOrStruct())) { - for (std::map::iterator i = filepointers.begin(); i != filepointers.end(); ++i) { - const Variable* var = symbolDatabase->getVariableFromVarId(i->first); + for (std::pair& filepointer : filepointers) { + const Variable* var = symbolDatabase->getVariableFromVarId(filepointer.first); if (!var || !(var->isLocal() || var->isGlobal() || var->isStatic())) { - i->second.mode = UNKNOWN_OM; - i->second.mode_indent = 0; - i->second.op_indent = indent; - i->second.lastOperation = Filepointer::UNKNOWN_OP; + filepointer.second.mode = UNKNOWN_OM; + filepointer.second.mode_indent = 0; + filepointer.second.op_indent = indent; + filepointer.second.lastOperation = Filepointer::UNKNOWN_OP; } } continue; @@ -326,10 +326,10 @@ void CheckIO::checkFileUsage() } } } - for (std::map::iterator i = filepointers.begin(); i != filepointers.end(); ++i) { - i->second.op_indent = 0; - i->second.mode = UNKNOWN_OM; - i->second.lastOperation = Filepointer::UNKNOWN_OP; + for (std::pair& filepointer : filepointers) { + filepointer.second.op_indent = 0; + filepointer.second.mode = UNKNOWN_OM; + filepointer.second.lastOperation = Filepointer::UNKNOWN_OP; } } } diff --git a/lib/checkother.cpp b/lib/checkother.cpp index b2fba947e..64a9b7762 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1979,12 +1979,11 @@ namespace { for (const Function &func : scope.functionList) { functionsByName[func.tokenDef->str()].push_back(&func); } - for (StringFunctionMap::iterator it = functionsByName.begin(); - it != functionsByName.end(); ++it) { - const std::list::const_iterator nc = std::find_if(it->second.begin(), it->second.end(), notconst); - if (nc == it->second.end()) { + for (std::pair>& it : functionsByName) { + const std::list::const_iterator nc = std::find_if(it.second.begin(), it.second.end(), notconst); + if (nc == it.second.end()) { // ok to add all of them - constFunctions.splice(constFunctions.end(), it->second); + constFunctions.splice(constFunctions.end(), it.second); } } } diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index b29b58756..e033b04ad 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -245,8 +245,8 @@ void Variables::readAliases(unsigned int varid, const Token* tok) VariableUsage *usage = find(varid); if (usage) { - for (std::set::iterator aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) { - VariableUsage *aliased = find(*aliases); + for (unsigned int aliases : usage->_aliases) { + VariableUsage *aliased = find(aliases); if (aliased) { aliased->_read = true; diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 79d8c34e4..3c3198bad 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -202,14 +202,14 @@ static std::string executeAddon(const AddonInfo &addonInfo, pythonExe = cmdFileName(defaultPythonExe); else { #ifdef _WIN32 - const char *p[] = { "python3.exe", "python.exe" }; + const char *py_exes[] = { "python3.exe", "python.exe" }; #else - const char *p[] = { "python3", "python" }; + const char *py_exes[] = { "python3", "python" }; #endif - for (int i = 0; i < 2; ++i) { + for (const char* py_exe : py_exes) { std::string out; - if (executeCommand(p[i], split("--version"), redirect, &out) && out.compare(0, 7, "Python ") == 0 && std::isdigit(out[7])) { - pythonExe = p[i]; + if (executeCommand(py_exe, split("--version"), redirect, &out) && out.compare(0, 7, "Python ") == 0 && std::isdigit(out[7])) { + pythonExe = py_exe; break; } } diff --git a/lib/pathmatch.cpp b/lib/pathmatch.cpp index 79525cd80..8889cc5ca 100644 --- a/lib/pathmatch.cpp +++ b/lib/pathmatch.cpp @@ -29,8 +29,8 @@ PathMatch::PathMatch(const std::vector &excludedPaths, bool caseSen : mExcludedPaths(excludedPaths), mCaseSensitive(caseSensitive) { if (!mCaseSensitive) - for (std::vector::iterator i = mExcludedPaths.begin(); i != mExcludedPaths.end(); ++i) - std::transform(i->begin(), i->end(), i->begin(), ::tolower); + for (std::string& excludedPath : mExcludedPaths) + std::transform(excludedPath.begin(), excludedPath.end(), excludedPath.begin(), ::tolower); mWorkingDirectory.push_back(Path::getCurrentPath()); } diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 7d293f2ef..428203136 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -69,8 +69,8 @@ Preprocessor::Preprocessor(Settings& settings, ErrorLogger *errorLogger) : mSett Preprocessor::~Preprocessor() { - for (std::map::iterator it = mTokenLists.begin(); it != mTokenLists.end(); ++it) - delete it->second; + for (std::pair& tokenList : mTokenLists) + delete tokenList.second; } namespace { @@ -657,9 +657,9 @@ bool Preprocessor::loadFiles(const simplecpp::TokenList &rawtokens, std::vector< void Preprocessor::removeComments() { - for (std::map::iterator it = mTokenLists.begin(); it != mTokenLists.end(); ++it) { - if (it->second) - it->second->removeComments(); + for (std::pair& tokenList : mTokenLists) { + if (tokenList.second) + tokenList.second->removeComments(); } } @@ -982,8 +982,8 @@ unsigned int Preprocessor::calculateChecksum(const simplecpp::TokenList &tokens1 void Preprocessor::simplifyPragmaAsm(simplecpp::TokenList *tokenList) { Preprocessor::simplifyPragmaAsmPrivate(tokenList); - for (std::map::iterator it = mTokenLists.begin(); it != mTokenLists.end(); ++it) { - Preprocessor::simplifyPragmaAsmPrivate(it->second); + for (std::pair& list : mTokenLists) { + Preprocessor::simplifyPragmaAsmPrivate(list.second); } } diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 4fe0efb92..5043f4a17 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -712,15 +712,15 @@ void SymbolDatabase::createSymbolDatabaseClassInfo() return; // fill in using info - for (std::list::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { - for (std::list::iterator i = it->usingList.begin(); i != it->usingList.end(); ++i) { + for (Scope& scope : scopeList) { + for (Scope::UsingInfo& usingInfo : scope.usingList) { // only find if not already found - if (i->scope == nullptr) { + if (usingInfo.scope == nullptr) { // check scope for match - const Scope * const scope = findScope(i->start->tokAt(2), &(*it)); - if (scope) { + const Scope * const found = findScope(usingInfo.start->tokAt(2), &scope); + if (found) { // set found scope - i->scope = scope; + usingInfo.scope = found; break; } } @@ -728,11 +728,11 @@ void SymbolDatabase::createSymbolDatabaseClassInfo() } // fill in base class info - for (std::list::iterator it = typeList.begin(); it != typeList.end(); ++it) { + for (Type& type : typeList) { // finish filling in base class info - for (Type::BaseInfo & i : it->derivedFrom) { - const Type* found = findType(i.nameTok, it->enclosingScope); - if (found && found->findDependency(&(*it))) { + for (Type::BaseInfo & i : type.derivedFrom) { + const Type* found = findType(i.nameTok, type.enclosingScope); + if (found && found->findDependency(&type)) { // circular dependency //mTokenizer->syntaxError(nullptr); } else { @@ -742,9 +742,9 @@ void SymbolDatabase::createSymbolDatabaseClassInfo() } // fill in friend info - for (std::list::iterator it = typeList.begin(); it != typeList.end(); ++it) { - for (Type::FriendInfo &friendInfo : it->friendList) { - friendInfo.type = findType(friendInfo.nameStart, it->enclosingScope); + for (Type & type : typeList) { + for (Type::FriendInfo &friendInfo : type.friendList) { + friendInfo.type = findType(friendInfo.nameStart, type.enclosingScope); } } } @@ -753,18 +753,18 @@ void SymbolDatabase::createSymbolDatabaseClassInfo() void SymbolDatabase::createSymbolDatabaseVariableInfo() { // fill in variable info - for (std::list::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { + for (Scope& scope : scopeList) { // find variables - it->getVariableList(mSettings); + scope.getVariableList(mSettings); } // fill in function arguments - for (std::list::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { + for (Scope& scope : scopeList) { std::list::iterator func; - for (func = it->functionList.begin(); func != it->functionList.end(); ++func) { + for (func = scope.functionList.begin(); func != scope.functionList.end(); ++func) { // add arguments - func->addArguments(this, &*it); + func->addArguments(this, &scope); } } } @@ -772,17 +772,17 @@ void SymbolDatabase::createSymbolDatabaseVariableInfo() void SymbolDatabase::createSymbolDatabaseCopyAndMoveConstructors() { // fill in class and struct copy/move constructors - for (std::list::iterator scope = scopeList.begin(); scope != scopeList.end(); ++scope) { - if (!scope->isClassOrStruct()) + for (Scope& scope : scopeList) { + if (!scope.isClassOrStruct()) continue; std::list::iterator func; - for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { + for (func = scope.functionList.begin(); func != scope.functionList.end(); ++func) { if (!func->isConstructor() || func->minArgCount() != 1) continue; const Variable* firstArg = func->getArgumentVar(0); - if (firstArg->type() == scope->definedType) { + if (firstArg->type() == scope.definedType) { if (firstArg->isRValueReference()) func->type = Function::eMoveConstructor; else if (firstArg->isReference() && !firstArg->isPointer()) @@ -791,7 +791,7 @@ void SymbolDatabase::createSymbolDatabaseCopyAndMoveConstructors() if (func->type == Function::eCopyConstructor || func->type == Function::eMoveConstructor) - scope->numCopyOrMoveConstructors++; + scope.numCopyOrMoveConstructors++; } } } @@ -799,35 +799,35 @@ void SymbolDatabase::createSymbolDatabaseCopyAndMoveConstructors() void SymbolDatabase::createSymbolDatabaseFunctionScopes() { // fill in function scopes - for (std::list::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { - if (it->type == Scope::eFunction) - functionScopes.push_back(&*it); + for (Scope & scope : scopeList) { + if (scope.type == Scope::eFunction) + functionScopes.push_back(&scope); } } void SymbolDatabase::createSymbolDatabaseClassAndStructScopes() { // fill in class and struct scopes - for (std::list::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { - if (it->isClassOrStruct()) - classAndStructScopes.push_back(&*it); + for (Scope& scope : scopeList) { + if (scope.isClassOrStruct()) + classAndStructScopes.push_back(&scope); } } void SymbolDatabase::createSymbolDatabaseFunctionReturnTypes() { // fill in function return types - for (std::list::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { + for (Scope& scope : scopeList) { std::list::iterator func; - for (func = it->functionList.begin(); func != it->functionList.end(); ++func) { + for (func = scope.functionList.begin(); func != scope.functionList.end(); ++func) { // add return types if (func->retDef) { const Token *type = func->retDef; while (Token::Match(type, "static|const|struct|union|enum")) type = type->next(); if (type) { - func->retType = findVariableTypeInBase(&*it, type); + func->retType = findVariableTypeInBase(&scope, type); if (!func->retType) func->retType = findTypeInNested(type, func->nestedIn); } @@ -840,10 +840,9 @@ void SymbolDatabase::createSymbolDatabaseNeedInitialization() { if (mTokenizer->isC()) { // For C code it is easy, as there are no constructors and no default values - for (std::list::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { - Scope *scope = &(*it); - if (scope->definedType) - scope->definedType->needInitialization = Type::NeedInitialization::True; + for (Scope& scope : scopeList) { + if (scope.definedType) + scope.definedType->needInitialization = Type::NeedInitialization::True; } } else { // For C++, it is more difficult: Determine if user defined type needs initialization... @@ -853,21 +852,19 @@ void SymbolDatabase::createSymbolDatabaseNeedInitialization() do { unknowns = 0; - for (std::list::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { - Scope *scope = &(*it); - - if (!scope->definedType) { + for (Scope& scope : scopeList) { + if (!scope.definedType) { mBlankTypes.emplace_back(); - scope->definedType = &mBlankTypes.back(); + scope.definedType = &mBlankTypes.back(); } - if (scope->isClassOrStruct() && scope->definedType->needInitialization == Type::NeedInitialization::Unknown) { + if (scope.isClassOrStruct() && scope.definedType->needInitialization == Type::NeedInitialization::Unknown) { // check for default constructor bool hasDefaultConstructor = false; std::list::const_iterator func; - for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { + for (func = scope.functionList.begin(); func != scope.functionList.end(); ++func) { if (func->type == Function::eConstructor) { // check for no arguments: func ( ) if (func->argCount() == 0) { @@ -887,7 +884,7 @@ void SymbolDatabase::createSymbolDatabaseNeedInitialization() // We assume the default constructor initializes everything. // Another check will figure out if the constructor actually initializes everything. if (hasDefaultConstructor) - scope->definedType->needInitialization = Type::NeedInitialization::False; + scope.definedType->needInitialization = Type::NeedInitialization::False; // check each member variable to see if it needs initialization else { @@ -895,7 +892,7 @@ void SymbolDatabase::createSymbolDatabaseNeedInitialization() bool unknown = false; std::list::const_iterator var; - for (var = scope->varlist.begin(); var != scope->varlist.end() && !needInitialization; ++var) { + for (var = scope.varlist.begin(); var != scope.varlist.end() && !needInitialization; ++var) { if (var->isClass()) { if (var->type()) { // does this type need initialization? @@ -911,16 +908,16 @@ void SymbolDatabase::createSymbolDatabaseNeedInitialization() } if (needInitialization) - scope->definedType->needInitialization = Type::NeedInitialization::True; + scope.definedType->needInitialization = Type::NeedInitialization::True; else if (!unknown) - scope->definedType->needInitialization = Type::NeedInitialization::False; + scope.definedType->needInitialization = Type::NeedInitialization::False; else { - if (scope->definedType->needInitialization == Type::NeedInitialization::Unknown) + if (scope.definedType->needInitialization == Type::NeedInitialization::Unknown) unknowns++; } } - } else if (scope->type == Scope::eUnion && scope->definedType->needInitialization == Type::NeedInitialization::Unknown) - scope->definedType->needInitialization = Type::NeedInitialization::True; + } else if (scope.type == Scope::eUnion && scope.definedType->needInitialization == Type::NeedInitialization::Unknown) + scope.definedType->needInitialization = Type::NeedInitialization::True; } retry++; @@ -928,11 +925,9 @@ void SymbolDatabase::createSymbolDatabaseNeedInitialization() // this shouldn't happen so output a debug warning if (retry == 100 && mSettings->debugwarnings) { - for (std::list::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { - const Scope *scope = &(*it); - - if (scope->isClassOrStruct() && scope->definedType->needInitialization == Type::NeedInitialization::Unknown) - debugMessage(scope->classDef, "SymbolDatabase::SymbolDatabase couldn't resolve all user defined types."); + for (const Scope& scope : scopeList) { + if (scope.isClassOrStruct() && scope.definedType->needInitialization == Type::NeedInitialization::Unknown) + debugMessage(scope.classDef, "SymbolDatabase::SymbolDatabase couldn't resolve all user defined types."); } } } @@ -945,25 +940,23 @@ void SymbolDatabase::createSymbolDatabaseVariableSymbolTable() std::fill_n(mVariableList.begin(), mVariableList.size(), (const Variable*)nullptr); // check all scopes for variables - for (std::list::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { - Scope *scope = &(*it); - + for (Scope& scope : scopeList) { // add all variables - for (std::list::iterator var = scope->varlist.begin(); var != scope->varlist.end(); ++var) { + for (std::list::iterator var = scope.varlist.begin(); var != scope.varlist.end(); ++var) { const unsigned int varId = var->declarationId(); if (varId) mVariableList[varId] = &(*var); // fix up variables without type if (!var->type() && !var->typeStartToken()->isStandardType()) { - const Type *type = findType(var->typeStartToken(), scope); + const Type *type = findType(var->typeStartToken(), &scope); if (type) var->type(type); } } // add all function parameters - for (std::list::iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { - for (std::list::iterator arg = func->argumentList.begin(); arg != func->argumentList.end(); ++arg) { + for (Function& func : scope.functionList) { + for (std::list::iterator arg = func.argumentList.begin(); arg != func.argumentList.end(); ++arg) { // check for named parameters if (arg->nameToken() && arg->declarationId()) { const unsigned int declarationId = arg->declarationId(); @@ -971,7 +964,7 @@ void SymbolDatabase::createSymbolDatabaseVariableSymbolTable() mVariableList[declarationId] = &(*arg); // fix up parameters without type if (!arg->type() && !arg->typeStartToken()->isStandardType()) { - const Type *type = findTypeInNested(arg->typeStartToken(), scope); + const Type *type = findTypeInNested(arg->typeStartToken(), &scope); if (type) arg->type(type); } diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a5f38a45d..787a1fb67 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6392,13 +6392,13 @@ void Tokenizer::simplifyFunctionParameters() if (argumentNames.size() != argumentNames2.size()) { //move back 'tok1' to the last ';' tok1 = tok1->previous(); - for (std::map::iterator it = argumentNames.begin(); it != argumentNames.end(); ++it) { - if (argumentNames2.find(it->first) == argumentNames2.end()) { + for (std::pair& argumentName : argumentNames) { + if (argumentNames2.find(argumentName.first) == argumentNames2.end()) { //add the missing parameter argument declaration tok1->insertToken(";"); - tok1->insertToken(it->first); + tok1->insertToken(argumentName.first); //register the change inside argumentNames2 - argumentNames2[it->first] = tok1->next(); + argumentNames2[argumentName.first] = tok1->next(); tok1->insertToken("int"); } } diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index c127944a1..ad60ed806 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -4100,9 +4100,9 @@ static void valueFlowForwardAssign(Token * const tok, if (!var->isPointer() && !var->isSmartPointer()) values.remove_if(std::mem_fn(&ValueFlow::Value::isTokValue)); if (tok->astParent()) { - for (std::list::iterator it = values.begin(); it != values.end(); ++it) { - const std::string info = "Assignment '" + tok->astParent()->expressionString() + "', assigned value is " + it->infoString(); - it->errorPath.emplace_back(tok, info); + for (ValueFlow::Value& value : values) { + const std::string info = "Assignment '" + tok->astParent()->expressionString() + "', assigned value is " + value.infoString(); + value.errorPath.emplace_back(tok, info); } } diff --git a/test/testexprengine.cpp b/test/testexprengine.cpp index 333cf9dfe..f403eae35 100644 --- a/test/testexprengine.cpp +++ b/test/testexprengine.cpp @@ -138,10 +138,10 @@ private: replace(line, "(fp.gt ", "(> "); replace(line, "(fp.lt ", "(< "); int par = 0; - for (int pos = 0; pos < line.size(); ++pos) { - if (line[pos] == '(') + for (char pos : line) { + if (pos == '(') par++; - else if (line[pos] == ')') + else if (pos == ')') --par; } if (par < 0)