diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 8d07b0ff1..79536b267 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6061,9 +6061,14 @@ void Tokenizer::simplifyInitVar() if (tok->str() == "return") continue; - if (Token::Match(tok, "class|struct|union| %type% *| %var% ( &| %any% ) ;") || - Token::Match(tok, "%type% *| %var% ( %type% (")) { + if (Token::Match(tok, "class|struct|union| %type% *| %var% ( &| %any% ) ;")) { tok = initVar(tok); + } else if (Token::Match(tok, "%type% *| %var% ( %type% (")) { + const Token* tok2 = tok->tokAt(2); + if (!tok2->link()) + tok2 = tok2->next(); + if (!tok2->link() || (tok2->link()->strAt(1) == ";" && !Token::simpleMatch(tok2->linkAt(2), ") ("))) + tok = initVar(tok); } else if (Token::Match(tok, "class|struct|union| %type% *| %var% ( &| %any% ) ,")) { Token *tok1 = tok; while (tok1->str() != ",") diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index d491c0982..d3a105b55 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -63,6 +63,7 @@ private: TEST_CASE(tokenize28); // #4725 (writing asm() around "^{}") TEST_CASE(tokenize29); // #5506 (segmentation fault upon invalid code) TEST_CASE(tokenize30); // #5356 (segmentation fault upon invalid code) + TEST_CASE(tokenize31); // #3503 (Wrong handling of member function taking function pointer as argument) // don't freak out when the syntax is wrong TEST_CASE(wrong_syntax1); @@ -841,6 +842,23 @@ private: tokenizeAndStringify("struct template struct B { }; B < 0 > b;"); } + // #3503 - don't "simplify" SetFunction member function to a variable + void tokenize31() { + ASSERT_EQUALS("struct TTestClass { TTestClass ( ) { }\n" + "void SetFunction ( Other * m_f ) { }\n" + "} ;", + tokenizeAndStringify("struct TTestClass { TTestClass() { }\n" + " void SetFunction(Other(*m_f)()) { }\n" + "};")); + + ASSERT_EQUALS("struct TTestClass { TTestClass ( ) { }\n" + "void SetFunction ( Other * m_f ) ;\n" + "} ;", + tokenizeAndStringify("struct TTestClass { TTestClass() { }\n" + " void SetFunction(Other(*m_f)());\n" + "};")); + } + void wrong_syntax1() { { const char code[] ="TR(kvmpio, PROTO(int rw), ARGS(rw), TP_(aa->rw;))";