Skip to content

Commit

Permalink
Fix #13094 internalError for function pointer typedef (#6795)
Browse files Browse the repository at this point in the history
We have a block `// Special handling of function pointer cast` but the
simplifications are incorrect.
  • Loading branch information
chrchr-github committed Sep 13, 2024
1 parent 67d570c commit d1ca4e3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,9 +752,10 @@ namespace {
return;

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

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

// Special handling of function pointer cast
const bool isFunctionPointer = Token::Match(mNameToken, "%name% )");
if (isFunctionPointer && isCast(tok->previous())) {
tok->insertToken("*");
Token* const tok_1 = insertTokens(tok, std::pair<Token*, Token*>(mRangeType.first, mNameToken->linkAt(1)));
Expand Down
22 changes: 22 additions & 0 deletions test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ class TestSimplifyTypedef : public TestFixture {
TEST_CASE(simplifyTypedefFunction8);
TEST_CASE(simplifyTypedefFunction9);
TEST_CASE(simplifyTypedefFunction10); // #5191
TEST_CASE(simplifyTypedefFunction11);

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

Expand Down Expand Up @@ -4315,6 +4316,27 @@ class TestSimplifyTypedef : public TestFixture {
tok(code,false));
}

void simplifyTypedefFunction11() {
const char code[] = "typedef void (*func_t) (int);\n"
"void g(int);\n"
"void f(void* p) {\n"
" if (g != func_t(p)) {}\n"
" if (g != (func_t)p) {}\n"
"}\n";
TODO_ASSERT_EQUALS("void g ( int ) ; "
"void f ( void * p ) { "
"if ( g != ( void ( * ) ( int ) ) ( p ) ) { } "
"if ( g != ( void ( * ) ( int ) ) p ) { } "
"}",
"void g ( int ) ; "
"void f ( void * p ) { "
"if ( g != void * ( p ) ) { } "
"if ( g != ( void * ) p ) { } "
"}",
tok(code,false));
ignore_errout(); // we are not interested in the output
}

void simplifyTypedefStruct() {
const char code1[] = "typedef struct S { int x; } xyz;\n"
"xyz var;";
Expand Down

0 comments on commit d1ca4e3

Please sign in to comment.