source-to-source transformation: how-to?

Hi All,

I intend to use clang for C source-to-source transformation. I want to enhance
the code with additional redundance so that it will be possible to detect
hardware errors.

I had no problems to obtain the AST. Now I am trying to modify it. Let's take
for example the CompoundStmt. It seems that it is only possible to append
additional statments to it. But I would like to insert new ones on arbitrary
positions. So, my idea was to read the old AST and produce a new one which
will contain my modifications. My question is: Is this the way to go? Did I
overlook another simpler solution?

Thanks for any help and suggestions,

Hi Ute,

Adding statements to CompoundStmt is certainly one way to do things, although I'm not certain if the C pretty-printer is completely functional (yet) to generate completely compilable C code in all cases. A downside of this approach is that you also lose the comments of the original source code, macros and header files are expanded/included, etc.

A potentially better approach for your needs is to use the rewriter module, although I imagine it really depends on the kinds of transformations you want to do. With the rewriter the idea is to not change the AST and then pretty-print it out; rather changes to the source can be made by selectively eliminating/replacing/adding statements and then those edits are mirrored in situ in the original source code (I'm being vague because I don't know the exact details of the interface). The advantage of this approach is that by just editing certain statements and having just those changes pretty-printed out, the comments of the original file can be preserved, #included header files do not have to be inlined, and so on. I haven't used the rewriter myself, but others have and may be able to wade it more with specific advice.

I guess what would help is if you could give us all a better idea of what kind of transformations you are interested in doing.


Hello Ted and Taras,

A potentially better approach for your needs is to use the rewriter
module, although I imagine it really depends on the kinds of
transformations you want to do.

Many thanks for that hint. I am still quite disappointed that I overlooked the

I think that the rewriter is sufficient. I will not change the general
structure of the program. I will just replace instructions such as additions
and so on with code which implements those instruction in a more safe way.

At some points such as loops or if statements I am required to add a bit more
code which even will contain a bit of additional control flow. Furthermore, I
will move the computation of the conditions so that for example if(a<b){...}
will become int c = a < b; if ( c ){...}. It seems to me that the rewriter
supports that too.

There is an arguable more mature alternative to clang for this. Elsa
supports the kind of workflow that you prose here. People have done
similar transforms using elsa.

I had a look at elsa but I must admit, I was faster in getting results with
clang and the rewriter. I will have a look at clang and elsa to decide which
is better suited for my needs. In principle everything could be done with the
rewriter. But maybe elsa is more comfortable.

Many thanks again,