Plugin clang


I am trying to implement a plugin for clang that prints all c++ classes and they are base classes.

I can’t find in Doxygen how to access the base class’s name.

Can someone help finish this piece of code(see below)?



class ClassFinderConsumer : public ASTConsumer {
virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) {
const Decl *D = *i;
if (const CXXRecordDecl *ND = dyn_cast(D)) {
llvm::errs() << "Class: " << ND->getNameAsString() << "\nBase(s): ";
for (const auto& ItBase : ND->bases()) {
const QualType& T = ItBase.getType();

// Then…

//llvm::errs() << ItBase.getType() << " ";
llvm::errs() << “\n”;

return true;

Call T->getAsCXXRecordDecl() and see if you get one back. You might not get one if the base is a template parameter. Then you can call RD->getName() for the DeclarationName (which could be none) and try to print it.