Skip to content

Commit d1ca4e3

Browse files
Fix #13094 internalError for function pointer typedef (#6795)
We have a block `// Special handling of function pointer cast` but the simplifications are incorrect.
1 parent 67d570c commit d1ca4e3

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

lib/tokenize.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,10 @@ namespace {
752752
return;
753753

754754
mUsed = true;
755+
const bool isFunctionPointer = Token::Match(mNameToken, "%name% )");
755756

756757
// Special handling for T(...) when T is a pointer
757-
if (Token::Match(tok, "%name% [({]") && !Token::simpleMatch(tok->linkAt(1), ") (")) {
758+
if (Token::Match(tok, "%name% [({]") && !isFunctionPointer) {
758759
bool pointerType = false;
759760
for (const Token* type = mRangeType.first; type != mRangeType.second; type = type->next()) {
760761
if (type->str() == "*" || type->str() == "&") {
@@ -791,7 +792,6 @@ namespace {
791792
}
792793

793794
// Special handling of function pointer cast
794-
const bool isFunctionPointer = Token::Match(mNameToken, "%name% )");
795795
if (isFunctionPointer && isCast(tok->previous())) {
796796
tok->insertToken("*");
797797
Token* const tok_1 = insertTokens(tok, std::pair<Token*, Token*>(mRangeType.first, mNameToken->linkAt(1)));

test/testsimplifytypedef.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ class TestSimplifyTypedef : public TestFixture {
233233
TEST_CASE(simplifyTypedefFunction8);
234234
TEST_CASE(simplifyTypedefFunction9);
235235
TEST_CASE(simplifyTypedefFunction10); // #5191
236+
TEST_CASE(simplifyTypedefFunction11);
236237

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

@@ -4315,6 +4316,27 @@ class TestSimplifyTypedef : public TestFixture {
43154316
tok(code,false));
43164317
}
43174318

4319+
void simplifyTypedefFunction11() {
4320+
const char code[] = "typedef void (*func_t) (int);\n"
4321+
"void g(int);\n"
4322+
"void f(void* p) {\n"
4323+
" if (g != func_t(p)) {}\n"
4324+
" if (g != (func_t)p) {}\n"
4325+
"}\n";
4326+
TODO_ASSERT_EQUALS("void g ( int ) ; "
4327+
"void f ( void * p ) { "
4328+
"if ( g != ( void ( * ) ( int ) ) ( p ) ) { } "
4329+
"if ( g != ( void ( * ) ( int ) ) p ) { } "
4330+
"}",
4331+
"void g ( int ) ; "
4332+
"void f ( void * p ) { "
4333+
"if ( g != void * ( p ) ) { } "
4334+
"if ( g != ( void * ) p ) { } "
4335+
"}",
4336+
tok(code,false));
4337+
ignore_errout(); // we are not interested in the output
4338+
}
4339+
43184340
void simplifyTypedefStruct() {
43194341
const char code1[] = "typedef struct S { int x; } xyz;\n"
43204342
"xyz var;";

0 commit comments

Comments
 (0)