[cfe-dev] source-to-source transformation: how-to?

Ted Kremenek kremenek at apple.com
Mon Feb 18 12:03:16 PST 2008


On Feb 18, 2008, at 12:26 AM, Ute Wappler wrote:

> 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?

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.

Ted



More information about the cfe-dev mailing list