diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index faa6f25911d..5857dfbb4a8 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -1193,8 +1193,7 @@ void CheckStl::invalidContainer() if (info.tok->variable()->isReference() && !isVariableDecl(info.tok) && reaches(info.tok->variable()->nameToken(), tok, nullptr)) { - if ((assignExpr && Token::Match(assignExpr->astOperand1(), "& %varid%", info.tok->varId())) || // TODO: fix AST - Token::Match(assignExpr, "& %varid% {|(", info.tok->varId())) { + if ((assignExpr && Token::Match(assignExpr->astOperand1()->previous(), "& %varid%", info.tok->varId()))) { return false; } diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 5f48baea018..6a5a1984682 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -1817,7 +1817,7 @@ static Token * createAstAtToken(Token *tok) } typetok = typetok->next(); } - if (Token::Match(typetok, "%var% =") && typetok->varId()) + if ((Token::Match(typetok, "%var% =") || Token::Match(typetok, "%var% {")) && typetok->varId()) tok = typetok; // Do not create AST for function declaration diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 7b2bee0d80e..b6f84e1ec56 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -439,6 +439,7 @@ class TestTokenizer : public TestFixture { TEST_CASE(astcompound); TEST_CASE(astfuncdecl); TEST_CASE(astarrayinit); + TEST_CASE(astbracedinit); TEST_CASE(startOfExecutableScope); @@ -6522,21 +6523,21 @@ class TestTokenizer : public TestFixture { tokenizer.createLinks2(); tokenizer.simplifyCAlternativeTokens(); tokenizer.list.front()->assignIndexes(); - } else { // Full - tokenizer.simplifyTokens1(""); - } - // set varid.. - for (Token *tok = tokenizer.list.front(); tok; tok = tok->next()) { - if (tok->str() == "var") - tok->varId(1); - } + // set varid.. + for (Token *tok = tokenizer.list.front(); tok; tok = tok->next()) { + if (tok->str() == "var") + tok->varId(1); + } - // Create AST.. - tokenizer.prepareTernaryOpForAST(); - tokenizer.list.createAst(); + // Create AST.. + tokenizer.prepareTernaryOpForAST(); + tokenizer.list.createAst(); - tokenizer.list.validateAst(false); + tokenizer.list.validateAst(false); + } else { // Full + tokenizer.simplifyTokens1(""); + } // Basic AST validation for (const Token *tok = tokenizer.list.front(); tok; tok = tok->next()) { @@ -7535,6 +7536,12 @@ class TestTokenizer : public TestFixture { ASSERT_EQUALS("a2[2[ 12, 34,{", testAst("int a[2][2]{ { 1, 2 }, { 3, 4 } };")); } + void astbracedinit() { + ASSERT_EQUALS("ab{", testAst("int &a { b };", AstStyle::Simple, ListSimplification::Full)); + ASSERT_EQUALS("a0{", testAst("int &&a { 0 };", AstStyle::Simple, ListSimplification::Full)); + ASSERT_EQUALS("anullptr{", testAst("int *a { nullptr };", AstStyle::Simple, ListSimplification::Full)); + } + #define isStartOfExecutableScope(offset, code) isStartOfExecutableScope_(offset, code, __FILE__, __LINE__) template bool isStartOfExecutableScope_(int offset, const char (&code)[size], const char* file, int line) {