diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index e73e9dcd4..addc5f32c 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5048,7 +5048,7 @@ bool Tokenizer::simplifyKnownVariables() } } - if (Token::Match(tok3->next(), "%varid% ++|--", varid) && MathLib::isInt(value)) + if (indentlevel == indentlevel3 && Token::Match(tok3->next(), "%varid% ++|--", varid) && MathLib::isInt(value)) { const std::string op(tok3->strAt(2)); if (Token::Match(tok3, "[{};] %any% %any% ;")) @@ -5066,7 +5066,7 @@ bool Tokenizer::simplifyKnownVariables() ret = true; } - if (Token::Match(tok3->next(), "++|-- %varid%", varid) && MathLib::isInt(value) && + if (indentlevel == indentlevel3 && Token::Match(tok3->next(), "++|-- %varid%", varid) && MathLib::isInt(value) && !Token::Match(tok3->tokAt(3), "[.[]")) { incdec(value, tok3->strAt(1)); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 2c27100cd..706d88a78 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -608,7 +608,8 @@ private: " if (c>0) { c++; }\n" " c++;\n" "}\n"; - ASSERT_EQUALS("void f ( int & c ) { c = 3 ; ; { ; } ; }", tok(code)); + TODO_ASSERT_EQUALS("void f ( int & c ) { c = 3 ; ; { ; } ; }", tok(code)); + ASSERT_EQUALS("void f ( int & c ) { c = 1 ; ; { c ++ ; } c ++ ; }", tok(code)); } @@ -622,7 +623,8 @@ private: " if (c>0) { ++c; }\n" " ++c;\n" "}\n"; - ASSERT_EQUALS("void f ( int & c ) { c = 3 ; ; { ; } ; }", tok(code)); + TODO_ASSERT_EQUALS("void f ( int & c ) { c = 3 ; ; { ; } ; }", tok(code)); + ASSERT_EQUALS("void f ( int & c ) { c = 1 ; ; { ++ c ; } ++ c ; }", tok(code)); } { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 6d78e98ad..b7c941e44 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -100,6 +100,7 @@ private: TEST_CASE(simplifyKnownVariables20); TEST_CASE(simplifyKnownVariables21); TEST_CASE(simplifyKnownVariables22); + TEST_CASE(simplifyKnownVariables23); TEST_CASE(match1); @@ -1079,6 +1080,41 @@ private: simplifyKnownVariables(code)); } + void simplifyKnownVariables23() + { + // This testcase is related to ticket #1596 + const char code[] = "void foo(int x)\n" + "{\n" + " int a[10], c = 0;\n" + " if (x) {\n" + " a[c] = 0;\n" + " c++;\n" + " } else {\n" + " a[c] = 0;\n" + " }\n" + "}\n"; + + // wanted result + TODO_ASSERT_EQUALS( + "void foo ( int x ) " + "{" + " int a [ 10 ] ; int c ; c = 0 ;" + " if ( x ) { a [ 0 ] = 0 ; c = 1 ; }" + " else { a [ 0 ] = 0 ; } " + "}", + simplifyKnownVariables(code)); + + // Current result + ASSERT_EQUALS( + "void foo ( int x ) " + "{" + " int a [ 10 ] ; int c ; c = 0 ;" + " if ( x ) { a [ 0 ] = 0 ; c ++ ; }" + " else { a [ c ] = 0 ; } " + "}", + simplifyKnownVariables(code)); + } + void match1() { // Match "%var% | %var%"