From 31b576de3b766ce540ba7a3fbec31af5c1306ceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 1 Dec 2011 17:07:55 +0100 Subject: [PATCH] Fixed #3334 (Test for same expression on both sides of '&' reports false positive) --- lib/checkother.cpp | 28 ++++++++++++++++++++++++++++ test/testother.cpp | 5 +++++ 2 files changed, 33 insertions(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index b848dfd2d..d047516d0 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2544,6 +2544,34 @@ void CheckOther::checkExpressionRange(const std::list &constFunctions, std::map::const_iterator it = expressions.getMap().begin(); const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); for (; it != expressions.getMap().end(); ++it) { + // check expression.. + bool valid = true; + unsigned int parantheses = 0; // () + unsigned int brackets = 0; // [] + const std::string &expr = it->first; + for (std::string::size_type pos = 0; pos < expr.size(); ++pos) { + if (expr[pos] == '(') { + ++parantheses; + } else if (expr[pos] == ')') { + if (parantheses == 0) { + valid = false; + break; + } + --parantheses; + } else if (expr[pos] == '[') { + ++brackets; + } else if (expr[pos] == ']') { + if (brackets == 0) { + valid = false; + break; + } + --brackets; + } + } + + if (!valid || parantheses!=0 || brackets!=0) + continue; + if (it->second.count > 1 && (it->first.find("(") == std::string::npos || !inconclusiveFunctionCall(symbolDatabase, constFunctions, it->second))) { diff --git a/test/testother.cpp b/test/testother.cpp index 196bb3757..6aed31d2e 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3672,6 +3672,11 @@ private: "}"); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '&'.\n", errout.str()); + check("void foo() {\n" + " if (a1[a2[c & 0xff] & 0xff]) {}\n" + "}"); + ASSERT_EQUALS("", errout.str()); + check("void d(const char f, int o, int v)\n" "{\n" " if (((f=='R') && (o == 1) && ((v < 2) || (v > 99))) ||\n"