[cfe-dev] Modifying struct layout

Eli Friedman eli.friedman at gmail.com
Tue Jan 17 14:53:49 PST 2012


On Tue, Jan 17, 2012 at 2:31 PM, Cristiano Giuffrida <c.giuffrida at vu.nl> wrote:
> Hi,
> 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?

Yes, you want a clang plugin.  If you're doing a source-to-source
transformation, you don't need to mutate the AST; just analyze the
declared fields, then use the Rewriter to rearrange/insert fields as
necessary.

-Eli



More information about the cfe-dev mailing list