Skip to content

Commit

Permalink
Fix #12943 FP knownConditionTrueFalse with lambda in if condition (#6714
Browse files Browse the repository at this point in the history
)
  • Loading branch information
chrchr-github committed Aug 21, 2024
1 parent e3613c4 commit 0a11422
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
return endInitList.top().second == scope;
};

std::stack<const Token*> inIfCondition;

auto addLambda = [this, &scope](const Token* tok, const Token* lambdaEndToken) -> const Token* {
const Token* lambdaStartToken = lambdaEndToken->link();
const Token* argStart = lambdaStartToken->astParent();
Expand Down Expand Up @@ -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();
Expand All @@ -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());
Expand Down
10 changes: 10 additions & 0 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ class TestSymbolDatabase : public TestFixture {
TEST_CASE(createSymbolDatabaseFindAllScopes6);
TEST_CASE(createSymbolDatabaseFindAllScopes7);
TEST_CASE(createSymbolDatabaseFindAllScopes8); // #12761
TEST_CASE(createSymbolDatabaseFindAllScopes9);

TEST_CASE(createSymbolDatabaseIncompleteVars);

Expand Down Expand Up @@ -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()
{
{
Expand Down

0 comments on commit 0a11422

Please sign in to comment.