diff --git a/plugins/clang/duchain/builder.cpp b/plugins/clang/duchain/builder.cpp --- a/plugins/clang/duchain/builder.cpp +++ b/plugins/clang/duchain/builder.cpp @@ -374,6 +374,8 @@ CXChildVisitResult buildUse(CXCursor cursor); CXChildVisitResult buildMacroExpansion(CXCursor cursor); + + template CXChildVisitResult buildCompoundStatement(CXCursor cursor); CXChildVisitResult buildCXXBaseSpecifier(CXCursor cursor); CXChildVisitResult buildParmDecl(CXCursor cursor); @@ -1215,11 +1217,12 @@ return CXChildVisit_Recurse; } +template CXChildVisitResult Visitor::buildCompoundStatement(CXCursor cursor) { - if (m_parentContext->context->type() == DUContext::Function) + if (CK == CXCursor_LambdaExpr || m_parentContext->context->type() == DUContext::Function) { - auto context = createContext(cursor); + auto context = createContext(cursor); CurrentContext newParent(context, m_parentContext->keepAliveContexts); PushValue pushCurrent(m_parentContext, &newParent); clang_visitChildren(cursor, &visitCursor, this); @@ -1560,7 +1563,9 @@ case CXCursor_MacroExpansion: return visitor->buildMacroExpansion(cursor); case CXCursor_CompoundStmt: - return visitor->buildCompoundStatement(cursor); + return visitor->buildCompoundStatement(cursor); + case CXCursor_LambdaExpr: + return visitor->buildCompoundStatement(cursor); case CXCursor_CXXBaseSpecifier: return visitor->buildCXXBaseSpecifier(cursor); case CXCursor_ParmDecl: diff --git a/plugins/clang/tests/test_duchain.h b/plugins/clang/tests/test_duchain.h --- a/plugins/clang/tests/test_duchain.h +++ b/plugins/clang/tests/test_duchain.h @@ -88,6 +88,7 @@ void testUsesCreatedForDeclarations(); void testReparseIncludeGuard(); void testExternC(); + void testLambda(); void testReparseUnchanged_data(); void testReparseUnchanged(); void testTypeAliasTemplate(); diff --git a/plugins/clang/tests/test_duchain.cpp b/plugins/clang/tests/test_duchain.cpp --- a/plugins/clang/tests/test_duchain.cpp +++ b/plugins/clang/tests/test_duchain.cpp @@ -1965,6 +1965,27 @@ m_projectController->closeAllProjects(); } +void TestDUChain::testLambda() +{ + TestFile file(QStringLiteral("auto lambda = [](int p1, int p2, int p3) { int var1, var2; };"), QStringLiteral("cpp")); + QVERIFY(file.parseAndWait()); + { + DUChainReadLocker lock; + QVERIFY(file.topContext()); + QCOMPARE(file.topContext()->localDeclarations().size(), 1); + QCOMPARE(file.topContext()->childContexts().size(), 1); + + auto lambdaContext = file.topContext()->childContexts().first(); + + QCOMPARE(lambdaContext->type(), DUContext::Function); + + QCOMPARE(lambdaContext->localDeclarations().size(), 3); + QCOMPARE(lambdaContext->childContexts().size(), 1); + QCOMPARE(lambdaContext->childContexts().first()->type(), DUContext::Other); + QCOMPARE(lambdaContext->childContexts().first()->localDeclarations().size(), 2); + } +} + void TestDUChain::testQtIntegration() { QTemporaryDir includeDir;