How can I mutate some information in the AST, given a Decl* ?

Hi,

I have a Decl* which can be cast into the appropriate Decl types.

I want to remove some specific information about that Decl from the AST.

For example, if it isa ClassTemplateDecl, I need to remove its default template arguments if it has them.

Or if I have an EnumDecl that has a specific type specified, like:

(enum a :int {a,b,c}:wink:

That information needs to be erased.

How can I perform these kinds of changes to a Decl?

Manasij Mukherjee

I’m afraid that you can’t because AST is supposed to be immutable once built. You might be able to achieve this through source to source translation…

Have a look at lib/Sema/TreeTransform.h, it should allow you to transform your existing AST into new one.

Well, I already have a 'new' source.
So, the same could be achieved by making clang forget about the old
declaration.
Can I do that?

If clang already knows about:
template<typename T=int> class Foo;

and I have a new source that is about to get parsed:

template<typename T=int> class Foo {
  //implementation
}

Will it be possible to just 'erase' the old one?

Manasij Mukherjee

Can you tell us what are you trying to do exactly? I’m confused because you asked about modifying ast and now you’re talking about ‘new’ source and I don’t quite get it.

I know very little about this but I know that you can’t mutate the original AST. TreeTransfrom should allow you to generate a new modified AST while preserving invariants. The way I understand this simply removing all default template parameters would break the AST in all the places where you’re instantiating the template with default arguments.

I am trying to (automatically) generate forward declarations which can be included before the original header.
But there are a few conflicts,
Say…template default arguments can only appear once.
Now, the forward declarations must have the default arguments as other things(like functions) may use the type in its argument or return type without specifying all the parameters.

So, I am trying to change the old information when including my original file which has the full definition.
I would prefer not to change the original file as it may be a system header.