Fixed #1297 (typedef causes internal error in vlc/modules/access/imem.c)

This commit is contained in:
Robert Reif 2010-01-22 17:27:40 +01:00 committed by Daniel Marjamäki
parent d4b13eee9f
commit 9f853cb164
2 changed files with 63 additions and 4 deletions

View File

@ -620,6 +620,13 @@ void Tokenizer::simplifyTypedef()
if (simplifyType) if (simplifyType)
{ {
bool inCast = false;
if ((tok2->previous()->str() == "(" && tok2->next()->str() == ")") ||
(Token::Match(tok2->tokAt(-2), "static_cast <") &&
Token::Match(tok2->next(), "> (")))
inCast = true;
if (start && end && !functionPtr) if (start && end && !functionPtr)
{ {
tok2->str(start->str()); tok2->str(start->str());
@ -673,11 +680,15 @@ void Tokenizer::simplifyTypedef()
Token *tok3 = tok2; Token *tok3 = tok2;
tok2->insertToken("*"); tok2->insertToken("*");
tok2 = tok2->next(); tok2 = tok2->next();
tok2 = tok2->next();
// skip over typedef parameter if (!inCast)
if (tok2->next()->str() == "(") {
tok2 = tok2->next()->link(); tok2 = tok2->next();
// skip over typedef parameter
if (tok2->next()->str() == "(")
tok2 = tok2->next()->link();
}
tok2->insertToken(")"); tok2->insertToken(")");
tok2 = tok2->next(); tok2 = tok2->next();

View File

@ -158,6 +158,7 @@ private:
TEST_CASE(simplifyTypedef21); TEST_CASE(simplifyTypedef21);
TEST_CASE(simplifyTypedef22); TEST_CASE(simplifyTypedef22);
TEST_CASE(simplifyTypedef23); TEST_CASE(simplifyTypedef23);
TEST_CASE(simplifyTypedef24);
TEST_CASE(reverseArraySyntax) TEST_CASE(reverseArraySyntax)
TEST_CASE(simplify_numeric_condition) TEST_CASE(simplify_numeric_condition)
@ -2760,6 +2761,53 @@ private:
ASSERT_EQUALS(expected, tok(code, false)); ASSERT_EQUALS(expected, tok(code, false));
} }
void simplifyTypedef24()
{
{
const char code[] = "typedef int (*fp)();\n"
"void g( fp f )\n"
"{\n"
" fp f2 = (fp)f;\n"
"}";
const char expected[] =
"; "
"void g ( int ( * f ) ( ) ) "
"{ "
"int ( * f2 ) ( ) = ( int ( * ) ( ) ) f ; "
"}";
ASSERT_EQUALS(expected, tok(code, false));
// TODO: the definition and assignment should be split up
const char todo[] =
"; "
"void g ( fp f ) "
"{ "
"int ( * f2 ) ( ) ; f2 = ( int ( * ) ( ) ) f ; "
"}";
TODO_ASSERT_EQUALS(todo, tok(code, false));
}
{
const char code[] = "typedef int (*fp)();\n"
"void g( fp f )\n"
"{\n"
" fp f2 = static_cast<fp>(f);\n"
"}";
const char expected[] =
"; "
"void g ( int ( * f ) ( ) ) "
"{ "
"int ( * f2 ) ( ) = static_cast < int ( * ) ( ) > ( f ) ; "
"}";
ASSERT_EQUALS(expected, tok(code, false));
}
}
void reverseArraySyntax() void reverseArraySyntax()
{ {
ASSERT_EQUALS("a [ 13 ]", tok("13[a]")); ASSERT_EQUALS("a [ 13 ]", tok("13[a]"));