Refactor: Use visitAstNodes in checkleakautovar

This commit is contained in:
Rikard Falkeborn 2020-07-09 22:40:52 +02:00
parent b812d48397
commit 2ace2b006a
1 changed files with 9 additions and 14 deletions

View File

@ -463,27 +463,21 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
VarInfo varInfo1(*varInfo); // VarInfo for if code VarInfo varInfo1(*varInfo); // VarInfo for if code
VarInfo varInfo2(*varInfo); // VarInfo for else code VarInfo varInfo2(*varInfo); // VarInfo for else code
// Recursively scan variable comparisons in condition
std::stack<const Token *> tokens;
// Skip expressions before commas // Skip expressions before commas
const Token * astOperand2AfterCommas = tok->next()->astOperand2(); const Token * astOperand2AfterCommas = tok->next()->astOperand2();
while (Token::simpleMatch(astOperand2AfterCommas, ",")) while (Token::simpleMatch(astOperand2AfterCommas, ","))
astOperand2AfterCommas = astOperand2AfterCommas->astOperand2(); astOperand2AfterCommas = astOperand2AfterCommas->astOperand2();
tokens.push(astOperand2AfterCommas);
while (!tokens.empty()) { // Recursively scan variable comparisons in condition
const Token *tok3 = tokens.top(); visitAstNodes(astOperand2AfterCommas, [&](const Token *tok3) {
tokens.pop();
if (!tok3) if (!tok3)
continue; return ChildrenToVisit::none;
if (tok3->str() == "&&" || tok3->str() == "||") { if (tok3->str() == "&&" || tok3->str() == "||") {
// FIXME: handle && ! || better // FIXME: handle && ! || better
tokens.push(tok3->astOperand1()); return ChildrenToVisit::op1_and_op2;
tokens.push(tok3->astOperand2());
continue;
} }
if (tok3->str() == "(" && Token::Match(tok3->astOperand1(), "UNLIKELY|LIKELY")) { if (tok3->str() == "(" && Token::Match(tok3->astOperand1(), "UNLIKELY|LIKELY")) {
tokens.push(tok3->astOperand2()); return ChildrenToVisit::op2;
continue;
} else if (tok3->str() == "(" && Token::Match(tok3->previous(), "%name%")) { } else if (tok3->str() == "(" && Token::Match(tok3->previous(), "%name%")) {
const std::vector<const Token *> params = getArguments(tok3->previous()); const std::vector<const Token *> params = getArguments(tok3->previous());
for (const Token *par : params) { for (const Token *par : params) {
@ -500,7 +494,7 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
varInfo2.erase(vartok->varId()); varInfo2.erase(vartok->varId());
} }
} }
continue; return ChildrenToVisit::none;
} }
const Token *vartok = nullptr; const Token *vartok = nullptr;
@ -517,7 +511,8 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
} else if (astIsVariableComparison(tok3, "==", "-1", &vartok)) { } else if (astIsVariableComparison(tok3, "==", "-1", &vartok)) {
varInfo1.erase(vartok->varId()); varInfo1.erase(vartok->varId());
} }
} return ChildrenToVisit::none;
});
checkScope(closingParenthesis->next(), &varInfo1, notzero, recursiveCount); checkScope(closingParenthesis->next(), &varInfo1, notzero, recursiveCount);
closingParenthesis = closingParenthesis->linkAt(1); closingParenthesis = closingParenthesis->linkAt(1);