From 0e2273833e0d1659881d8e2b0a502f5f3917f5ed Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sat, 1 Aug 2009 20:57:54 +0700 Subject: [PATCH] Fixed ticket #539 (Tokenizer: don't replace constants variables with varid 0) http://sourceforge.net/apps/trac/cppcheck/ticket/539 --- src/tokenize.cpp | 6 ++++++ test/testtokenize.cpp | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 604f3cb6b..75bc440b8 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -1400,6 +1400,12 @@ void Tokenizer::simplifyTokenList() if (Token::Match(tok, "const %type% %var% = %num% ;")) { unsigned int varId = tok->tokAt(2)->varId(); + if (varId == 0) + { + tok = tok->tokAt(5); + continue; + } + const char *num = tok->strAt(4); int indent = 1; for (Token *tok2 = tok->tokAt(6); tok2; tok2 = tok2->next()) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index e2a58a21f..dcd16a857 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -98,6 +98,7 @@ private: TEST_CASE(simplifyKnownVariables9); TEST_CASE(simplifyKnownVariables10); TEST_CASE(simplifyKnownVariables11); + TEST_CASE(simplifyKnownVariables12); TEST_CASE(match1); @@ -864,6 +865,25 @@ private: ASSERT_EQUALS(" const int foo = 0 ; int main ( ) { int foo ; foo = 0 ; }", ostr.str()); } + void simplifyKnownVariables12() + { + const char code[] = "ENTER_NAMESPACE(project_namespace)\n" + "const double pi = 3.14;\n" + "int main(){}\n"; + // tokenize.. + OurTokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + tokenizer.setVarId(); + tokenizer.simplifyTokenList(); + + std::ostringstream ostr; + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) + ostr << " " << tok->str(); + ASSERT_EQUALS(" ENTER_NAMESPACE ( project_namespace ) const double pi = 3.14 ; int main ( ) { }", ostr.str()); + } + void match1() { // Match "%var% | %var%"