value flow: refactoring. broke out function for skipping value simplifications in expressions.

This commit is contained in:
Daniel Marjamäki 2014-01-11 14:54:10 +01:00
parent 85dcb14813
commit eaf8c83db5
2 changed files with 19 additions and 9 deletions

View File

@ -72,6 +72,16 @@ static bool bailoutFunctionPar(const Token *tok)
return arg && !arg->isConst() && arg->isReference();
}
static const Token * skipValueInConditionalExpression(const Token *tok)
{
while (tok && !Token::Match(tok, "%oror%|&&|?|:")) {
while (Token::Match(tok->astParent(), "%oror%|&&|?") && tok->astParent()->astOperand1() == tok)
tok = tok->astParent();
tok = tok->astParent();
}
return tok;
}
static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLogger, const Settings *settings)
{
for (Token *tok = tokenlist->front(); tok; tok = tok->next()) {
@ -149,15 +159,8 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
break;
}
// skip if variable is conditionally used in ?: expression.
const Token *parent = tok2;
while (parent && !Token::Match(parent, "%oror%|&&|?|:")) {
while (Token::Match(parent->astParent(), "%oror%|&&|?") &&
parent->astParent()->astOperand1() == parent)
parent = parent->astParent();
parent = parent->astParent();
}
if (parent) {
// skip if variable is conditionally used in ?: expression
if (const Token *parent = skipValueInConditionalExpression(tok2)) {
if (settings->debugwarnings)
bailout(tokenlist,
errorLogger,

View File

@ -113,6 +113,13 @@ private:
ASSERT_EQUALS(true, testValueOfX(code, 2U, 0));
ASSERT_EQUALS(false, testValueOfX(code, 3U, 0));
code = "void f(int *x) {\n"
" if ((x=ret())&&\n"
" (*x==0));\n" // <- x is not 0
" if (x==0) {}\n"
"}";
ASSERT_EQUALS(false, testValueOfX(code, 3U, 0));
// function calls
code = "void f(int x) {\n"
" a = x;\n"