[cfe-dev] about AST of clang

Douglas Gregor dgregor at apple.com
Mon Apr 23 08:27:56 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

More information about the cfe-dev mailing list