Decide in Clang if the visited CXXRecordDecl is a class, structure, or union

I use Clang to create AST source code from C ++ and RecursiveASTVisitor to move around the tree.

I would like to make a decision when the post declaration is visited, if it is a class, structure or union. I have an overridden function VisitCXXRecordDecl (clang :: CXXRecordDecl) . In this function, I can check any information about CXXRecordDecl that the class offers, but I have no idea how to get the information.

Can anyone help me?

+6
source share
2 answers

Just use the isStruct , isClass and isUnion functions or call getTagKind to get TagKind you can switch on if you want. They are in the TagDecl base class.

+8
source

At run time, C ++ makes no distinction between class and structure, and a union can only be distinguished from the fact that all its data members share the address space.

Thus, the only way to achieve this is to include metadata in your class / struct / union definitions that support the differences that are important to you. For instance:

 typedef enum { class_ct, struct_ct, union_ct } c_type; class foo { public: c_type whattype() { return class_ct; } }; struct bar { public: c_type whattype() { return struct_ct; } }; union baz { public: c_type whattype() { return union_ct; } }; 

// B

+2
source

Source: https://habr.com/ru/post/915106/


All Articles