[clang] Traverse init-captures while indexing
authorNathan Ridge <zeratul976@hotmail.com>
Tue, 8 Sep 2020 00:10:45 +0000 (20:10 -0400)
committerNathan Ridge <zeratul976@hotmail.com>
Tue, 22 Sep 2020 06:32:09 +0000 (02:32 -0400)
Fixes https://github.com/clangd/clangd/issues/496

Differential Revision: https://reviews.llvm.org/D87257

clang-tools-extra/clangd/unittests/XRefsTests.cpp
clang/lib/Index/IndexBody.cpp
clang/test/Index/cxx14-lambdas.cpp

index d2337dcbd7b31886e81e8d62cda93337bf3bfdc5..2176b59ab9ede43218cea24887ea54163e7243a0 100644 (file)
@@ -1582,6 +1582,12 @@ TEST(FindReferences, WithinAST) {
           f.[[^~]]Foo();
         }
       )cpp",
+      R"cpp(// Lambda capture initializer
+        void foo() {
+          int [[w^aldo]] = 42;
+          auto lambda = [x = [[waldo]]](){};
+        }
+      )cpp",
   };
   for (const char *Test : Tests) {
     Annotations T(Test);
index 01cf559d7057e890a7de9f1a14a89048cc610fce..e4944fd0fc3b7197b2ff84d8584a7831048754f5 100644 (file)
@@ -391,11 +391,13 @@ public:
     if (C->capturesThis() || C->capturesVLAType())
       return true;
 
+    if (!base::TraverseStmt(Init))
+      return false;
+
     if (C->capturesVariable() && IndexCtx.shouldIndexFunctionLocalSymbols())
       return IndexCtx.handleReference(C->getCapturedVar(), C->getLocation(),
                                       Parent, ParentDC, SymbolRoleSet());
 
-    // FIXME: Lambda init-captures.
     return true;
   }
 
index abca65b518bc9801f11bd52669f5a95d0db2bca8..0c9aaec6c88e5ab62c3083d1128d21d99a730e6b 100644 (file)
@@ -28,7 +28,9 @@ struct X {
 // CHECK-LOAD: cxx14-lambdas.cpp:8:7: ReturnStmt= Extent=[8:7 - 8:29]
 
 // RUN: env CINDEXTEST_INDEXLOCALSYMBOLS=1 c-index-test -index-file -std=c++14 %s | FileCheck -check-prefix=CHECK-INDEX %s
+// CHECK-INDEX: [indexEntityReference]: kind: variable | name: localA | USR: c:cxx14-lambdas.cpp@100@S@X@F@f#@localA | lang: C | cursor: DeclRefExpr=localA:6:9 | loc: 7:27
 // CHECK-INDEX: [indexEntityReference]: kind: variable | name: ptr | USR: c:cxx14-lambdas.cpp@139@S@X@F@f#@Sa@F@operator()#I#1@ptr | lang: C | cursor: VariableRef=ptr:7:20 | loc: 7:20
+// CHECK-INDEX: [indexEntityReference]: kind: variable | name: localB | USR: c:cxx14-lambdas.cpp@100@S@X@F@f#@localB | lang: C | cursor: DeclRefExpr=localB:6:17 | loc: 7:42
 // CHECK-INDEX: [indexEntityReference]: kind: variable | name: copy | USR: c:cxx14-lambdas.cpp@154@S@X@F@f#@Sa@F@operator()#I#1@copy | lang: C | cursor: VariableRef=copy:7:35 | loc: 7:35
 // CHECK-INDEX: [indexDeclaration]: kind: variable | name: x | USR: c:cxx14-lambdas.cpp@170@S@X@F@f#@Sa@F@operator()#I#1@x | lang: C | cursor: ParmDecl=x:7:59 (Definition) | loc: 7:59
 // CHECK-INDEX: [indexEntityReference]: kind: typedef | name: Integer | USR: c:cxx14-lambdas.cpp@T@Integer | lang: C | cursor: TypeRef=Integer:3:13 | loc: 7:51