Symbol database: refactoring - differentiate between member data initialization and assignment and save the order of variable declarations. makes it possible to create some additional checks. ticket: #1895

This commit is contained in:
Robert Reif 2010-09-09 07:26:40 +02:00 committed by Daniel Marjamäki
parent c3762903a9
commit 6d35396720
2 changed files with 76 additions and 27 deletions

View File

@ -748,13 +748,27 @@ void CheckClass::SpaceInfo::getVarList()
check->reportError(vartok, Severity::debug, "debug", "CheckClass::SpaceInfo::getVarList found variable \'" + vartok->str() + "\' with varid 0."); 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<Var>::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) void CheckClass::SpaceInfo::initVar(const std::string &varname)
{ {
std::list<Var>::iterator i; std::list<Var>::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<Var>::iterator i; std::list<Var>::iterator i;
for (i = varlist.begin(); i != varlist.end(); ++i) for (i = varlist.begin(); i != varlist.end(); ++i)
i->init = value; i->assign = true;
}
void CheckClass::SpaceInfo::clearAllVar()
{
std::list<Var>::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::list<std::s
// assignment in the initializer.. // assignment in the initializer..
// : var(value = x) // : var(value = x)
if (Token::Match(ftok->tokAt(2), "%var% =")) if (Token::Match(ftok->tokAt(2), "%var% ="))
initVar(ftok->strAt(2)); assignVar(ftok->strAt(2));
} }
Assign |= (ftok->str() == ":"); Assign |= (ftok->str() == ":");
@ -827,7 +852,7 @@ void CheckClass::SpaceInfo::initializeVarList(const Func &func, std::list<std::s
// Variable getting value from stream? // Variable getting value from stream?
if (Token::Match(ftok, ">> %var%")) if (Token::Match(ftok, ">> %var%"))
{ {
initVar(ftok->strAt(1)); assignVar(ftok->strAt(1));
} }
// Before a new statement there is "[{};)=]" // Before a new statement there is "[{};)=]"
@ -840,7 +865,7 @@ void CheckClass::SpaceInfo::initializeVarList(const Func &func, std::list<std::s
// Using the operator= function to initialize all variables.. // Using the operator= function to initialize all variables..
if (Token::simpleMatch(ftok->next(), "* this = ")) if (Token::simpleMatch(ftok->next(), "* this = "))
{ {
markAllVar(true); assignAllVar();
break; break;
} }
@ -873,14 +898,14 @@ void CheckClass::SpaceInfo::initializeVarList(const Func &func, std::list<std::s
// Clearing all variables.. // Clearing all variables..
if (Token::simpleMatch(ftok, "memset ( this ,")) if (Token::simpleMatch(ftok, "memset ( this ,"))
{ {
markAllVar(true); assignAllVar();
return; return;
} }
// Clearing array.. // Clearing array..
else if (Token::Match(ftok, "memset ( %var% ,")) else if (Token::Match(ftok, "memset ( %var% ,"))
{ {
initVar(ftok->strAt(2)); assignVar(ftok->strAt(2));
ftok = ftok->next()->link(); ftok = ftok->next()->link();
continue; continue;
} }
@ -893,7 +918,7 @@ void CheckClass::SpaceInfo::initializeVarList(const Func &func, std::list<std::s
{ {
if (tok2->str() == "this") if (tok2->str() == "this")
{ {
markAllVar(true); assignAllVar();
return; return;
} }
} }
@ -902,7 +927,7 @@ void CheckClass::SpaceInfo::initializeVarList(const Func &func, std::list<std::s
// assume that all variables are initialized // assume that all variables are initialized
if (std::find(callstack.begin(), callstack.end(), ftok->str()) != callstack.end()) if (std::find(callstack.begin(), callstack.end(), ftok->str()) != callstack.end())
{ {
markAllVar(true); assignAllVar();
return; return;
} }
@ -929,7 +954,7 @@ void CheckClass::SpaceInfo::initializeVarList(const Func &func, std::list<std::s
// there is a called member function, but it has no implementation, so we assume it initializes everything // there is a called member function, but it has no implementation, so we assume it initializes everything
else else
{ {
markAllVar(true); assignAllVar();
} }
} }
@ -938,11 +963,11 @@ void CheckClass::SpaceInfo::initializeVarList(const Func &func, std::list<std::s
{ {
// could be a base class virtual function, so we assume it initializes everything // could be a base class virtual function, so we assume it initializes everything
if (!derivedFrom.empty()) if (!derivedFrom.empty())
markAllVar(true); assignAllVar();
// has friends, so we assume it initializes everything // has friends, so we assume it initializes everything
if (!friendList.empty()) if (!friendList.empty())
markAllVar(true); assignAllVar();
// the function is external and it's neither friend nor inherited virtual function. // the function is external and it's neither friend nor inherited virtual function.
// assume all variables that are passed to it are initialized.. // assume all variables that are passed to it are initialized..
@ -961,7 +986,7 @@ void CheckClass::SpaceInfo::initializeVarList(const Func &func, std::list<std::s
} }
if (tok->isName()) if (tok->isName())
{ {
initVar(tok->strAt(0)); assignVar(tok->strAt(0));
} }
} }
} }
@ -971,37 +996,37 @@ void CheckClass::SpaceInfo::initializeVarList(const Func &func, std::list<std::s
// Assignment of member variable? // Assignment of member variable?
else if (Token::Match(ftok, "%var% =")) else if (Token::Match(ftok, "%var% ="))
{ {
initVar(ftok->strAt(0)); assignVar(ftok->strAt(0));
} }
// Assignment of array item of member variable? // Assignment of array item of member variable?
else if (Token::Match(ftok, "%var% [ %any% ] =")) else if (Token::Match(ftok, "%var% [ %any% ] ="))
{ {
initVar(ftok->strAt(0)); assignVar(ftok->strAt(0));
} }
// Assignment of array item of member variable? // Assignment of array item of member variable?
else if (Token::Match(ftok, "%var% [ %any% ] [ %any% ] =")) else if (Token::Match(ftok, "%var% [ %any% ] [ %any% ] ="))
{ {
initVar(ftok->strAt(0)); assignVar(ftok->strAt(0));
} }
// Assignment of array item of member variable? // Assignment of array item of member variable?
else if (Token::Match(ftok, "* %var% =")) else if (Token::Match(ftok, "* %var% ="))
{ {
initVar(ftok->strAt(1)); assignVar(ftok->strAt(1));
} }
// Assignment of struct member of member variable? // Assignment of struct member of member variable?
else if (Token::Match(ftok, "%var% . %any% =")) else if (Token::Match(ftok, "%var% . %any% ="))
{ {
initVar(ftok->strAt(0)); assignVar(ftok->strAt(0));
} }
// The functions 'clear' and 'Clear' are supposed to initialize variable. // The functions 'clear' and 'Clear' are supposed to initialize variable.
if (Token::Match(ftok, "%var% . clear|Clear (")) if (Token::Match(ftok, "%var% . clear|Clear ("))
{ {
initVar(ftok->strAt(0)); assignVar(ftok->strAt(0));
} }
} }
} }
@ -1132,7 +1157,7 @@ void CheckClass::constructors()
continue; continue;
// Mark all variables not used // Mark all variables not used
info->markAllVar(false); info->clearAllVar();
std::list<std::string> callstack; std::list<std::string> callstack;
info->initializeVarList(*it, callstack); info->initializeVarList(*it, callstack);
@ -1145,7 +1170,7 @@ void CheckClass::constructors()
if (var->isClass && it->type == Func::Constructor) if (var->isClass && it->type == Func::Constructor)
continue; continue;
if (var->init || var->isStatic) if (var->assign || var->init || var->isStatic)
continue; continue;
// It's non-static and it's not initialized => error // It's non-static and it's not initialized => error

View File

@ -132,9 +132,11 @@ private:
class Var class Var
{ {
public: 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_), : token(token_),
init(init_), index(index_),
assign(false),
init(false),
access(access_), access(access_),
isMutable(mutable_), isMutable(mutable_),
isStatic(static_), isStatic(static_),
@ -145,6 +147,12 @@ private:
/** @brief variable token */ /** @brief variable token */
const Token *token; const Token *token;
/** @brief order declared */
unsigned int index;
/** @brief has this variable been assigned? */
bool assign;
/** @brief has this variable been initialized? */ /** @brief has this variable been initialized? */
bool init; bool init;
@ -239,17 +247,33 @@ private:
AccessControl access; AccessControl access;
unsigned int numConstructors; 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 * @brief initialize a variable in the varlist
* @param varname name of variable to mark initialized * @param varname name of variable to mark initialized
*/ */
void initVar(const std::string &varname); 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 * @brief set all variables in list assigned
* @param value state to mark all variables
*/ */
void markAllVar(bool value); void assignAllVar();
/**
* @brief set all variables in list not assigned and not initialized
*/
void clearAllVar();
/** @brief initialize varlist */ /** @brief initialize varlist */
void getVarList(); void getVarList();