Refactoring CheckMemoryLeak::notvar, use AST instead of token parsing

This commit is contained in:
Daniel Marjamäki 2015-07-21 20:27:59 +02:00
parent 1b8252181d
commit 24269b1061
2 changed files with 11 additions and 16 deletions

View File

@ -517,11 +517,13 @@ const char *CheckMemoryLeak::functionArgAlloc(const Function *func, unsigned int
} }
bool CheckMemoryLeakInFunction::notvar(const Token *tok, unsigned int varid, bool endpar) bool CheckMemoryLeakInFunction::notvar(const Token *tok, unsigned int varid)
{ {
const std::string end(endpar ? " &&|)" : " [;)&|]"); if (!tok)
return bool(Token::Match(tok, ("! %varid%" + end).c_str(), varid) || return false;
Token::Match(tok, ("! ( %varid% )" + end).c_str(), varid)); if (Token::Match(tok, "&&|;"))
return notvar(tok->astOperand1(),varid) || notvar(tok->astOperand2(),varid);
return tok->str() == "!" && tok->astOperand1()->varId() == varid;
} }
@ -987,7 +989,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
// Make sure the "use" will not be added // Make sure the "use" will not be added
tok = tok->next()->link(); tok = tok->next()->link();
continue; continue;
} else if (Token::simpleMatch(tok, "if (") && notvar(tok->tokAt(2), varid, true)) { } else if (Token::simpleMatch(tok, "if (") && notvar(tok->next()->astOperand2(), varid)) {
addtoken(&rettail, tok, "if(!var)"); addtoken(&rettail, tok, "if(!var)");
// parse the if-body. // parse the if-body.
@ -1113,7 +1115,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
addtoken(&rettail, tok, "while(var)"); addtoken(&rettail, tok, "while(var)");
tok = end; tok = end;
continue; continue;
} else if (varid && Token::simpleMatch(tok, "while (") && notvar(tok->tokAt(2), varid, true)) { } else if (varid && Token::simpleMatch(tok, "while (") && notvar(tok->next()->astOperand2(), varid)) {
addtoken(&rettail, tok, "while(!var)"); addtoken(&rettail, tok, "while(!var)");
tok = end; tok = end;
continue; continue;
@ -1121,14 +1123,8 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
addtoken(&rettail, tok, "loop"); addtoken(&rettail, tok, "loop");
if (varid > 0) { if (varid > 0 && notvar(tok->next()->astOperand2(), varid))
for (const Token *tok2 = tok->tokAt(2); tok2 && tok2 != end; tok2 = tok2->next()) { addtoken(&rettail, tok, "!var");
if (notvar(tok2, varid)) {
addtoken(&rettail, tok2, "!var");
break;
}
}
}
continue; continue;
} }

View File

@ -219,10 +219,9 @@ public:
* @brief %Check if there is a "!var" match inside a condition * @brief %Check if there is a "!var" match inside a condition
* @param tok first token to match * @param tok first token to match
* @param varid variable id * @param varid variable id
* @param endpar if this is true the "!var" must be followed by ")"
* @return true if match * @return true if match
*/ */
static bool notvar(const Token *tok, unsigned int varid, bool endpar = false); static bool notvar(const Token *tok, unsigned int varid);
/** /**
* Inspect a function call. the call_func and getcode are recursive * Inspect a function call. the call_func and getcode are recursive