Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I use Clang to build up an AST from C++ source code and RecursiveASTVisitor to traverse the tree.

I would like to decide at a visited declaration of record if it is class, struct 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 thie information.

Can Anyone help me?

like image 739
bmolnar Avatar asked May 07 '12 16:05

bmolnar


2 Answers

Just use the isStruct, isClass, and isUnion member functions, or call getTagKind to get a TagKind value you can switch on if you prefer. They're in the TagDecl base class.

like image 114
Richard Smith Avatar answered Sep 20 '22 13:09

Richard Smith


At runtime, C++ doesn't make a distinction between class and struct, and union is only distinguishable by the fact that its data members all share address space.

So the only way to accomplish this would be to include meta data in your class/struct/union definitions supporting the distinctions that are important to you. For example:

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

like image 34
Bill Weinman Avatar answered Sep 19 '22 13:09

Bill Weinman