diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 13b7ac82c..88f79730a 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -1687,17 +1687,24 @@ bool Tokenizer::simplifyKnownVariables() if (Token::Match(tok3->next(), "%varid% ++|--", varid)) { tok3 = tok3->next(); - tok3->str(value.c_str()); - incdec(value, tok3->strAt(1)); - tok3->deleteNext(); + const std::string op(tok3->strAt(1)); + if (!Token::Match(tok3->previous(), "; %any% %any% ;")) + { + tok3->str(value.c_str()); + tok3->deleteNext(); + } + incdec(value, op); } if (Token::Match(tok3->next(), "++|-- %varid%", varid)) { incdec(value, tok3->strAt(1)); - tok3->deleteNext(); + if (!Token::Match(tok3, "; %any% %any% ;")) + { + tok3->deleteNext(); + tok3->next()->str(value.c_str()); + } tok3 = tok3->next(); - tok3->str(value.c_str()); } } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index a1401e91f..7ec70d3c1 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -85,6 +85,7 @@ private: TEST_CASE(simplifyKnownVariables5); TEST_CASE(simplifyKnownVariables6); TEST_CASE(simplifyKnownVariables7); + TEST_CASE(simplifyKnownVariables8); TEST_CASE(multiCompare); @@ -557,6 +558,28 @@ private: ASSERT_EQUALS(std::string(" void foo ( ) { int i = 22 ; abc [ 22 ] = 1 ; abc [ 24 ] = 2 ; }"), ostr.str()); } + void simplifyKnownVariables8() + { + const char code[] = "void foo()\n" + "{\n" + " int i = 22;\n" + " i++;\n" + " abc[i] = 0;\n" + "}\n"; + // tokenize.. + OurTokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + tokenizer.setVarId(); + tokenizer.simplifyKnownVariables(); + + std::ostringstream ostr; + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) + ostr << " " << tok->str(); + ASSERT_EQUALS(std::string(" void foo ( ) { int i = 22 ; i ++ ; abc [ 23 ] = 0 ; }"), ostr.str()); + } + void multiCompare()