diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 349ca6c0ad1..3e7d2b5dae5 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2972,6 +2972,19 @@ static unsigned int tokDistance(const Token* tok1, const Token* tok2) { // use w return dist; } +// a parenthesis starting with a ptr-operator is a nested declarator, otherwise it is a parameter list +static bool isNestedDeclarator(const Token* paren) +{ + if (!Token::simpleMatch(paren, "(")) + return false; + const Token* tok = paren->next(); + if (Token::simpleMatch(tok, "::")) + tok = tok->next(); + while (Token::Match(tok, "%name% ::")) + tok = tok->tokAt(2); + return Token::Match(tok, "*|&|&&"); +} + static const Token* skipConstVolatileBackwards(const Token* tok) { while (Token::Match(tok, "const|volatile")) tok = tok->previous(); @@ -3447,6 +3460,35 @@ bool Tokenizer::simplifyUsing() tok1->deleteThis(); substitute = true; } + } else if (fpArgList && fpQual && Token::Match(tok1->next(), "%name%") && + Token::findsimplematch(fpQual->next(), "(", fpQual->link())) { + // nested declarator, e.g. function returning function pointer: int (*(*)(int))(float) + Token* const nameStart = tok1->next(); + Token* nameEnd = nameStart; + while (Token::Match(nameEnd, "%name% :: %name%")) + nameEnd = nameEnd->tokAt(2); + if (Token::simpleMatch(nameEnd->next(), "(")) + nameEnd = nameEnd->linkAt(1); + else { + while (Token::simpleMatch(nameEnd->next(), "[")) + nameEnd = nameEnd->linkAt(1); + } + if (!nameEnd->next()) + syntaxError(nameEnd); + TokenList::copyTokens(tok1, start, usingEnd->previous()); + tok1->deleteThis(); + // the name goes after the ptr-operators of the innermost nested declarator. + // after deleteThis() tok1 is the first copied token, so this is the copy of fpQual + Token* pos = tok1->tokAt(tokDistance(start, fpQual))->next(); + for (;;) { + while (Token::Match(pos, "%name%|::|*|&|&&")) + pos = pos->next(); + if (!isNestedDeclarator(pos)) + break; + pos = pos->next(); + } + Token::move(nameStart, nameEnd, pos->previous()); + substitute = true; } else if (fpArgList && fpQual && Token::Match(tok1->next(), "%name%")) { // function pointer const bool isFuncDecl = Token::simpleMatch(tok1->tokAt(2), "("); diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index 200371ace5c..093518eb566 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -106,6 +106,7 @@ class TestSimplifyUsing : public TestFixture { TEST_CASE(simplifyUsing10720); TEST_CASE(simplifyUsing13873); // function declaration TEST_CASE(simplifyUsing14877); + TEST_CASE(simplifyUsing15315); TEST_CASE(scopeInfo1); TEST_CASE(scopeInfo2); @@ -1813,6 +1814,30 @@ class TestSimplifyUsing : public TestFixture { ASSERT_EQUALS(expected, tok(code)); } + void simplifyUsing15315() { + const char code[] = "using D = int(*(*)(int))(float);\n" + "void f(D d);\n" + "D g(int);\n" + "D N::h(char c) { return nullptr; }\n"; + const char expected[] = "void f ( int ( * ( * d ) ( int ) ) ( float ) ) ; " + "int ( * ( * g ( int ) ) ( int ) ) ( float ) ; " + "int ( * ( * N :: h ( char c ) ) ( int ) ) ( float ) { return nullptr ; }"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout_str()); + + ASSERT_THROW_INTERNAL(tok("using D = int(*(*)(int))(float);\nD d"), SYNTAX); + + const char code2[] = "using F = int (*())(float);\n" + "F f;\n" + "using G = int (*(std::string))(float);\n" + "G g;\n"; + ASSERT_EQUALS("int * f ( ) ; int * g ( std :: string ) ;", tok(code2)); + + const char code3[] = "using D = int (*(*)(int))(float);\n" + "D a[2];\n"; + ASSERT_EQUALS("int ( * ( * a [ 2 ] ) ( int ) ) ( float ) ;", tok(code3)); + } + void scopeInfo1() { const char code[] = "struct A {\n" " enum class Mode { UNKNOWN, ENABLED, NONE, };\n"