Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,9 @@ void Tokenizer::simplifyTypedef()
std::map<std::string, std::set<std::string>> numberOfTypedefs;
for (Token* tok = list.front(); tok; tok = tok->next()) {
if (tok->str() == "typedef") {
if (Token::simpleMatch(tok, "typedef ( *") && Token::simpleMatch(tok->linkAt(1), ") ("))
// Implicit return type
tok->insertToken("int");
TypedefSimplifier ts(tok);
if (ts.fail() || !ts.nameToken())
continue;
Expand Down
28 changes: 28 additions & 0 deletions test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ class TestSimplifyTypedef : public TestFixture {
TEST_CASE(simplifyTypedefFunction9);
TEST_CASE(simplifyTypedefFunction10); // #5191
TEST_CASE(simplifyTypedefFunction11);
TEST_CASE(simplifyTypedefFunction12);

TEST_CASE(simplifyTypedefStruct); // #12081 - volatile struct

Expand All @@ -261,6 +262,7 @@ class TestSimplifyTypedef : public TestFixture {
TEST_CASE(typedefInfo2);
TEST_CASE(typedefInfo3);
TEST_CASE(typedefInfo4);
TEST_CASE(typedefInfo5);
}

class TokenizerTest final : public Tokenizer
Expand Down Expand Up @@ -4444,6 +4446,15 @@ class TestSimplifyTypedef : public TestFixture {
ignore_errout(); // we are not interested in the output
}

void simplifyTypedefFunction12() {
const char code[] = "typedef (*pfi)(void);\n"
"pfi f;\n";

const char expected[] = "int ( * f ) ( void ) ;";
ASSERT_EQUALS(expected, tok(code, dinit(TokOptions, $.debugwarnings = false)));
ASSERT_EQUALS("", errout_str());
}

void simplifyTypedefStruct() {
const char code1[] = "typedef struct S { int x; } xyz;\n"
"xyz var;";
Expand Down Expand Up @@ -4673,6 +4684,23 @@ class TestSimplifyTypedef : public TestFixture {
" <info name=\"coord\" file=\"file.c\" line=\"1\" column=\"1\" tagline=\"1\" tagcolumn=\"16\" used=\"1\" isFunctionPointer=\"0\"/>\n"
" </typedef-info>\n", xml);
}

void typedefInfo5() {
const std::string xml = dumpTypedefInfo("typedef (*pfi)(void);\n");
ASSERT_EQUALS(" <typedef-info>\n"
" <info name=\"pfi\" file=\"file.c\" line=\"1\" column=\"1\" used=\"0\" isFunctionPointer=\"1\">\n"
" <token line=\"1\" column=\"1\" str=\"typedef\"/>\n"
" <token line=\"1\" column=\"0\" str=\"int\"/>\n"
" <token line=\"1\" column=\"9\" str=\"(\"/>\n"
" <token line=\"1\" column=\"10\" str=\"*\"/>\n"
" <token line=\"1\" column=\"11\" str=\"pfi\"/>\n"
" <token line=\"1\" column=\"14\" str=\")\"/>\n"
" <token line=\"1\" column=\"15\" str=\"(\"/>\n"
" <token line=\"1\" column=\"16\" str=\"void\"/>\n"
" <token line=\"1\" column=\"20\" str=\")\"/>\n"
" </info>\n"
" </typedef-info>\n", xml);
}
};

REGISTER_TEST(TestSimplifyTypedef)
Loading