Skip to content

Commit b82e866

Browse files
authored
Fix #14744: Invalid AST for braced init and references/pointers (#8549)
1 parent 8bc093c commit b82e866

3 files changed

Lines changed: 21 additions & 15 deletions

File tree

lib/checkstl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,8 +1188,7 @@ void CheckStl::invalidContainer()
11881188
if (info.tok->variable()->isReference() && !isVariableDecl(info.tok) &&
11891189
reaches(info.tok->variable()->nameToken(), tok, nullptr)) {
11901190

1191-
if ((assignExpr && Token::Match(assignExpr->astOperand1(), "& %varid%", info.tok->varId())) || // TODO: fix AST
1192-
Token::Match(assignExpr, "& %varid% {|(", info.tok->varId())) {
1191+
if ((assignExpr && Token::Match(assignExpr->astOperand1()->previous(), "& %varid%", info.tok->varId()))) {
11931192
return false;
11941193
}
11951194

lib/tokenlist.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1837,7 +1837,7 @@ static Token * createAstAtToken(Token *tok)
18371837
}
18381838
typetok = typetok->next();
18391839
}
1840-
if (Token::Match(typetok, "%var% =") && typetok->varId())
1840+
if (Token::Match(typetok, "%var% [={]"))
18411841
tok = typetok;
18421842

18431843
// Do not create AST for function declaration

test/testtokenize.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ class TestTokenizer : public TestFixture {
439439
TEST_CASE(astcompound);
440440
TEST_CASE(astfuncdecl);
441441
TEST_CASE(astarrayinit);
442+
TEST_CASE(astbracedinit);
442443

443444
TEST_CASE(startOfExecutableScope);
444445

@@ -6522,21 +6523,21 @@ class TestTokenizer : public TestFixture {
65226523
tokenizer.createLinks2();
65236524
tokenizer.simplifyCAlternativeTokens();
65246525
tokenizer.list.front()->assignIndexes();
6525-
} else { // Full
6526-
tokenizer.simplifyTokens1("");
6527-
}
65286526

6529-
// set varid..
6530-
for (Token *tok = tokenizer.list.front(); tok; tok = tok->next()) {
6531-
if (tok->str() == "var")
6532-
tok->varId(1);
6533-
}
6527+
// set varid..
6528+
for (Token *tok = tokenizer.list.front(); tok; tok = tok->next()) {
6529+
if (tok->str() == "var")
6530+
tok->varId(1);
6531+
}
65346532

6535-
// Create AST..
6536-
tokenizer.prepareTernaryOpForAST();
6537-
tokenizer.list.createAst();
6533+
// Create AST..
6534+
tokenizer.prepareTernaryOpForAST();
6535+
tokenizer.list.createAst();
65386536

6539-
tokenizer.list.validateAst(false);
6537+
tokenizer.list.validateAst(false);
6538+
} else { // Full
6539+
tokenizer.simplifyTokens1("");
6540+
}
65406541

65416542
// Basic AST validation
65426543
for (const Token *tok = tokenizer.list.front(); tok; tok = tok->next()) {
@@ -7535,6 +7536,12 @@ class TestTokenizer : public TestFixture {
75357536
ASSERT_EQUALS("a2[2[ 12, 34,{", testAst("int a[2][2]{ { 1, 2 }, { 3, 4 } };"));
75367537
}
75377538

7539+
void astbracedinit() {
7540+
ASSERT_EQUALS("ab{", testAst("int &a { b };", AstStyle::Simple, ListSimplification::Full));
7541+
ASSERT_EQUALS("a0{", testAst("int &&a { 0 };", AstStyle::Simple, ListSimplification::Full));
7542+
ASSERT_EQUALS("anullptr{", testAst("int *a { nullptr };", AstStyle::Simple, ListSimplification::Full));
7543+
}
7544+
75387545
#define isStartOfExecutableScope(offset, code) isStartOfExecutableScope_(offset, code, __FILE__, __LINE__)
75397546
template<size_t size>
75407547
bool isStartOfExecutableScope_(int offset, const char (&code)[size], const char* file, int line) {

0 commit comments

Comments
 (0)