Fixed #1875 (CheckClass::getVarList: simplify function use)

This commit is contained in:
Robert Reif 2010-07-17 12:25:14 +02:00 committed by Daniel Marjamäki
parent db7c9848d0
commit ad9d1375aa
2 changed files with 33 additions and 54 deletions

View File

@ -41,11 +41,12 @@ CheckClass instance;
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
CheckClass::Var *CheckClass::getVarList(const Token *tok1, bool withClasses, bool isStruct) CheckClass::Var *CheckClass::getVarList(const Token *tok1)
{ {
// Get variable list.. // Get variable list..
Var *varlist = NULL; Var *varlist = NULL;
unsigned int indentlevel = 0; unsigned int indentlevel = 0;
bool isStruct = tok1->str() == "struct";
bool priv = !isStruct; bool priv = !isStruct;
for (const Token *tok = tok1; tok; tok = tok->next()) for (const Token *tok = tok1; tok; tok = tok->next())
{ {
@ -131,14 +132,13 @@ CheckClass::Var *CheckClass::getVarList(const Token *tok1, bool withClasses, boo
} }
// Is it a variable declaration? // Is it a variable declaration?
bool isClass = false;
if (Token::Match(next, "%type% %var% ;|:")) if (Token::Match(next, "%type% %var% ;|:"))
{ {
if (withClasses) if (!next->isStandardType())
varname = next->strAt(1); isClass = true;
else if (next->isStandardType())
varname = next->strAt(1); varname = next->strAt(1);
else if (Token::findmatch(_tokenizer->tokens(), ("enum " + next->str()).c_str()))
varname = next->strAt(1);
} }
// Structure? // Structure?
@ -158,10 +158,9 @@ CheckClass::Var *CheckClass::getVarList(const Token *tok1, bool withClasses, boo
// Array? // Array?
else if (Token::Match(next, "%type% %var% [") && next->next()->str() != "operator") else if (Token::Match(next, "%type% %var% [") && next->next()->str() != "operator")
{ {
if (!withClasses && !next->isStandardType()) if (!next->isStandardType())
{ isClass = true;
continue;
}
varname = next->strAt(1); varname = next->strAt(1);
} }
@ -172,15 +171,17 @@ CheckClass::Var *CheckClass::getVarList(const Token *tok1, bool withClasses, boo
varname = next->strAt(4); varname = next->strAt(4);
// std::string.. // std::string..
else if (withClasses && Token::Match(next, "%type% :: %type% %var% ;")) else if (Token::Match(next, "%type% :: %type% %var% ;"))
{ {
isClass = true;
varname = next->strAt(3); varname = next->strAt(3);
} }
// Container.. // Container..
else if (withClasses && (Token::Match(next, "%type% :: %type% <") || else if (Token::Match(next, "%type% :: %type% <") ||
Token::Match(next, "%type% <"))) Token::Match(next, "%type% <"))
{ {
isClass = true;
// find matching ">" // find matching ">"
int level = 0; int level = 0;
for (; next; next = next->next()) for (; next; next = next->next())
@ -203,7 +204,7 @@ CheckClass::Var *CheckClass::getVarList(const Token *tok1, bool withClasses, boo
// If the varname was set in the if-blocks above, create a entry for this variable.. // If the varname was set in the if-blocks above, create a entry for this variable..
if (!varname.empty() && varname != "operator") if (!varname.empty() && varname != "operator")
{ {
Var *var = new Var(varname, false, priv, isMutable, isStatic, varlist); Var *var = new Var(varname, false, priv, isMutable, isStatic, isClass, varlist);
varlist = var; varlist = var;
} }
} }
@ -647,10 +648,7 @@ void CheckClass::constructors()
{ {
if (argsMatch(tok->tokAt(offset1), constructor_token->tokAt(offset2))) if (argsMatch(tok->tokAt(offset1), constructor_token->tokAt(offset2)))
{ {
if (operatorEqual || copyConstructor) constructorList.push_back(Constructor(constructor_token, access, true, operatorEqual, copyConstructor));
constructorList.push_back(Constructor(constructor_token, access, true, operatorEqual, copyConstructor));
else
constructorList.push_front(Constructor(constructor_token, access, true, false, false));
hasBody = true; hasBody = true;
break; break;
@ -669,10 +667,7 @@ void CheckClass::constructors()
// function body found? // function body found?
if (!hasBody) if (!hasBody)
{ {
if (operatorEqual || copyConstructor) constructorList.push_back(Constructor(tok, access, false, operatorEqual, copyConstructor));
constructorList.push_back(Constructor(tok, access, false, operatorEqual, copyConstructor));
else
constructorList.push_front(Constructor(tok, access, false, false, false));
} }
tok = next->next(); tok = next->next();
@ -684,10 +679,7 @@ void CheckClass::constructors()
// skip destructor and other classes // skip destructor and other classes
if (!Token::Match(tok->previous(), "~|::")) if (!Token::Match(tok->previous(), "~|::"))
{ {
if (operatorEqual || copyConstructor) constructorList.push_back(Constructor(tok, access, true, operatorEqual, copyConstructor));
constructorList.push_back(Constructor(tok, access, true, operatorEqual, copyConstructor));
else
constructorList.push_front(Constructor(tok, access, true, false, false));
} }
// skip over function body // skip over function body
@ -700,8 +692,8 @@ void CheckClass::constructors()
} }
} }
// Get variables that are not classes... // Get variables...
Var *varlist = getVarList(tok1, false, isStruct); Var *varlist = getVarList(tok1);
// There are no constructors. // There are no constructors.
if (numConstructors == 0) if (numConstructors == 0)
@ -709,7 +701,7 @@ void CheckClass::constructors()
// If there is a private variable, there should be a constructor.. // If there is a private variable, there should be a constructor..
for (const Var *var = varlist; var; var = var->next) for (const Var *var = varlist; var; var = var->next)
{ {
if (var->priv && !var->isStatic) if (var->priv && !var->isClass && !var->isStatic)
{ {
noConstructorError(tok1, className, isStruct); noConstructorError(tok1, className, isStruct);
break; break;
@ -718,28 +710,9 @@ void CheckClass::constructors()
} }
std::list<Constructor>::const_iterator it; std::list<Constructor>::const_iterator it;
bool hasClasses = false;
for (it = constructorList.begin(); it != constructorList.end(); ++it) for (it = constructorList.begin(); it != constructorList.end(); ++it)
{ {
// check for end of regular constructors and start of copy constructors
bool needClasses = it->isCopyConstructor || it->isOperatorEqual;
if (needClasses != hasClasses)
{
hasClasses = needClasses;
// Delete the varlist..
while (varlist)
{
Var *nextvar = varlist->next;
delete varlist;
varlist = nextvar;
}
// Get variables including classes
varlist = getVarList(tok1, true, isStruct);
}
if (!it->hasBody) if (!it->hasBody)
continue; continue;
@ -749,6 +722,10 @@ void CheckClass::constructors()
// Check if any variables are uninitialized // Check if any variables are uninitialized
for (Var *var = varlist; var; var = var->next) for (Var *var = varlist; var; var = var->next)
{ {
// skip classes for regular constructor
if (var->isClass && !(it->isCopyConstructor || it->isOperatorEqual))
continue;
if (var->init || var->isStatic) if (var->init || var->isStatic)
continue; continue;
@ -1752,7 +1729,7 @@ void CheckClass::checkConst()
} }
// Get class variables... // Get class variables...
varlist = getVarList(classTok, true, classTok->str() == "struct"); varlist = getVarList(classTok);
// parse in this class definition to see if there are any simple getter functions // parse in this class definition to see if there are any simple getter functions
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next())

View File

@ -109,12 +109,13 @@ private:
class Var class Var
{ {
public: public:
Var(const std::string &name_, bool init_ = false, bool priv_ = false, bool mutable_ = false, bool static_ = false, Var *next_ = 0) Var(const std::string &name_, bool init_ = false, bool priv_ = false, bool mutable_ = false, bool static_ = false, bool class_ = false, Var *next_ = 0)
: name(name_), : name(name_),
init(init_), init(init_),
priv(priv_), priv(priv_),
isMutable(mutable_), isMutable(mutable_),
isStatic(static_), isStatic(static_),
isClass(class_),
next(next_) next(next_)
{ {
} }
@ -134,6 +135,9 @@ private:
/** @brief is this variable static? */ /** @brief is this variable static? */
bool isStatic; bool isStatic;
/** @brief is this variable a class (or unknown type)? */
bool isClass;
/** @brief next Var item */ /** @brief next Var item */
Var *next; Var *next;
@ -158,10 +162,8 @@ private:
/** /**
* @brief get varlist from a class definition * @brief get varlist from a class definition
* @param tok1 pointer to class definition * @param tok1 pointer to class definition
* @param withClasses if class variables should be extracted too.
* @param isStruct is this a struct?
*/ */
Var *getVarList(const Token *tok1, bool withClasses, bool isStruct); Var *getVarList(const Token *tok1);
bool sameFunc(int nest, const Token *firstEnd, const Token *secondEnd); bool sameFunc(int nest, const Token *firstEnd, const Token *secondEnd);
bool isMemberFunc(const Token *tok); bool isMemberFunc(const Token *tok);