From 7bcc1ac8a910670b0680a329a733d6accb6345c3 Mon Sep 17 00:00:00 2001 From: Tom Sobczynski Date: Thu, 19 Nov 2026 16:26:53 -0500 Subject: [PATCH 1/2] Fix #15244 syntaxError and unsimplified function body when a specifier precedes a trailing return type isTrailingReturnType accepted only `) . type`, so a function or lambda with a cv or ref qualifier, noexcept, noexcept(...), throw(...), mutable, constexpr, consteval or static before `->` had its body taken for a braced initializer, and simplifyVarDecl did not simplify it. Inside such a body, findLambdaEndScope then accepted a structured binding like `[p, c]{f()}` as a lambda. Step back over those specifiers and return the parameter list's `)`. Co-Authored-By: Claude Opus 5.5 --- lib/tokenize.cpp | 17 +++++++++++++-- test/testtokenize.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 60fbe92968c..cf5200fa952 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7511,6 +7511,7 @@ void Tokenizer::simplifyVarDecl(const bool only_k_r_fpar) simplifyVarDecl(list.front(), nullptr, only_k_r_fpar); } +// Returns the closing parenthesis of the parameter list if tok ends a trailing return type static Token* isTrailingReturnType(Token* tok) { while (Token::Match(tok, "%name%|::|>")) { @@ -7519,8 +7520,20 @@ static Token* isTrailingReturnType(Token* tok) else tok = tok->tokAt(-1); } - if (tok && Token::simpleMatch(tok->tokAt(-1), ") .")) - return tok->tokAt(-1); + if (!Token::simpleMatch(tok, ".")) + return nullptr; + // Skip the specifiers between the parameter list and the trailing return type + tok = tok->previous(); + while (tok) { + if (Token::Match(tok, "const|volatile|&|&&|noexcept|mutable|constexpr|consteval|static")) + tok = tok->previous(); + else if (tok->str() == ")" && Token::Match(tok->link()->previous(), "noexcept|throw")) + tok = tok->link()->tokAt(-2); + else + break; + } + if (Token::simpleMatch(tok, ")")) + return tok; return nullptr; } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index fbc291d238e..3b93ceccfdc 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -241,6 +241,7 @@ class TestTokenizer : public TestFixture { TEST_CASE(vardecl34); TEST_CASE(vardecl35); TEST_CASE(vardecl36); + TEST_CASE(vardecl37); TEST_CASE(vardecl_stl_1); TEST_CASE(vardecl_stl_2); TEST_CASE(vardecl_stl_3); @@ -3069,6 +3070,54 @@ class TestTokenizer : public TestFixture { ASSERT_EQUALS(expected, tokenizeAndStringify(code)); } + void vardecl37() { // #15244 specifiers before a trailing return type + { + const char code[] = "struct S {\n" + " auto f() const -> int;\n" + " auto g() volatile & -> int;\n" + " auto h() && -> int;\n" + " auto i() const noexcept -> int;\n" + "};\n" + "auto S::f() const -> int { int a = 1, b = 2; return a + b; }\n" + "auto S::g() volatile & -> int { int a = 1, b = 2; return a + b; }\n" + "auto S::h() && -> int { int a = 1, b = 2; return a + b; }\n" + "auto S::i() const noexcept -> int { int a = 1, b = 2; return a + b; }\n" + "auto j() noexcept -> int { int a = 1, b = 2; return a + b; }\n" + "auto k() noexcept(true) -> int { int a = 1, b = 2; return a + b; }\n"; + const char expected[] = "struct S {\n" + "auto f ( ) const . int ;\n" + "auto g ( ) volatile & . int ;\n" + "auto h ( ) && . int ;\n" + "auto i ( ) const noexcept . int ;\n" + "} ;\n" + "auto S :: f ( ) const . int { int a ; a = 1 ; int b ; b = 2 ; return a + b ; }\n" + "auto S :: g ( ) volatile & . int { int a ; a = 1 ; int b ; b = 2 ; return a + b ; }\n" + "auto S :: h ( ) && . int { int a ; a = 1 ; int b ; b = 2 ; return a + b ; }\n" + "auto S :: i ( ) const noexcept . int { int a ; a = 1 ; int b ; b = 2 ; return a + b ; }\n" + "auto j ( ) noexcept . int { int a ; a = 1 ; int b ; b = 2 ; return a + b ; }\n" + "auto k ( ) noexcept ( true ) . int { int a ; a = 1 ; int b ; b = 2 ; return a + b ; }"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); + ignore_errout(); + } + { // a structured binding is not a lambda + const char code[] = "struct R { int a; int b; };\n" + "R f();\n" + "auto g() noexcept -> int { auto [p, c]{f()}; return p; }\n"; + ASSERT_EQUALS("struct R { int a ; int b ; } ;\n" + "R f ( ) ;\n" + "auto g ( ) noexcept . int { auto [ p , c ] { f ( ) } ; return p ; }", + tokenizeAndStringify(code)); + ignore_errout(); + } + { // lambdas + const char code[] = "auto l = [](int x) mutable -> int { int a = 1, b = 2; return a + b + x; };\n" + "auto m = [](int x) noexcept(true) -> int { int a = 1, b = 2; return a + b + x; };\n"; + ASSERT_EQUALS("auto l ; l = [ ] ( int x ) mutable . int { int a ; a = 1 ; int b ; b = 2 ; return a + b + x ; } ;\n" + "auto m ; m = [ ] ( int x ) noexcept ( true ) . int { int a ; a = 1 ; int b ; b = 2 ; return a + b + x ; } ;", + tokenizeAndStringify(code)); + } + } + void volatile_variables() { { const char code[] = "volatile int a=0;\n" From f19f0e11ca096857313e2f4272d20ffc7e142d48 Mon Sep 17 00:00:00 2001 From: Tom Sobczynski Date: Thu, 19 Nov 2026 16:26:53 -0500 Subject: [PATCH 2/2] Fix #15245 syntaxError on new[] with a braced initializer ending in a parenthesis In the K&R parameter pass, simplifyVarDecl skips a parenthesised group and steps past its `)`. When that `)` is the last token before tokEnd, the step lands on tokEnd and the loop increment then steps past it, so the loop runs to the end of the token list and raises a syntax error. findLambdaEndScope takes `[2]{(1)}` in `new int[2]{(1)}` for a lambda, which produces such a range. Don't step onto tokEnd. Co-Authored-By: Claude Opus 5.5 --- lib/tokenize.cpp | 3 ++- test/testtokenize.cpp | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index cf5200fa952..5ee8fb715a6 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7594,7 +7594,8 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co if (only_k_r_fpar && finishedwithkr) { if (Token::Match(tok, "(|[|{")) { tok = tok->link(); - if (tok->next() && Token::Match(tok, ") !!{")) + // Don't step onto tokEnd: the loop increment would then step past it + if (tok->next() && tok->next() != tokEnd && Token::Match(tok, ") !!{")) tok = tok->next(); else continue; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 3b93ceccfdc..f445e8cf061 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -242,6 +242,7 @@ class TestTokenizer : public TestFixture { TEST_CASE(vardecl35); TEST_CASE(vardecl36); TEST_CASE(vardecl37); + TEST_CASE(vardecl38); TEST_CASE(vardecl_stl_1); TEST_CASE(vardecl_stl_2); TEST_CASE(vardecl_stl_3); @@ -3118,6 +3119,11 @@ class TestTokenizer : public TestFixture { } } + void vardecl38() { // #15245 range ending in ')' before a braced initializer's '}' + ASSERT_EQUALS("int * q { new int [ 2 ] { ( 1 ) } } ;", + tokenizeAndStringify("int* q{new int[2]{(1)}};")); + } + void volatile_variables() { { const char code[] = "volatile int a=0;\n"