From b881718d9f943f4de866bb00a19ee8bf8c0f171b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 7 Nov 2010 10:42:08 +0100 Subject: [PATCH] Tokenizer: better bailout in simplifyKnownVariables when there is loop --- lib/tokenize.cpp | 4 +++- test/testtokenize.cpp | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 5af12facc..c01a765b4 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5961,9 +5961,11 @@ bool Tokenizer::simplifyKnownVariables() break; // Stop if something like 'while (--var)' is found - if (tok3->str() == "while" || tok3->str() == "do") + if (tok3->str() == "for" || tok3->str() == "while" || tok3->str() == "do") { const Token *endpar = tok3->next()->link(); + if (Token::simpleMatch(endpar, ") {")) + endpar = endpar->next()->link(); bool bailout = false; for (const Token *tok4 = tok3; tok4 && tok4 != endpar; tok4 = tok4->next()) { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 7eb4f0b92..cc8eb7aee 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -123,7 +123,8 @@ private: TEST_CASE(simplifyKnownVariables30); TEST_CASE(simplifyKnownVariables31); TEST_CASE(simplifyKnownVariables32); // const - TEST_CASE(simplifyKnownVariablesBailOutFor); + TEST_CASE(simplifyKnownVariablesBailOutFor1); + TEST_CASE(simplifyKnownVariablesBailOutFor2); TEST_CASE(simplifyKnownVariablesBailOutMemberFunction); TEST_CASE(varid1); @@ -1870,7 +1871,7 @@ private: ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); } - void simplifyKnownVariablesBailOutFor() + void simplifyKnownVariablesBailOutFor1() { const char code[] = "void foo() {\n" " for (int i = 0; i < 10; ++i) { }\n" @@ -1881,6 +1882,19 @@ private: ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); } + void simplifyKnownVariablesBailOutFor2() + { + const char code[] = "void foo() {\n" + " int i = 0;\n" + " while (i < 10) { ++i; }\n" + "}\n"; + const char expected[] = "void foo ( ) {\n" + "int i ; i = 0 ;\n" + "while ( i < 10 ) { ++ i ; }\n" + "}"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + } + void simplifyKnownVariablesBailOutMemberFunction() { const char code[] = "void foo(obj a) {\n"