From 157498e6945c31ce37ab0e52083053b7de229759 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Wed, 5 Jan 2011 17:42:55 +0100 Subject: [PATCH] Fixed #2412 (typedef: struct with inheritance) --- lib/tokenize.cpp | 135 +++++++++++++++++++++++------------- test/testsimplifytokens.cpp | 15 ++++ 2 files changed, 100 insertions(+), 50 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 765522e5a..0c630aa4c 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -720,6 +720,78 @@ struct SpaceInfo const Token * classEnd; }; +static Token *splitDefinitionFromTypedef(Token *tok) +{ + Token *tok1; + std::string name; + bool isConst = false; + + if (tok->next()->str() == "const") + { + tok->next()->deleteThis(); + isConst = true; + } + + if (tok->tokAt(2)->str() == "{") // unnamed + { + tok1 = tok->tokAt(2)->link(); + + if (tok1 && tok1->next()) + { + // use typedef name if available + if (Token::Match(tok1->next(), "%type%")) + name = tok1->next()->str(); + else // create a unique name + { + static long count = 0; + name = "Unnamed" + MathLib::toString(count++); + } + tok->tokAt(1)->insertToken(name.c_str()); + } + else + return NULL; + } + else if (tok->strAt(3) == ":") + { + tok1 = tok->tokAt(4); + while (tok1 && tok1->str() != "{") + tok1 = tok1->next(); + if (!tok1) + return NULL; + + tok1 = tok1->link(); + + name = tok->tokAt(2)->str(); + } + else // has a name + { + tok1 = tok->tokAt(3)->link(); + + if (!tok1) + return NULL; + + name = tok->tokAt(2)->str(); + } + + tok1->insertToken(";"); + tok1 = tok1->next(); + tok1->insertToken("typedef"); + tok1 = tok1->next(); + Token * tok3 = tok1; + if (isConst) + { + tok1->insertToken("const"); + tok1 = tok1->next(); + } + tok1->insertToken(tok->next()->strAt(0)); // struct, union or enum + tok1 = tok1->next(); + tok1->insertToken(name.c_str()); + tok->deleteThis(); + tok = tok3; + + return tok; +} + void Tokenizer::simplifyTypedef() { std::vector spaceInfo; @@ -767,60 +839,23 @@ void Tokenizer::simplifyTypedef() if (Token::Match(tok->next(), "const| struct|enum|union|class %type% {") || Token::Match(tok->next(), "const| struct|enum|union|class {")) { - Token *tok1; - std::string name; - bool isConst = false; - - if (tok->next()->str() == "const") + Token *tok1 = splitDefinitionFromTypedef(tok); + if (!tok1) + continue; + tok = tok1; + } + else if (Token::Match(tok->next(), "const| struct|class %type% :")) + { + Token *tok1 = tok; + while (tok1 && tok1->str() != ";" && tok1->str() != "{") + tok1 = tok1->next(); + if (tok1 && tok1->str() == "{") { - tok->next()->deleteThis(); - isConst = true; - } - - if (tok->tokAt(2)->str() == "{") // unnamed - { - tok1 = tok->tokAt(2)->link(); - - if (tok1 && tok1->next()) - { - // use typedef name if available - if (Token::Match(tok1->next(), "%type%")) - name = tok1->next()->str(); - else // create a unique name - { - static long count = 0; - name = "Unnamed" + MathLib::toString(count++); - } - tok->tokAt(1)->insertToken(name.c_str()); - } - else - continue; - } - else // has a name - { - tok1 = tok->tokAt(3)->link(); - + tok1 = splitDefinitionFromTypedef(tok); if (!tok1) continue; - - name = tok->tokAt(2)->str(); + tok = tok1; } - - tok1->insertToken(";"); - tok1 = tok1->next(); - tok1->insertToken("typedef"); - tok1 = tok1->next(); - Token * tok3 = tok1; - if (isConst) - { - tok1->insertToken("const"); - tok1 = tok1->next(); - } - tok1->insertToken(tok->next()->strAt(0)); // struct, union or enum - tok1 = tok1->next(); - tok1->insertToken(name.c_str()); - tok->deleteThis(); - tok = tok3; } /** @todo add support for struct and union */ diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index f57bde0f4..cb4256620 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -230,6 +230,7 @@ private: TEST_CASE(simplifyTypedef70); // ticket #2348 TEST_CASE(simplifyTypedef71); // ticket #2348 TEST_CASE(simplifyTypedef72); // ticket #2375 + TEST_CASE(simplifyTypedef73); // ticket #2412 TEST_CASE(simplifyTypedefFunction1); TEST_CASE(simplifyTypedefFunction2); // ticket #1685 @@ -4757,6 +4758,20 @@ private: } } + void simplifyTypedef73() // ticket #2412 + { + const char code[] = "struct B {};\n" + "typedef struct A : public B {\n" + " void f();\n" + "} a, *aPtr;\n"; + const std::string expected = "struct B { } ; " + "struct A : public B { " + "void f ( ) ; " + "} ;"; + ASSERT_EQUALS(expected, sizeof_(code)); + ASSERT_EQUALS("", errout.str()); + } + void simplifyTypedefFunction1() { {