[PATCH] D112491: Add `LambdaCapture`-related matchers.

James King via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 26 11:50:41 PDT 2021


jcking1034 added a comment.

@aaron.ballman for the purpose of these matchers, what @ymandel said is correct - the goal is to allow `LambdaCapture`s themselves to be bindable.



================
Comment at: clang/include/clang/ASTMatchers/ASTMatchers.h:4629-4630
+///   matches `[x](){}`.
+AST_MATCHER_P(LambdaCapture, refersToVarDecl, internal::Matcher<VarDecl>,
+              InnerMatcher) {
+  auto *capturedVar = Node.getCapturedVar();
----------------
ymandel wrote:
> aaron.ballman wrote:
> > The name here is a bit unclear -- whether it is the matcher matching `int x;` or the `x` from the capture is not clear from the name. The comment suggests it's matching `x` from the capture, but I think it's actually matching the `int x;` variable declaration.
> > 
> > Being clear on what's matched here is important when we think about initializers:
> > ```
> > void foo() {
> >   int x = 12;
> >   auto f = [x = 100](){};
> > }
> > ```
> > and
> > ```
> > lambdaExpr(hasAnyCapture(lambdaCapture(refersToVarDecl(hasName("x"), hasInitializer(integerLiteral(equals(100))))))
> > ```
> > Would you expect this to match? (This might be a good test case to add.)
> In a similar vein, do we want a separate matcher on the name of the capture itself? e.g. an overload of `hasName`? And what about matchers for the initializers?  Those don't have to land in this patch, but do you think those would be doable?
I would expect @aaron.ballman's initializer example to match, and I added a similar test case to the one  described. I think that if a capture does not have an initializer, then `refersToVarDecl` will match on the variable declaration before the lambda. However, if a capture does have an initializer, that initializer itself seems to be represented as a `VarDecl` in the AST, which is the `VarDecl` that gets matched.

For that reason, I think we may not need to have a separate matcher on the name of the capture itself. Additionally, since captures with/without initializers are both represented the same way, there may not be a good way to distinguish between them, so matchers for initializers may not be possible.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D112491/new/

https://reviews.llvm.org/D112491



More information about the cfe-commits mailing list