Fixed #1250 (tokenize typedef of union)

This commit is contained in:
Robert Reif 2010-01-11 22:58:57 +01:00 committed by Daniel Marjamäki
parent d3b7f5931f
commit 7b6bf80057
2 changed files with 38 additions and 10 deletions

View File

@ -394,10 +394,10 @@ void Tokenizer::simplifyTypedef()
else if (tok->str() != "typedef") else if (tok->str() != "typedef")
continue; continue;
// pull struct or enum definition out of typedef // pull struct, union or enum definition out of typedef
// use typedef name for unnamed struct or enum // use typedef name for unnamed struct, union or enum
if (Token::Match(tok->next(), "struct|enum %type% {") || if (Token::Match(tok->next(), "struct|enum|union %type% {") ||
Token::Match(tok->next(), "struct|enum {")) Token::Match(tok->next(), "struct|enum|union {"))
{ {
Token *tok1; Token *tok1;
std::string name; std::string name;
@ -436,7 +436,7 @@ void Tokenizer::simplifyTypedef()
tok1->insertToken("typedef"); tok1->insertToken("typedef");
tok1 = tok1->next(); tok1 = tok1->next();
Token * tok3 = tok1; Token * tok3 = tok1;
tok1->insertToken(tok->next()->strAt(0)); // struct or enum tok1->insertToken(tok->next()->strAt(0)); // struct, union or enum
tok1 = tok1->next(); tok1 = tok1->next();
tok1->insertToken(name.c_str()); tok1->insertToken(name.c_str());
tok->deleteThis(); tok->deleteThis();

View File

@ -149,6 +149,7 @@ private:
TEST_CASE(simplifyTypedef12); TEST_CASE(simplifyTypedef12);
TEST_CASE(simplifyTypedef13); TEST_CASE(simplifyTypedef13);
TEST_CASE(simplifyTypedef14); TEST_CASE(simplifyTypedef14);
TEST_CASE(simplifyTypedef15);
TEST_CASE(reverseArraySyntax) TEST_CASE(reverseArraySyntax)
TEST_CASE(simplify_numeric_condition) TEST_CASE(simplify_numeric_condition)
@ -2355,6 +2356,34 @@ private:
} }
void simplifyTypedef10() void simplifyTypedef10()
{
const char code[] = "typedef union s S, * PS\n;"
"typedef union t { int a; float b ; } T, *TP;\n"
"typedef union { int a; float b; } U;\n"
"typedef union { int a; float b; } * V;\n"
"S s;\n"
"PS ps;\n"
"T t;\n"
"TP tp;\n"
"U u;\n"
"V v;";
const char expected[] =
"typedef union s S ; typedef union s * PS ; "
"union t { int a ; float b ; } ; typedef union t T ; typedef union t * TP ; "
"union U { int a ; float b ; } ; typedef union U U ; "
"union Unnamed1 { int a ; float b ; } ; typedef union Unnamed1 * V ; "
"union s s ; "
"union s * ps ; "
"union t t ; "
"union t * tp ; "
"union U u ; "
"union Unnamed1 * v ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void simplifyTypedef11()
{ {
const char code[] = "typedef enum { a = 0 , b = 1 , c = 2 } abc;\n" const char code[] = "typedef enum { a = 0 , b = 1 , c = 2 } abc;\n"
"typedef enum xyz { x = 0 , y = 1 , z = 2 } XYZ;\n" "typedef enum xyz { x = 0 , y = 1 , z = 2 } XYZ;\n"
@ -2370,7 +2399,7 @@ private:
ASSERT_EQUALS(expected, tok(code, false)); ASSERT_EQUALS(expected, tok(code, false));
} }
void simplifyTypedef11() void simplifyTypedef12()
{ {
const char code[] = "typedef vector<int> V1;\n" const char code[] = "typedef vector<int> V1;\n"
"typedef std::vector<int> V2;\n" "typedef std::vector<int> V2;\n"
@ -2390,7 +2419,7 @@ private:
ASSERT_EQUALS(expected, tok(code, false)); ASSERT_EQUALS(expected, tok(code, false));
} }
void simplifyTypedef12() void simplifyTypedef13()
{ {
// ticket # 1167 // ticket # 1167
const char code[] = "typedef std::pair<int(*)(void*), void*> Func;" const char code[] = "typedef std::pair<int(*)(void*), void*> Func;"
@ -2409,7 +2438,7 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void simplifyTypedef13() void simplifyTypedef14()
{ {
// ticket # 1232 // ticket # 1232
const char code[] = "template <typename F, unsigned int N> struct E" const char code[] = "template <typename F, unsigned int N> struct E"
@ -2436,7 +2465,7 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void simplifyTypedef14() void simplifyTypedef15()
{ {
{ {
const char code[] = "typedef char frame[10];\n" const char code[] = "typedef char frame[10];\n"
@ -2466,7 +2495,6 @@ private:
ASSERT_EQUALS("a [ 13 ]", tok("13[a]")); ASSERT_EQUALS("a [ 13 ]", tok("13[a]"));
} }
void simplify_numeric_condition() void simplify_numeric_condition()
{ {
{ {