https://github.com/Serafean created
https://github.com/llvm/llvm-project/pull/174116
```
int foo(){
int i = 42;
int j = 43;
auto l1 = [x = i, &j ](){};
}
```
in the lambda capture:
- `x` is visited as VariableRef (referencing an unvisited VarDecl at the same
location)
- `i` is visited as DelcRefExpr (fair enough, same as in an assignment)
- `j` is visited as VariableRef
Visit `x` as a `VarDecl`, and stop visiting it as a `VariableRef`, as it makes
no sense (referencing itself at an unvisited cursor).
>From d93a17e1fd88048541cf6355f32e2533985dff88 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Bedn=C3=A1r?= <[email protected]>
Date: Wed, 31 Dec 2025 17:27:30 +0100
Subject: [PATCH] [libclang]Visit lambda init-capture as vardecl
int foo(){
int i = 42;
int j = 43;
auto l1 = [x = i, &j ](){};
}
in the lambda capture:
- x is visited as VariableRef (referencing an unvisited VarDecl at the same
location)
- i is visited as DelcRefExpr (fair enough, same as in an assignment)
- j is visited as VariableRef
Visit x as a VarDecl, and stop visiting it as a VariableRef, as it makes
no sense (referencing itself at an unvisited cursor).
---
clang/test/Index/cxx14-lambdas.cpp | 4 ++--
clang/tools/libclang/CIndex.cpp | 25 ++++++++++++-------------
2 files changed, 14 insertions(+), 15 deletions(-)
diff --git a/clang/test/Index/cxx14-lambdas.cpp
b/clang/test/Index/cxx14-lambdas.cpp
index 0c9aaec6c88e5..dfcf812ae3016 100644
--- a/clang/test/Index/cxx14-lambdas.cpp
+++ b/clang/test/Index/cxx14-lambdas.cpp
@@ -17,9 +17,9 @@ struct X {
// CHECK-LOAD: cxx14-lambdas.cpp:7:19: CallExpr= Extent=[7:19 - 9:6]
// CHECK-LOAD: cxx14-lambdas.cpp:7:19: UnexposedExpr= Extent=[7:19 - 9:6]
// CHECK-LOAD: cxx14-lambdas.cpp:7:19: LambdaExpr= Extent=[7:19 - 9:6]
-// CHECK-LOAD: cxx14-lambdas.cpp:7:20: VariableRef=ptr:7:20 Extent=[7:20 -
7:23]
-// CHECK-LOAD: cxx14-lambdas.cpp:7:35: VariableRef=copy:7:35 Extent=[7:35 -
7:39]
+// CHECK-LOAD: cxx14-lambdas.cpp:7:20: VarDecl=ptr:7:20 (Definition)
Extent=[7:20 - 7:33]
// CHECK-LOAD: cxx14-lambdas.cpp:7:27: DeclRefExpr=localA:6:9 Extent=[7:27 -
7:33]
+// CHECK-LOAD: cxx14-lambdas.cpp:7:35: VarDecl=copy:7:35 (Definition)
Extent=[7:35 - 7:48]
// CHECK-LOAD: cxx14-lambdas.cpp:7:42: DeclRefExpr=localB:6:17 Extent=[7:42 -
7:48]
// CHECK-LOAD: cxx14-lambdas.cpp:7:59: ParmDecl=x:7:59 (Definition)
Extent=[7:51 - 7:60]
// CHECK-LOAD: cxx14-lambdas.cpp:7:51: TypeRef=Integer:3:13 Extent=[7:51 -
7:58]
diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp
index 32e84248c1b27..57ba389ca5037 100644
--- a/clang/tools/libclang/CIndex.cpp
+++ b/clang/tools/libclang/CIndex.cpp
@@ -3896,19 +3896,18 @@ bool CursorVisitor::RunVisitorWorkList(VisitorWorkList
&WL) {
for (LambdaExpr::capture_iterator C = E->explicit_capture_begin(),
CEnd = E->explicit_capture_end();
C != CEnd; ++C) {
- if (!C->capturesVariable())
- continue;
- // TODO: handle structured bindings here ?
- if (!isa<VarDecl>(C->getCapturedVar()))
- continue;
- if (Visit(MakeCursorVariableRef(cast<VarDecl>(C->getCapturedVar()),
- C->getLocation(), TU)))
- return true;
- }
- // Visit init captures
- for (auto InitExpr : E->capture_inits()) {
- if (InitExpr && Visit(InitExpr))
- return true;
+
+ if (const auto *CV = C->getCapturedVar(); CV && isa<VarDecl>(CV)) {
+ if (CV->isInitCapture()) {
+ // Init capture is a declaration, create VarDecl cursor
+ if (Visit(MakeCXCursor(cast<VarDecl>(CV), TU, RegionOfInterest)))
+ return true;
+ } else
+ // Non init capture is a VariableRef
+ if (Visit(MakeCursorVariableRef(cast<VarDecl>(CV),
C->getLocation(),
+ TU)))
+ return true;
+ }
}
TypeLoc TL = E->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits