From 827cfac91eabcec9c743c313c016e75bac8ae5fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 10 May 2014 20:49:29 +0200 Subject: [PATCH] Refactoring of CheckOther::checkModuloAlwaysTrueFalse using AST --- lib/checkother.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index e58b0a467..2ff2c14bc 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3004,11 +3004,22 @@ void CheckOther::checkModuloAlwaysTrueFalse() for (std::size_t i = 0; i < functions; ++i) { const Scope * scope = symbolDatabase->functionScopes[i]; for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { - if ((Token::Match(tok, "% %num% %comp% %num%")) && - (!tok->tokAt(4) || !tok->tokAt(4)->isArithmeticalOp()) && (!tok->astParent() || !tok->astParent()->isArithmeticalOp())) { - if (MathLib::isLessEqual(tok->strAt(1), tok->strAt(3))) - moduloAlwaysTrueFalseError(tok, tok->strAt(1)); + if (!tok->isComparisonOp()) + continue; + const Token *num,*modulo; + if (Token::simpleMatch(tok->astOperand1(),"%") && Token::Match(tok->astOperand2(),"%num%")) { + modulo = tok->astOperand1(); + num = tok->astOperand2(); + } else if (Token::Match(tok->astOperand1(),"%num%") && Token::simpleMatch(tok->astOperand2(),"%")) { + num = tok->astOperand1(); + modulo = tok->astOperand2(); + } else { + continue; } + + if (Token::Match(modulo->astOperand2(), "%num%") && + MathLib::isLessEqual(modulo->astOperand2()->str(),num->str())) + moduloAlwaysTrueFalseError(tok, modulo->astOperand2()->str()); } } }