From 9983066f62e3738be2f9ae65df624c3a47cf8775 Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sat, 3 Oct 2009 17:03:54 +0700 Subject: [PATCH] Fixed #770 (Tokenizer: Var id not set for static variables in some cases) http://sourceforge.net/apps/trac/cppcheck/ticket/770 --- src/tokenize.cpp | 26 +++++++++++++++++++++++++- test/testtokenize.cpp | 10 +++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 7a81c4844..beec1a731 100755 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -2597,10 +2597,11 @@ void Tokenizer::simplifyVarDecl() Token *type0 = tok; if (!Token::Match(type0, "%type%")) continue; - if (Token::Match(type0, "else|return|static")) + if (Token::Match(type0, "else|return")) continue; bool isconst = false; + bool isstatic = false; Token *tok2 = type0; unsigned int typelen = 1; @@ -2609,6 +2610,9 @@ void Tokenizer::simplifyVarDecl() if (tok2->str() == "const") isconst = true; + else if (tok2->str() == "static") + isstatic = true; + tok2 = tok2->next(); ++typelen; } @@ -2620,7 +2624,17 @@ void Tokenizer::simplifyVarDecl() if (Token::Match(tok2, "%type% %var% ,|=")) { if (tok2->next()->str() != "operator") + { tok2 = tok2->tokAt(2); // The ',' or '=' token + + if (isstatic && tok2->str() == "=") + { + if (Token::Match(tok2->next(), "%num% ,")) + tok2 = tok2->tokAt(2); + else + tok2 = NULL; + } + } else tok2 = NULL; } @@ -2628,7 +2642,17 @@ void Tokenizer::simplifyVarDecl() else if (Token::Match(tok2, "%type% * %var% ,|=")) { if (tok2->tokAt(2)->str() != "operator") + { tok2 = tok2->tokAt(3); // The ',' token + + if (isstatic && tok2->str() == "=") + { + if (Token::Match(tok2->next(), "%num% ,")) + tok2 = tok2->tokAt(2); + else + tok2 = NULL; + } + } else tok2 = NULL; } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index bc7a7fa8d..0e7b4d904 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -2302,27 +2302,27 @@ private: { const char code[] = "static int a, b;"; - TODO_ASSERT_EQUALS("static int a ; static int b ;", tokenizeAndStringify(code)); + ASSERT_EQUALS("static int a ; static int b ;", tokenizeAndStringify(code)); } { const char code[] = "static unsigned int a, b;"; - TODO_ASSERT_EQUALS("static unsigned int a ; static unsigned int b ;", tokenizeAndStringify(code)); + ASSERT_EQUALS("static unsigned int a ; static unsigned int b ;", tokenizeAndStringify(code)); } { const char code[] = "static int a=1, b=1;"; - TODO_ASSERT_EQUALS("static int a = 1 ; static int b = 1 ;", tokenizeAndStringify(code)); + ASSERT_EQUALS("static int a = 1 ; static int b = 1 ;", tokenizeAndStringify(code)); } { const char code[] = "static int *a, *b;"; - TODO_ASSERT_EQUALS("static int * a ; static int * b ;", tokenizeAndStringify(code)); + ASSERT_EQUALS("static int * a ; static int * b ;", tokenizeAndStringify(code)); } { const char code[] = "static unsigned int *a=0, *b=0;"; - TODO_ASSERT_EQUALS("static unsigned int * a = 0 ; static unsigned int * b = 0 ;", tokenizeAndStringify(code)); + ASSERT_EQUALS("static unsigned int * a = 0 ; static unsigned int * b = 0 ;", tokenizeAndStringify(code)); } }