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) { if (_settings->valueFlow) {
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (!tok->isName() || !tok->values.empty()) if (!tok->isName() || tok->values.empty())
continue; continue;
const Variable *var = tok->variable(); 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. // skip if variable is conditionally used in ?: expression.
const Token *parent = tok2->astParent(); 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(); parent = parent->astParent();
}
if (parent) { if (parent) {
if (settings->debugwarnings) if (settings->debugwarnings)
bailout(tokenlist, bailout(tokenlist,
errorLogger, errorLogger,
tok2, tok2,
"no simplification of " + tok2->str() + " within " + (Token::Match(parent,"[?:]") ? "?:" : parent->str()) + " expression"); "no simplification of " + tok2->str() + " within " + (Token::Match(parent,"[?:]") ? "?:" : parent->str()) + " expression");
continue; continue;
} }

View File

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