Fixed #2412 (typedef: struct with inheritance)

This commit is contained in:
Robert Reif 2011-01-05 17:42:55 +01:00 committed by Daniel Marjamäki
parent ed6f683573
commit 157498e694
2 changed files with 100 additions and 50 deletions

View File

@ -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<long>(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> 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")
{
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<long>(count++);
}
tok->tokAt(1)->insertToken(name.c_str());
}
else
continue;
}
else // has a name
{
tok1 = tok->tokAt(3)->link();
Token *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)
else if (Token::Match(tok->next(), "const| struct|class %type% :"))
{
tok1->insertToken("const");
Token *tok1 = tok;
while (tok1 && tok1->str() != ";" && tok1->str() != "{")
tok1 = tok1->next();
if (tok1 && tok1->str() == "{")
{
tok1 = splitDefinitionFromTypedef(tok);
if (!tok1)
continue;
tok = tok1;
}
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 */

View File

@ -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()
{
{