From 152ea116faae11bd646f5bc305105a00cfad4c20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 27 May 2015 20:25:58 +0200 Subject: [PATCH 1/3] Fixed #6710 (valueFlowBeforeCondition: function call in loop seems to cause FN) --- lib/valueflow.cpp | 2 +- test/testvalueflow.cpp | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 8cfeda710..58b2047bf 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -697,7 +697,7 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, SymbolDatabase *symbo const Token *vartok = Token::findmatch(tok2->link(), "%varid%", tok2, varid); while (Token::Match(vartok, "%name% = %num% ;") && !vartok->tokAt(2)->getValue(num)) vartok = Token::findmatch(vartok->next(), "%varid%", tok2, varid); - if (vartok) { + if (isVariableChanged(tok2->link(), tok2, varid) && vartok) { if (settings->debugwarnings) { std::string errmsg = "variable "; if (var) diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index b70580dc1..0d81696f1 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -522,18 +522,21 @@ private: ASSERT_EQUALS(false, testValueOfX(code, 4U, 0)); ASSERT_EQUALS(false, testValueOfX(code, 5U, 0)); - bailout("void f(int x) {\n" - " if (x != 123) { b = x; }\n" - " if (x == 123) {}\n" - "}"); - ASSERT_EQUALS("[test.cpp:2]: (debug) ValueFlow bailout: variable x stopping on }\n", errout.str()); - code = "void f(int x) {\n" " a = x;\n" " if (abc) { x = 1; }\n" // <- condition must be false if x is 7 in next line " if (x == 7) { }\n" "}"; ASSERT_EQUALS(true, testValueOfX(code, 2U, 7)); + + code = "void dostuff(const int *x);\n" // #6710 + "void f(const int *x) {\n" + " for (int i = *x; i > 0; --i) {\n" + " dostuff(x);\n" + " }\n" + " if (x) {}\n" + "}"; + ASSERT_EQUALS(true, testValueOfX(code, 3U, 0)); } void valueFlowBeforeConditionGlobalVariables() { From 0836bf3d33d0860a39890a498ee2bf76226e78cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 27 May 2015 20:26:26 +0200 Subject: [PATCH 2/3] astyle formatting --- lib/checkother.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index a8aaa33f4..23172e2d8 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2221,7 +2221,7 @@ void CheckOther::checkDuplicateExpression() if (assignment) selfAssignmentError(tok, tok->astOperand1()->expressionString()); else { - if (_tokenizer->isCPP() && _settings->standards.cpp==Standards::CPP11 && tok->str() == "==") { + if (_tokenizer->isCPP() && _settings->standards.cpp==Standards::CPP11 && tok->str() == "==") { const Token* parent = tok->astParent(); while (parent && parent->astParent()) { parent = parent->astParent(); From 22c608dd2c2245ac865cc75b75c22074cb70c0c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 27 May 2015 20:50:03 +0200 Subject: [PATCH 3/3] Reverted fix for #6710. It didnt work as I wanted. --- lib/valueflow.cpp | 2 +- test/testvalueflow.cpp | 15 ++++++--------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 58b2047bf..8cfeda710 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -697,7 +697,7 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, SymbolDatabase *symbo const Token *vartok = Token::findmatch(tok2->link(), "%varid%", tok2, varid); while (Token::Match(vartok, "%name% = %num% ;") && !vartok->tokAt(2)->getValue(num)) vartok = Token::findmatch(vartok->next(), "%varid%", tok2, varid); - if (isVariableChanged(tok2->link(), tok2, varid) && vartok) { + if (vartok) { if (settings->debugwarnings) { std::string errmsg = "variable "; if (var) diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 0d81696f1..b70580dc1 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -522,21 +522,18 @@ private: ASSERT_EQUALS(false, testValueOfX(code, 4U, 0)); ASSERT_EQUALS(false, testValueOfX(code, 5U, 0)); + bailout("void f(int x) {\n" + " if (x != 123) { b = x; }\n" + " if (x == 123) {}\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (debug) ValueFlow bailout: variable x stopping on }\n", errout.str()); + code = "void f(int x) {\n" " a = x;\n" " if (abc) { x = 1; }\n" // <- condition must be false if x is 7 in next line " if (x == 7) { }\n" "}"; ASSERT_EQUALS(true, testValueOfX(code, 2U, 7)); - - code = "void dostuff(const int *x);\n" // #6710 - "void f(const int *x) {\n" - " for (int i = *x; i > 0; --i) {\n" - " dostuff(x);\n" - " }\n" - " if (x) {}\n" - "}"; - ASSERT_EQUALS(true, testValueOfX(code, 3U, 0)); } void valueFlowBeforeConditionGlobalVariables() {