diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 7b8e9b6f947..dd79300bbcb 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -153,6 +153,8 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes() return endInitList.top().second == scope; }; + std::stack inIfCondition; + auto addLambda = [this, &scope](const Token* tok, const Token* lambdaEndToken) -> const Token* { const Token* lambdaStartToken = lambdaEndToken->link(); const Token* argStart = lambdaStartToken->astParent(); @@ -732,7 +734,8 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes() scope->checkVariable(tok->tokAt(2), AccessControl::Local, mSettings); // check for variable declaration and add it to new scope if found else if (scope->type == Scope::eCatch) scope->checkVariable(tok->tokAt(2), AccessControl::Throw, mSettings); // check for variable declaration and add it to new scope if found - tok = scopeStartTok; + tok = tok->next(); + inIfCondition.push(scopeStartTok); } else if (Token::Match(tok, "%var% {")) { endInitList.emplace(tok->linkAt(1), scope); tok = tok->next(); @@ -741,6 +744,8 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes() } else if (tok->str() == "{") { if (inInitList()) { endInitList.emplace(tok->link(), scope); + } else if (!inIfCondition.empty() && tok == inIfCondition.top()) { + inIfCondition.pop(); } else if (isExecutableScope(tok)) { scopeList.emplace_back(this, tok, scope, Scope::eUnconditional, tok); scope->nestedList.push_back(&scopeList.back()); diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index f3389e9a711..0992f24cd81 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -433,6 +433,7 @@ class TestSymbolDatabase : public TestFixture { TEST_CASE(createSymbolDatabaseFindAllScopes6); TEST_CASE(createSymbolDatabaseFindAllScopes7); TEST_CASE(createSymbolDatabaseFindAllScopes8); // #12761 + TEST_CASE(createSymbolDatabaseFindAllScopes9); TEST_CASE(createSymbolDatabaseIncompleteVars); @@ -5849,6 +5850,15 @@ class TestSymbolDatabase : public TestFixture { ASSERT(myst1->scope() != myst2->scope()); } + void createSymbolDatabaseFindAllScopes9() // #12943 + { + GET_SYMBOL_DB("void f(int n) {\n" + " if ([](int i) { return i == 2; }(n)) {}\n" + "}\n"); + ASSERT(db && db->scopeList.size() == 4); + ASSERT_EQUALS(db->scopeList.back().type, Scope::eLambda); + } + void createSymbolDatabaseIncompleteVars() { {