Robert Reif: Fixed #1162 (add support to tokenize typedefs with templates)

This commit is contained in:
Daniel Marjamäki 2009-12-29 07:28:00 +01:00
parent d3d3ad101f
commit 84ce6ba75a
2 changed files with 80 additions and 17 deletions

View File

@ -395,11 +395,6 @@ void Tokenizer::simplifyTypedef()
else if (tok->str() != "typedef") else if (tok->str() != "typedef")
continue; continue;
const char *type1 = 0;
const char *type2 = 0;
const char *typeName = 0;
bool pointer = false;
// pull struct name { ... } out of typedef // pull struct name { ... } out of typedef
if (Token::Match(tok->next(), "struct %type% {")) if (Token::Match(tok->next(), "struct %type% {"))
{ {
@ -472,10 +467,51 @@ void Tokenizer::simplifyTypedef()
tok = tok3; tok = tok3;
} }
if (Token::Match(tok->next(), "%type% *| %type% ;") || const char *type1 = 0;
const char *type2 = 0;
const char *typeName = 0;
bool pointer = false;
Token *start = 0;
Token *end = 0;
if (Token::Match(tok->next(), "%type% <") ||
Token::Match(tok->next(), "%type% :: %type% <") ||
Token::Match(tok->next(), "%type% *| %type% ;") ||
Token::Match(tok->next(), "%type% %type% *| %type% ;")) Token::Match(tok->next(), "%type% %type% *| %type% ;"))
{ {
if (tok->tokAt(3)->str() == ";") if ((tok->tokAt(2)->str() == "<") ||
(tok->tokAt(4) && (tok->tokAt(4)->str() == "<")))
{
int level = 1;
start = tok->next();
if (tok->tokAt(2)->str() == "<")
end = tok->tokAt(3);
else
end = tok->tokAt(5);
for (; end ; end = end->next())
{
if (end->str() == ">")
{
level--;
if (level == 0)
break;
}
if (end->str() == "<")
level++;
}
if (Token::Match(end->next(), "%type% ;"))
{
typeName = end->strAt(1);
tok = end->tokAt(2);
}
else
continue;
}
else if (tok->tokAt(3)->str() == ";")
{ {
type1 = tok->strAt(1); type1 = tok->strAt(1);
type2 = 0; type2 = 0;
@ -548,7 +584,9 @@ void Tokenizer::simplifyTypedef()
Token::Match(tok2->tokAt(-3), "!!typedef")) Token::Match(tok2->tokAt(-3), "!!typedef"))
{ {
// Check for enum and typedef with same name. // Check for enum and typedef with same name.
if (tok2->tokAt(-1)->str() != type1) if (type1 && (tok2->tokAt(-1)->str() != type1))
simplifyType = true;
else if (!type1)
simplifyType = true; simplifyType = true;
} }
else else
@ -560,6 +598,18 @@ void Tokenizer::simplifyTypedef()
} }
if (simplifyType) if (simplifyType)
{
if (start && end)
{
tok2->str(start->str());
Token * nextToken;
for (nextToken = start->next(); nextToken != end->next(); nextToken = nextToken->next())
{
tok2->insertToken(nextToken->strAt(0));
tok2 = tok2->next();
}
}
else
{ {
tok2->str(type1); tok2->str(type1);
if (type2) if (type2)
@ -572,6 +622,7 @@ void Tokenizer::simplifyTypedef()
tok2->insertToken("*"); tok2->insertToken("*");
tok2 = tok2->next(); tok2 = tok2->next();
} }
}
simplifyType = false; simplifyType = false;
} }

View File

@ -2294,6 +2294,9 @@ private:
"typedef struct t { int a; } T, *TP;" "typedef struct t { int a; } T, *TP;"
"typedef enum { a = 0 , b = 1 , c = 2 } abc;" "typedef enum { a = 0 , b = 1 , c = 2 } abc;"
"typedef enum xyz { a = 0 , b = 1 , c = 2 } ABC;" "typedef enum xyz { a = 0 , b = 1 , c = 2 } ABC;"
"typedef vector<int> V1;"
"typedef std::vector<int> V2;"
"typedef std::vector<std::vector<int> > V3;"
"INT ti;\n" "INT ti;\n"
"UINT tui;\n" "UINT tui;\n"
"PINT tpi;\n" "PINT tpi;\n"
@ -2303,7 +2306,10 @@ private:
"T t;\n" "T t;\n"
"TP tp;\n" "TP tp;\n"
"abc e1;\n" "abc e1;\n"
"ABC e2;"; "ABC e2;\n"
"V1 v1;\n"
"V2 v2;\n"
"V3 v3;";
const char expected[] = const char expected[] =
"typedef int INT ; " "typedef int INT ; "
@ -2314,6 +2320,9 @@ private:
"struct t { int a ; } ; typedef struct t T ; typedef struct t * TP ; " "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 abc { a = 0 , b = 1 , c = 2 } ; typedef enum abc abc ; "
"enum xyz { a = 0 , b = 1 , c = 2 } ; typedef enum xyz ABC ; " "enum xyz { a = 0 , b = 1 , c = 2 } ; typedef enum xyz ABC ; "
"typedef vector < int > V1 ; "
"typedef std :: vector < int > V2 ; "
"typedef std :: vector < std :: vector < int > > V3 ; "
"int ti ; " "int ti ; "
"unsigned int tui ; " "unsigned int tui ; "
"int * tpi ; " "int * tpi ; "
@ -2323,7 +2332,10 @@ private:
"struct t t ; " "struct t t ; "
"struct t * tp ; " "struct t * tp ; "
"enum abc e1 ; " "enum abc e1 ; "
"enum xyz e2 ;"; "enum xyz e2 ; "
"vector < int > v1 ; "
"std :: vector < int > v2 ; "
"std :: vector < std :: vector < int > > v3 ;";
ASSERT_EQUALS(expected, tok(code, false)); ASSERT_EQUALS(expected, tok(code, false));
} }