Uninitialized variables: Refactor CheckUninitVar::isVariableUsage

This commit is contained in:
Daniel Marjamki 2013-02-02 15:21:54 +01:00
parent 6bb8dd605a
commit 9c8d600ca9
2 changed files with 10 additions and 10 deletions

View File

@ -1360,7 +1360,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
} }
// Use variable // Use variable
else if (!suppressErrors && isVariableUsage(tok, var.isPointer())) else if (!suppressErrors && isVariableUsage(tok, var.isPointer(), _tokenizer->isCPP()))
uninitvarError(tok, tok->str()); uninitvarError(tok, tok->str());
else else
@ -1388,7 +1388,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
} else { } else {
// Use variable // Use variable
if (!suppressErrors && isVariableUsage(tok, var.isPointer())) if (!suppressErrors && isVariableUsage(tok, var.isPointer(), _tokenizer->isCPP()))
uninitvarError(tok, tok->str()); uninitvarError(tok, tok->str());
else else
@ -1412,7 +1412,7 @@ bool CheckUninitVar::checkIfForWhileHead(const Token *startparentheses, const Va
continue; continue;
} }
if (isVariableUsage(tok, var.isPointer())) { if (isVariableUsage(tok, var.isPointer(), _tokenizer->isCPP())) {
if (!suppressErrors) if (!suppressErrors)
uninitvarError(tok, tok->str()); uninitvarError(tok, tok->str());
else else
@ -1443,7 +1443,7 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const
if (isMemberVariableUsage(tok, var.isPointer(), membervar)) if (isMemberVariableUsage(tok, var.isPointer(), membervar))
usetok = tok; usetok = tok;
} else { } else {
if (isVariableUsage(tok, var.isPointer())) if (isVariableUsage(tok, var.isPointer(), _tokenizer->isCPP()))
usetok = tok; usetok = tok;
else else
return true; return true;
@ -1462,7 +1462,7 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const
return false; return false;
} }
bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer) const bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool cpp)
{ {
if (vartok->previous()->str() == "return") if (vartok->previous()->str() == "return")
return true; return true;
@ -1510,7 +1510,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer) const
} }
if (Token::Match(vartok->previous(), "++|--|%op%")) { if (Token::Match(vartok->previous(), "++|--|%op%")) {
if (vartok->previous()->str() == ">>" && _tokenizer->isCPP()) { if (cpp && vartok->previous()->str() == ">>") {
// assume that variable is initialized // assume that variable is initialized
return false; return false;
} }
@ -1555,7 +1555,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer) const
return true; return true;
} }
if (_tokenizer->isCPP() && Token::Match(vartok->next(), "<<|>>")) { if (cpp && Token::Match(vartok->next(), "<<|>>")) {
// Is this calculation done in rhs? // Is this calculation done in rhs?
const Token *tok = vartok; const Token *tok = vartok;
while (tok && Token::Match(tok, "%var%|.|::")) while (tok && Token::Match(tok, "%var%|.|::"))
@ -1565,7 +1565,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer) const
// Is variable a known POD type then this is a variable usage, // Is variable a known POD type then this is a variable usage,
// otherwise we assume it's not. // otherwise we assume it's not.
const Variable *var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(vartok->varId()); const Variable *var = vartok->variable();
return (var && var->typeStartToken()->isStandardType()); return (var && var->typeStartToken()->isStandardType());
} }
@ -1603,7 +1603,7 @@ bool CheckUninitVar::isMemberVariableUsage(const Token *tok, bool isPointer, con
if (Token::Match(tok, "%var% . %var%") && tok->strAt(2) == membervar) if (Token::Match(tok, "%var% . %var%") && tok->strAt(2) == membervar)
return true; return true;
else if (Token::Match(tok->previous(), "[(,] %var% [,)]") && isVariableUsage(tok, isPointer)) else if (Token::Match(tok->previous(), "[(,] %var% [,)]") && isVariableUsage(tok, isPointer, _tokenizer->isCPP()))
return true; return true;
return false; return false;

View File

@ -67,7 +67,7 @@ public:
bool checkScopeForVariable(const Scope* scope, const Token *tok, const Variable& var, bool * const possibleInit, bool * const noreturn, const std::string &membervar); bool checkScopeForVariable(const Scope* scope, const Token *tok, const Variable& var, bool * const possibleInit, bool * const noreturn, const std::string &membervar);
bool checkIfForWhileHead(const Token *startparentheses, const Variable& var, bool suppressErrors, bool isuninit, const std::string &membervar); bool checkIfForWhileHead(const Token *startparentheses, const Variable& var, bool suppressErrors, bool isuninit, const std::string &membervar);
bool checkLoopBody(const Token *tok, const Variable& var, const std::string &membervar); bool checkLoopBody(const Token *tok, const Variable& var, const std::string &membervar);
bool isVariableUsage(const Token *vartok, bool ispointer) const; static bool isVariableUsage(const Token *vartok, bool ispointer, bool cpp);
bool isMemberVariableAssignment(const Token *tok, const std::string &membervar) const; bool isMemberVariableAssignment(const Token *tok, const std::string &membervar) const;
bool isMemberVariableUsage(const Token *tok, bool isPointer, const std::string &membervar) const; bool isMemberVariableUsage(const Token *tok, bool isPointer, const std::string &membervar) const;