[cfe-dev] AST transformations

Reid Kleckner reid.kleckner at gmail.com
Thu Mar 10 08:07:28 PST 2011


On Thu, Mar 10, 2011 at 10:37 AM, Michael Boyer <mwb7w at cs.virginia.edu> wrote:
> I am trying to use Clang to analyze and modify source code at the AST
> level. The class that seems most relevant for AST analysis is
> RecursiveASTVisitor. I have seen some comments on this list indicating
> that the AST is immutable once created. My understanding is that a
> RecursiveASTVisitor would be called _after_ AST creation, making it
> difficult or impossible to modify the AST using this interface.

True.  I've been assured that mutating the AST is very difficult to do
correctly, but I don't fully understand why.  Perhaps it would be a
good idea to write up an FAQ explaining why clang doesn't support it,
with an example of why a simple local modification can break other
parts of the AST (ie switching the cmp condition).

> What classes should I be looking at instead? I know that the
> RewriteObjC class uses ASTConsumer; however, looking at the source
> it's not immediately clear to me whether it is transforming the AST or
> just inserting extra declarations/statements/etc. at the source level.

The recommended approach for transforming the program source is to
analyze the AST and use the source locations in the AST to make
textual, source-level edits.

Reid



More information about the cfe-dev mailing list