Skip to content

Commit 1ade97c

Browse files
Partial fix for #11738 Bad AST for array with aggregate initialization (#8396)
1 parent bf42abd commit 1ade97c

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

lib/tokenlist.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -589,10 +589,16 @@ static bool iscpp11init_impl(const Token * const tok)
589589
if (nameToken->str() == ">" && nameToken->link())
590590
nameToken = nameToken->link()->previous();
591591
if (Token::Match(nameToken, "]|*")) {
592-
const Token* newTok = nameToken->link() ? nameToken->link()->previous() : nameToken->previous();
593-
while (Token::Match(newTok, "%type%|::|*") && !newTok->isKeyword())
594-
newTok = newTok->previous();
595-
if (Token::simpleMatch(newTok, "new"))
592+
const Token* tok2 = nameToken;
593+
if (tok2->link()) {
594+
while (tok2 && tok2->link())
595+
tok2 = tok2->link()->previous();
596+
}
597+
else
598+
tok2 = tok2->previous();
599+
while (Token::Match(tok2, "%type%|::|*") && !tok2->isKeyword())
600+
tok2 = tok2->previous();
601+
if (Token::Match(tok2, "new|%var%"))
596602
return true;
597603
}
598604

test/testastutils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ class TestAstUtils : public TestFixture {
8181
ASSERT_EQUALS(true, findLambdaEndToken("[](void) mutable -> const * int { return x; }"));
8282
ASSERT_EQUALS(true, findLambdaEndToken("[](void) constexpr -> const ** int { return x; }"));
8383
ASSERT_EQUALS(true, findLambdaEndToken("[](void) constexpr -> const * const* int { return x; }"));
84-
ASSERT_EQUALS(false, findLambdaEndToken("int** a[] { new int*[2] { new int, new int} }", "[ ]"));
85-
ASSERT_EQUALS(false, findLambdaEndToken("int** a[] { new int*[2] { new int, new int} }", "[ 2"));
84+
ASSERT_EQUALS(false, findLambdaEndToken("int** a[] { new int*[2] { new int, new int} };", "[ ]"));
85+
ASSERT_EQUALS(false, findLambdaEndToken("int** a[] { new int*[2] { new int, new int} };", "[ 2"));
8686
ASSERT_EQUALS(false, findLambdaEndToken("shared_ptr<Type *[]> sp{ new Type *[2] {new Type, new Type}, Deleter<Type>{ 2 } };", "[ 2"));
8787
ASSERT_EQUALS(true, findLambdaEndToken("int i = 5 * []{ return 7; }();", "[", /*checkNext*/ false));
8888
}

test/testtokenize.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ class TestTokenizer : public TestFixture {
437437
TEST_CASE(astenumdecl);
438438
TEST_CASE(astcompound);
439439
TEST_CASE(astfuncdecl);
440+
TEST_CASE(astarrayinit);
440441

441442
TEST_CASE(startOfExecutableScope);
442443

@@ -7492,6 +7493,11 @@ class TestTokenizer : public TestFixture {
74927493
ASSERT_EQUALS("", testAst("::int32_t f();"));
74937494
}
74947495

7496+
void astarrayinit() { // #11738
7497+
ASSERT_EQUALS("a2[12,{", testAst("int a[2]{ 1, 2 };"));
7498+
ASSERT_EQUALS("a2[2[ 12, 34,{", testAst("int a[2][2]{ { 1, 2 }, { 3, 4 } };"));
7499+
}
7500+
74957501
#define isStartOfExecutableScope(offset, code) isStartOfExecutableScope_(offset, code, __FILE__, __LINE__)
74967502
template<size_t size>
74977503
bool isStartOfExecutableScope_(int offset, const char (&code)[size], const char* file, int line) {
@@ -8641,6 +8647,10 @@ class TestTokenizer : public TestFixture {
86418647
"{ 1",
86428648
Token::Cpp11init::CPP11INIT);
86438649

8650+
testIsCpp11init("int a[2]{ 1, 2 }; \n",
8651+
"{ 1",
8652+
Token::Cpp11init::CPP11INIT);
8653+
86448654
ASSERT_NO_THROW(tokenizeAndStringify("template<typename U> struct X {};\n" // don't crash
86458655
"template<typename T> auto f(T t) -> X<decltype(t + 1)> {}\n"));
86468656
ASSERT_EQUALS("[test.cpp:2:22]: (debug) auto token with no type. [autoNoType]\n", errout_str());

0 commit comments

Comments
 (0)