diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 42e21d415..5747b8632 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -179,50 +179,21 @@ void CheckOther::clarifyCalculation() 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 (tok->str() == "?") { - // condition - const Token *cond = tok->previous(); - if (cond->isName() || cond->isNumber()) - cond = cond->previous(); - else if (cond->str() == ")") - cond = cond->link()->previous(); - else - continue; + // ? operator where lhs is arithmetical expression + if (tok->str() != "?" || !tok->astOperand1() || !tok->astOperand1()->isArithmeticalOp()) + continue; - if (cond && cond->str() == "!") - cond = cond->previous(); - - if (!cond) - continue; - - // calculation - if (!cond->isArithmeticalOp()) - continue; - - const std::string &op = cond->str(); - cond = cond->previous(); - - // skip previous multiplications.. - while (cond && cond->previous()) { - if ((cond->isName() || cond->isNumber()) && cond->previous()->str() == "*") - cond = cond->tokAt(-2); - else if (cond->str() == ")") - cond = cond->link()->previous(); - else - break; - } - - if (!cond) - continue; - - // first multiplication operand - if (cond->str() == ")") { - clarifyCalculationError(cond, op); - } else if (cond->isName() || cond->isNumber()) { - if (Token::Match(cond->previous(),"return|=|+|-|,|(") || cond->strAt(-1) == op) - clarifyCalculationError(cond, op); - } + // Is code clarified by parentheses already? + const Token *tok2 = tok->astOperand1(); + for (; tok2; tok2 = tok2->next()) { + if (tok2->str() == "(") + tok2 = tok2->link(); + else if (tok2->str() == ")" || tok2->str() == "?") + break; } + + if (tok2 && tok2->str() == "?") + clarifyCalculationError(tok, tok->astOperand1()->str()); } } } diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 966e9bc26..3d80ebf9c 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -667,7 +667,7 @@ static void compileExpression(Token *&tok, std::stack &op) void TokenList::createAst() { for (Token *tok = _front; tok; tok = tok ? tok->next() : NULL) { - if (tok->str() == "return" || !tok->previous() || Token::Match(tok, "%var% (|[|.|=") || Token::Match(tok->previous(), "[;{}] %cop%")) { + if (tok->str() == "return" || !tok->previous() || Token::Match(tok, "%var% (|[|.|=|::") || Token::Match(tok->previous(), "[;{}] %cop%")) { std::stack operands; compileExpression(tok, operands); } diff --git a/test/testother.cpp b/test/testother.cpp index e62d668d7..482f1a2c7 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4138,6 +4138,11 @@ private: "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Clarify calculation precedence for '*' and '?'.\n", errout.str()); + check("void f() {\n" + " return (2*a)?b:c;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + // Ticket #2585 - segmentation fault for invalid code check("abcdef?""?<" "123456?""?>"