Fixed #770 (Tokenizer: Var id not set for static variables in some cases)

http://sourceforge.net/apps/trac/cppcheck/ticket/770
This commit is contained in:
Slava Semushin 2009-10-03 17:03:54 +07:00
parent 0448753b13
commit 9983066f62
2 changed files with 30 additions and 6 deletions

View File

@ -2597,10 +2597,11 @@ void Tokenizer::simplifyVarDecl()
Token *type0 = tok; Token *type0 = tok;
if (!Token::Match(type0, "%type%")) if (!Token::Match(type0, "%type%"))
continue; continue;
if (Token::Match(type0, "else|return|static")) if (Token::Match(type0, "else|return"))
continue; continue;
bool isconst = false; bool isconst = false;
bool isstatic = false;
Token *tok2 = type0; Token *tok2 = type0;
unsigned int typelen = 1; unsigned int typelen = 1;
@ -2609,6 +2610,9 @@ void Tokenizer::simplifyVarDecl()
if (tok2->str() == "const") if (tok2->str() == "const")
isconst = true; isconst = true;
else if (tok2->str() == "static")
isstatic = true;
tok2 = tok2->next(); tok2 = tok2->next();
++typelen; ++typelen;
} }
@ -2620,7 +2624,17 @@ void Tokenizer::simplifyVarDecl()
if (Token::Match(tok2, "%type% %var% ,|=")) if (Token::Match(tok2, "%type% %var% ,|="))
{ {
if (tok2->next()->str() != "operator") if (tok2->next()->str() != "operator")
{
tok2 = tok2->tokAt(2); // The ',' or '=' token 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 else
tok2 = NULL; tok2 = NULL;
} }
@ -2628,7 +2642,17 @@ void Tokenizer::simplifyVarDecl()
else if (Token::Match(tok2, "%type% * %var% ,|=")) else if (Token::Match(tok2, "%type% * %var% ,|="))
{ {
if (tok2->tokAt(2)->str() != "operator") if (tok2->tokAt(2)->str() != "operator")
{
tok2 = tok2->tokAt(3); // The ',' token tok2 = tok2->tokAt(3); // The ',' token
if (isstatic && tok2->str() == "=")
{
if (Token::Match(tok2->next(), "%num% ,"))
tok2 = tok2->tokAt(2);
else
tok2 = NULL;
}
}
else else
tok2 = NULL; tok2 = NULL;
} }

View File

@ -2302,27 +2302,27 @@ private:
{ {
const char code[] = "static int a, b;"; 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;"; 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;"; 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;"; 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;"; 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));
} }
} }