<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Hey Robert,</p>
<p>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.<br>
</p>
<p>Additionally:
<a class="moz-txt-link-freetext" href="https://clang.llvm.org/docs/LibASTMatchersReference.html">https://clang.llvm.org/docs/LibASTMatchersReference.html</a></p>
<p>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!</p>
<p>---<br>
</p>
<p>Step 1:</p>
<p>Check the AST output and what nodes you want to retrieve.</p>
<p>Step 2:</p>
<p>Do a wide matching with normal node matchers.</p>
<p>Step 3:</p>
<p>Narrow the matches down with special additional properties that
you can filter for.<br>
</p>
<p>---<br>
</p>
<p>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 ;)).<br>
</p>
<p>Match 3 is a `fieldDecl` that you need to narrow down.</p>
<p>Match 4 is a `memberExpr`.</p>
<p>You will mostly figure out what to look for once you see the AST.</p>
<p>If you want one matcher to find them all you need some
specialized sub-matchers. You can then combine them with `anyOf()
/ allOf() / unless()`.</p>
<p>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.</p>
<p>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.<br>
</p>
<p>---<br>
</p>
<p>Beware of templates: every instantiation might create the AST
matches again (you will see in the AST).<br>
It really depends on what you want to do.</p>
<p>If you want to create some transformations the multiple
instantiations might interfere.<br>
If you develop on windows you must ensure that the templates are
actually instantiated or use `-fno-delayed-template-parsing`.</p>
<p>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 :/)</p>
<p>Best Regards<br>
</p>
<div class="moz-cite-prefix">Am 09.08.21 um 00:37 schrieb Robert
Ankeney via cfe-users:<br>
</div>
<blockquote type="cite"
cite="mid:CAJL6GZpnr9pbwKCudYC+F27OPoYvK6PFwJ9z8QPP+dRfxNVVDA@mail.gmail.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<div dir="ltr">I'm looking to create AST matchers for variables
that are templated. For example, in the code:
<div><br>
</div>
<div>template <class T> class Class // template
<(class or typename or int/float/etc) T><br>
{<br>
public:<br>
<br>
void Func(T* Param) // 1) match Param<br>
{<br>
Param = nullptr; // 2) match Param<br>
}<br>
<br>
T* Var = nullptr; // 3) match Var<br>
};<br>
<br>
int main()<br>
{<br>
Class<float> MyClass;<br>
float FloatVal;<br>
MyClass.Var = &FloatVal; // 4) match Var<br>
}<br>
</div>
<div><br>
</div>
<div>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!</div>
<div><br>
</div>
<div>Thanks,</div>
<div>Robert</div>
<div><br>
</div>
<div><br>
</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
cfe-users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:cfe-users@lists.llvm.org">cfe-users@lists.llvm.org</a>
<a class="moz-txt-link-freetext" href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users</a>
</pre>
</blockquote>
</body>
</html>