Skip to content

Commit ab3007f

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 c3665c5 commit ab3007f

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
@@ -7597,7 +7597,8 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co
75977597
if (only_k_r_fpar && finishedwithkr) {
75987598
if (Token::Match(tok, "(|[|{")) {
75997599
tok = tok->link();
7600-
if (tok->next() && Token::Match(tok, ") !!{"))
7600+
// Don't step onto tokEnd: the loop increment would then step past it
7601+
if (tok->next() && tok->next() != tokEnd && Token::Match(tok, ") !!{"))
76017602
tok = tok->next();
76027603
else
76037604
continue;

‎test/testtokenize.cpp‎

Lines changed: 6 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(vardecl38);
244245
TEST_CASE(vardecl_stl_1);
245246
TEST_CASE(vardecl_stl_2);
246247
TEST_CASE(vardecl_stl_3);
@@ -3081,6 +3082,11 @@ class TestTokenizer : public TestFixture {
30813082
ASSERT_EQUALS(expected, tokenizeAndStringify(code));
30823083
}
30833084

3085+
void vardecl38() { // #15245 range ending in ')' before a braced initializer's '}'
3086+
ASSERT_EQUALS("int * q { new int [ 2 ] { ( 1 ) } } ;",
3087+
tokenizeAndStringify("int* q{new int[2]{(1)}};"));
3088+
}
3089+
30843090
void volatile_variables() {
30853091
{
30863092
const char code[] = "volatile int a=0;\n"

0 commit comments

Comments
 (0)