[cfe-dev] Clang AST Matcher - Matching nested function and method calls

Manuel Klimek via cfe-dev cfe-dev at lists.llvm.org
Wed Mar 28 01:55:47 PDT 2018


On Wed, Mar 28, 2018 at 7:32 AM David Lai via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> Hi,
>
>
>
> I am quite new to clang AST matchers and wondering if you could help me
> with a match. I am writing a matcher to generate replacements.
>
> Consider this,
>
> SomeClientBuilder SCB(Foo& f) {
>   return SomeClientBuilder.setFoo(f);
> }
>
> class SomeClientBuilder {
>   Fruit apple(…);
>   Fruit pear(…);
>
> };
>
> I would like to change
>     “return SCB(foo).apple(req).get(…)”
> to
>     “return SCB().pear(req).via(foo).get(…)”
>
> I would like to create a matcher that:
>     - matches SCB so it has 1 argument of type Foo
>     - matches method named apple() and is a method in SomeClientBuilder
>
> After many attempts, I came up with two matchers.
>
> 1. cxxMemberCallExpr(callee(cxxMethodDecl(hasName("apple"),
> ofClass(cxxRecordDecl(hasName("SomeClientBuilder "))))))
>     - this matcher does not restrict SCB to have 1 argument of type Foo
>

You should be able to compine this with the "on" matcher for
cxxMemberCallExpr.
See http://clang.llvm.org/docs/LibASTMatchersReference.html

on() takes an expr matcher as argument, with which you can match the nested
call expr.


>
> 2. callExpr(callee(functionDecl(hasName("SCB"))), argumentCountIs(1),
> hasArgument(0, hasType(cxxRecordDecl(hasName("Foo")))))
>     - this matcher does not match the “apple()” methods
>
> I have tried combining the matchers together with no success. What do you
> think would be the best way to accomplish what I want to do?
>
>
>
>
>
> Thanks,
>
> David Lai
>
>
>
>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180328/7c9bf9e6/attachment.html>


More information about the cfe-dev mailing list