added more comments

This commit is contained in:
Daniel Marjamäki 2010-12-31 15:56:41 +01:00
parent b634242a58
commit 4d8fae186b
1 changed files with 11 additions and 3 deletions

View File

@ -301,13 +301,17 @@ void CheckNullPointer::nullPointerLinkedList()
void CheckNullPointer::nullPointerStructByDeRefAndChec() void CheckNullPointer::nullPointerStructByDeRefAndChec()
{ {
// don't check vars that has been tested against null already // Dereferencing a struct pointer and then checking if it's NULL..
// skipvar: don't check vars that has been tested against null already
std::set<unsigned int> skipvar; std::set<unsigned int> skipvar;
skipvar.insert(0); skipvar.insert(0);
// Dereferencing a struct pointer and then checking if it's NULL.. // Scan through all tokens
for (const Token *tok1 = _tokenizer->tokens(); tok1; tok1 = tok1->next()) for (const Token *tok1 = _tokenizer->tokens(); tok1; tok1 = tok1->next())
{ {
// Checking if some pointer is null.
// then add the pointer to skipvar => is it known that it isn't NULL
if (Token::Match(tok1, "if|while ( !| %var% )")) if (Token::Match(tok1, "if|while ( !| %var% )"))
{ {
tok1 = tok1->tokAt(2); tok1 = tok1->tokAt(2);
@ -360,8 +364,10 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
if (skipvar.find(varid1) != skipvar.end()) if (skipvar.find(varid1) != skipvar.end())
continue; continue;
// name of struct pointer
const std::string varname(tok1->str()); const std::string varname(tok1->str());
// count { and } using tok2
unsigned int indentlevel2 = 0; unsigned int indentlevel2 = 0;
for (const Token *tok2 = tok1->tokAt(3); tok2; tok2 = tok2->next()) for (const Token *tok2 = tok1->tokAt(3); tok2; tok2 = tok2->next())
{ {
@ -375,7 +381,7 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
--indentlevel2; --indentlevel2;
} }
// goto destination.. // label. goto destination..
else if (tok2->isName() && Token::simpleMatch(tok2->next(), ":")) else if (tok2->isName() && Token::simpleMatch(tok2->next(), ":"))
break; break;
@ -397,6 +403,8 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
else if (indentlevel2 == 0 && tok2->str() == "return") else if (indentlevel2 == 0 && tok2->str() == "return")
break; break;
// Check if pointer is null.
// TODO: false negatives for something like: "if (p &&.."?
else if (Token::Match(tok2, "if ( !| %varid% )", varid1)) else if (Token::Match(tok2, "if ( !| %varid% )", varid1))
{ {
// Is this variable a pointer? // Is this variable a pointer?