diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index f495bf857..355d10571 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -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 ? " &&|)" : " [;)&|]"); - return bool(Token::Match(tok, ("! %varid%" + end).c_str(), varid) || - Token::Match(tok, ("! ( %varid% )" + end).c_str(), varid)); + if (!tok) + return false; + 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::listnext()->link(); 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)"); // parse the if-body. @@ -1113,7 +1115,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listtokAt(2), varid, true)) { + } else if (varid && Token::simpleMatch(tok, "while (") && notvar(tok->next()->astOperand2(), varid)) { addtoken(&rettail, tok, "while(!var)"); tok = end; continue; @@ -1121,14 +1123,8 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list 0) { - for (const Token *tok2 = tok->tokAt(2); tok2 && tok2 != end; tok2 = tok2->next()) { - if (notvar(tok2, varid)) { - addtoken(&rettail, tok2, "!var"); - break; - } - } - } + if (varid > 0 && notvar(tok->next()->astOperand2(), varid)) + addtoken(&rettail, tok, "!var"); continue; } diff --git a/lib/checkmemoryleak.h b/lib/checkmemoryleak.h index 7df103ec4..e82f7f8c6 100644 --- a/lib/checkmemoryleak.h +++ b/lib/checkmemoryleak.h @@ -219,10 +219,9 @@ public: * @brief %Check if there is a "!var" match inside a condition * @param tok first token to match * @param varid variable id - * @param endpar if this is true the "!var" must be followed by ")" * @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