Refactoring: Use the Token::link instead of loop

This commit is contained in:
Daniel Marjamäki 2009-08-17 22:23:37 +02:00
parent 40fc605e2b
commit 2cdb0abb82
3 changed files with 18 additions and 39 deletions

View File

@ -267,9 +267,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const char *varname[], con
if (value <= size) if (value <= size)
condition_out_of_bounds = false;; condition_out_of_bounds = false;;
// Goto the end of the for loop.. // Goto the end paranthesis of the for-statement: "for (x; y; z)" ..
while (tok2 && tok2->str() != ")") tok2 = tok->next()->link();
tok2 = tok2->next();
if (!tok2 || !tok2->tokAt(5)) if (!tok2 || !tok2->tokAt(5))
break; break;

View File

@ -62,8 +62,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
// * var = strndup("hello", 3); // * var = strndup("hello", 3);
if (tok2 && tok2->str() == "(") if (tok2 && tok2->str() == "(")
{ {
while (tok2 && tok2->str() != ")") tok2 = tok2->link();
tok2 = tok2->next();
tok2 = tok2 ? tok2->next() : NULL; tok2 = tok2 ? tok2->next() : NULL;
} }
if (! tok2) if (! tok2)
@ -143,8 +142,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getReallocationType(const Token *tok
// * var = (char *)realloc(..; // * var = (char *)realloc(..;
if (tok2 && tok2->str() == "(") if (tok2 && tok2->str() == "(")
{ {
while (tok2 && tok2->str() != ")") tok2 = tok2->link();
tok2 = tok2->next();
tok2 = tok2 ? tok2->next() : NULL; tok2 = tok2 ? tok2->next() : NULL;
} }
if (! tok2) if (! tok2)
@ -735,8 +733,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
addtoken("if(var)"); addtoken("if(var)");
// Make sure the "use" will not be added // Make sure the "use" will not be added
while (tok->str() != ")") tok = tok->next()->link();
tok = tok->next();
} }
else if (Token::simpleMatch(tok, std::string("if ( " + varnameStr + " == -1 )").c_str()) || else if (Token::simpleMatch(tok, std::string("if ( " + varnameStr + " == -1 )").c_str()) ||
Token::simpleMatch(tok, std::string("if ( " + varnameStr + " < 0 )").c_str())) Token::simpleMatch(tok, std::string("if ( " + varnameStr + " < 0 )").c_str()))
@ -2068,24 +2065,14 @@ void CheckMemoryLeakStructMember::check()
else if (Token::Match(tok3, "if ( ! %var% . %varid% )", structmemberid)) else if (Token::Match(tok3, "if ( ! %var% . %varid% )", structmemberid))
{ {
// Goto the ")" // Goto the ")"
while (tok3->str() != ")") tok3 = tok3->next()->link();
tok3 = tok3->next();
// Skip block.. // make sure we have ") {".. it should be
unsigned int indentlevel = 0; if (!Token::simpleMatch(tok3, ") {"))
while (tok3)
{
if (tok3->str() == "{")
++indentlevel;
else if (tok3->str() == "}")
{
if (indentlevel <= 1)
break; break;
--indentlevel;
} // Goto the "}"
tok3 = tok3->next(); tok3 = tok3->next()->link();
}
} }
// Returning from function.. // Returning from function..

View File

@ -977,24 +977,17 @@ void CheckOther::nullPointer()
// Locate the end of the while loop.. // Locate the end of the while loop..
const Token *tok2 = tok->tokAt(4); const Token *tok2 = tok->tokAt(4);
int indentlevel = 0;
while (tok2)
{
if (tok2->str() == "{") if (tok2->str() == "{")
++indentlevel; tok2 = tok2->link();
else if (tok2->str() == "}") else
{ {
if (indentlevel <= 1) while (tok2 && tok2->str() != ";")
break;
--indentlevel;
}
else if (indentlevel == 0 && tok2->str() == ";")
break;
tok2 = tok2->next(); tok2 = tok2->next();
} }
// Goto next token // Goto next token
tok2 = tok2 ? tok2->next() : 0; if (tok2)
tok2 = tok2->next();
// Check if the variable is dereferenced.. // Check if the variable is dereferenced..
while (tok2) while (tok2)