From 547f120ee906157ea83036455bdf11b0d224464e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 4 Mar 2009 06:03:51 +0000 Subject: [PATCH] known variables: better handling of ++ and -- --- src/tokenize.cpp | 20 ++++++++++++++++---- test/testtokenize.cpp | 4 ++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index d677dadfa..b995a966d 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -1713,20 +1713,32 @@ bool Tokenizer::simplifyKnownVariables() if (Token::Match(tok3->next(), "%varid% ++|--", varid)) { - tok3 = tok3->next(); - const std::string op(tok3->strAt(1)); - if (!Token::Match(tok3->previous(), "; %any% %any% ;")) + const std::string op(tok3->strAt(2)); + if (Token::Match(tok3, "; %any% %any% ;")) { + tok3->deleteNext(); + tok3->deleteNext(); + } + else + { + tok3 = tok3->next(); tok3->str(value.c_str()); tok3->deleteNext(); } incdec(value, op); + tok2->tokAt(2)->str(value.c_str()); } if (Token::Match(tok3->next(), "++|-- %varid%", varid)) { incdec(value, tok3->strAt(1)); - if (!Token::Match(tok3, "; %any% %any% ;")) + tok2->tokAt(2)->str(value.c_str()); + if (Token::Match(tok3, "; %any% %any% ;")) + { + tok3->deleteNext(); + tok3->deleteNext(); + } + else { tok3->deleteNext(); tok3->next()->str(value.c_str()); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 8f0f90466..e75a188ec 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -552,7 +552,7 @@ private: 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 ; abc [ 22 ] = 1 ; abc [ 24 ] = 2 ; }"), ostr.str()); + ASSERT_EQUALS(std::string(" void foo ( ) { int i = 24 ; abc [ 22 ] = 1 ; abc [ 24 ] = 2 ; }"), ostr.str()); } void simplifyKnownVariables8() @@ -574,7 +574,7 @@ private: 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()); + ASSERT_EQUALS(std::string(" void foo ( ) { int i = 23 ; ; abc [ 23 ] = 0 ; }"), ostr.str()); }