Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #12943 FP knownConditionTrueFalse with lambda in if condition #6714

Merged
merged 5 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading