From 9ced26a7a112bf1d50adb6fcd41db5a7ec77640c Mon Sep 17 00:00:00 2001 From: Rikard Falkeborn Date: Fri, 10 Jul 2020 00:50:57 +0200 Subject: [PATCH] Refactor: Use visitAstNodes in checkcondition --- lib/checkcondition.cpp | 46 ++++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index 22f166646..97dad288e 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -688,16 +688,11 @@ void CheckCondition::multiCondition2() ErrorPath errorPath; if (type == MULTICONDITIONTYPE::INNER) { - std::stack tokens1; - tokens1.push(cond1); - while (!tokens1.empty()) { - const Token *firstCondition = tokens1.top(); - tokens1.pop(); + visitAstNodes(cond1, [&](const Token* firstCondition) { if (!firstCondition) - continue; + return ChildrenToVisit::none; if (firstCondition->str() == "&&") { - tokens1.push(firstCondition->astOperand1()); - tokens1.push(firstCondition->astOperand2()); + return ChildrenToVisit::op1_and_op2; } else if (!firstCondition->hasKnownIntValue()) { if (!isReturnVar && isOppositeCond(false, mTokenizer->isCPP(), firstCondition, cond2, mSettings->library, true, true, &errorPath)) { if (!isAliased(vars)) @@ -706,7 +701,8 @@ void CheckCondition::multiCondition2() identicalInnerConditionError(firstCondition, cond2, errorPath); } } - } + return ChildrenToVisit::none; + }); } else { visitAstNodes(cond2, [&](const Token *secondCondition) { if (secondCondition->str() == "||" || secondCondition->str() == "&&") @@ -1436,20 +1432,15 @@ void CheckCondition::alwaysTrueFalse() // Don't warn when there are expanded macros.. bool isExpandedMacro = false; - std::stack tokens; - tokens.push(tok); - while (!tokens.empty()) { - const Token *tok2 = tokens.top(); - tokens.pop(); + visitAstNodes(tok, [&](const Token * tok2) { if (!tok2) - continue; - tokens.push(tok2->astOperand1()); - tokens.push(tok2->astOperand2()); + return ChildrenToVisit::none; if (tok2->isExpandedMacro()) { isExpandedMacro = true; - break; + return ChildrenToVisit::done; } - } + return ChildrenToVisit::op1_and_op2; + }); if (isExpandedMacro) continue; for (const Token *parent = tok; parent; parent = parent->astParent()) { @@ -1464,24 +1455,21 @@ void CheckCondition::alwaysTrueFalse() // don't warn when condition checks sizeof result bool hasSizeof = false; bool hasNonNumber = false; - tokens.push(tok); - while (!tokens.empty()) { - const Token *tok2 = tokens.top(); - tokens.pop(); + visitAstNodes(tok, [&](const Token * tok2) { if (!tok2) - continue; + return ChildrenToVisit::none; if (tok2->isNumber()) - continue; + return ChildrenToVisit::none; if (Token::simpleMatch(tok2->previous(), "sizeof (")) { hasSizeof = true; - continue; + return ChildrenToVisit::none; } if (tok2->isComparisonOp() || tok2->isArithmeticalOp()) { - tokens.push(tok2->astOperand1()); - tokens.push(tok2->astOperand2()); + return ChildrenToVisit::op1_and_op2; } else hasNonNumber = true; - } + return ChildrenToVisit::none; + }); if (!hasNonNumber && hasSizeof) continue;