From 9f853cb1646c5cc6eae4b74432e710b614805dd1 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Fri, 22 Jan 2010 17:27:40 +0100 Subject: [PATCH] Fixed #1297 (typedef causes internal error in vlc/modules/access/imem.c) --- lib/tokenize.cpp | 19 +++++++++++---- test/testsimplifytokens.cpp | 48 +++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 669c0671e..9f797e337 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -620,6 +620,13 @@ void Tokenizer::simplifyTypedef() 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) { tok2->str(start->str()); @@ -673,11 +680,15 @@ void Tokenizer::simplifyTypedef() Token *tok3 = tok2; tok2->insertToken("*"); tok2 = tok2->next(); - tok2 = tok2->next(); - // skip over typedef parameter - if (tok2->next()->str() == "(") - tok2 = tok2->next()->link(); + if (!inCast) + { + tok2 = tok2->next(); + + // skip over typedef parameter + if (tok2->next()->str() == "(") + tok2 = tok2->next()->link(); + } tok2->insertToken(")"); tok2 = tok2->next(); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index dd6391ea8..e8d753407 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -158,6 +158,7 @@ private: TEST_CASE(simplifyTypedef21); TEST_CASE(simplifyTypedef22); TEST_CASE(simplifyTypedef23); + TEST_CASE(simplifyTypedef24); TEST_CASE(reverseArraySyntax) TEST_CASE(simplify_numeric_condition) @@ -2760,6 +2761,53 @@ private: 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(f);\n" + "}"; + + const char expected[] = + "; " + "void g ( int ( * f ) ( ) ) " + "{ " + "int ( * f2 ) ( ) = static_cast < int ( * ) ( ) > ( f ) ; " + "}"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + } + void reverseArraySyntax() { ASSERT_EQUALS("a [ 13 ]", tok("13[a]"));