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

David Blaikie dblaikie at gmail.com
Thu Aug 15 08:08:00 PDT 2013


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



More information about the cfe-dev mailing list