From eaf8c83db5c413865d40b45ca37d775f2afc5197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 11 Jan 2014 14:54:10 +0100 Subject: [PATCH] value flow: refactoring. broke out function for skipping value simplifications in expressions. --- lib/valueflow.cpp | 21 ++++++++++++--------- test/testvalueflow.cpp | 7 +++++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 364813fae..f5eac13a9 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -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, diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 4d934207a..df0a49c54 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -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"