Fixed #1338 (simplify typedefs with global and nested namespace)

This commit is contained in:
Robert Reif 2010-01-31 21:46:18 +01:00 committed by Daniel Marjamäki
parent db2aff03c7
commit 64afcc8179
2 changed files with 52 additions and 48 deletions

View File

@ -457,26 +457,37 @@ void Tokenizer::simplifyTypedef()
bool functionRef = false; bool functionRef = false;
bool hasTemplate = false; bool hasTemplate = false;
if (Token::Match(tok->next(), "%type% <") || if (Token::Match(tok->next(), "::") ||
Token::Match(tok->next(), "%type% %type% <") || Token::Match(tok->next(), "%type%"))
Token::Match(tok->next(), "%type% :: %type% <") ||
Token::Match(tok->next(), "%type% %type% :: %type% <"))
{ {
// template
hasTemplate = true;
int level = 1;
typeStart = tok->next(); typeStart = tok->next();
offset = 1;
if (tok->tokAt(2)->str() == "<") typeEnd = tok->tokAt(offset++);
typeEnd = tok->tokAt(3);
else if (tok->tokAt(3)->str() == "<") bool atEnd = false;
typeEnd = tok->tokAt(4); while (!atEnd)
else if (tok->tokAt(4)->str() == "<") {
typeEnd = tok->tokAt(5); if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "::"))
typeEnd = tok->tokAt(offset++);
if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "%type%") &&
tok->tokAt(offset + 1) && !Token::Match(tok->tokAt(offset + 1), "[|;|,"))
typeEnd = tok->tokAt(offset++);
else else
typeEnd = tok->tokAt(6); atEnd = true;
}
}
else
continue; // invalid input
// check for template
if (tok->tokAt(offset)->str() == "<")
{
int level = 1;
int paren = 0; int paren = 0;
hasTemplate = true;
typeEnd = tok->tokAt(offset + 1);
for (; typeEnd ; typeEnd = typeEnd->next()) for (; typeEnd ; typeEnd = typeEnd->next())
{ {
if (typeEnd->str() == ">") if (typeEnd->str() == ">")
@ -509,48 +520,19 @@ void Tokenizer::simplifyTypedef()
} }
tok = typeEnd; tok = typeEnd;
} offset = 1;
else if (Token::Match(tok->next(), "%type%"))
{
typeStart = tok->next();
typeEnd = tok->tokAt(offset++);
if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "%type%") &&
tok->tokAt(offset + 1) && !Token::Match(tok->tokAt(offset + 1), "[|;|,"))
{
typeEnd = tok->tokAt(offset++);
if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "::"))
{
typeEnd = tok->tokAt(offset++);
if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "%type%") &&
tok->tokAt(offset + 1) && !Token::Match(tok->tokAt(offset + 1), "[|;|,"))
{
typeEnd = tok->tokAt(offset++);
}
}
else if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "%type%") &&
tok->tokAt(offset + 1) && !Token::Match(tok->tokAt(offset + 1), "[|;|,"))
{
typeEnd = tok->tokAt(offset++);
}
}
}
else
{
// unhandled typedef, skip it and continue
continue;
} }
// check for pointers and references
while (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "*|&")) while (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "*|&"))
pointers.push_back(tok->tokAt(offset++)->str()); pointers.push_back(tok->tokAt(offset++)->str());
if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "%type%")) if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "%type%"))
{ {
// found the type name
typeName = tok->strAt(offset++); typeName = tok->strAt(offset++);
// check for array
if (tok->tokAt(offset) && tok->tokAt(offset)->str() == "[") if (tok->tokAt(offset) && tok->tokAt(offset)->str() == "[")
{ {
arrayStart = tok->tokAt(offset); arrayStart = tok->tokAt(offset);
@ -574,6 +556,7 @@ void Tokenizer::simplifyTypedef()
arrayEnd = tok->tokAt(offset++); arrayEnd = tok->tokAt(offset++);
} }
// check for end or another
if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), ";|,")) if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), ";|,"))
tok = tok->tokAt(offset); tok = tok->tokAt(offset);
else else

View File

@ -166,6 +166,7 @@ private:
TEST_CASE(simplifyTypedef27); TEST_CASE(simplifyTypedef27);
TEST_CASE(simplifyTypedef28); TEST_CASE(simplifyTypedef28);
TEST_CASE(simplifyTypedef29); TEST_CASE(simplifyTypedef29);
TEST_CASE(simplifyTypedef30);
TEST_CASE(reverseArraySyntax) TEST_CASE(reverseArraySyntax)
TEST_CASE(simplify_numeric_condition) TEST_CASE(simplify_numeric_condition)
@ -3001,6 +3002,26 @@ private:
ASSERT_EQUALS(expected, tok(code, false)); ASSERT_EQUALS(expected, tok(code, false));
} }
void simplifyTypedef30()
{
const char code[] = "typedef ::std::list<int> int_list;\n"
"typedef ::std::list<int>::iterator int_list_iterator;\n"
"typedef ::std::list<int> int_list_array[10];\n"
"int_list il;\n"
"int_list_iterator ili;\n"
"int_list_array ila;";
const char expected[] =
"; "
"; "
"; "
":: std :: list < int > il ; "
":: std :: list < int > :: iterator ili ; "
":: std :: list < int > ila [ 10 ] ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void reverseArraySyntax() void reverseArraySyntax()
{ {
ASSERT_EQUALS("a [ 13 ]", tok("13[a]")); ASSERT_EQUALS("a [ 13 ]", tok("13[a]"));