Commit dd038eb1 authored by Guillaume Racicot's avatar Guillaume Racicot Committed by Kevin Funk
Browse files

Make lambda introduce a context in DU chain

Summary:
Lambda in clang completion were not introducing a context. This
had the effect of making all local variable in a global lambda
highlighted as a global

BUG: 387994

Reviewers: #kdevelop, mwolff

Reviewed By: #kdevelop, mwolff

Subscribers: kfunk, mwolff, kdevelop-devel

Differential Revision: https://phabricator.kde.org/D11303

(cherry picked from commit 335786a1)
parent 2d229fd9
......@@ -374,6 +374,8 @@ struct Visitor
CXChildVisitResult buildUse(CXCursor cursor);
CXChildVisitResult buildMacroExpansion(CXCursor cursor);
template<CXCursorKind CK>
CXChildVisitResult buildCompoundStatement(CXCursor cursor);
CXChildVisitResult buildCXXBaseSpecifier(CXCursor cursor);
CXChildVisitResult buildParmDecl(CXCursor cursor);
......@@ -1215,11 +1217,12 @@ CXChildVisitResult Visitor::buildMacroExpansion(CXCursor cursor)
return CXChildVisit_Recurse;
}
template<CXCursorKind CK>
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<CXCursor_CompoundStmt, DUContext::Other>(cursor);
auto context = createContext<CK, CK == CXCursor_LambdaExpr ? DUContext::Function : DUContext::Other>(cursor);
CurrentContext newParent(context, m_parentContext->keepAliveContexts);
PushValue<CurrentContext*> pushCurrent(m_parentContext, &newParent);
clang_visitChildren(cursor, &visitCursor, this);
......@@ -1560,7 +1563,9 @@ CXChildVisitResult visitCursor(CXCursor cursor, CXCursor parent, CXClientData da
case CXCursor_MacroExpansion:
return visitor->buildMacroExpansion(cursor);
case CXCursor_CompoundStmt:
return visitor->buildCompoundStatement(cursor);
return visitor->buildCompoundStatement<CXCursor_CompoundStmt>(cursor);
case CXCursor_LambdaExpr:
return visitor->buildCompoundStatement<CXCursor_LambdaExpr>(cursor);
case CXCursor_CXXBaseSpecifier:
return visitor->buildCXXBaseSpecifier(cursor);
case CXCursor_ParmDecl:
......
......@@ -1965,6 +1965,27 @@ void TestDUChain::testGccCompatibility()
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;
......
......@@ -88,6 +88,7 @@ private Q_SLOTS:
void testUsesCreatedForDeclarations();
void testReparseIncludeGuard();
void testExternC();
void testLambda();
void testReparseUnchanged_data();
void testReparseUnchanged();
void testTypeAliasTemplate();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment