From c1ab08a6ad91b1d09012eb43e27ebecefee641f2 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 21 Aug 2024 15:49:38 +0200 Subject: [PATCH 1/5] Update symboldatabase.cpp --- lib/symboldatabase.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 7573bb2c42b..4eb1cecf43c 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -153,6 +153,8 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes() return endInitList.top().second == scope; }; + bool inIfCondition = false; + 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,7 @@ 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(); } else if (Token::Match(tok, "%var% {")) { endInitList.emplace(tok->linkAt(1), scope); tok = tok->next(); @@ -741,6 +743,8 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes() } else if (tok->str() == "{") { if (inInitList()) { endInitList.emplace(tok->link(), scope); + } else if (inIfCondition) { + inIfCondition = false; } else if (isExecutableScope(tok)) { scopeList.emplace_back(this, tok, scope, Scope::eUnconditional, tok); scope->nestedList.push_back(&scopeList.back()); From 1ecb73773731e921b389d81c624246a39c4d4a85 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 21 Aug 2024 15:51:17 +0200 Subject: [PATCH 2/5] Update testsymboldatabase.cpp --- test/testsymboldatabase.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 70bca8111db..ede4fd94587 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() { { From 7e594864032f16819ebd3a7bb27499af9fada7a1 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 21 Aug 2024 15:53:31 +0200 Subject: [PATCH 3/5] Update symboldatabase.cpp --- lib/symboldatabase.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 4eb1cecf43c..9240f2a09a6 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -735,6 +735,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes() 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 = tok->next(); + inIfCondition = true; } else if (Token::Match(tok, "%var% {")) { endInitList.emplace(tok->linkAt(1), scope); tok = tok->next(); From e05fba1c256904b7d881864b5125223b85c51e72 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 21 Aug 2024 17:27:10 +0200 Subject: [PATCH 4/5] Update symboldatabase.cpp --- lib/symboldatabase.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 9240f2a09a6..43ca8e24bb5 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -153,7 +153,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes() return endInitList.top().second == scope; }; - bool inIfCondition = false; + std::stack inIfCondition; auto addLambda = [this, &scope](const Token* tok, const Token* lambdaEndToken) -> const Token* { const Token* lambdaStartToken = lambdaEndToken->link(); @@ -735,7 +735,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes() 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 = tok->next(); - inIfCondition = true; + inIfCondition.push(tok->link()->next()); } else if (Token::Match(tok, "%var% {")) { endInitList.emplace(tok->linkAt(1), scope); tok = tok->next(); @@ -744,8 +744,8 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes() } else if (tok->str() == "{") { if (inInitList()) { endInitList.emplace(tok->link(), scope); - } else if (inIfCondition) { - inIfCondition = false; + } 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()); From 0e3d91fe9f216e09189c29603e5a2065a991a91f Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 21 Aug 2024 17:40:39 +0200 Subject: [PATCH 5/5] Update symboldatabase.cpp --- lib/symboldatabase.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 43ca8e24bb5..2f729534ee4 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -735,7 +735,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes() 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 = tok->next(); - inIfCondition.push(tok->link()->next()); + inIfCondition.push(scopeStartTok); } else if (Token::Match(tok, "%var% {")) { endInitList.emplace(tok->linkAt(1), scope); tok = tok->next();