Search code examples
c++cclangclang++llvm-clang

How to get the structure members from a TagDecl with clang


I have an AST consumer that get all the TagDecl which are structure with

clang::TagDecl::isStruct()

How to get members of the structure (declaration, type) in an array like the FunctionDecl class:

clang::FunctionDecl::getParamDecl(unsigned i)

Or in any other ways?


Solution

  • The TagDecl must be cast to a RecordDecl that have the methods to get the members / fields information.

    clang::TagDecl*t;
    clang::RecordDecl*r;
    clang::RecordDecl::field_iterator jt;
    
    for(jt = r->field_begin(); jt != r->field_end();++jt)
    {
        std::cout << jt->getType().getAsString() << " " << jt->getNameAsString() << std::endl;
    }