[cfe-dev] Modifying struct layout

John McCall rjmccall at apple.com
Tue Jan 17 14:49:44 PST 2012

On Jan 17, 2012, at 2:31 PM, Cristiano Giuffrida wrote:
> I'm interested in programmatically modifying the layout of arbitrary
> (non-packed) structs in C programs using a source-to-source
> transformation. In particular, I would like to: (1) reorder the
> members of a struct basing on a predetermined criteria; (2) add
> predefined padding (or dummy members of a predefined size) at the
> beginning of the struct and between consecutive struct members.
> LLVM types are immutable and I don't think the transformation can be
> implemented as an LLVM pass. I'm considering using clang (ideally a
> clang plugin) for the job, but I'm not sure this is possible/easy to
> do.
> Is there any way to achieve (1) and/or (2) with clang? For example,
> changing an existing type declaration
> (clang::TypeDecl::setTypeForDecl()) with a newly-created type or
> modifying ASTRecordLayout directly?

If you want to do source-to-source translation, I don't understand why
you care about modifying the ASTs at all.  Just find the struct
declarations you care about, create a similar struct with the layout
you want, print that out, and have Clang's rewriter replace the original
source range with the new text.


More information about the cfe-dev mailing list