Skip to content

Commit deaedb0

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

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

‎lib/tokenize.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7594,7 +7594,8 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co
75947594
if (only_k_r_fpar && finishedwithkr) {
75957595
if (Token::Match(tok, "(|[|{")) {
75967596
tok = tok->link();
7597-
if (tok->next() && Token::Match(tok, ") !!{"))
7597+
// Don't step onto tokEnd: the loop increment would then step past it
7598+
if (tok->next() && tok->next() != tokEnd && Token::Match(tok, ") !!{"))
75987599
tok = tok->next();
75997600
else
76007601
continue;

‎test/testtokenize.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ class TestTokenizer : public TestFixture {
242242
TEST_CASE(vardecl35);
243243
TEST_CASE(vardecl36);
244244
TEST_CASE(vardecl37);
245+
TEST_CASE(vardecl38);
245246
TEST_CASE(vardecl_stl_1);
246247
TEST_CASE(vardecl_stl_2);
247248
TEST_CASE(vardecl_stl_3);
@@ -3118,6 +3119,11 @@ class TestTokenizer : public TestFixture {
31183119
}
31193120
}
31203121

3122+
void vardecl38() { // #15245 range ending in ')' before a braced initializer's '}'
3123+
ASSERT_EQUALS("int * q { new int [ 2 ] { ( 1 ) } } ;",
3124+
tokenizeAndStringify("int* q{new int[2]{(1)}};"));
3125+
}
3126+
31213127
void volatile_variables() {
31223128
{
31233129
const char code[] = "volatile int a=0;\n"

0 commit comments

Comments
 (0)