diff --git a/lib/checkother.cpp b/lib/checkother.cpp index ed579199b..7dd067319 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2304,6 +2304,24 @@ void CheckOther::checkZeroDivisionOrUselessCondition() } } +// TODO: this utility function should probably be moved to some common file +static std::string astStringify(const Token *top) +{ + const Token *start = top; + while (start->astOperand1() && start->astOperand2()) + start = start->astOperand1(); + const Token *end = top; + while (end->astOperand1() && end->astOperand2()) + end = end->astOperand2(); + std::string str; + for (const Token *tok = start; tok && tok != end; tok = tok->next()) { + str += tok->str(); + if (Token::Match(tok, "%var%|%num% %var%|%num%")) + str += " "; + } + return str + end->str(); +} + void CheckOther::zerodivcondError(const Token *tokcond, const Token *tokdiv) { std::list callstack; @@ -2314,9 +2332,13 @@ void CheckOther::zerodivcondError(const Token *tokcond, const Token *tokdiv) callstack.push_back(tokdiv); } std::string condition; - if (Token::Match(tokcond, "%num% <|<=")) { + if (!tokcond) { + // getErrorMessages + } else if (Token::Match(tokcond, "%num% <|<=")) { condition = tokcond->strAt(2) + ((tokcond->strAt(1) == "<") ? ">" : ">=") + tokcond->str(); - } else if (tokcond) { + } else if (tokcond->isComparisonOp()) { + condition = astStringify(tokcond); + } else { if (tokcond->str() == "!") condition = tokcond->next()->str() + "==0"; else