From ed4e3c9e793c2ff12cf9535ca5d2a2aaf7381486 Mon Sep 17 00:00:00 2001 From: William Jakobsson <50847546+wjakobsson@users.noreply.github.com> Date: Fri, 4 Dec 2026 15:14:09 +0100 Subject: [PATCH 1/4] fix --- lib/tokenize.cpp | 23 +++++++++++++++++++++++ test/testsimplifyusing.cpp | 13 +++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 349ca6c0ad1..87a2e4093c1 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3447,6 +3447,29 @@ 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); + TokenList::copyTokens(tok1, start, usingEnd->previous()); + tok1->deleteThis(); + // the name goes into the innermost parenthesized declarator + Token* decl = tok1->tokAt(tokDistance(start, fpQual)); + for (;;) { + Token* inner = decl->next(); + while (inner != decl->link() && inner->str() != "(") + inner = inner->next(); + if (inner == decl->link()) + break; + decl = inner; + } + Token::move(nameStart, nameEnd, decl->link()->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..8f801de854a 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,18 @@ 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()); + } + void scopeInfo1() { const char code[] = "struct A {\n" " enum class Mode { UNKNOWN, ENABLED, NONE, };\n" From 56987e609e189f1aad38a701e8103da7c6217236 Mon Sep 17 00:00:00 2001 From: William Jakobsson <50847546+wjakobsson@users.noreply.github.com> Date: Fri, 4 Dec 2026 15:52:10 +0100 Subject: [PATCH 2/4] stop segfault --- lib/tokenize.cpp | 2 ++ test/testsimplifyusing.cpp | 2 ++ 2 files changed, 4 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 87a2e4093c1..9802932618f 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3456,6 +3456,8 @@ bool Tokenizer::simplifyUsing() nameEnd = nameEnd->tokAt(2); if (Token::simpleMatch(nameEnd->next(), "(")) nameEnd = nameEnd->linkAt(1); + if (!nameEnd->next()) + syntaxError(nameEnd); TokenList::copyTokens(tok1, start, usingEnd->previous()); tok1->deleteThis(); // the name goes into the innermost parenthesized declarator diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index 8f801de854a..5caf14c4280 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -1824,6 +1824,8 @@ class TestSimplifyUsing : public TestFixture { "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); } void scopeInfo1() { From ebfdc7090a2cce9b39956a2bd4903b5106e428e4 Mon Sep 17 00:00:00 2001 From: William Jakobsson <50847546+wjakobsson@users.noreply.github.com> Date: Fri, 4 Dec 2026 16:10:00 +0100 Subject: [PATCH 3/4] handle lists and suffixes --- lib/tokenize.cpp | 30 ++++++++++++++++++++++-------- test/testsimplifyusing.cpp | 10 ++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 9802932618f..20f6348d788 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3456,21 +3456,35 @@ bool Tokenizer::simplifyUsing() 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 into the innermost parenthesized declarator - Token* decl = tok1->tokAt(tokDistance(start, fpQual)); + // a parenthesis starting with a ptr-operator is a nested declarator, otherwise it is a parameter list + const auto isNestedDeclarator = [](const Token* paren) { + if (!Token::simpleMatch(paren, "(")) + return false; + const Token* t = paren->next(); + if (Token::simpleMatch(t, "::")) + t = t->next(); + while (Token::Match(t, "%name% ::")) + t = t->tokAt(2); + return Token::Match(t, "*|&|&&"); + }; + // the name goes after the ptr-operators of the innermost nested declarator + Token* pos = tok1->tokAt(tokDistance(start, fpQual))->next(); for (;;) { - Token* inner = decl->next(); - while (inner != decl->link() && inner->str() != "(") - inner = inner->next(); - if (inner == decl->link()) + while (Token::Match(pos, "%name%|::|*|&|&&")) + pos = pos->next(); + if (!isNestedDeclarator(pos)) break; - decl = inner; + pos = pos->next(); } - Token::move(nameStart, nameEnd, decl->link()->previous()); + Token::move(nameStart, nameEnd, pos->previous()); substitute = true; } else if (fpArgList && fpQual && Token::Match(tok1->next(), "%name%")) { // function pointer diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index 5caf14c4280..093518eb566 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -1826,6 +1826,16 @@ class TestSimplifyUsing : public TestFixture { 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() { From 4f486e216add375e974ba49cdb8b91f64a4cf6f8 Mon Sep 17 00:00:00 2001 From: William Jakobsson <50847546+wjakobsson@users.noreply.github.com> Date: Fri, 4 Dec 2026 16:25:47 +0100 Subject: [PATCH 4/4] cleanup --- lib/tokenize.cpp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 20f6348d788..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(); @@ -3464,18 +3477,8 @@ bool Tokenizer::simplifyUsing() syntaxError(nameEnd); TokenList::copyTokens(tok1, start, usingEnd->previous()); tok1->deleteThis(); - // a parenthesis starting with a ptr-operator is a nested declarator, otherwise it is a parameter list - const auto isNestedDeclarator = [](const Token* paren) { - if (!Token::simpleMatch(paren, "(")) - return false; - const Token* t = paren->next(); - if (Token::simpleMatch(t, "::")) - t = t->next(); - while (Token::Match(t, "%name% ::")) - t = t->tokAt(2); - return Token::Match(t, "*|&|&&"); - }; - // the name goes after the ptr-operators of the innermost nested declarator + // 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%|::|*|&|&&"))