[cfe-dev] Getting RecordDecl from CXXBaseSpecifier

Justin Arruda paradoxum at gmail.com
Mon May 13 05:15:49 PDT 2013


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.

i.e.
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.

[test-input] http://pastebin.com/iSCnMmsd

--
Justin Arruda

PS Please call out any poor etiquette on my part! Sorry in advance.



More information about the cfe-dev mailing list