From 690c37633b3ecde13c45a54fda75174c0f505906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 17 Feb 2014 20:07:38 +0100 Subject: [PATCH] ValueFlow: Fixed bad values after break/continue --- lib/valueflow.cpp | 6 ++++++ test/testvalueflow.cpp | 19 ++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 68330ec1f..a3ead1317 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -543,6 +543,12 @@ static void valueFlowAfterAssign(TokenList *tokenlist, ErrorLogger *errorLogger, tok2 = tok2->linkAt(2); } + else if (Token::Match(tok2, "break|continue")) { + if (settings->debugwarnings) + bailout(tokenlist, errorLogger, tok2, "variable " + var->nameToken()->str() + ". noreturn conditional scope."); + break; + } + if (tok2->varId() == varid) { // bailout: assignment if (Token::Match(tok2->previous(), "!!* %var% =")) { diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 08697c111..e1a287d86 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -571,7 +571,7 @@ private: code = "void f(int a) {\n" " int x = a;\n" - " b = x;\n" + " b = x;\n" // <- line 3 " if (a!=132) {}\n" "}"; ASSERT_EQUALS(true, testValueOfX(code, 3U, 132)); @@ -581,9 +581,22 @@ private: " if (n) { a = n; }\n" " else { a = 0; }\n" " int x = a;\n" - " if (a > 0) { a = b / x; }\n" + " if (a > 0) { a = b / x; }\n" // <- line 6 "}"; - ASSERT_EQUALS(false, testValueOfX(code, 6U, 0)); + ASSERT_EQUALS(false, testValueOfX(code, 6U, 0)); // x is not 0 at line 6 + + // break + code = "void f() {\n" + " for (;;) {\n" + " int x = 1;\n" + " if (!abc()) {\n" + " x = 2;\n" + " break;\n" + " }\n" + " a = x;\n" // <- line 8 + " }\n" + "}\n"; + ASSERT_EQUALS(false, testValueOfX(code, 8U, 2)); // x is not 2 at line 8 } void valueFlowForLoop() {