Source to Source Transformation guide lines

Hello,

I’m working in a project involving source to source transformation of some arbitrary c code , i’m looking forward to using Clang
my main problem is where to start , i can find approx. no documentation on topic

  1. how complete is the CIndex interface , what limitations can it impose ?

  2. any existing source to source applications based on clang that can be of help as a starting point ?

  3. can the LLVM Kaleidoscope lessons be of help in understanding how to use clang libraries ?

Thanks,
Mohamed Yousef

Hi,

I haven’t used CIndex so I can’t help you on that, but others of course can help.

To get started, take a look at clang/lib/Frontend/RewriteObjC.cpp. This code rewrites objc to C. There are two approaches for tsource-to-source transformation (Rewriting, AST re-build). Im the Rewriting approach the AST is traversed and the transformed code is generated as you traverse the AST, this is what is used in RewriteObjC. The other approach is to build the transformed AST from the original AST and then use the prettyprinter to generate the code. This approach is flexible and allows you to apply a chain of transformations on the AST.

You can copy the Clang main file and start your tool under clang/tools. Try to trace the main file to know how the ObjC rewritter is invoked.

Hope this helps,
Moataz

It's relatively complete for cross-referencing C code, but it does not have enough coverage of Clang's APIs for real source-to-source transformation. We'd like CIndex to move in that direction, but we're not there yet.

  - Doug

Hi,

There are two approaches for tsource-to-source
transformation (Rewriting, AST re-build). Im the Rewriting approach the AST
is traversed and the transformed code is generated as you traverse the AST,
this is what is used in RewriteObjC. The other approach is to build the
transformed AST from the original AST and then use the prettyprinter to
generate the code. This approach is flexible and allows you to apply a chain
of transformations on the AST.

There is a third option as well: traverse the AST and make notes of
changes that need to be made and where, and then search-and-replace in
the source text (e.g. by alternating between coying from the source
and from a modification to a new file). I used that with good results
for a tool that did some fancy search-and-replace on hand-written code
that will stay maintained by humans. The only problem I've run into is
that getting the actual source position for something can be tricky
(but possible) when it's a macro.

Best Regards
Magnus Reftel