diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 0bbfe27b9..56547951e 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -748,13 +748,27 @@ void CheckClass::SpaceInfo::getVarList() check->reportError(vartok, Severity::debug, "debug", "CheckClass::SpaceInfo::getVarList found variable \'" + vartok->str() + "\' with varid 0."); } - varlist.push_back(Var(vartok, false, varaccess, isMutable, isStatic, isClass)); + addVar(vartok, varaccess, isMutable, isStatic, isClass); } } } //--------------------------------------------------------------------------- +void CheckClass::SpaceInfo::assignVar(const std::string &varname) +{ + std::list::iterator i; + + for (i = varlist.begin(); i != varlist.end(); ++i) + { + if (i->token->str() == varname) + { + i->assign = true; + return; + } + } +} + void CheckClass::SpaceInfo::initVar(const std::string &varname) { std::list::iterator i; @@ -769,12 +783,23 @@ void CheckClass::SpaceInfo::initVar(const std::string &varname) } } -void CheckClass::SpaceInfo::markAllVar(bool value) +void CheckClass::SpaceInfo::assignAllVar() { std::list::iterator i; for (i = varlist.begin(); i != varlist.end(); ++i) - i->init = value; + i->assign = true; +} + +void CheckClass::SpaceInfo::clearAllVar() +{ + std::list::iterator i; + + for (i = varlist.begin(); i != varlist.end(); ++i) + { + i->assign = false; + i->init = false; + } } //--------------------------------------------------------------------------- @@ -801,7 +826,7 @@ void CheckClass::SpaceInfo::initializeVarList(const Func &func, std::listtokAt(2), "%var% =")) - initVar(ftok->strAt(2)); + assignVar(ftok->strAt(2)); } Assign |= (ftok->str() == ":"); @@ -827,7 +852,7 @@ void CheckClass::SpaceInfo::initializeVarList(const Func &func, std::list> %var%")) { - initVar(ftok->strAt(1)); + assignVar(ftok->strAt(1)); } // Before a new statement there is "[{};)=]" @@ -840,7 +865,7 @@ void CheckClass::SpaceInfo::initializeVarList(const Func &func, std::listnext(), "* this = ")) { - markAllVar(true); + assignAllVar(); break; } @@ -873,14 +898,14 @@ void CheckClass::SpaceInfo::initializeVarList(const Func &func, std::liststrAt(2)); + assignVar(ftok->strAt(2)); ftok = ftok->next()->link(); continue; } @@ -893,7 +918,7 @@ void CheckClass::SpaceInfo::initializeVarList(const Func &func, std::liststr() == "this") { - markAllVar(true); + assignAllVar(); return; } } @@ -902,7 +927,7 @@ void CheckClass::SpaceInfo::initializeVarList(const Func &func, std::liststr()) != callstack.end()) { - markAllVar(true); + assignAllVar(); return; } @@ -929,7 +954,7 @@ void CheckClass::SpaceInfo::initializeVarList(const Func &func, std::listisName()) { - initVar(tok->strAt(0)); + assignVar(tok->strAt(0)); } } } @@ -971,37 +996,37 @@ void CheckClass::SpaceInfo::initializeVarList(const Func &func, std::liststrAt(0)); + assignVar(ftok->strAt(0)); } } } @@ -1132,7 +1157,7 @@ void CheckClass::constructors() continue; // Mark all variables not used - info->markAllVar(false); + info->clearAllVar(); std::list callstack; info->initializeVarList(*it, callstack); @@ -1145,7 +1170,7 @@ void CheckClass::constructors() if (var->isClass && it->type == Func::Constructor) continue; - if (var->init || var->isStatic) + if (var->assign || var->init || var->isStatic) continue; // It's non-static and it's not initialized => error diff --git a/lib/checkclass.h b/lib/checkclass.h index fe54601ae..135e2de51 100644 --- a/lib/checkclass.h +++ b/lib/checkclass.h @@ -132,9 +132,11 @@ private: class Var { public: - Var(const Token *token_, bool init_ = false, AccessControl access_ = Public, bool mutable_ = false, bool static_ = false, bool class_ = false) + Var(const Token *token_, unsigned int index_, AccessControl access_ = Public, bool mutable_ = false, bool static_ = false, bool class_ = false) : token(token_), - init(init_), + index(index_), + assign(false), + init(false), access(access_), isMutable(mutable_), isStatic(static_), @@ -145,6 +147,12 @@ private: /** @brief variable token */ const Token *token; + /** @brief order declared */ + unsigned int index; + + /** @brief has this variable been assigned? */ + bool assign; + /** @brief has this variable been initialized? */ bool init; @@ -239,17 +247,33 @@ private: AccessControl access; unsigned int numConstructors; + /** + * @brief assign a variable in the varlist + * @param varname name of variable to mark assigned + */ + void assignVar(const std::string &varname); + /** * @brief initialize a variable in the varlist * @param varname name of variable to mark initialized */ void initVar(const std::string &varname); + void addVar(const Token *token_, AccessControl access_, bool mutable_, bool static_, bool class_) + { + varlist.push_back(Var(token_, varlist.size(), access_, mutable_, static_, class_)); + + } + /** - * @brief mark all variables in list - * @param value state to mark all variables + * @brief set all variables in list assigned */ - void markAllVar(bool value); + void assignAllVar(); + + /** + * @brief set all variables in list not assigned and not initialized + */ + void clearAllVar(); /** @brief initialize varlist */ void getVarList();