Recursive ASTMatcher expressions

Hi my name is Luke Titley,

I’m a c++ developer based in London and I’m experimenting with clang libtooling.
It’s really a lot of fun and already I can see the power of using AST matchers for automatic re-factoring.

clang, really does provide a top notch set of tools.

There’s one thing I’m not 100 % clear on.
Is is possible to create recursive/(self referential) ASTMatcher expressions?

For example, if I wanted a matcher to match this scenario.

int var = 1;
int * foo = &var;
int * bar = foo;
int * baz = bar;
… Add infinitum …

I’d like my matcher to match
foo
bar
and baz

I want to track every pointer that is initialized with a reference to a variable that is initialized with a value of 1.

I imagine this would be done with some sort of self referential definition. Is that correct ?

Thanks in advance,
and thanks for all the hard work!

Luke

Hi my name is Luke Titley,

I’m a c++ developer based in London and I’m experimenting with clang libtooling.
It’s really a lot of fun and already I can see the power of using AST matchers for automatic re-factoring.

clang, really does provide a top notch set of tools.

There’s one thing I’m not 100 % clear on.
Is is possible to create recursive/(self referential) ASTMatcher expressions?

For example, if I wanted a matcher to match this scenario.

int var = 1;
int * foo = &var;
int * bar = foo;
int * baz = bar;
… Add infinitum …

I’d like my matcher to match
foo
bar
and baz

I want to track every pointer that is initialized with a reference to a variable that is initialized with a value of 1.

I imagine this would be done with some sort of self referential definition. Is that correct ?

Well, we have self-referential matchers (equalsBoundNode), but this would also require looping over the initialization sequences, which we don’t support yet.

Currently the easiest way is to get a callback for each pointer, and then do the traversal yourself in the callback.
I’d also be curious whether we can implement matchers that would lead to this, but I think they’d need to be somehow integrated into the matching process (you’d want to memoize based on the matches, otherwise you have an extra O(N) you don’t really need).

Cheers,
/Manuel

Okay thanks that makes sense.

Thank you,
Luke