[cfe-users] Matcher for templated variable

Jonas Toth via cfe-users cfe-users at lists.llvm.org
Tue Aug 17 11:03:51 PDT 2021

Hey Robert,

I can highely recommend `godbolt.org`, it has support for clang-query as
additional tool when you select clang as the compiler as well as AST output.

Additionally: https://clang.llvm.org/docs/LibASTMatchersReference.html

There you get all the possible matchers you can combine. You can check
out clang-tidy code for checks do something similar and get inspiration
on how to combine matchers, too!


Step 1:

Check the AST output and what nodes you want to retrieve.

Step 2:

Do a wide matching with normal node matchers.

Step 3:

Narrow the matches down with special additional properties that you can
filter for.


Match 2 is related to a `declRefExpr`. You can match on a variable and
then find all references to that variable through `declRefExpr()`. It
might be the operator-call you are interested too (or both ;)).

Match 3 is a `fieldDecl` that you need to narrow down.

Match 4 is a `memberExpr`.

You will mostly figure out what to look for once you see the AST.

If you want one matcher to find them all you need some specialized
sub-matchers. You can then combine them with `anyOf() / allOf() / unless()`.

It might help to create a small little clang-tidy check and just do your
matching there and write some warnings for each match. I think its
easier to test on real code once the matchers get more complicated or
you want to test it out on a project.

I am unsure if `clang-query` can already perform such an operation on a
project, I know that there was work on this! Maybe you can research that
first to save some work.


Beware of templates: every instantiation might create the AST matches
again (you will see in the AST).
It really depends on what you want to do.

If you want to create some transformations the multiple instantiations
might interfere.
If you develop on windows you must ensure that the templates are
actually instantiated or use `-fno-delayed-template-parsing`.

I hope this mostly general answer guides you into a good direction. If
not, don't hesitate to ask again! :) (But I am likely slow to answer,
sry :/)

Best Regards

Am 09.08.21 um 00:37 schrieb Robert Ankeney via cfe-users:
> I'm looking to create AST matchers for variables that are templated.
> For example, in the code:
> template <class T> class Class  // template <(class or typename or
> int/float/etc) T>
> {
> public:
> void Func(T* Param)            // 1) match Param
> {
>    Param = nullptr;                 // 2) match Param
> }
> T* Var = nullptr;                    // 3) match Var
> };
> int main()
> {
>    Class<float> MyClass;
>    float FloatVal;
>    MyClass.Var = &FloatVal;  // 4) match Var
> }
> Ideally, I'd like one matcher to find each of the 4 matches, but I
> suspect I need individual matchers for each of them.  For the first
> match above I have varDecl(hasType(templateTypeParmType())), but I
> haven't had luck with clang-query in finding the other 3. Any help
> would be much appreciated!
> Thanks,
> Robert
> _______________________________________________
> cfe-users mailing list
> cfe-users at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20210817/bfd264b7/attachment.html>

More information about the cfe-users mailing list