Null Pointer: Refactoring - broke out CheckNullPointer::isPointer

This commit is contained in:
Daniel Marjamäki 2010-12-31 18:30:04 +01:00
parent 04eb9cf305
commit 3e5614a5a3
2 changed files with 32 additions and 15 deletions

View File

@ -145,7 +145,27 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown)
return false; return false;
} }
bool CheckNullPointer::isPointer(const unsigned int varid)
{
// Check if given variable is a pointer
const Token *tok = Token::findmatch(_tokenizer->tokens(), "%varid%", varid);
tok = tok->tokAt(-2);
// maybe not a pointer
if (!Token::Match(tok, "%type% * %varid% [;)=]", varid))
return false;
// it is a pointer
if (!tok->previous() ||
Token::Match(tok->previous(), "[({};]") ||
tok->previous()->isName())
{
return true;
}
// it is not a pointer
return false;
}
void CheckNullPointer::nullPointerAfterLoop() void CheckNullPointer::nullPointerAfterLoop()
{ {
@ -187,17 +207,9 @@ void CheckNullPointer::nullPointerAfterLoop()
if (CheckNullPointer::isPointerDeRef(tok2, unknown)) if (CheckNullPointer::isPointerDeRef(tok2, unknown))
{ {
// Is loop variable a pointer? // Is loop variable a pointer?
const Token *tok3 = Token::findmatch(_tokenizer->tokens(), "%type% * %varid% [;)=]", varid); if (isPointer(varid))
if (!tok3)
break;
if (!tok3->previous() ||
Token::Match(tok3->previous(), "[({};]") ||
tok3->previous()->isName())
{
nullPointerError(tok2, varname); nullPointerError(tok2, varname);
} }
}
break; break;
} }
} }
@ -277,8 +289,7 @@ void CheckNullPointer::nullPointerLinkedList()
if (indentlevel4 <= 1) if (indentlevel4 <= 1)
{ {
// Is this variable a pointer? // Is this variable a pointer?
const Token *tempTok = Token::findmatch(_tokenizer->tokens(), "%type% * %varid% [;)=]", varid); if (isPointer(varid))
if (tempTok)
nullPointerError(tok1, varname, tok3->linenr()); nullPointerError(tok1, varname, tok3->linenr());
break; break;
@ -408,8 +419,7 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
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?
const Token *tempTok = Token::findmatch(_tokenizer->tokens(), "%type% * %varid% [;)=]", varid1); if (isPointer(varid1))
if (tempTok)
nullPointerError(tok1, varname, tok2->linenr()); nullPointerError(tok1, varname, tok2->linenr());
break; break;
} }
@ -431,10 +441,11 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
const std::string varname(tok->strAt(3)); const std::string varname(tok->strAt(3));
// Check that variable is a pointer.. // Check that variable is a pointer..
const Token *decltok = Token::findmatch(_tokenizer->tokens(), "%varid%", varid); if (!isPointer(varid))
if (!Token::Match(decltok->tokAt(-3), "[{};,(] %type% *"))
continue; continue;
const Token * const decltok = Token::findmatch(_tokenizer->tokens(), "%varid%", varid);
for (const Token *tok1 = tok->previous(); tok1 && tok1 != decltok; tok1 = tok1->previous()) for (const Token *tok1 = tok->previous(); tok1 && tok1 != decltok; tok1 = tok1->previous())
{ {
if (tok1->str() == ")" && Token::Match(tok1->link()->tokAt(-3), "%varid% = %var%", varid)) if (tok1->str() == ")" && Token::Match(tok1->link()->tokAt(-3), "%varid% = %var%", varid))

View File

@ -123,6 +123,12 @@ public:
private: private:
/**
* Check if a variable is a pointer
* @param varid variable id for variable
*/
bool isPointer(const unsigned int varid);
/** /**
* @brief Does one part of the check for nullPointer(). * @brief Does one part of the check for nullPointer().
* Locate insufficient null-pointer handling after loop * Locate insufficient null-pointer handling after loop