From 61365ea0e5035799bdd523184b884204a92d5a5e Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Wed, 17 Oct 2012 00:25:20 +0200 Subject: [PATCH] Fixed #4293 (FP: Variable is not simplified, causing a false positive). --- lib/tokenize.cpp | 11 +++++++++-- test/testtokenize.cpp | 9 +++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index ced2e3d5a..e94fbc09d 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5848,7 +5848,12 @@ bool Tokenizer::simplifyKnownVariables() // constants.. { std::map constantValues; + bool goback = false; for (Token *tok = list.front(); tok; tok = tok->next()) { + if (goback) { + tok = tok->previous(); + goback = false; + } if (tok->isName() && Token::Match(tok, "static| const| static| %type% const| %var% = %any% ;")) { bool isconst = false; for (const Token *tok2 = tok; tok2->str() != "="; tok2 = tok2->next()) { @@ -5878,10 +5883,12 @@ bool Tokenizer::simplifyKnownVariables() constantValues[vartok->varId()] = valuetok->str(); // remove statement - while (tok1->str() != ";") - tok1->deleteThis(); + while (tok1->next()->str() != ";") + tok1->deleteNext(); + tok1->deleteNext(); tok1->deleteThis(); tok = tok1; + goback = true; } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index da09c8ff0..e32424ebc 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -307,6 +307,7 @@ private: TEST_CASE(simplify_constants); TEST_CASE(simplify_constants2); TEST_CASE(simplify_constants3); + TEST_CASE(simplify_constants4); TEST_CASE(simplify_null); TEST_CASE(simplifyMulAndParens); // Ticket #2784 + #3184 @@ -4791,6 +4792,14 @@ private: ASSERT_EQUALS(expected, tokenizeAndStringify(code,true)); } + void simplify_constants4() { + const char code[] = "static const int bSize = 4;\n" + "static const int aSize = 50;\n" + "const int x = bSize;\n" + "const int y = aSize;\n"; + ASSERT_EQUALS("const int x = 4 ;\nconst int y = 50 ;", tokenizeAndStringify(code,true)); + } + void simplify_null() { { const char code[] =