Refactor: Use visitAstNodes in checkcondition

This commit is contained in:
Rikard Falkeborn 2020-07-10 00:50:57 +02:00
parent 82fe6193fa
commit 9ced26a7a1
1 changed files with 17 additions and 29 deletions

View File

@ -688,16 +688,11 @@ void CheckCondition::multiCondition2()
ErrorPath errorPath; ErrorPath errorPath;
if (type == MULTICONDITIONTYPE::INNER) { if (type == MULTICONDITIONTYPE::INNER) {
std::stack<const Token *> tokens1; visitAstNodes(cond1, [&](const Token* firstCondition) {
tokens1.push(cond1);
while (!tokens1.empty()) {
const Token *firstCondition = tokens1.top();
tokens1.pop();
if (!firstCondition) if (!firstCondition)
continue; return ChildrenToVisit::none;
if (firstCondition->str() == "&&") { if (firstCondition->str() == "&&") {
tokens1.push(firstCondition->astOperand1()); return ChildrenToVisit::op1_and_op2;
tokens1.push(firstCondition->astOperand2());
} else if (!firstCondition->hasKnownIntValue()) { } else if (!firstCondition->hasKnownIntValue()) {
if (!isReturnVar && isOppositeCond(false, mTokenizer->isCPP(), firstCondition, cond2, mSettings->library, true, true, &errorPath)) { if (!isReturnVar && isOppositeCond(false, mTokenizer->isCPP(), firstCondition, cond2, mSettings->library, true, true, &errorPath)) {
if (!isAliased(vars)) if (!isAliased(vars))
@ -706,7 +701,8 @@ void CheckCondition::multiCondition2()
identicalInnerConditionError(firstCondition, cond2, errorPath); identicalInnerConditionError(firstCondition, cond2, errorPath);
} }
} }
} return ChildrenToVisit::none;
});
} else { } else {
visitAstNodes(cond2, [&](const Token *secondCondition) { visitAstNodes(cond2, [&](const Token *secondCondition) {
if (secondCondition->str() == "||" || secondCondition->str() == "&&") if (secondCondition->str() == "||" || secondCondition->str() == "&&")
@ -1436,20 +1432,15 @@ void CheckCondition::alwaysTrueFalse()
// Don't warn when there are expanded macros.. // Don't warn when there are expanded macros..
bool isExpandedMacro = false; bool isExpandedMacro = false;
std::stack<const Token*> tokens; visitAstNodes(tok, [&](const Token * tok2) {
tokens.push(tok);
while (!tokens.empty()) {
const Token *tok2 = tokens.top();
tokens.pop();
if (!tok2) if (!tok2)
continue; return ChildrenToVisit::none;
tokens.push(tok2->astOperand1());
tokens.push(tok2->astOperand2());
if (tok2->isExpandedMacro()) { if (tok2->isExpandedMacro()) {
isExpandedMacro = true; isExpandedMacro = true;
break; return ChildrenToVisit::done;
} }
} return ChildrenToVisit::op1_and_op2;
});
if (isExpandedMacro) if (isExpandedMacro)
continue; continue;
for (const Token *parent = tok; parent; parent = parent->astParent()) { for (const Token *parent = tok; parent; parent = parent->astParent()) {
@ -1464,24 +1455,21 @@ void CheckCondition::alwaysTrueFalse()
// don't warn when condition checks sizeof result // don't warn when condition checks sizeof result
bool hasSizeof = false; bool hasSizeof = false;
bool hasNonNumber = false; bool hasNonNumber = false;
tokens.push(tok); visitAstNodes(tok, [&](const Token * tok2) {
while (!tokens.empty()) {
const Token *tok2 = tokens.top();
tokens.pop();
if (!tok2) if (!tok2)
continue; return ChildrenToVisit::none;
if (tok2->isNumber()) if (tok2->isNumber())
continue; return ChildrenToVisit::none;
if (Token::simpleMatch(tok2->previous(), "sizeof (")) { if (Token::simpleMatch(tok2->previous(), "sizeof (")) {
hasSizeof = true; hasSizeof = true;
continue; return ChildrenToVisit::none;
} }
if (tok2->isComparisonOp() || tok2->isArithmeticalOp()) { if (tok2->isComparisonOp() || tok2->isArithmeticalOp()) {
tokens.push(tok2->astOperand1()); return ChildrenToVisit::op1_and_op2;
tokens.push(tok2->astOperand2());
} else } else
hasNonNumber = true; hasNonNumber = true;
} return ChildrenToVisit::none;
});
if (!hasNonNumber && hasSizeof) if (!hasNonNumber && hasSizeof)
continue; continue;