[cfe-dev] [LLVMdev] Dumping memory layout with clang

Reid Kleckner rnk at google.com
Tue Jul 1 11:11:16 PDT 2014


On Tue, Jul 1, 2014 at 10:59 AM, Duncan P. N. Exon Smith <
dexonsmith at apple.com> wrote:

> +cfe-dev, bcc: llvm-dev
>
> > On 2014-Jun-30, at 23:17, Matthias Goldhoorn <
> matthias.goldhoorn at uni-bremen.de> wrote:
> >
> > Dear dev's,
> > first sorry for this "user" question here, but i don't found a llvm-user
> Mailinglist. I hope it is okay to ask here.
>
> This looks related to the clang API, so it probably belongs on cfe-dev
> [1], although cfe-users [2] is another possibility.
>
> [1]: http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
> [2]: http://lists.cs.uiuc.edu/mailman/listinfo/cfe-users
>
> >
> > Hi search for a way to dump the memory layout of a
> class/structure/datatype with clang (x64 primary target).
> > I have a simple application based on [1].
>

Is Clang's -fdump-record-layouts flag sufficient for your needs?

$ cat t.cpp
struct A {
  char a;
  int b;
};
int x[sizeof(struct A)];

$ clang -c t.cpp -Xclang -fdump-record-layouts

*** Dumping AST Record Layout
   0 | struct A
   0 |   char a
   4 |   int b
     | [sizeof=8, align=4
     |  nvsize=8, nvalign=4]

It's an undocumented -cc1 option so you have to escape it with -Xclang, and
you need to use 'sizeof(MyStruct)' in the source code somewhere, but it's
good for answering quick questions.


> > I also added this function
> >
> >
> >      bool VisitFieldDecl(FieldDecl *F)
> >     {
> >       F->dump();
> >       std::cerr << F->getQualifiedNameAsString()  << " " <<
> F->getBitWidthValue(*Context) << " " << std::endl;
> >       F->dump() ;
> >       std::cerr << "-----------------------------------------" <<
> std::endl;
> >       return true;
> >     }
> >
> >
> > Unfortunately getBitWidthValue also returns zero for my types.
> >
> > I need the complete memory-layout recursively for each class and all
> nested types. Including sizes/offsets.
> >
> > Maybe the AST is the wrong place, and i need a other hook to start, can
> you give me some hints?
> >
> >   [1]: http://clang.llvm.org/docs/RAVFrontendAction.html
> >
> > --
> >  Dipl.-Inf. Matthias Goldhoorn
> >  Space and Underwater Robotic
> >
> >  Universität Bremen
> >  FB 3 - Mathematik und Informatik
> >  AG Robotik
> >  Robert-Hooke-Straße 5
> >  28359 Bremen, Germany
> >
> >  Tel.:     +49 421 178 45-4193
> >  Zentrale: +49 421 178 45-6550
> >  Fax:      +49 421 178 45-4150
> >  E-Mail:
> > matthias.goldhoorn at uni-bremen.de
> >
> >
> >  Weitere Informationen:
> > http://www.informatik.uni-bremen.de/robotik
> > _______________________________________________
> > LLVM Developers mailing list
> > LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140701/91886f08/attachment.html>


More information about the cfe-dev mailing list