Getting RecordDecl from CXXBaseSpecifier

Hello all. New to cfe-dev (and participating in mailing lists in general).

The question I'm hoping to have answered is as simple as the subject
implies. I have a CXXRecordDecl,
and I am trying to determine which of its bases is derived from a
class that I know this CXXRecordDecl
is also derived from.

class A;
class B : public A;
class C;

class D : public B, public C
Looking for which base D gets its A from.

I am also seeking general advice for my project as it pertains to my
usage of clang. I am using
libtooling and ASTMatchers to create Python bindings for a game engine.

This is some sample input I am using for my generator: [test-input].

When attempting to locate which base of MyBaz and MyDerived gets their
inheritance of Colossus::MyBase,
I've tried the following strategy: Obtain the type of the base
specifier, and using an appropriate
DeclContext, look up the declaration. Once I have the declaration, I
can use its methods to determine if
it's derived from Colossus::MyBase (or is that class).

// Get the qualified name of the base
QualType type = aBaseSpecifier->getType();
DeclarationName baseDeclName( type.getBaseTypeIdentifier() );

// Grab a context to look up the decl
const DeclContext* lookupContext = theCxxRecordDecl->getLookupContext();

DeclContext::lookup_const_result lr = lookupContext->lookup(baseDeclName);

This strategy seems somewhat valid. When I use the "LookupContext" of
the derived class, the base of MyBaz
is correctly identified as MyDerived. However, MyDerived's base is not
found by that context. Alternately,
I tried to use the ASTContext's TranslationUnitDecl as my lookup
context thinking that if I start at the root
DeclContext everything should be found. In doing that however, all of
my lookups failed. I fear I'm taking
a completely wrongheaded approach here.


Looking up the name of the type is too complicated. Also, why do you want the decl? If you want to compare for equality, the type is more interesting.

In any case, the QualType returned by CXXBaseSpecifier::getType() will always (perhaps unless you're dealing with a template definition) be a RecordType underneath; just use type->getAs<RecordType>() to get the RecordType. Then you can use getDecl() on the result, which returns a RecordDecl, but in C++ every RecordDecl is a CXXRecordDecl, so you can just cast<CXXRecordDecl> it.


I want to retrieve the Decl so that I may use its isDerivedFrom
method. I know that my target class is derived from class A. I am
trying to determine which base of class is derived from class A.

My main misunderstanding was not knowing how Decls and Types are
related. I believe your solution of using getAs and getDecl is
precisely what I was looking for!


You can also use getType()->getAsCXXRecordDecl(), which is a little more convenient and also copes with the obscure case of deriving from the injected-class-name within a class template.

Ugh, I can't believe that I missed that method in the docs for Type.
Sorry to waste everyone's time with (what I now realize) is a trivial

Thanks for the quick responses!