Dumping memory layout with clang

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.

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].

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]:

+cfe-dev, bcc: llvm-dev

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

+cfe-dev, bcc: llvm-dev

>
> 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.