diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 6b8ec7ecb..091912233 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1390,10 +1390,19 @@ void Tokenizer::simplifyTypedef() if (sameStartEnd) typeEnd = typeStart; + // Is this a "T()" expression where T is a pointer type? + const bool isPointerTypeCall = !inOperator && Token::Match(tok2, "%name% ( )") && !pointers.empty(); + // start substituting at the typedef name by replacing it with the type Token* replStart = tok2; // track first replaced token for (Token* tok3 = typeStart; tok3->str() != ";"; tok3 = tok3->next()) tok3->isSimplifiedTypedef(true); + if (isPointerTypeCall) { + tok2->deleteThis(); + tok2->insertToken("0"); + tok2 = tok2->next(); + tok2->next()->insertToken("0"); + } tok2->str(typeStart->str()); // restore qualification if it was removed diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 55f6a5b4b..baf845e51 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -188,6 +188,7 @@ private: TEST_CASE(simplifyTypedef139); TEST_CASE(simplifyTypedef140); // #10798 TEST_CASE(simplifyTypedef141); // #10144 + TEST_CASE(simplifyTypedef142); // T() when T is a pointer type TEST_CASE(simplifyTypedefFunction1); TEST_CASE(simplifyTypedefFunction2); // ticket #1685 @@ -3051,6 +3052,18 @@ private: ASSERT_EQUALS("class C { struct I { } ; } ;", tok(code)); } + void simplifyTypedef142() { // T() when T is a pointer type + // #11232 + const char code1[] = "typedef int* T;\n" + "a = T();\n"; + ASSERT_EQUALS("a = ( int * ) 0 ;", tok(code1)); + + // #9479 + const char code2[] = "typedef int* T;\n" + "void f(T = T()){}\n"; + ASSERT_EQUALS("void f ( int * = ( int * ) 0 ) { }", tok(code2)); + } + void simplifyTypedefFunction1() { { const char code[] = "typedef void (*my_func)();\n"