Robert Reif: Fixed #1108 (Only trivial typedefs are tokenized properly)

This commit is contained in:
Daniel Marjamäki 2009-12-22 19:10:33 +01:00
parent 2e0566bf7a
commit 5b87a51aa5
2 changed files with 79 additions and 1 deletions

View File

@ -400,7 +400,37 @@ void Tokenizer::simplifyTypedef()
const char *typeName = 0;
bool pointer = false;
if (Token::Match(tok->next(), "%type% %type% ;") ||
// pull struct name { ... } out of typedef
if (Token::Match(tok->next(), "struct %type% {"))
{
int structLevel = 1;
Token *tok1 = tok->tokAt(4);
for (; tok1; tok1 = tok1->next())
{
if (tok1->str() == "}")
{
--structLevel;
if (structLevel == 0)
break;
}
if (tok1->str() == "{")
++structLevel;
}
tok1->insertToken(";");
tok1 = tok1->next();
tok1->insertToken("typedef");
tok1 = tok1->next();
Token * tok2 = tok1;
tok1->insertToken("struct");
tok1 = tok1->next();
tok1->insertToken(tok->strAt(2));
tok->deleteThis();
tok = tok2;
}
if (Token::Match(tok->next(), "%type% *| %type% ;") ||
Token::Match(tok->next(), "%type% %type% *| %type% ;"))
{
if (tok->tokAt(3)->str() == ";")
@ -410,6 +440,14 @@ void Tokenizer::simplifyTypedef()
typeName = tok->strAt(2);
tok = tok->tokAt(3);
}
else if (tok->tokAt(2)->str() == "*")
{
pointer = true;
type1 = tok->strAt(1);
type2 = 0;
typeName = tok->strAt(3);
tok = tok->tokAt(4);
}
else
{
pointer = (tok->tokAt(3)->str() == "*");
@ -616,6 +654,9 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[])
// Split up variable declarations.
simplifyVarDecl();
// typedef..
simplifyTypedef();
// Handle templates..
simplifyTemplates();

View File

@ -141,6 +141,7 @@ private:
TEST_CASE(simplifyTypedef5)
TEST_CASE(simplifyTypedef6)
TEST_CASE(simplifyTypedef7);
TEST_CASE(simplifyTypedef8);
TEST_CASE(reverseArraySyntax)
TEST_CASE(simplify_numeric_condition)
@ -2264,6 +2265,42 @@ private:
ASSERT_EQUALS(code, tok(code, false));
}
void simplifyTypedef8()
{
const char code[] = "typedef int INT;\n"
"typedef unsigned int UINT;\n"
"typedef int * PINT;\n"
"typedef unsigned int * PUINT;\n"
"typedef struct s S, * PS\n;"
"typedef struct t { int a; } T, *TP;"
"INT ti;\n"
"UINT tui;\n"
"PINT tpi;\n"
"PUINT tpui;"
"S s;\n"
"PS ps;\n"
"T t;\n"
"TP tp;\n";
const char expected[] =
"typedef int INT ; "
"typedef unsigned int UINT ; "
"typedef int * PINT ; "
"typedef unsigned int * PUINT ; "
"typedef struct s S ; typedef struct s * PS ; "
"struct t { int a ; } ; typedef struct t T ; typedef struct t * TP ; "
"int ti ; "
"unsigned int tui ; "
"int * tpi ; "
"unsigned int * tpui ; "
"struct s s ; "
"struct s * ps ; "
"struct t t ; "
"struct t * tp ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void reverseArraySyntax()
{
ASSERT_EQUALS("a [ 13 ]", tok("13[a]"));