Raks
Raks

Reputation: 1763

clang-AST traversal - How to get member variables of a class

I want to traverse the AST of a simple class having one member variable and one method. I have figured out that the class is represented as CXXRecordDecl.

What is the api within CXXREcordDecl to get the list of member variables which are represented as FieldDecl ?

Upvotes: 3

Views: 1448

Answers (1)

Benjamin Bannier
Benjamin Bannier

Reputation: 58594

The fields can be retrieved with RecordDecl::fields (there exist also methods the get the begin and end iterators of that range), e.g. for a CXXRecordDecl

CXXRecordDecl* cl = ...;
for (const auto& field : cl->fields) {
    const auto& name = field->getName();
    const auto field_cl = field->getType()->getAsCXXRecordDecl(); 
}

Similarly you would access the methods with methods().

Upvotes: 5

Related Questions