Skip to content

Commit 7549fdd

Browse files
tsobczynskiclaude
andcommitted
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 <noreply@anthropic.com>
1 parent 64cee51 commit 7549fdd

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

‎lib/tokenize.cpp‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7511,6 +7511,7 @@ void Tokenizer::simplifyVarDecl(const bool only_k_r_fpar)
75117511
simplifyVarDecl(list.front(), nullptr, only_k_r_fpar);
75127512
}
75137513

7514+
// Returns the closing parenthesis of the parameter list if tok ends a trailing return type
75147515
static Token* isTrailingReturnType(Token* tok)
75157516
{
75167517
while (Token::Match(tok, "%name%|::|>")) {
@@ -7519,8 +7520,20 @@ static Token* isTrailingReturnType(Token* tok)
75197520
else
75207521
tok = tok->tokAt(-1);
75217522
}
7522-
if (tok && Token::simpleMatch(tok->tokAt(-1), ") ."))
7523-
return tok->tokAt(-1);
7523+
if (!Token::simpleMatch(tok, "."))
7524+
return nullptr;
7525+
// Skip the specifiers between the parameter list and the trailing return type
7526+
tok = tok->previous();
7527+
while (tok) {
7528+
if (Token::Match(tok, "const|volatile|&|&&|noexcept|mutable|constexpr|consteval|static"))
7529+
tok = tok->previous();
7530+
else if (tok->str() == ")" && Token::Match(tok->link()->previous(), "noexcept|throw"))
7531+
tok = tok->link()->tokAt(-2);
7532+
else
7533+
break;
7534+
}
7535+
if (Token::simpleMatch(tok, ")"))
7536+
return tok;
75247537
return nullptr;
75257538
}
75267539

‎test/testtokenize.cpp‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ class TestTokenizer : public TestFixture {
241241
TEST_CASE(vardecl34);
242242
TEST_CASE(vardecl35);
243243
TEST_CASE(vardecl36);
244+
TEST_CASE(vardecl37);
244245
TEST_CASE(vardecl_stl_1);
245246
TEST_CASE(vardecl_stl_2);
246247
TEST_CASE(vardecl_stl_3);
@@ -3069,6 +3070,54 @@ class TestTokenizer : public TestFixture {
30693070
ASSERT_EQUALS(expected, tokenizeAndStringify(code));
30703071
}
30713072

3073+
void vardecl37() { // #15244 specifiers before a trailing return type
3074+
{
3075+
const char code[] = "struct S {\n"
3076+
" auto f() const -> int;\n"
3077+
" auto g() volatile & -> int;\n"
3078+
" auto h() && -> int;\n"
3079+
" auto i() const noexcept -> int;\n"
3080+
"};\n"
3081+
"auto S::f() const -> int { int a = 1, b = 2; return a + b; }\n"
3082+
"auto S::g() volatile & -> int { int a = 1, b = 2; return a + b; }\n"
3083+
"auto S::h() && -> int { int a = 1, b = 2; return a + b; }\n"
3084+
"auto S::i() const noexcept -> int { int a = 1, b = 2; return a + b; }\n"
3085+
"auto j() noexcept -> int { int a = 1, b = 2; return a + b; }\n"
3086+
"auto k() noexcept(true) -> int { int a = 1, b = 2; return a + b; }\n";
3087+
const char expected[] = "struct S {\n"
3088+
"auto f ( ) const . int ;\n"
3089+
"auto g ( ) volatile & . int ;\n"
3090+
"auto h ( ) && . int ;\n"
3091+
"auto i ( ) const noexcept . int ;\n"
3092+
"} ;\n"
3093+
"auto S :: f ( ) const . int { int a ; a = 1 ; int b ; b = 2 ; return a + b ; }\n"
3094+
"auto S :: g ( ) volatile & . int { int a ; a = 1 ; int b ; b = 2 ; return a + b ; }\n"
3095+
"auto S :: h ( ) && . int { int a ; a = 1 ; int b ; b = 2 ; return a + b ; }\n"
3096+
"auto S :: i ( ) const noexcept . int { int a ; a = 1 ; int b ; b = 2 ; return a + b ; }\n"
3097+
"auto j ( ) noexcept . int { int a ; a = 1 ; int b ; b = 2 ; return a + b ; }\n"
3098+
"auto k ( ) noexcept ( true ) . int { int a ; a = 1 ; int b ; b = 2 ; return a + b ; }";
3099+
ASSERT_EQUALS(expected, tokenizeAndStringify(code));
3100+
ignore_errout();
3101+
}
3102+
{ // a structured binding is not a lambda
3103+
const char code[] = "struct R { int a; int b; };\n"
3104+
"R f();\n"
3105+
"auto g() noexcept -> int { auto [p, c]{f()}; return p; }\n";
3106+
ASSERT_EQUALS("struct R { int a ; int b ; } ;\n"
3107+
"R f ( ) ;\n"
3108+
"auto g ( ) noexcept . int { auto [ p , c ] { f ( ) } ; return p ; }",
3109+
tokenizeAndStringify(code));
3110+
ignore_errout();
3111+
}
3112+
{ // lambdas
3113+
const char code[] = "auto l = [](int x) mutable -> int { int a = 1, b = 2; return a + b + x; };\n"
3114+
"auto m = [](int x) noexcept(true) -> int { int a = 1, b = 2; return a + b + x; };\n";
3115+
ASSERT_EQUALS("auto l ; l = [ ] ( int x ) mutable . int { int a ; a = 1 ; int b ; b = 2 ; return a + b + x ; } ;\n"
3116+
"auto m ; m = [ ] ( int x ) noexcept ( true ) . int { int a ; a = 1 ; int b ; b = 2 ; return a + b + x ; } ;",
3117+
tokenizeAndStringify(code));
3118+
}
3119+
}
3120+
30723121
void volatile_variables() {
30733122
{
30743123
const char code[] = "volatile int a=0;\n"

0 commit comments

Comments
 (0)