Skip to content

Commit de8ce94

Browse files
committed
Fix function pointer typedef simplification (#5935)
1 parent f81f6f0 commit de8ce94

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

lib/tokenize.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,10 @@ namespace {
861861
}
862862

863863
if (isFunctionPointer) {
864-
if (Token::Match(after, "( * %name% ) ("))
864+
// Keep the argument list for a parenthesized pointer declarator: fp *(name).
865+
if (after->previous() != tok3 && Token::Match(after->previous(), "* ( %name% ) ;|,|="))
866+
after = after->link()->next();
867+
else if (Token::Match(after, "( * %name% ) ("))
865868
after = after->link()->linkAt(1)->next();
866869
else if (after->str() == "(") {
867870
useAfterVarRange = false;

test/testsimplifytypedef.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class TestSimplifyTypedef : public TestFixture {
6565
TEST_CASE(cfp5);
6666
TEST_CASE(cfp6);
6767
TEST_CASE(cfp7);
68+
TEST_CASE(cfp8);
6869
TEST_CASE(carray1);
6970
TEST_CASE(carray2);
7071
TEST_CASE(carray3);
@@ -522,6 +523,16 @@ class TestSimplifyTypedef : public TestFixture {
522523
ASSERT_EQUALS("uint32_t g ( ) ; uint32_t ( * f ) ( uint32_t n ) ;", simplifyTypedef(code));
523524
}
524525

526+
void cfp8() { // #5935
527+
const char code[] = "typedef TypeDefStruct *(*ThisIsTheProblem)(Type *Var);\n"
528+
"typedef struct Struct1 {\n"
529+
" ThisIsTheProblem *(AnotherType);\n"
530+
"} Struct1;\n";
531+
const char expected[] = "struct Struct1 { TypeDefStruct * ( * * ( AnotherType ) ) ( Type * Var ) ; } ;";
532+
ASSERT_EQUALS(expected, simplifyTypedefC(code));
533+
ASSERT_EQUALS(expected, simplifyTypedef(code));
534+
}
535+
525536
void carray1() {
526537
const char code[] = "typedef int t[20];\n"
527538
"t x;\n";

0 commit comments

Comments
 (0)