From df8504f0ea7f92c4326f16abf8750894ebd63a3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 23 Dec 2011 12:13:39 +0100 Subject: [PATCH] Clarify calculation: Fixed false positives when there are various function calls --- lib/checkother.cpp | 14 ++++++++++---- test/testother.cpp | 5 +++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 971eadb28..c9090d38c 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -73,9 +73,9 @@ void CheckOther::clarifyCalculation() return; for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { - if (tok->strAt(1) == "?") { + if (tok->str() == "?" && tok->previous()) { // condition - const Token *cond = tok; + const Token *cond = tok->previous(); if (cond->isName() || cond->isNumber()) cond = cond->previous(); else if (cond->str() == ")") @@ -97,8 +97,14 @@ void CheckOther::clarifyCalculation() cond = cond->previous(); // skip previous multiplications.. - while (cond && cond->strAt(-1) == "*" && (cond->isName() || cond->isNumber())) - cond = cond->tokAt(-2); + 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; diff --git a/test/testother.cpp b/test/testother.cpp index f95a5c5fb..d1b185b62 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3338,6 +3338,11 @@ private: " return x >> ! y ? 8 : 2;\n" "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Clarify calculation precedence for >> and ?\n", errout.str()); + + check("int f() {\n" + " return (shift < sizeof(int64_t)*8 ? 1 : 2);\n" + "}"); + ASSERT_EQUALS("", errout.str()); } // clarify conditions with = and comparison