[cfe-dev] How can I get the detailed information of Structure/Union

Gábor Kozár kozargabor at gmail.com
Sat Aug 17 07:10:51 PDT 2013


> *If I had to guess, I would assume somewhere on the ASTContext, if it's not
on the clang Type itself - but I could be wrong.*
*
*
You're correct, it's called ASTContext::getTypeSize:* *
http://clang.llvm.org/doxygen/classclang_1_1ASTContext.html#a3e16bd856974e4ac9710e0c507a60cee

> *And is there any method that can provide me the information of alignment
for some structure type?*

To get such information - like type size, and alignment, and so on - you
usually will want to check the ASTContext type first. There is a method for
getting alignment, but finding it among ASTContext's methods falls to you.
:)


2013/8/15 David Blaikie <dblaikie at gmail.com>

> On Thu, Aug 15, 2013 at 1:26 AM, Arthur Yoo <phjy007 at gmail.com> wrote:
> > David:
> >
> > Thank you very much for your help. Now I can get the the information of a
> > structure's fields.   :-)
> >
> > By the way, I want to know that does Clang provide us the methods to get
> the
> > length of some data type? For examples, the length of int is 4 bytes, the
> > length of char is one byte and so on.
> > And is there any method that can provide me the information of alignment
> for
> > some structure type?
>
> I imagine you could discover what code to use for this by, say,
> writing the code:
>
> int main() {
>   static_assert(sizeof(int) == 42, "foo");
> }
>
> compiling it, breaking on the compilation error, then working
> backwards through the stack/calls to see where the sizeof value was
> derived. I tend to take this sort of approach when I don't know where
> something is in the compiler.
>
> If I had to guess, I would assume somewhere on the ASTContext, if it's
> not on the clang Type itself - but I could be wrong.
>
> >
> > Thank you.
> >
> >
> >
> > 2013/8/15 David Blaikie <dblaikie at gmail.com>
> >>
> >> On Wed, Aug 14, 2013 at 7:09 AM, Arthur Yoo <phjy007 at gmail.com> wrote:
> >> > Hi all,
> >> >
> >> > I can use isStructureType() and isUnionType() to check whether an Expr
> >> > is
> >> > structure type or union type. Now I want to get the structure/union
> >> > type's
> >> > detailed information, such as the fields and their types of a
> structure.
> >> > How
> >> > can I get these information? Thanks a lot.
> >>
> >> So you have a Type already (since isStructureType isn't a member of
> >> Expr, it's a member of Type), so looking at the Type documentation:
> >>
> >> http://clang.llvm.org/doxygen/classclang_1_1Type.html
> >>
> >> & then follow through to the source of isStructureType (
> >> http://clang.llvm.org/doxygen/Type_8cpp_source.html#l00366 ) you'll
> >> see it's simply:
> >>
> >> if (const RecordType *RT = getAs<RecordType>())
> >>   return RT->getDecl()->isStruct();
> >> return false;
> >>
> >> So you can do something similar -
> >> cast<RecordDecl>(cast<RecordType>(T)->getDecl()) and then once you've
> >> got the RecordDecl you can use its API to access the list of members,
> >> etc. (you may need to make sure it's a definition of a record, not
> >> just a declaration, etc)
> >> >
> >> > --
> >> > Best regards,
> >> > Arthur Yoo
> >> >
> >> > _______________________________________________
> >> > cfe-dev mailing list
> >> > cfe-dev at cs.uiuc.edu
> >> > http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
> >> >
> >
> >
> > --
> > Best regards,
> > Arthur Yoo
> _______________________________________________
> 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/20130817/29772010/attachment.html>


More information about the cfe-dev mailing list