Source transformations in frontend plugin before compilation

Hi all,

I want to annotate malloc(sizeof(ty)) calls with the type in the sizeof so that I can use the information in the IR later on (in a transformation plugin). My current approach is to do a somewhat hacky source transformation, replacing the call with:
malloc(({ ty * volatile ptr = NULL; (size_t)(ptr + 1); }))
so that I can take the pointer type and replace the size back with a constant in the IR.

If someone can think of a better way to do this inside a clang frontend plugin, I’m open to that, but currently I’m having issues just getting this to work. I have a working plugin that finds the sizeof expressions and a Rewriter instance that replaces them. However, I don’t know how to apply the changes to the input file so that the input with be parsed again before being passed on to the codegen. There is a Reparse function in ASTUnit, but I cannot find (in the docs or online) how this is supposed to be used.
Does anyone know how to correctly use this?

Cheers,
Taddeüs

Taddeus,

Maybe you will find this thread useful: [cfe-dev] AST modifications that apply to the binary
http://lists.llvm.org/pipermail/cfe-dev/2016-November/051668.html

~Jakub