Clarify calculation: Fixed false positives when there are various function calls

This commit is contained in:
Daniel Marjamäki 2011-12-23 12:13:39 +01:00
parent ba5558748d
commit df8504f0ea
2 changed files with 15 additions and 4 deletions

View File

@ -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;

View File

@ -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