Fixed #1338 (simplify typedefs with global and nested namespace)
This commit is contained in:
parent
db2aff03c7
commit
64afcc8179
|
@ -457,26 +457,37 @@ void Tokenizer::simplifyTypedef()
|
|||
bool functionRef = false;
|
||||
bool hasTemplate = false;
|
||||
|
||||
if (Token::Match(tok->next(), "%type% <") ||
|
||||
Token::Match(tok->next(), "%type% %type% <") ||
|
||||
Token::Match(tok->next(), "%type% :: %type% <") ||
|
||||
Token::Match(tok->next(), "%type% %type% :: %type% <"))
|
||||
if (Token::Match(tok->next(), "::") ||
|
||||
Token::Match(tok->next(), "%type%"))
|
||||
{
|
||||
// template
|
||||
hasTemplate = true;
|
||||
int level = 1;
|
||||
typeStart = tok->next();
|
||||
offset = 1;
|
||||
|
||||
if (tok->tokAt(2)->str() == "<")
|
||||
typeEnd = tok->tokAt(3);
|
||||
else if (tok->tokAt(3)->str() == "<")
|
||||
typeEnd = tok->tokAt(4);
|
||||
else if (tok->tokAt(4)->str() == "<")
|
||||
typeEnd = tok->tokAt(5);
|
||||
typeEnd = tok->tokAt(offset++);
|
||||
|
||||
bool atEnd = false;
|
||||
while (!atEnd)
|
||||
{
|
||||
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
|
||||
typeEnd = tok->tokAt(6);
|
||||
atEnd = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
continue; // invalid input
|
||||
|
||||
// check for template
|
||||
if (tok->tokAt(offset)->str() == "<")
|
||||
{
|
||||
int level = 1;
|
||||
int paren = 0;
|
||||
hasTemplate = true;
|
||||
typeEnd = tok->tokAt(offset + 1);
|
||||
for (; typeEnd ; typeEnd = typeEnd->next())
|
||||
{
|
||||
if (typeEnd->str() == ">")
|
||||
|
@ -509,48 +520,19 @@ void Tokenizer::simplifyTypedef()
|
|||
}
|
||||
|
||||
tok = typeEnd;
|
||||
}
|
||||
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;
|
||||
offset = 1;
|
||||
}
|
||||
|
||||
// check for pointers and references
|
||||
while (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "*|&"))
|
||||
pointers.push_back(tok->tokAt(offset++)->str());
|
||||
|
||||
if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "%type%"))
|
||||
{
|
||||
// found the type name
|
||||
typeName = tok->strAt(offset++);
|
||||
|
||||
// check for array
|
||||
if (tok->tokAt(offset) && tok->tokAt(offset)->str() == "[")
|
||||
{
|
||||
arrayStart = tok->tokAt(offset);
|
||||
|
@ -574,6 +556,7 @@ void Tokenizer::simplifyTypedef()
|
|||
arrayEnd = tok->tokAt(offset++);
|
||||
}
|
||||
|
||||
// check for end or another
|
||||
if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), ";|,"))
|
||||
tok = tok->tokAt(offset);
|
||||
else
|
||||
|
|
|
@ -166,6 +166,7 @@ private:
|
|||
TEST_CASE(simplifyTypedef27);
|
||||
TEST_CASE(simplifyTypedef28);
|
||||
TEST_CASE(simplifyTypedef29);
|
||||
TEST_CASE(simplifyTypedef30);
|
||||
TEST_CASE(reverseArraySyntax)
|
||||
TEST_CASE(simplify_numeric_condition)
|
||||
|
||||
|
@ -3001,6 +3002,26 @@ private:
|
|||
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()
|
||||
{
|
||||
ASSERT_EQUALS("a [ 13 ]", tok("13[a]"));
|
||||
|
|
Loading…
Reference in New Issue