Skip to content

Commit c3f3b79

Browse files
authored
Fix #15245 syntaxError on new[] with a braced initializer ending in a parenthesis (#9087)
1 parent 61b2587 commit c3f3b79

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)