From 94c49bc34e26b1602a680f3cf89fdfa3166eeafd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 22 Aug 2009 10:23:55 +0200 Subject: [PATCH] Fixed #608 (Tokenizer: simplifyKnownVariables doesn't handle 'while (--i)' correctly) --- src/tokenize.cpp | 18 ++++++++++++++++++ test/testtokenize.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index c09c73577..f6c2e0971 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -2827,6 +2827,24 @@ bool Tokenizer::simplifyKnownVariables() break; } + // Stop if something like 'while (--var)' is found + if (tok3->str() == "while") + { + const Token *endpar = tok3->next()->link(); + bool bailout = false; + for (const Token *tok4 = tok3; tok4 != endpar; tok4 = tok4->next()) + { + if (Token::Match(tok4, "++|-- %varid%", varid) || + Token::Match(tok4, "%varid% ++|--", varid)) + { + bailout = true; + break; + } + } + if (bailout) + break; + } + if (bailOutFromLoop) { // This could be a loop, skip it, but only if it doesn't contain diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 97f966888..305cc837b 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -78,6 +78,7 @@ private: TEST_CASE(simplifyKnownVariables10); TEST_CASE(simplifyKnownVariables11); TEST_CASE(simplifyKnownVariables12); + TEST_CASE(simplifyKnownVariables13); TEST_CASE(match1); @@ -873,6 +874,29 @@ private: ASSERT_EQUALS(" ENTER_NAMESPACE ( project_namespace ) const double pi = 3.14 ; int main ( ) { }", ostr.str()); } + void simplifyKnownVariables13() + { + const char code[] = "void f()\n" + "{\n" + " int i = 10;\n" + " while(--i) {}\n" + "}\n"; + + // tokenize.. + Tokenizer 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(" void f ( ) { int i ; i = 10 ; while ( -- i ) { } }", ostr.str()); + } + + void match1() { // Match "%var% | %var%"