Symbol database: refactoring variable handling. Ticket: #1895

This commit is contained in:
Robert Reif 2010-08-13 07:34:34 +02:00 committed by Daniel Marjamäki
parent 794c0a8f78
commit b92644a30c
2 changed files with 46 additions and 23 deletions

View File

@ -428,7 +428,7 @@ void CheckClass::SpaceInfo::getVarList()
// This is the start of a statement // This is the start of a statement
const Token *next = tok->next(); const Token *next = tok->next();
std::string varname; const Token *vartok = 0;
// If next token contains a ":".. it is not part of a variable declaration // If next token contains a ":".. it is not part of a variable declaration
if (next->str().find(":") != std::string::npos) if (next->str().find(":") != std::string::npos)
@ -464,6 +464,15 @@ void CheckClass::SpaceInfo::getVarList()
next = next->next(); next = next->next();
} }
// It it a nested derived class or structure?
if (Token::Match(next, "class|struct %type% :"))
{
next = next->tokAt(2);
while (next->str() != "{")
next = next->next();
continue;
}
// Is it a variable declaration? // Is it a variable declaration?
bool isClass = false; bool isClass = false;
if (Token::Match(next, "%type% %var% ;|:")) if (Token::Match(next, "%type% %var% ;|:"))
@ -471,22 +480,22 @@ void CheckClass::SpaceInfo::getVarList()
if (!next->isStandardType()) if (!next->isStandardType())
isClass = true; isClass = true;
varname = next->strAt(1); vartok = next->tokAt(1);
} }
// Structure? // Structure?
else if (Token::Match(next, "struct|union %type% %var% ;")) else if (Token::Match(next, "struct|union %type% %var% ;"))
{ {
varname = next->strAt(2); vartok = next->tokAt(2);
} }
// Pointer? // Pointer?
else if (Token::Match(next, "%type% * %var% ;")) else if (Token::Match(next, "%type% * %var% ;"))
varname = next->strAt(2); vartok = next->tokAt(2);
else if (Token::Match(next, "%type% %type% * %var% ;")) else if (Token::Match(next, "%type% %type% * %var% ;"))
varname = next->strAt(3); vartok = next->tokAt(3);
else if (Token::Match(next, "%type% :: %type% * %var% ;")) else if (Token::Match(next, "%type% :: %type% * %var% ;"))
varname = next->strAt(4); vartok = next->tokAt(4);
// Array? // Array?
else if (Token::Match(next, "%type% %var% [") && next->next()->str() != "operator") else if (Token::Match(next, "%type% %var% [") && next->next()->str() != "operator")
@ -494,20 +503,20 @@ void CheckClass::SpaceInfo::getVarList()
if (!next->isStandardType()) if (!next->isStandardType())
isClass = true; isClass = true;
varname = next->strAt(1); vartok = next->tokAt(1);
} }
// Pointer array? // Pointer array?
else if (Token::Match(next, "%type% * %var% [")) else if (Token::Match(next, "%type% * %var% ["))
varname = next->strAt(2); vartok = next->tokAt(2);
else if (Token::Match(next, "%type% :: %type% * %var% [")) else if (Token::Match(next, "%type% :: %type% * %var% ["))
varname = next->strAt(4); vartok = next->tokAt(4);
// std::string.. // std::string..
else if (Token::Match(next, "%type% :: %type% %var% ;")) else if (Token::Match(next, "%type% :: %type% %var% ;"))
{ {
isClass = true; isClass = true;
varname = next->strAt(3); vartok = next->tokAt(3);
} }
// Container.. // Container..
@ -529,14 +538,28 @@ void CheckClass::SpaceInfo::getVarList()
} }
} }
if (next && Token::Match(next, "> %var% ;")) if (next && Token::Match(next, "> %var% ;"))
varname = next->strAt(1); vartok = next->tokAt(1);
else if (next && Token::Match(next, "> * %var% ;")) else if (next && Token::Match(next, "> * %var% ;"))
varname = next->strAt(2); vartok = next->tokAt(2);
} }
// If the varname was set in the if-blocks above, create a entry for this variable.. // If the vartok was set in the if-blocks above, create a entry for this variable..
if (!varname.empty() && varname != "operator") if (vartok && vartok->str() != "operator")
varlist.push_back(Var(varname, false, priv, isMutable, isStatic, isClass)); {
#if 0 //ndef NDEBUG
if (vartok->varId() == 0)
{
// debug message that something is wrong..
std::cout << "getVarList debug-information: missing varid: "
<< vartok->linenr()
<< ": "
<< vartok->str()
<< std::endl;
}
#endif
varlist.push_back(Var(vartok, false, priv, isMutable, isStatic, isClass));
}
} }
} }
@ -548,7 +571,7 @@ void CheckClass::SpaceInfo::initVar(const std::string &varname)
for (i = varlist.begin(); i != varlist.end(); ++i) for (i = varlist.begin(); i != varlist.end(); ++i)
{ {
if (i->name == varname) if (i->token->str() == varname)
{ {
i->init = true; i->init = true;
return; return;
@ -958,10 +981,10 @@ void CheckClass::constructors()
} }
if (classNameUsed) if (classNameUsed)
operatorEqVarError(it->token, info->className, var->name); operatorEqVarError(it->token, info->className, var->token->str());
} }
else if (it->access != Private && !var->isStatic) else if (it->access != Private && !var->isStatic)
uninitVarError(it->token, info->className, var->name); uninitVarError(it->token, info->className, var->token->str());
} }
} }
} }
@ -1865,7 +1888,7 @@ bool CheckClass::isMemberVar(const SpaceInfo *info, const Token *tok)
std::list<Var>::const_iterator var; std::list<Var>::const_iterator var;
for (var = info->varlist.begin(); var != info->varlist.end(); ++var) for (var = info->varlist.begin(); var != info->varlist.end(); ++var)
{ {
if (var->name == tok->str()) if (var->token->str() == tok->str())
{ {
return !var->isMutable; return !var->isMutable;
} }

View File

@ -132,8 +132,8 @@ private:
class Var class Var
{ {
public: public:
Var(const std::string &name_, bool init_ = false, bool priv_ = false, bool mutable_ = false, bool static_ = false, bool class_ = false) Var(const Token *token_, bool init_ = false, bool priv_ = false, bool mutable_ = false, bool static_ = false, bool class_ = false)
: name(name_), : token(token_),
init(init_), init(init_),
priv(priv_), priv(priv_),
isMutable(mutable_), isMutable(mutable_),
@ -142,8 +142,8 @@ private:
{ {
} }
/** @brief name of variable */ /** @brief variable token */
const std::string name; const Token *token;
/** @brief has this variable been initialized? */ /** @brief has this variable been initialized? */
bool init; bool init;