Skip to content

Commit

Permalink
Fix #12338 "Analysis failed (lambda not recognized)" with trailing ty…
Browse files Browse the repository at this point in the history
…pe in lambda parameter (#5869)
  • Loading branch information
chrchr-github committed Jan 12, 2024
1 parent 14e540a commit 09189b8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
9 changes: 9 additions & 0 deletions lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,15 @@ const Token* findLambdaEndTokenWithoutAST(const Token* tok) {
tok = tok->link()->next();
if (Token::simpleMatch(tok, "(") && tok->link())
tok = tok->link()->next();
if (Token::simpleMatch(tok, ".")) { // trailing return type
tok = tok->next();
while (Token::Match(tok, "%type%|%name%|::|&|&&|*|<|(")) {
if (tok->link())
tok = tok->link()->next();
else
tok = tok->next();
}
}
if (!(Token::simpleMatch(tok, "{") && tok->link()))
return nullptr;
return tok->link()->next();
Expand Down
45 changes: 36 additions & 9 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2814,15 +2814,42 @@ class TestSymbolDatabase : public TestFixture {
ASSERT_EQUALS(3, func->argCount());
}

void functionArgs20() { // #11769
const char code[] = "void f(void *(*g)(void *) = [](void *p) { return p; }) {}";
GET_SYMBOL_DB(code);
ASSERT(db != nullptr);
const Scope *scope = db->functionScopes.front();
const Function *func = scope->function;
ASSERT_EQUALS(1, func->argCount());
const Variable* arg = func->getArgumentVar(0);
TODO_ASSERT(arg->hasDefault());
void functionArgs20() {
{
const char code[] = "void f(void *(*g)(void *) = [](void *p) { return p; }) {}"; // #11769
GET_SYMBOL_DB(code);
ASSERT(db != nullptr);
const Scope *scope = db->functionScopes.front();
const Function *func = scope->function;
ASSERT_EQUALS(1, func->argCount());
const Variable* arg = func->getArgumentVar(0);
TODO_ASSERT(arg->hasDefault());
}
{
const char code[] = "void f() { auto g = [&](const std::function<int(int)>& h = [](int i) -> int { return i; }) {}; }"; // #12338
GET_SYMBOL_DB(code);
ASSERT(db != nullptr);
ASSERT_EQUALS(3, db->scopeList.size());
ASSERT_EQUALS(Scope::ScopeType::eLambda, db->scopeList.back().type);
}
{
const char code[] = "void f() {\n"
" auto g = [&](const std::function<const std::vector<int>&(const std::vector<int>&)>& h = [](const std::vector<int>& v) -> const std::vector<int>& { return v; }) {};\n"
"}\n";
GET_SYMBOL_DB(code);
ASSERT(db != nullptr);
ASSERT_EQUALS(3, db->scopeList.size());
ASSERT_EQUALS(Scope::ScopeType::eLambda, db->scopeList.back().type);
}
{
const char code[] = "void f() {\n"
" auto g = [&](const std::function<int(int)>& h = [](int i) -> decltype(0) { return i; }) {};\n"
"}\n";
GET_SYMBOL_DB(code);
ASSERT(db != nullptr);
ASSERT_EQUALS(3, db->scopeList.size());
ASSERT_EQUALS(Scope::ScopeType::eLambda, db->scopeList.back().type);
}
}

void functionArgs21() {
Expand Down

0 comments on commit 09189b8

Please sign in to comment.