[cfe-dev] about AST of clang

Manuel Klimek klimek at google.com
Tue Apr 24 04:46:08 PDT 2012

See http://www.mail-archive.com/cfe-commits@cs.uiuc.edu/msg49074.html
for a patch that's currently under review that contains an example on
how to implement source-to-source translations crossing TU boundaries
using the rewriter.


On Tue, Apr 24, 2012 at 1:40 PM, Zhe Chen <mr.zhechen at gmail.com> wrote:
>> 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
> Thanks!
> 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