[cfe-dev] about AST of clang

Zhe Chen mr.zhechen at gmail.com
Tue Apr 24 04:40:39 PDT 2012

> On Apr 22, 2012, at 5:11 AM, Zhe Chen <mr.zhechen at gmail.com> wrote:
>> If I want to do some instrumentation on C source codes, i.e. insert
>> some statements into the codes, I think that the best way may be the
>> following one: analysis the generated AST, find appropriate insert
>> points, and then directly modify the source code using
>> source-to-source transformations which preserve comments and so on,
>> since there is a perfect mapping between AST nodes and the source
>> code.
>> And, according to the above discussions, directly modifying AST nodes
>> is not practical, due to the consistency problem of the modified AST.
>> Is this conclusion correct? Any other opinion?
> Yes, this is a good way to go.
> An alternative, if you're just doing instrumentation, is to extend Clang's IR generation and simply introduce the instrumentation calls into the IR directly. It makes it easier to compile code with instrumentation (just by using your modified compiler) but takes away the ability to compile the instrumented code with some other compiler.
>        - Doug

Concerning the source-to-source transformation, the "Rewriter" class
is the best way of implementation?
In fact, I read the header file, but I found that the class is not
associated with any string of file name.
I mean, how can I modify the source code using the Rewriter class?
Is there any example?

More information about the cfe-dev mailing list