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;
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;
}

View File

@ -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));
}
}