From addMatcher(varDecl(...)) -> callExpr

Hi,

(let me know if requests like this should go to the “users” list instead)

I’m playing around with the tooling facilities in clang, and have been using RemoveCStrCalls.cpp as an example.

Here is the problem I’m trying to solve:

I have a set of variables of a given type, I would like to refactor to use another type. To do this, I need to change the template arguments, and re-organize the arguments to the constructor.

Example:

Say we have: Foo<int, long> my_foo(1, 2);

We want: Bar<my_int, my_long> my_bar(my_int(1), my_long(2));

To find all the instances of Foo, I have written a matcher like this:

Finder.addMatcher(
id(“foo_instance”,
varDecl(hasType(recordDecl(hasName(“::Foo”))))),
&Callback);

In my callback function I have managed to get access to the template arguments as I wanted to, and I got access to the instance name. So far so good…

But I can not figure out how I get access to the constructor arguments. I think I want to convert the VarDecl type to a CallExpr but I’m not sure that this is the right approach.

In my callback rutine I have the following code:

const VarDecl *decl = Result.Nodes.getNodeAs(“foo_instance”);
auto expr = decl->getInit();

And then I do not know how to continue…

A hint will be most appreciated.

Best regards
Allan W. Nielsen

I finally found what I was looking for:

for (auto i = expr->child_begin(); i != expr->child_end(); ++i, ++cnt)

Thanks for a great tool :slight_smile: