[cfe-dev] AST transformations
Vassil Vassilev
vasil.georgiev.vasilev at cern.ch
Fri Mar 11 03:12:21 PST 2011
On 03/11/2011 11:02 AM, Olaf Krzikalla wrote:
> Am 10.03.2011 20:32, schrieb Vassil Vassilev:
>> Here is what I did, I derive from clang::StmtVisitor and
>> clang::DeclVisitor (both of them have RetTy), which I set to
>> clang::Stmt* and clang::Decl* respectively. Then I traverse the
>> TranslationUnit top-down and I can return whatever AST node I want. Like
>> that you can replace the every specific type of AST node you want, when
>> going up. Even you can send to the parent more complex structure (than
>> clang::Stmt*, clang::Decl), containing complex context, which gives
>> hints to the parent how to deal with the returned node.
> To a certain degree I've just sketched that approach in the
> "ConditionalOperator::setCond is gone" thread. However the problem with
> this approach is, that you you can't use all the neat means (e.g.
> rewriting, CFG creation) coming with clang anymore. How have you solved it?
Yes you're right. I haven't solved it, but my case is very narrow and I
don't need to deal with rewriting (which I believe it is not possible)
and CFG creation.
>> Even more, if
>> you inject new node, you should make Sema think that it actually comes
>> from the Parser, which is difficult because you don't have source, right?
> Why should I have to deal with Sema at all? AST transformation and
> rewriting don't care about Sema.
Because I think it is much safer to let Sema create the nodes and not me ;)
You are right that AST transformation and rewriting shouldn't care about
sema in principle. However, in my opinion the clang AST is much closer
to the source code representation than to an AST (defined in the books),
because it contains a lot of information, which in any other case should
be kept in a symbol table. I have read somewhere in clang docs that all
this is intentional, so there isn't much that can be done.
Considering your other post "ConditionalOperator::setCond is gone" I
don't see any easy way of implementing that. Unless there is another
phase, which simplifies the current AST and provides mapping between the
reduced and the full AST. However, I don't think this is a compiler
task. Providing such functionality for few use-cases would be big
performance trade-off.
It would be nice if the compiler provide hook for a tool, which enables
these kind of transformations. Then the tool might have something like
you said. For complex transformations like node removal, the tool can
analyze and check if it make sense or not. Complex changes or node
substitutions may involve cascade of changes, which the tool can
perform. This, however, involves dependency analysis and as Aho says
"dependency analysis is not trivial" :). I think that having that
implemented would enable making clang AST immutable and even more safe
than it is now.
Thanks for raising that discussion!
Cheers,
Vassil
More information about the cfe-dev
mailing list