From bd9e6212b26199397e8766995962ec17fad1d34b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 1 Feb 2021 17:13:58 +0100 Subject: [PATCH] Refactoring, avoid 'magic' connection between vector items and variables --- lib/checkclass.cpp | 29 ++++++++++++++--------------- lib/checkclass.h | 18 ++++++++++-------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 55effa06b..9f5e44ddd 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -137,7 +137,9 @@ void CheckClass::constructors() } - std::vector usage(scope->varlist.size()); + std::vector usageList; + for (const Variable &var : scope->varlist) + usageList.push_back(Usage(&var)); for (const Function &func : scope->functionList) { if (!func.hasBody() || !(func.isConstructor() || func.type == Function::eOperatorEqual)) @@ -151,23 +153,22 @@ void CheckClass::constructors() } // Mark all variables not used - clearAllVar(usage); + clearAllVar(usageList); std::list callstack; - initializeVarList(func, callstack, scope, usage); + initializeVarList(func, callstack, scope, usageList); // Check if any variables are uninitialized - int count = -1; - for (const Variable &var : scope->varlist) { - ++count; + for (Usage &usage : usageList) { + const Variable& var = *usage.var; // check for C++11 initializer if (var.hasDefault()) { - usage[count].init = true; + usage.init = true; continue; } - if (usage[count].assign || usage[count].init || var.isStatic()) + if (usage.assign || usage.init || var.isStatic()) continue; if (var.valueType() && var.valueType()->pointer == 0 && var.type() && var.type()->needInitialization == Type::NeedInitialization::False && var.type()->derivedFrom.empty()) @@ -546,13 +547,11 @@ void CheckClass::assignVar(nonneg int varid, const Scope *scope, std::vector &usage) +void CheckClass::initVar(std::vector &usageList, nonneg int varid) { - int count = 0; - - for (std::list::const_iterator var = scope->varlist.begin(); var != scope->varlist.end(); ++var, ++count) { - if (var->declarationId() == varid) { - usage[count].init = true; + for (Usage& usage: usageList) { + if (usage.var->declarationId() == varid) { + usage.init = true; return; } } @@ -609,7 +608,7 @@ void CheckClass::initializeVarList(const Function &func, std::listlinkAt(1), "}|) ,|{")) { if (ftok->str() != func.name()) { - initVar(ftok->varId(), scope, usage); + initVar(usage, ftok->varId()); } else { // c++11 delegate constructor const Function *member = ftok->function(); // member function not found => assume it initializes all members diff --git a/lib/checkclass.h b/lib/checkclass.h index 9649a309e..8f0c8e688 100644 --- a/lib/checkclass.h +++ b/lib/checkclass.h @@ -272,7 +272,10 @@ private: // constructors helper function /** @brief Information about a member variable. Used when checking for uninitialized variables */ struct Usage { - Usage() : assign(false), init(false) { } + Usage(const Variable *var) : var(var), assign(false), init(false) { } + + /** Variable that this usage is for */ + const Variable *var; /** @brief has this variable been assigned? */ bool assign; @@ -293,23 +296,22 @@ private: /** * @brief initialize a variable in the varlist + * @param usageList reference to usage vector * @param varid id of variable to mark initialized - * @param scope pointer to variable Scope - * @param usage reference to usage vector */ - static void initVar(nonneg int varid, const Scope *scope, std::vector &usage); + static void initVar(std::vector &usageList, nonneg int varid); /** * @brief set all variables in list assigned - * @param usage reference to usage vector + * @param usageList reference to usage vector */ - static void assignAllVar(std::vector &usage); + static void assignAllVar(std::vector &usageList); /** * @brief set all variables in list not assigned and not initialized - * @param usage reference to usage vector + * @param usageList reference to usage vector */ - static void clearAllVar(std::vector &usage); + static void clearAllVar(std::vector &usageList); /** * @brief parse a scope for a constructor or member function and set the "init" flags in the provided varlist