Fixed #1666 (simplifyTypedef: add support for typedefs of functions)
This commit is contained in:
parent
d7118c4d36
commit
ac7515cdcc
|
@ -729,6 +729,7 @@ void Tokenizer::simplifyTypedef()
|
||||||
Token *arrayEnd = 0;
|
Token *arrayEnd = 0;
|
||||||
Token *typeDef = tok;
|
Token *typeDef = tok;
|
||||||
int offset = 1;
|
int offset = 1;
|
||||||
|
bool function = false;
|
||||||
bool functionPtr = false;
|
bool functionPtr = false;
|
||||||
bool functionRef = false;
|
bool functionRef = false;
|
||||||
|
|
||||||
|
@ -747,7 +748,7 @@ void Tokenizer::simplifyTypedef()
|
||||||
typeEnd = tok->tokAt(offset++);
|
typeEnd = tok->tokAt(offset++);
|
||||||
|
|
||||||
if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "%type%") &&
|
if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "%type%") &&
|
||||||
tok->tokAt(offset + 1) && !Token::Match(tok->tokAt(offset + 1), "[|;|,"))
|
tok->tokAt(offset + 1) && !Token::Match(tok->tokAt(offset + 1), "[|;|,|("))
|
||||||
typeEnd = tok->tokAt(offset++);
|
typeEnd = tok->tokAt(offset++);
|
||||||
else
|
else
|
||||||
atEnd = true;
|
atEnd = true;
|
||||||
|
@ -833,6 +834,23 @@ void Tokenizer::simplifyTypedef()
|
||||||
// check for end or another
|
// 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);
|
||||||
|
|
||||||
|
// or a function typedef
|
||||||
|
else if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "("))
|
||||||
|
{
|
||||||
|
function = true;
|
||||||
|
if (tok->tokAt(offset)->link()->next())
|
||||||
|
{
|
||||||
|
argStart = tok->tokAt(offset);
|
||||||
|
argEnd = tok->tokAt(offset)->link();
|
||||||
|
tok = argEnd->next();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// internal error
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// unhandled typedef, skip it and continue
|
// unhandled typedef, skip it and continue
|
||||||
|
@ -840,15 +858,18 @@ void Tokenizer::simplifyTypedef()
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "( *|& %type% ) ("))
|
else if (tok->tokAt(offset) && Token::Match(tok->tokAt(offset), "( *|&| %type% ) ("))
|
||||||
{
|
{
|
||||||
if (tok->tokAt(offset + 4)->link()->next())
|
functionPtr = tok->tokAt(offset + 1)->str() == "*";
|
||||||
|
functionRef = tok->tokAt(offset + 1)->str() == "&";
|
||||||
|
function = tok->tokAt(offset + 2)->str() == ")";
|
||||||
|
if (!function)
|
||||||
|
offset++;
|
||||||
|
if (tok->tokAt(offset + 3)->link()->next())
|
||||||
{
|
{
|
||||||
functionPtr = tok->tokAt(offset + 1)->str() == "*";
|
typeName = tok->tokAt(offset + 1);
|
||||||
functionRef = tok->tokAt(offset + 1)->str() == "&";
|
argStart = tok->tokAt(offset + 3);
|
||||||
typeName = tok->tokAt(offset + 2);
|
argEnd = tok->tokAt(offset + 3)->link();
|
||||||
argStart = tok->tokAt(offset + 4);
|
|
||||||
argEnd = tok->tokAt(offset + 4)->link();
|
|
||||||
tok = argEnd->next();
|
tok = argEnd->next();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -1009,14 +1030,14 @@ void Tokenizer::simplifyTypedef()
|
||||||
tok2 = tok2->next();
|
tok2 = tok2->next();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (functionPtr || functionRef)
|
if (functionPtr || functionRef || function)
|
||||||
{
|
{
|
||||||
tok2->insertToken("(");
|
tok2->insertToken("(");
|
||||||
tok2 = tok2->next();
|
tok2 = tok2->next();
|
||||||
Token *tok3 = tok2;
|
Token *tok3 = tok2;
|
||||||
if (functionPtr)
|
if (functionPtr)
|
||||||
tok2->insertToken("*");
|
tok2->insertToken("*");
|
||||||
else
|
else if (functionRef)
|
||||||
tok2->insertToken("&");
|
tok2->insertToken("&");
|
||||||
tok2 = tok2->next();
|
tok2 = tok2->next();
|
||||||
|
|
||||||
|
|
|
@ -186,6 +186,8 @@ private:
|
||||||
TEST_CASE(simplifyTypedef45); // ticket #1613
|
TEST_CASE(simplifyTypedef45); // ticket #1613
|
||||||
TEST_CASE(simplifyTypedef46); // ticket #1615
|
TEST_CASE(simplifyTypedef46); // ticket #1615
|
||||||
|
|
||||||
|
TEST_CASE(simplifyTypedefFunction);
|
||||||
|
|
||||||
TEST_CASE(reverseArraySyntax)
|
TEST_CASE(reverseArraySyntax)
|
||||||
TEST_CASE(simplify_numeric_condition)
|
TEST_CASE(simplify_numeric_condition)
|
||||||
|
|
||||||
|
@ -3909,6 +3911,29 @@ private:
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void simplifyTypedefFunction()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
const char code[] = "typedef void (my_func)(arg_class*);\n"
|
||||||
|
"std::queue<my_func *> func_queue;";
|
||||||
|
|
||||||
|
// The expected result..
|
||||||
|
const std::string expected("; "
|
||||||
|
"std :: queue < void ( * ) ( arg_class * ) > func_queue ;");
|
||||||
|
ASSERT_EQUALS(expected, sizeof_(code));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const char code[] = "typedef void my_func(arg_class*);\n"
|
||||||
|
"std::queue<my_func *> func_queue;";
|
||||||
|
|
||||||
|
// The expected result..
|
||||||
|
const std::string expected("; "
|
||||||
|
"std :: queue < void ( * ) ( arg_class * ) > func_queue ;");
|
||||||
|
ASSERT_EQUALS(expected, sizeof_(code));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void reverseArraySyntax()
|
void reverseArraySyntax()
|
||||||
{
|
{
|
||||||
ASSERT_EQUALS("a [ 13 ]", tok("13[a]"));
|
ASSERT_EQUALS("a [ 13 ]", tok("13[a]"));
|
||||||
|
|
Loading…
Reference in New Issue