diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 2dddc9c23..0f7c3a937 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4949,16 +4949,35 @@ bool Tokenizer::simplifyKnownVariables() const bool pointeralias(tok2->tokAt(2)->isName() || tok2->tokAt(2)->str() == "&"); - std::string value(tok2->strAt(2)); - if (value == "]") - value = tok2->strAt(4); - else if (value == "&") - value = tok2->strAt(3); + std::string value; + + Token *tok3 = NULL; + + if (Token::Match(tok2->tokAt(-2), "for ( %varid% = %num% ; %varid% <|<= %num% ; ++| %varid% ++| ) {", varid)) + { + const std::string compareop = tok2->strAt(5); + if (compareop == "<") + value = tok2->strAt(6); + else + value = MathLib::toString(MathLib::toLongNumber(tok2->strAt(6)) + 1); + + // Skip for-body.. + tok3 = tok2->previous()->link()->next()->link()->next(); + } + else + { + value = tok2->strAt(2); + if (value == "]") + value = tok2->strAt(4); + else if (value == "&") + value = tok2->strAt(3); + if (Token::simpleMatch(tok2->next(), "= &")) + tok2 = tok2->tokAt(3); + tok3 = tok2->next(); + } Token* bailOutFromLoop = 0; - if (Token::simpleMatch(tok2->next(), "= &")) - tok2 = tok2->tokAt(3); int indentlevel3 = indentlevel; // indentlevel for tok3 - for (Token *tok3 = tok2->next(); tok3; tok3 = tok3->next()) + for (; tok3; tok3 = tok3->next()) { if (tok3->str() == "{") { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index bf88ec3b9..301974799 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -101,6 +101,7 @@ private: TEST_CASE(simplifyKnownVariables21); TEST_CASE(simplifyKnownVariables22); TEST_CASE(simplifyKnownVariables23); + TEST_CASE(simplifyKnownVariables24); TEST_CASE(match1); @@ -1145,6 +1146,28 @@ private: simplifyKnownVariables(code)); } + void simplifyKnownVariables24() + { + // This testcase is related to ticket #1596 + const char code[] = "void foo()\n" + "{\n" + " int c;\n" + " for (c=0;c<10;++c) { }\n" + " a[c] = 0;\n" + "}\n"; + + // Current result + ASSERT_EQUALS( + "void foo ( ) " + "{" + " int c ;" + " for ( c = 0 ; c < 10 ; ++ c ) { }" + " a [ 10 ] = 0 ; " + "}", + simplifyKnownVariables(code)); + } + + void match1() { // Match "%var% | %var%"