Refactoring and commenting CheckNullPointer::nullPointerAfterLoop

This commit is contained in:
Daniel Marjamäki 2010-12-31 14:17:10 +01:00
parent 59c86b5876
commit 3de9d9cb31
1 changed files with 16 additions and 18 deletions

View File

@ -152,41 +152,41 @@ void CheckNullPointer::nullPointerAfterLoop()
// Locate insufficient null-pointer handling after loop // Locate insufficient null-pointer handling after loop
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{ {
// only interested in while ( %var% )
// TODO: Aren't there false negatives. Shouldn't other loops be handled such as:
// - while ( ! %var% )
// - while ( %var% && .. )
if (! Token::Match(tok, "while ( %var% )")) if (! Token::Match(tok, "while ( %var% )"))
continue; continue;
// Get variable id for the loop variable
const unsigned int varid(tok->tokAt(2)->varId()); const unsigned int varid(tok->tokAt(2)->varId());
if (varid == 0) if (varid == 0)
continue; continue;
// Get variable name for the loop variable
const std::string varname(tok->strAt(2)); const std::string varname(tok->strAt(2));
// Locate the end of the while loop.. // Locate the end of the while loop body..
const Token *tok2 = tok->tokAt(4); const Token *tok2 = tok->tokAt(4)->link();
if (tok2->str() == "{")
tok2 = tok2->link();
else
{
while (tok2 && tok2->str() != ";")
tok2 = tok2->next();
}
// Goto next token // Check if the variable is dereferenced after the while loop
if (tok2) while (tok2 = tok2 ? tok2->next() : 0)
tok2 = tok2->next();
// Check if the variable is dereferenced..
while (tok2)
{ {
// Don't check into inner scopes or outer scopes. Stop checking if "break" is found
if (tok2->str() == "{" || tok2->str() == "}" || tok2->str() == "break") if (tok2->str() == "{" || tok2->str() == "}" || tok2->str() == "break")
break; break;
// loop variable is found..
if (tok2->varId() == varid) if (tok2->varId() == varid)
{ {
// dummy variable.. is it unknown if pointer is dereferenced or not?
bool unknown = false; bool unknown = false;
// Is the loop variable dereferenced?
if (CheckNullPointer::isPointerDeRef(tok2, unknown)) if (CheckNullPointer::isPointerDeRef(tok2, unknown))
{ {
// Is this variable a pointer? // Is loop variable a pointer?
const Token *tok3 = Token::findmatch(_tokenizer->tokens(), "%type% * %varid% [;)=]", varid); const Token *tok3 = Token::findmatch(_tokenizer->tokens(), "%type% * %varid% [;)=]", varid);
if (!tok3) if (!tok3)
break; break;
@ -200,8 +200,6 @@ void CheckNullPointer::nullPointerAfterLoop()
} }
break; break;
} }
tok2 = tok2->next();
} }
} }
} }