From 7b6bf8005797eccaed602e78692412ae9bfc80f4 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Mon, 11 Jan 2010 22:58:57 +0100 Subject: [PATCH] Fixed #1250 (tokenize typedef of union) --- lib/tokenize.cpp | 10 +++++----- test/testsimplifytokens.cpp | 38 ++++++++++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 6c4eb6a3a..663dafd54 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -394,10 +394,10 @@ void Tokenizer::simplifyTypedef() else if (tok->str() != "typedef") continue; - // pull struct or enum definition out of typedef - // use typedef name for unnamed struct or enum - if (Token::Match(tok->next(), "struct|enum %type% {") || - Token::Match(tok->next(), "struct|enum {")) + // pull struct, union or enum definition out of typedef + // use typedef name for unnamed struct, union or enum + if (Token::Match(tok->next(), "struct|enum|union %type% {") || + Token::Match(tok->next(), "struct|enum|union {")) { Token *tok1; std::string name; @@ -436,7 +436,7 @@ void Tokenizer::simplifyTypedef() tok1->insertToken("typedef"); tok1 = tok1->next(); 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->insertToken(name.c_str()); tok->deleteThis(); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 72f205bf4..6f75d04fb 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -149,6 +149,7 @@ private: TEST_CASE(simplifyTypedef12); TEST_CASE(simplifyTypedef13); TEST_CASE(simplifyTypedef14); + TEST_CASE(simplifyTypedef15); TEST_CASE(reverseArraySyntax) TEST_CASE(simplify_numeric_condition) @@ -2355,6 +2356,34 @@ private: } 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" "typedef enum xyz { x = 0 , y = 1 , z = 2 } XYZ;\n" @@ -2370,7 +2399,7 @@ private: ASSERT_EQUALS(expected, tok(code, false)); } - void simplifyTypedef11() + void simplifyTypedef12() { const char code[] = "typedef vector V1;\n" "typedef std::vector V2;\n" @@ -2390,7 +2419,7 @@ private: ASSERT_EQUALS(expected, tok(code, false)); } - void simplifyTypedef12() + void simplifyTypedef13() { // ticket # 1167 const char code[] = "typedef std::pair Func;" @@ -2409,7 +2438,7 @@ private: ASSERT_EQUALS("", errout.str()); } - void simplifyTypedef13() + void simplifyTypedef14() { // ticket # 1232 const char code[] = "template struct E" @@ -2436,7 +2465,7 @@ private: ASSERT_EQUALS("", errout.str()); } - void simplifyTypedef14() + void simplifyTypedef15() { { const char code[] = "typedef char frame[10];\n" @@ -2466,7 +2495,6 @@ private: ASSERT_EQUALS("a [ 13 ]", tok("13[a]")); } - void simplify_numeric_condition() { {