value flow: improved analysis of expressions below ?, && and || operators

This commit is contained in:
Daniel Marjamäki 2014-01-11 12:44:55 +01:00
parent 60348da1b5
commit 02b92efd1a
3 changed files with 15 additions and 11 deletions

View File

@ -755,7 +755,7 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
if (_settings->valueFlow) {
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (!tok->isName() || !tok->values.empty())
if (!tok->isName() || tok->values.empty())
continue;
const Variable *var = tok->variable();

View File

@ -116,13 +116,17 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
// skip if variable is conditionally used in ?: expression.
const Token *parent = tok2->astParent();
while (parent && !Token::Match(parent, "%oror%|&&?|:"))
while (parent && !Token::Match(parent, "%oror%|&&|?|:")) {
while (Token::Match(parent->astParent(), "%oror%|&&|?") &&
parent->astParent()->astOperand1() == parent)
parent = parent->astParent();
parent = parent->astParent();
}
if (parent) {
if (settings->debugwarnings)
bailout(tokenlist,
errorLogger,
tok2,
bailout(tokenlist,
errorLogger,
tok2,
"no simplification of " + tok2->str() + " within " + (Token::Match(parent,"[?:]") ? "?:" : parent->str()) + " expression");
continue;
}

View File

@ -105,12 +105,12 @@ private:
ASSERT_EQUALS(true, testValueOfX(code, 3U, 123));
// guarding by &&
bailout("void f(int x) {\n"
" if (!x || \n" // <- x can be 0
" a/x) {}\n" // <- x can't be 0
" if (x==0) {}\n"
"}");
TODO_ASSERT_EQUALS(true, false, testValueOfX(code, 2U, 0));
code = "void f(int x) {\n"
" if (!x || \n" // <- x can be 0
" a/x) {}\n" // <- x can't be 0
" if (x==0) {}\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 2U, 0));
ASSERT_EQUALS(false, testValueOfX(code, 3U, 0));
// bailout: ?: