fixed some modernize-loop-convert clang-tidy warnings (#2815)

This commit is contained in:
Oliver Stöneberg 2020-09-21 19:30:47 +02:00 committed by GitHub
parent 98b6238450
commit 7189b303ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 138 additions and 157 deletions

View File

@ -80,8 +80,8 @@ QString ErrorItem::toString() const
str += GuiSeverity::toString(severity) +"\n"; str += GuiSeverity::toString(severity) +"\n";
str += summary + "\n"; str += summary + "\n";
str += message + "\n"; str += message + "\n";
for (int i = 0; i < errorPath.size(); i++) { for (const QErrorPathItem& i : errorPath) {
str += " " + errorPath[i].file + ": " + QString::number(errorPath[i].line) + "\n"; str += " " + i.file + ": " + QString::number(i.line) + "\n";
} }
return str; return str;
} }

View File

@ -1042,9 +1042,9 @@ void MainWindow::analysisDone()
enableResultsButtons(); enableResultsButtons();
for (int i = 0; i < MaxRecentProjects + 1; i++) { for (QAction* recentProjectAct : mRecentProjectActs) {
if (mRecentProjectActs[i] != nullptr) if (recentProjectAct != nullptr)
mRecentProjectActs[i]->setEnabled(true); recentProjectAct->setEnabled(true);
} }
// Notify user - if the window is not active - that check is ready // Notify user - if the window is not active - that check is ready
@ -1068,9 +1068,9 @@ void MainWindow::checkLockDownUI()
if (mScratchPad) if (mScratchPad)
mScratchPad->setEnabled(false); mScratchPad->setEnabled(false);
for (int i = 0; i < MaxRecentProjects + 1; i++) { for (QAction* recentProjectAct : mRecentProjectActs) {
if (mRecentProjectActs[i] != nullptr) if (recentProjectAct != nullptr)
mRecentProjectActs[i]->setEnabled(false); recentProjectAct->setEnabled(false);
} }
} }
@ -1772,9 +1772,9 @@ void MainWindow::openRecentProject()
void MainWindow::updateMRUMenuItems() void MainWindow::updateMRUMenuItems()
{ {
for (int i = 0; i < MaxRecentProjects + 1; i++) { for (QAction* recentProjectAct : mRecentProjectActs) {
if (mRecentProjectActs[i] != nullptr) if (recentProjectAct != nullptr)
mUI.mMenuFile->removeAction(mRecentProjectActs[i]); mUI.mMenuFile->removeAction(recentProjectAct);
} }
QStringList projects = mSettings->value(SETTINGS_MRU_PROJECTS).toStringList(); QStringList projects = mSettings->value(SETTINGS_MRU_PROJECTS).toStringList();

View File

@ -160,8 +160,8 @@ ProjectFileDialog::ProjectFileDialog(ProjectFile *projectFile, QWidget *parent)
// Platforms.. // Platforms..
Platforms platforms; Platforms platforms;
for (int i = 0; i < numberOfBuiltinPlatforms; i++) for (cppcheck::Platform::PlatformType builtinPlatform : builtinPlatforms)
mUI.mComboBoxPlatform->addItem(platforms.get(builtinPlatforms[i]).mTitle); mUI.mComboBoxPlatform->addItem(platforms.get(builtinPlatform).mTitle);
QStringList platformFiles; QStringList platformFiles;
foreach (QString sp, searchPaths) { foreach (QString sp, searchPaths) {
if (sp.endsWith("/cfg")) if (sp.endsWith("/cfg"))

View File

@ -726,9 +726,8 @@ void ResultsTree::contextMenuEvent(QContextMenuEvent * e)
mContextItem = mModel.itemFromIndex(index); mContextItem = mModel.itemFromIndex(index);
if (mContextItem && mApplications->getApplicationCount() > 0 && mContextItem->parent()) { if (mContextItem && mApplications->getApplicationCount() > 0 && mContextItem->parent()) {
//Disconnect all signals //Disconnect all signals
for (int i = 0; i < actions.size(); i++) { for (QAction* action : actions) {
disconnect(action, SIGNAL(triggered()), signalMapper, SLOT(map()));
disconnect(actions[i], SIGNAL(triggered()), signalMapper, SLOT(map()));
} }
disconnect(signalMapper, SIGNAL(mapped(int)), disconnect(signalMapper, SIGNAL(mapped(int)),

View File

@ -136,13 +136,13 @@ void ThreadHandler::setThreadCount(const int count)
void ThreadHandler::removeThreads() void ThreadHandler::removeThreads()
{ {
for (int i = 0; i < mThreads.size(); i++) { for (CheckThread* thread : mThreads) {
mThreads[i]->terminate(); thread->terminate();
disconnect(mThreads[i], &CheckThread::done, disconnect(thread, &CheckThread::done,
this, &ThreadHandler::threadDone); this, &ThreadHandler::threadDone);
disconnect(mThreads[i], &CheckThread::fileChecked, disconnect(thread, &CheckThread::fileChecked,
&mResults, &ThreadResult::fileChecked); &mResults, &ThreadResult::fileChecked);
delete mThreads[i]; delete thread;
} }
mThreads.clear(); mThreads.clear();
@ -175,8 +175,8 @@ void ThreadHandler::stop()
{ {
mCheckStartTime = QDateTime(); mCheckStartTime = QDateTime();
mAnalyseWholeProgram = false; mAnalyseWholeProgram = false;
for (int i = 0; i < mThreads.size(); i++) { for (CheckThread* thread : mThreads) {
mThreads[i]->stop(); thread->stop();
} }
} }

View File

@ -37,8 +37,7 @@ namespace {
void CheckInternal::checkTokenMatchPatterns() void CheckInternal::checkTokenMatchPatterns()
{ {
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) { for (const Scope *scope : symbolDatabase->functionScopes) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch (")) if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch ("))
continue; continue;
@ -130,8 +129,7 @@ void CheckInternal::checkRedundantTokCheckError(const Token* tok)
void CheckInternal::checkTokenSimpleMatchPatterns() void CheckInternal::checkTokenSimpleMatchPatterns()
{ {
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) { for (const Scope* scope : symbolDatabase->functionScopes) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
if (!Token::simpleMatch(tok, "Token :: simpleMatch (") && !Token::simpleMatch(tok, "Token :: findsimplematch (")) if (!Token::simpleMatch(tok, "Token :: simpleMatch (") && !Token::simpleMatch(tok, "Token :: findsimplematch ("))
continue; continue;
@ -151,9 +149,7 @@ void CheckInternal::checkTokenSimpleMatchPatterns()
// Check for [xyz] usage - but exclude standalone square brackets // Check for [xyz] usage - but exclude standalone square brackets
unsigned int char_count = 0; unsigned int char_count = 0;
for (std::string::size_type pos = 0; pos < pattern.size(); ++pos) { for (char c : pattern) {
char c = pattern[pos];
if (c == ' ') { if (c == ' ') {
char_count = 0; char_count = 0;
} else if (c == ']') { } else if (c == ']') {
@ -168,9 +164,7 @@ void CheckInternal::checkTokenSimpleMatchPatterns()
// Check | usage: Count characters before the symbol // Check | usage: Count characters before the symbol
char_count = 0; char_count = 0;
for (std::string::size_type pos = 0; pos < pattern.size(); ++pos) { for (char c : pattern) {
const char c = pattern[pos];
if (c == ' ') { if (c == ' ') {
char_count = 0; char_count = 0;
} else if (c == '|') { } else if (c == '|') {
@ -219,8 +213,7 @@ namespace {
void CheckInternal::checkMissingPercentCharacter() void CheckInternal::checkMissingPercentCharacter()
{ {
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) { for (const Scope* scope : symbolDatabase->functionScopes) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch (")) if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch ("))
continue; continue;
@ -262,8 +255,7 @@ void CheckInternal::checkMissingPercentCharacter()
void CheckInternal::checkUnknownPattern() void CheckInternal::checkUnknownPattern()
{ {
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) { for (const Scope* scope : symbolDatabase->functionScopes) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch (")) if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch ("))
continue; continue;
@ -297,8 +289,7 @@ void CheckInternal::checkUnknownPattern()
void CheckInternal::checkRedundantNextPrevious() void CheckInternal::checkRedundantNextPrevious()
{ {
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) { for (const Scope* scope : symbolDatabase->functionScopes) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
if (tok->str() != ".") if (tok->str() != ".")
continue; continue;
@ -329,8 +320,7 @@ void CheckInternal::checkRedundantNextPrevious()
void CheckInternal::checkExtraWhitespace() void CheckInternal::checkExtraWhitespace()
{ {
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) { for (const Scope* scope : symbolDatabase->functionScopes) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
if (!Token::Match(tok, "Token :: simpleMatch|findsimplematch|Match|findmatch (")) if (!Token::Match(tok, "Token :: simpleMatch|findsimplematch|Match|findmatch ("))
continue; continue;

View File

@ -146,22 +146,22 @@ void CheckIO::checkFileUsage()
indent++; indent++;
else if (tok->str() == "}") { else if (tok->str() == "}") {
indent--; indent--;
for (std::map<int, Filepointer>::iterator i = filepointers.begin(); i != filepointers.end(); ++i) { for (std::pair<const int, Filepointer>& filepointer : filepointers) {
if (indent < i->second.mode_indent) { if (indent < filepointer.second.mode_indent) {
i->second.mode_indent = 0; filepointer.second.mode_indent = 0;
i->second.mode = UNKNOWN_OM; filepointer.second.mode = UNKNOWN_OM;
} }
if (indent < i->second.op_indent) { if (indent < filepointer.second.op_indent) {
i->second.op_indent = 0; filepointer.second.op_indent = 0;
i->second.lastOperation = Filepointer::UNKNOWN_OP; 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 } else if (tok->str() == "return" || tok->str() == "continue" || tok->str() == "break" || mSettings->library.isnoreturn(tok)) { // Reset upon return, continue or break
for (std::map<int, Filepointer>::iterator i = filepointers.begin(); i != filepointers.end(); ++i) { for (std::pair<const int, Filepointer>& filepointer : filepointers) {
i->second.mode_indent = 0; filepointer.second.mode_indent = 0;
i->second.mode = UNKNOWN_OM; filepointer.second.mode = UNKNOWN_OM;
i->second.op_indent = 0; filepointer.second.op_indent = 0;
i->second.lastOperation = Filepointer::UNKNOWN_OP; filepointer.second.lastOperation = Filepointer::UNKNOWN_OP;
} }
} else if (Token::Match(tok, "%var% =") && } else if (Token::Match(tok, "%var% =") &&
(tok->strAt(2) != "fopen" && tok->strAt(2) != "freopen" && tok->strAt(2) != "tmpfile" && (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); 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 (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())) { if (!tok->function() || (tok->function()->nestedIn && tok->function()->nestedIn->isClassOrStruct())) {
for (std::map<int, Filepointer>::iterator i = filepointers.begin(); i != filepointers.end(); ++i) { for (std::pair<const int, Filepointer>& filepointer : filepointers) {
const Variable* var = symbolDatabase->getVariableFromVarId(i->first); const Variable* var = symbolDatabase->getVariableFromVarId(filepointer.first);
if (!var || !(var->isLocal() || var->isGlobal() || var->isStatic())) { if (!var || !(var->isLocal() || var->isGlobal() || var->isStatic())) {
i->second.mode = UNKNOWN_OM; filepointer.second.mode = UNKNOWN_OM;
i->second.mode_indent = 0; filepointer.second.mode_indent = 0;
i->second.op_indent = indent; filepointer.second.op_indent = indent;
i->second.lastOperation = Filepointer::UNKNOWN_OP; filepointer.second.lastOperation = Filepointer::UNKNOWN_OP;
} }
} }
continue; continue;
@ -326,10 +326,10 @@ void CheckIO::checkFileUsage()
} }
} }
} }
for (std::map<int, Filepointer>::iterator i = filepointers.begin(); i != filepointers.end(); ++i) { for (std::pair<const int, Filepointer>& filepointer : filepointers) {
i->second.op_indent = 0; filepointer.second.op_indent = 0;
i->second.mode = UNKNOWN_OM; filepointer.second.mode = UNKNOWN_OM;
i->second.lastOperation = Filepointer::UNKNOWN_OP; filepointer.second.lastOperation = Filepointer::UNKNOWN_OP;
} }
} }
} }

View File

@ -1979,12 +1979,11 @@ namespace {
for (const Function &func : scope.functionList) { for (const Function &func : scope.functionList) {
functionsByName[func.tokenDef->str()].push_back(&func); functionsByName[func.tokenDef->str()].push_back(&func);
} }
for (StringFunctionMap::iterator it = functionsByName.begin(); for (std::pair<const std::string, std::list<const Function*>>& it : functionsByName) {
it != functionsByName.end(); ++it) { const std::list<const Function*>::const_iterator nc = std::find_if(it.second.begin(), it.second.end(), notconst);
const std::list<const Function*>::const_iterator nc = std::find_if(it->second.begin(), it->second.end(), notconst); if (nc == it.second.end()) {
if (nc == it->second.end()) {
// ok to add all of them // ok to add all of them
constFunctions.splice(constFunctions.end(), it->second); constFunctions.splice(constFunctions.end(), it.second);
} }
} }
} }

View File

@ -245,8 +245,8 @@ void Variables::readAliases(unsigned int varid, const Token* tok)
VariableUsage *usage = find(varid); VariableUsage *usage = find(varid);
if (usage) { if (usage) {
for (std::set<unsigned int>::iterator aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) { for (unsigned int aliases : usage->_aliases) {
VariableUsage *aliased = find(*aliases); VariableUsage *aliased = find(aliases);
if (aliased) { if (aliased) {
aliased->_read = true; aliased->_read = true;

View File

@ -202,14 +202,14 @@ static std::string executeAddon(const AddonInfo &addonInfo,
pythonExe = cmdFileName(defaultPythonExe); pythonExe = cmdFileName(defaultPythonExe);
else { else {
#ifdef _WIN32 #ifdef _WIN32
const char *p[] = { "python3.exe", "python.exe" }; const char *py_exes[] = { "python3.exe", "python.exe" };
#else #else
const char *p[] = { "python3", "python" }; const char *py_exes[] = { "python3", "python" };
#endif #endif
for (int i = 0; i < 2; ++i) { for (const char* py_exe : py_exes) {
std::string out; std::string out;
if (executeCommand(p[i], split("--version"), redirect, &out) && out.compare(0, 7, "Python ") == 0 && std::isdigit(out[7])) { if (executeCommand(py_exe, split("--version"), redirect, &out) && out.compare(0, 7, "Python ") == 0 && std::isdigit(out[7])) {
pythonExe = p[i]; pythonExe = py_exe;
break; break;
} }
} }

View File

@ -29,8 +29,8 @@ PathMatch::PathMatch(const std::vector<std::string> &excludedPaths, bool caseSen
: mExcludedPaths(excludedPaths), mCaseSensitive(caseSensitive) : mExcludedPaths(excludedPaths), mCaseSensitive(caseSensitive)
{ {
if (!mCaseSensitive) if (!mCaseSensitive)
for (std::vector<std::string>::iterator i = mExcludedPaths.begin(); i != mExcludedPaths.end(); ++i) for (std::string& excludedPath : mExcludedPaths)
std::transform(i->begin(), i->end(), i->begin(), ::tolower); std::transform(excludedPath.begin(), excludedPath.end(), excludedPath.begin(), ::tolower);
mWorkingDirectory.push_back(Path::getCurrentPath()); mWorkingDirectory.push_back(Path::getCurrentPath());
} }

View File

@ -69,8 +69,8 @@ Preprocessor::Preprocessor(Settings& settings, ErrorLogger *errorLogger) : mSett
Preprocessor::~Preprocessor() Preprocessor::~Preprocessor()
{ {
for (std::map<std::string, simplecpp::TokenList *>::iterator it = mTokenLists.begin(); it != mTokenLists.end(); ++it) for (std::pair<const std::string, simplecpp::TokenList *>& tokenList : mTokenLists)
delete it->second; delete tokenList.second;
} }
namespace { namespace {
@ -657,9 +657,9 @@ bool Preprocessor::loadFiles(const simplecpp::TokenList &rawtokens, std::vector<
void Preprocessor::removeComments() void Preprocessor::removeComments()
{ {
for (std::map<std::string, simplecpp::TokenList*>::iterator it = mTokenLists.begin(); it != mTokenLists.end(); ++it) { for (std::pair<const std::string, simplecpp::TokenList*>& tokenList : mTokenLists) {
if (it->second) if (tokenList.second)
it->second->removeComments(); tokenList.second->removeComments();
} }
} }
@ -982,8 +982,8 @@ unsigned int Preprocessor::calculateChecksum(const simplecpp::TokenList &tokens1
void Preprocessor::simplifyPragmaAsm(simplecpp::TokenList *tokenList) void Preprocessor::simplifyPragmaAsm(simplecpp::TokenList *tokenList)
{ {
Preprocessor::simplifyPragmaAsmPrivate(tokenList); Preprocessor::simplifyPragmaAsmPrivate(tokenList);
for (std::map<std::string, simplecpp::TokenList *>::iterator it = mTokenLists.begin(); it != mTokenLists.end(); ++it) { for (std::pair<const std::string, simplecpp::TokenList *>& list : mTokenLists) {
Preprocessor::simplifyPragmaAsmPrivate(it->second); Preprocessor::simplifyPragmaAsmPrivate(list.second);
} }
} }

View File

@ -712,15 +712,15 @@ void SymbolDatabase::createSymbolDatabaseClassInfo()
return; return;
// fill in using info // fill in using info
for (std::list<Scope>::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { for (Scope& scope : scopeList) {
for (std::list<Scope::UsingInfo>::iterator i = it->usingList.begin(); i != it->usingList.end(); ++i) { for (Scope::UsingInfo& usingInfo : scope.usingList) {
// only find if not already found // only find if not already found
if (i->scope == nullptr) { if (usingInfo.scope == nullptr) {
// check scope for match // check scope for match
const Scope * const scope = findScope(i->start->tokAt(2), &(*it)); const Scope * const found = findScope(usingInfo.start->tokAt(2), &scope);
if (scope) { if (found) {
// set found scope // set found scope
i->scope = scope; usingInfo.scope = found;
break; break;
} }
} }
@ -728,11 +728,11 @@ void SymbolDatabase::createSymbolDatabaseClassInfo()
} }
// fill in base class info // fill in base class info
for (std::list<Type>::iterator it = typeList.begin(); it != typeList.end(); ++it) { for (Type& type : typeList) {
// finish filling in base class info // finish filling in base class info
for (Type::BaseInfo & i : it->derivedFrom) { for (Type::BaseInfo & i : type.derivedFrom) {
const Type* found = findType(i.nameTok, it->enclosingScope); const Type* found = findType(i.nameTok, type.enclosingScope);
if (found && found->findDependency(&(*it))) { if (found && found->findDependency(&type)) {
// circular dependency // circular dependency
//mTokenizer->syntaxError(nullptr); //mTokenizer->syntaxError(nullptr);
} else { } else {
@ -742,9 +742,9 @@ void SymbolDatabase::createSymbolDatabaseClassInfo()
} }
// fill in friend info // fill in friend info
for (std::list<Type>::iterator it = typeList.begin(); it != typeList.end(); ++it) { for (Type & type : typeList) {
for (Type::FriendInfo &friendInfo : it->friendList) { for (Type::FriendInfo &friendInfo : type.friendList) {
friendInfo.type = findType(friendInfo.nameStart, it->enclosingScope); friendInfo.type = findType(friendInfo.nameStart, type.enclosingScope);
} }
} }
} }
@ -753,18 +753,18 @@ void SymbolDatabase::createSymbolDatabaseClassInfo()
void SymbolDatabase::createSymbolDatabaseVariableInfo() void SymbolDatabase::createSymbolDatabaseVariableInfo()
{ {
// fill in variable info // fill in variable info
for (std::list<Scope>::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { for (Scope& scope : scopeList) {
// find variables // find variables
it->getVariableList(mSettings); scope.getVariableList(mSettings);
} }
// fill in function arguments // fill in function arguments
for (std::list<Scope>::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { for (Scope& scope : scopeList) {
std::list<Function>::iterator func; std::list<Function>::iterator func;
for (func = it->functionList.begin(); func != it->functionList.end(); ++func) { for (func = scope.functionList.begin(); func != scope.functionList.end(); ++func) {
// add arguments // add arguments
func->addArguments(this, &*it); func->addArguments(this, &scope);
} }
} }
} }
@ -772,17 +772,17 @@ void SymbolDatabase::createSymbolDatabaseVariableInfo()
void SymbolDatabase::createSymbolDatabaseCopyAndMoveConstructors() void SymbolDatabase::createSymbolDatabaseCopyAndMoveConstructors()
{ {
// fill in class and struct copy/move constructors // fill in class and struct copy/move constructors
for (std::list<Scope>::iterator scope = scopeList.begin(); scope != scopeList.end(); ++scope) { for (Scope& scope : scopeList) {
if (!scope->isClassOrStruct()) if (!scope.isClassOrStruct())
continue; continue;
std::list<Function>::iterator func; std::list<Function>::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) if (!func->isConstructor() || func->minArgCount() != 1)
continue; continue;
const Variable* firstArg = func->getArgumentVar(0); const Variable* firstArg = func->getArgumentVar(0);
if (firstArg->type() == scope->definedType) { if (firstArg->type() == scope.definedType) {
if (firstArg->isRValueReference()) if (firstArg->isRValueReference())
func->type = Function::eMoveConstructor; func->type = Function::eMoveConstructor;
else if (firstArg->isReference() && !firstArg->isPointer()) else if (firstArg->isReference() && !firstArg->isPointer())
@ -791,7 +791,7 @@ void SymbolDatabase::createSymbolDatabaseCopyAndMoveConstructors()
if (func->type == Function::eCopyConstructor || if (func->type == Function::eCopyConstructor ||
func->type == Function::eMoveConstructor) func->type == Function::eMoveConstructor)
scope->numCopyOrMoveConstructors++; scope.numCopyOrMoveConstructors++;
} }
} }
} }
@ -799,35 +799,35 @@ void SymbolDatabase::createSymbolDatabaseCopyAndMoveConstructors()
void SymbolDatabase::createSymbolDatabaseFunctionScopes() void SymbolDatabase::createSymbolDatabaseFunctionScopes()
{ {
// fill in function scopes // fill in function scopes
for (std::list<Scope>::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { for (Scope & scope : scopeList) {
if (it->type == Scope::eFunction) if (scope.type == Scope::eFunction)
functionScopes.push_back(&*it); functionScopes.push_back(&scope);
} }
} }
void SymbolDatabase::createSymbolDatabaseClassAndStructScopes() void SymbolDatabase::createSymbolDatabaseClassAndStructScopes()
{ {
// fill in class and struct scopes // fill in class and struct scopes
for (std::list<Scope>::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { for (Scope& scope : scopeList) {
if (it->isClassOrStruct()) if (scope.isClassOrStruct())
classAndStructScopes.push_back(&*it); classAndStructScopes.push_back(&scope);
} }
} }
void SymbolDatabase::createSymbolDatabaseFunctionReturnTypes() void SymbolDatabase::createSymbolDatabaseFunctionReturnTypes()
{ {
// fill in function return types // fill in function return types
for (std::list<Scope>::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { for (Scope& scope : scopeList) {
std::list<Function>::iterator func; std::list<Function>::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 // add return types
if (func->retDef) { if (func->retDef) {
const Token *type = func->retDef; const Token *type = func->retDef;
while (Token::Match(type, "static|const|struct|union|enum")) while (Token::Match(type, "static|const|struct|union|enum"))
type = type->next(); type = type->next();
if (type) { if (type) {
func->retType = findVariableTypeInBase(&*it, type); func->retType = findVariableTypeInBase(&scope, type);
if (!func->retType) if (!func->retType)
func->retType = findTypeInNested(type, func->nestedIn); func->retType = findTypeInNested(type, func->nestedIn);
} }
@ -840,10 +840,9 @@ void SymbolDatabase::createSymbolDatabaseNeedInitialization()
{ {
if (mTokenizer->isC()) { if (mTokenizer->isC()) {
// For C code it is easy, as there are no constructors and no default values // For C code it is easy, as there are no constructors and no default values
for (std::list<Scope>::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { for (Scope& scope : scopeList) {
Scope *scope = &(*it); if (scope.definedType)
if (scope->definedType) scope.definedType->needInitialization = Type::NeedInitialization::True;
scope->definedType->needInitialization = Type::NeedInitialization::True;
} }
} else { } else {
// For C++, it is more difficult: Determine if user defined type needs initialization... // For C++, it is more difficult: Determine if user defined type needs initialization...
@ -853,21 +852,19 @@ void SymbolDatabase::createSymbolDatabaseNeedInitialization()
do { do {
unknowns = 0; unknowns = 0;
for (std::list<Scope>::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { for (Scope& scope : scopeList) {
Scope *scope = &(*it); if (!scope.definedType) {
if (!scope->definedType) {
mBlankTypes.emplace_back(); 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 // check for default constructor
bool hasDefaultConstructor = false; bool hasDefaultConstructor = false;
std::list<Function>::const_iterator func; std::list<Function>::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) { if (func->type == Function::eConstructor) {
// check for no arguments: func ( ) // check for no arguments: func ( )
if (func->argCount() == 0) { if (func->argCount() == 0) {
@ -887,7 +884,7 @@ void SymbolDatabase::createSymbolDatabaseNeedInitialization()
// We assume the default constructor initializes everything. // We assume the default constructor initializes everything.
// Another check will figure out if the constructor actually initializes everything. // Another check will figure out if the constructor actually initializes everything.
if (hasDefaultConstructor) if (hasDefaultConstructor)
scope->definedType->needInitialization = Type::NeedInitialization::False; scope.definedType->needInitialization = Type::NeedInitialization::False;
// check each member variable to see if it needs initialization // check each member variable to see if it needs initialization
else { else {
@ -895,7 +892,7 @@ void SymbolDatabase::createSymbolDatabaseNeedInitialization()
bool unknown = false; bool unknown = false;
std::list<Variable>::const_iterator var; std::list<Variable>::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->isClass()) {
if (var->type()) { if (var->type()) {
// does this type need initialization? // does this type need initialization?
@ -911,16 +908,16 @@ void SymbolDatabase::createSymbolDatabaseNeedInitialization()
} }
if (needInitialization) if (needInitialization)
scope->definedType->needInitialization = Type::NeedInitialization::True; scope.definedType->needInitialization = Type::NeedInitialization::True;
else if (!unknown) else if (!unknown)
scope->definedType->needInitialization = Type::NeedInitialization::False; scope.definedType->needInitialization = Type::NeedInitialization::False;
else { else {
if (scope->definedType->needInitialization == Type::NeedInitialization::Unknown) if (scope.definedType->needInitialization == Type::NeedInitialization::Unknown)
unknowns++; unknowns++;
} }
} }
} else if (scope->type == Scope::eUnion && scope->definedType->needInitialization == Type::NeedInitialization::Unknown) } else if (scope.type == Scope::eUnion && scope.definedType->needInitialization == Type::NeedInitialization::Unknown)
scope->definedType->needInitialization = Type::NeedInitialization::True; scope.definedType->needInitialization = Type::NeedInitialization::True;
} }
retry++; retry++;
@ -928,11 +925,9 @@ void SymbolDatabase::createSymbolDatabaseNeedInitialization()
// this shouldn't happen so output a debug warning // this shouldn't happen so output a debug warning
if (retry == 100 && mSettings->debugwarnings) { if (retry == 100 && mSettings->debugwarnings) {
for (std::list<Scope>::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { for (const Scope& scope : scopeList) {
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.");
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); std::fill_n(mVariableList.begin(), mVariableList.size(), (const Variable*)nullptr);
// check all scopes for variables // check all scopes for variables
for (std::list<Scope>::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { for (Scope& scope : scopeList) {
Scope *scope = &(*it);
// add all variables // add all variables
for (std::list<Variable>::iterator var = scope->varlist.begin(); var != scope->varlist.end(); ++var) { for (std::list<Variable>::iterator var = scope.varlist.begin(); var != scope.varlist.end(); ++var) {
const unsigned int varId = var->declarationId(); const unsigned int varId = var->declarationId();
if (varId) if (varId)
mVariableList[varId] = &(*var); mVariableList[varId] = &(*var);
// fix up variables without type // fix up variables without type
if (!var->type() && !var->typeStartToken()->isStandardType()) { if (!var->type() && !var->typeStartToken()->isStandardType()) {
const Type *type = findType(var->typeStartToken(), scope); const Type *type = findType(var->typeStartToken(), &scope);
if (type) if (type)
var->type(type); var->type(type);
} }
} }
// add all function parameters // add all function parameters
for (std::list<Function>::iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { for (Function& func : scope.functionList) {
for (std::list<Variable>::iterator arg = func->argumentList.begin(); arg != func->argumentList.end(); ++arg) { for (std::list<Variable>::iterator arg = func.argumentList.begin(); arg != func.argumentList.end(); ++arg) {
// check for named parameters // check for named parameters
if (arg->nameToken() && arg->declarationId()) { if (arg->nameToken() && arg->declarationId()) {
const unsigned int declarationId = arg->declarationId(); const unsigned int declarationId = arg->declarationId();
@ -971,7 +964,7 @@ void SymbolDatabase::createSymbolDatabaseVariableSymbolTable()
mVariableList[declarationId] = &(*arg); mVariableList[declarationId] = &(*arg);
// fix up parameters without type // fix up parameters without type
if (!arg->type() && !arg->typeStartToken()->isStandardType()) { if (!arg->type() && !arg->typeStartToken()->isStandardType()) {
const Type *type = findTypeInNested(arg->typeStartToken(), scope); const Type *type = findTypeInNested(arg->typeStartToken(), &scope);
if (type) if (type)
arg->type(type); arg->type(type);
} }

View File

@ -6392,13 +6392,13 @@ void Tokenizer::simplifyFunctionParameters()
if (argumentNames.size() != argumentNames2.size()) { if (argumentNames.size() != argumentNames2.size()) {
//move back 'tok1' to the last ';' //move back 'tok1' to the last ';'
tok1 = tok1->previous(); tok1 = tok1->previous();
for (std::map<std::string, Token *>::iterator it = argumentNames.begin(); it != argumentNames.end(); ++it) { for (std::pair<const std::string, Token *>& argumentName : argumentNames) {
if (argumentNames2.find(it->first) == argumentNames2.end()) { if (argumentNames2.find(argumentName.first) == argumentNames2.end()) {
//add the missing parameter argument declaration //add the missing parameter argument declaration
tok1->insertToken(";"); tok1->insertToken(";");
tok1->insertToken(it->first); tok1->insertToken(argumentName.first);
//register the change inside argumentNames2 //register the change inside argumentNames2
argumentNames2[it->first] = tok1->next(); argumentNames2[argumentName.first] = tok1->next();
tok1->insertToken("int"); tok1->insertToken("int");
} }
} }

View File

@ -4100,9 +4100,9 @@ static void valueFlowForwardAssign(Token * const tok,
if (!var->isPointer() && !var->isSmartPointer()) if (!var->isPointer() && !var->isSmartPointer())
values.remove_if(std::mem_fn(&ValueFlow::Value::isTokValue)); values.remove_if(std::mem_fn(&ValueFlow::Value::isTokValue));
if (tok->astParent()) { if (tok->astParent()) {
for (std::list<ValueFlow::Value>::iterator it = values.begin(); it != values.end(); ++it) { for (ValueFlow::Value& value : values) {
const std::string info = "Assignment '" + tok->astParent()->expressionString() + "', assigned value is " + it->infoString(); const std::string info = "Assignment '" + tok->astParent()->expressionString() + "', assigned value is " + value.infoString();
it->errorPath.emplace_back(tok, info); value.errorPath.emplace_back(tok, info);
} }
} }

View File

@ -138,10 +138,10 @@ private:
replace(line, "(fp.gt ", "(> "); replace(line, "(fp.gt ", "(> ");
replace(line, "(fp.lt ", "(< "); replace(line, "(fp.lt ", "(< ");
int par = 0; int par = 0;
for (int pos = 0; pos < line.size(); ++pos) { for (char pos : line) {
if (line[pos] == '(') if (pos == '(')
par++; par++;
else if (line[pos] == ')') else if (pos == ')')
--par; --par;
} }
if (par < 0) if (par < 0)