CheckCondition::multiCondition2: Fix FN for pointers

This commit is contained in:
Daniel Marjamäki 2017-09-04 15:58:22 +02:00
parent 095e435031
commit d838dc2129
2 changed files with 15 additions and 4 deletions

View File

@ -494,9 +494,13 @@ void CheckCondition::multiCondition2()
if (cond->varId()) {
vars.insert(cond->varId());
const Variable *var = cond->variable();
nonlocal |= (var && (!var->isLocal() || var->isStatic()) && !var->isArgument());
// TODO: if var is pointer check what it points at
nonlocal |= (var && (var->isPointer() || var->isReference()));
if (!nonlocal && var) {
if (!(var->isLocal() || var->isStatic() || var->isArgument()))
nonlocal = true;
else if ((var->isPointer() || var->isReference()) && !Token::simpleMatch(cond->astParent(), "!"))
// TODO: if var is pointer check what it points at
nonlocal = true;
}
} else if (!nonlocal && cond->isName()) {
// varid is 0. this is possibly a nonlocal variable..
nonlocal = Token::Match(cond->astParent(), "%cop%|(|[");
@ -534,7 +538,6 @@ void CheckCondition::multiCondition2()
sameConditionAfterEarlyExitError(cond1, cond2);
}
}
}
if (Token::Match(tok, "%type% (") && nonlocal) // function call -> bailout if there are nonlocal variables
break;

View File

@ -1452,6 +1452,7 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:7]: (warning) Opposite inner 'if' condition leads to a dead code block.\n", errout.str());
// pointers...
check("void f(struct ABC *abc) {\n"
" struct AB *ab = abc->ab;\n"
" if (ab->a == 123){\n"
@ -1463,6 +1464,13 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
check("void f(const int *i) {\n"
" if (!i) return;\n"
" if (!num1tok) { *num1 = *num2; }\n"
" if (!i) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Same condition, second condition is always false\n", errout.str());
{
// #6095 - calling member function that might change the state
check("void f() {\n"