From 77fe9858e2094cfa4257c58def9c35b3e7dda72e Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Sat, 19 Feb 2011 14:18:37 -0500 Subject: [PATCH] fix #2587 (Spurious warning about struct hiding typedef) --- lib/tokenize.cpp | 32 +++++++++++++++++++++----------- test/testsimplifytokens.cpp | 18 ++++++++++++++++++ 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index f1672c027..dca75a1ad 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -837,19 +837,29 @@ static Token *splitDefinitionFromTypedef(Token *tok) tok1->insertToken(";"); tok1 = tok1->next(); - tok1->insertToken("typedef"); - tok1 = tok1->next(); - Token * tok3 = tok1; - if (isConst) + + if (tok1->next()->str() == ";" && tok1 && tok1->previous()->str() == "}") { - tok1->insertToken("const"); - tok1 = tok1->next(); + tok->deleteThis(); + tok1->deleteThis(); + return NULL; + } + else + { + tok1->insertToken("typedef"); + tok1 = tok1->next(); + Token * tok3 = tok1; + if (isConst) + { + tok1->insertToken("const"); + tok1 = tok1->next(); + } + tok1->insertToken(tok->next()->str()); // struct, union or enum + tok1 = tok1->next(); + tok1->insertToken(name.c_str()); + tok->deleteThis(); + tok = tok3; } - tok1->insertToken(tok->next()->str()); // struct, union or enum - tok1 = tok1->next(); - tok1->insertToken(name.c_str()); - tok->deleteThis(); - tok = tok3; return tok; } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index a4c507cd4..85af6da62 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -237,6 +237,7 @@ private: TEST_CASE(simplifyTypedef77); // ticket #2554 TEST_CASE(simplifyTypedef78); // ticket #2568 TEST_CASE(simplifyTypedef79); // ticket #2348 + TEST_CASE(simplifyTypedef80); // ticket #2587 TEST_CASE(simplifyTypedefFunction1); TEST_CASE(simplifyTypedefFunction2); // ticket #1685 @@ -4876,6 +4877,23 @@ private: ASSERT_EQUALS(expected, sizeof_(code)); } + void simplifyTypedef80() // ticket #2587 + { + const char code[] = "typedef struct s { };\n" + "void f() {\n" + " sizeof(struct s);\n" + "};\n"; + const std::string expected = "struct s { } ; " + "void f ( ) { " + "sizeof ( struct s ) ; " + "} ;"; + ASSERT_EQUALS(expected, sizeof_(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + void simplifyTypedefFunction1() { {