Refactorization: Made checkUninitVar::isVariableUsage() non-static

This commit is contained in:
PKEuS 2015-01-21 13:52:03 +01:00
parent 346532d312
commit d8e282fe76
2 changed files with 12 additions and 13 deletions

View File

@ -1452,7 +1452,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
} }
// Use variable // Use variable
else if (!suppressErrors && isVariableUsage(tok, var.isPointer(), alloc && *alloc, _tokenizer->isCPP())) { else if (!suppressErrors && isVariableUsage(tok, var.isPointer(), alloc && *alloc)) {
if (alloc && *alloc) if (alloc && *alloc)
uninitdataError(tok, tok->str()); uninitdataError(tok, tok->str());
else else
@ -1506,7 +1506,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
} else { } else {
// Use variable // Use variable
if (!suppressErrors && isVariableUsage(tok, var.isPointer(), alloc && *alloc, _tokenizer->isCPP())) { if (!suppressErrors && isVariableUsage(tok, var.isPointer(), alloc && *alloc)) {
if (alloc && *alloc) if (alloc && *alloc)
uninitdataError(tok, tok->str()); uninitdataError(tok, tok->str());
else else
@ -1545,7 +1545,7 @@ bool CheckUninitVar::checkIfForWhileHead(const Token *startparentheses, const Va
continue; continue;
} }
if (isVariableUsage(tok, var.isPointer(), alloc, _tokenizer->isCPP())) { if (isVariableUsage(tok, var.isPointer(), alloc)) {
if (!suppressErrors) if (!suppressErrors)
uninitvarError(tok, tok->str()); uninitvarError(tok, tok->str());
else else
@ -1595,7 +1595,7 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const
else if (Token::Match(tok->previous(), "[(,] %var% [,)]")) else if (Token::Match(tok->previous(), "[(,] %var% [,)]"))
return true; return true;
} else { } else {
if (isVariableUsage(tok, var.isPointer(), alloc, _tokenizer->isCPP())) if (isVariableUsage(tok, var.isPointer(), alloc))
usetok = tok; usetok = tok;
else if (tok->strAt(1) == "=") { else if (tok->strAt(1) == "=") {
// Is var used in rhs? // Is var used in rhs?
@ -1651,7 +1651,7 @@ void CheckUninitVar::checkRhs(const Token *tok, const Variable &var, bool alloc,
if (tok->str() == "=") if (tok->str() == "=")
rhs = true; rhs = true;
else if (rhs && tok->varId() == var.declarationId()) { else if (rhs && tok->varId() == var.declarationId()) {
if (membervar.empty() && isVariableUsage(tok, var.isPointer(), alloc, _tokenizer->isCPP())) if (membervar.empty() && isVariableUsage(tok, var.isPointer(), alloc))
uninitvarError(tok, tok->str()); uninitvarError(tok, tok->str());
else if (!membervar.empty() && isMemberVariableUsage(tok, var.isPointer(), alloc, membervar)) else if (!membervar.empty() && isMemberVariableUsage(tok, var.isPointer(), alloc, membervar))
uninitStructMemberError(tok, tok->str() + "." + membervar); uninitStructMemberError(tok, tok->str() + "." + membervar);
@ -1669,7 +1669,7 @@ void CheckUninitVar::checkRhs(const Token *tok, const Variable &var, bool alloc,
} }
} }
bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool alloc, bool cpp) bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool alloc) const
{ {
if (!alloc && vartok->previous()->str() == "return") if (!alloc && vartok->previous()->str() == "return")
return true; return true;
@ -1750,7 +1750,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all
} }
if (Token::Match(vartok->previous(), "++|--|%cop%")) { if (Token::Match(vartok->previous(), "++|--|%cop%")) {
if (cpp && vartok->previous()->str() == ">>") { if (_tokenizer->isCPP() && vartok->previous()->str() == ">>") {
// assume that variable is initialized // assume that variable is initialized
return false; return false;
} }
@ -1815,7 +1815,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all
return (!function || !function->isStatic()); return (!function || !function->isStatic());
} }
if (cpp && Token::Match(vartok->next(), "<<|>>")) { if (_tokenizer->isCPP() && 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%|.|::"))
@ -1894,7 +1894,7 @@ bool CheckUninitVar::isMemberVariableUsage(const Token *tok, bool isPointer, boo
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 (!isPointer && Token::Match(tok->previous(), "[(,] %var% [,)]") && isVariableUsage(tok, isPointer, alloc, _tokenizer->isCPP())) else if (!isPointer && Token::Match(tok->previous(), "[(,] %var% [,)]") && isVariableUsage(tok, isPointer, alloc))
return true; return true;
else if (!isPointer && Token::Match(tok->previous(), "= %var% ;")) else if (!isPointer && Token::Match(tok->previous(), "= %var% ;"))
@ -1911,7 +1911,7 @@ bool CheckUninitVar::isMemberVariableUsage(const Token *tok, bool isPointer, boo
else if (_settings->experimental && else if (_settings->experimental &&
!isPointer && !isPointer &&
Token::Match(tok->tokAt(-2), "[(,] & %var% [,)]") && Token::Match(tok->tokAt(-2), "[(,] & %var% [,)]") &&
isVariableUsage(tok, isPointer, alloc, _tokenizer->isCPP())) isVariableUsage(tok, isPointer, alloc))
return true; return true;
return false; return false;
@ -1942,7 +1942,6 @@ void CheckUninitVar::uninitStructMemberError(const Token *tok, const std::string
void CheckUninitVar::deadPointer() void CheckUninitVar::deadPointer()
{ {
const bool cpp = _tokenizer->isCPP();
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
std::list<Scope>::const_iterator scope; std::list<Scope>::const_iterator scope;
@ -1954,7 +1953,7 @@ void CheckUninitVar::deadPointer()
for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
if (tok->variable() && if (tok->variable() &&
tok->variable()->isPointer() && tok->variable()->isPointer() &&
isVariableUsage(tok, true, false, cpp)) { isVariableUsage(tok, true, false)) {
const Token *alias = tok->getValueTokenDeadPointer(); const Token *alias = tok->getValueTokenDeadPointer();
if (alias) { if (alias) {
deadPointerError(tok,alias); deadPointerError(tok,alias);

View File

@ -61,7 +61,7 @@ public:
bool checkIfForWhileHead(const Token *startparentheses, const Variable& var, bool suppressErrors, bool isuninit, bool alloc, const std::string &membervar); bool checkIfForWhileHead(const Token *startparentheses, const Variable& var, bool suppressErrors, bool isuninit, bool alloc, const std::string &membervar);
bool checkLoopBody(const Token *tok, const Variable& var, const bool alloc, const std::string &membervar, const bool suppressErrors); bool checkLoopBody(const Token *tok, const Variable& var, const bool alloc, const std::string &membervar, const bool suppressErrors);
void checkRhs(const Token *tok, const Variable &var, bool alloc, const std::string &membervar); void checkRhs(const Token *tok, const Variable &var, bool alloc, const std::string &membervar);
static bool isVariableUsage(const Token *vartok, bool ispointer, bool alloc, bool cpp); bool isVariableUsage(const Token *vartok, bool ispointer, bool alloc) const;
static bool isMemberVariableAssignment(const Token *tok, const std::string &membervar); static bool isMemberVariableAssignment(const Token *tok, const std::string &membervar);
bool isMemberVariableUsage(const Token *tok, bool isPointer, bool alloc, const std::string &membervar) const; bool isMemberVariableUsage(const Token *tok, bool isPointer, bool alloc, const std::string &membervar) const;