Refactoring of CheckOther::nullConstantDereference

This commit is contained in:
Daniel Marjamäki 2010-06-02 17:53:45 +02:00
parent 33b4254d33
commit 28ad69e4b3
1 changed files with 35 additions and 13 deletions

View File

@ -2201,27 +2201,49 @@ void CheckOther::nullPointer()
/** Derefencing null constant (simplified token list) */ /** Derefencing null constant (simplified token list) */
void CheckOther::nullConstantDereference() void CheckOther::nullConstantDereference()
{ {
// this is kept at 0 for all scopes that are not executing
unsigned int indentlevel = 0;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{ {
if (tok->str() == "(" && Token::simpleMatch(tok->previous(), "sizeof")) // start of executable scope..
tok = tok->link(); if (indentlevel == 0 && Token::Match(tok, ") const| {"))
indentlevel = 1;
else if (Token::simpleMatch(tok, "exit ( )")) else if (indentlevel >= 1)
{ {
while (tok && tok->str() != "}") if (tok->str() == "{")
++indentlevel;
else if (tok->str() == "}")
{ {
if (tok->str() == "{") if (indentlevel <= 2)
tok = tok->link(); indentlevel = 0;
tok = tok->next(); else
--indentlevel;
} }
}
else if (Token::simpleMatch(tok, "* 0")) if (tok->str() == "(" && Token::simpleMatch(tok->previous(), "sizeof"))
{ tok = tok->link();
if (Token::Match(tok->previous(), "[;{}=+-/(,]") ||
Token::Match(tok->previous(), "return|<<")) else if (Token::simpleMatch(tok, "exit ( )"))
{ {
nullPointerError(tok); // Goto end of scope
while (tok && tok->str() != "}")
{
if (tok->str() == "{")
tok = tok->link();
tok = tok->next();
}
}
else if (Token::simpleMatch(tok, "* 0"))
{
if (Token::Match(tok->previous(), "[;{}=+-/(,]") ||
Token::Match(tok->previous(), "return|<<"))
{
nullPointerError(tok);
}
} }
} }
} }