Robert Reif: Fixed #1161 (add tokenize support for typedefs with enum definitions)

This commit is contained in:
Daniel Marjamäki 2009-12-28 17:57:52 +01:00
parent 6a31fe6403
commit 28f2bf2150
2 changed files with 55 additions and 9 deletions

View File

@ -429,6 +429,48 @@ void Tokenizer::simplifyTypedef()
tok->deleteThis();
tok = tok2;
}
else if (Token::Match(tok->next(), "enum %type% {") ||
Token::Match(tok->next(), "enum {"))
{
Token *tok1;
Token *tok2 = 0;
if (tok->tokAt(2)->str() == "{")
tok1 = tok->tokAt(3);
else
{
tok1 = tok->tokAt(4);
tok2 = tok->tokAt(2);
}
for (; tok1; tok1 = tok1->next())
{
if (tok1->str() == "}")
break;
}
if (tok2 == 0)
{
if (Token::Match(tok1->next(), "%type%"))
{
tok2 = tok1->next();
tok->tokAt(1)->insertToken(tok2->strAt(0));
}
else
continue;
}
tok1->insertToken(";");
tok1 = tok1->next();
tok1->insertToken("typedef");
tok1 = tok1->next();
Token * tok3 = tok1;
tok1->insertToken("enum");
tok1 = tok1->next();
tok1->insertToken(tok2->strAt(0));
tok->deleteThis();
tok = tok3;
}
if (Token::Match(tok->next(), "%type% *| %type% ;") ||
Token::Match(tok->next(), "%type% %type% *| %type% ;"))
@ -464,7 +506,6 @@ void Tokenizer::simplifyTypedef()
{
typeName = tok->strAt(3);
tok = tok->tokAt(4);
}
}
@ -506,7 +547,9 @@ void Tokenizer::simplifyTypedef()
else if (Token::Match(tok2->tokAt(-2), "!!typedef") &&
Token::Match(tok2->tokAt(-3), "!!typedef"))
{
simplifyType = true;
// Check for enum and typedef with same name.
if (tok2->tokAt(-1)->str() != type1)
simplifyType = true;
}
else
{

View File

@ -1780,11 +1780,6 @@ private:
ASSERT_EQUALS("void f ( ) { for ( int a , b ; a < 10 ; a = a + 1 , b = b + 1 ) { ; } }", sizeof_(code));
}
{
const char code[] = "typedef enum { a = 0 , b = 1 , c = 2 } abc ;";
ASSERT_EQUALS(code, sizeof_(code));
}
{
const char code[] = "void f()\n"
"{\n"
@ -2297,6 +2292,8 @@ private:
"typedef unsigned int * PUINT;\n"
"typedef struct s S, * PS\n;"
"typedef struct t { int a; } T, *TP;"
"typedef enum { a = 0 , b = 1 , c = 2 } abc;"
"typedef enum xyz { a = 0 , b = 1 , c = 2 } ABC;"
"INT ti;\n"
"UINT tui;\n"
"PINT tpi;\n"
@ -2304,7 +2301,9 @@ private:
"S s;\n"
"PS ps;\n"
"T t;\n"
"TP tp;\n";
"TP tp;\n"
"abc e1;\n"
"ABC e2;";
const char expected[] =
"typedef int INT ; "
@ -2313,6 +2312,8 @@ private:
"typedef unsigned int * PUINT ; "
"typedef struct s S ; typedef struct s * PS ; "
"struct t { int a ; } ; typedef struct t T ; typedef struct t * TP ; "
"enum abc { a = 0 , b = 1 , c = 2 } ; typedef enum abc abc ; "
"enum xyz { a = 0 , b = 1 , c = 2 } ; typedef enum xyz ABC ; "
"int ti ; "
"unsigned int tui ; "
"int * tpi ; "
@ -2320,7 +2321,9 @@ private:
"struct s s ; "
"struct s * ps ; "
"struct t t ; "
"struct t * tp ;";
"struct t * tp ; "
"enum abc e1 ; "
"enum xyz e2 ;";
ASSERT_EQUALS(expected, tok(code, false));
}