getting info about fields in a RecursiveASTVisitor::VisitFieldDecl override

I’ve got an AST visitor override for class members:

bool VisitFieldDecl( FieldDecl * f )
RecordDecl * r = f->getParent() ;
cout << "CLASS: " << r->getName().str() << endl ;
cout << "MEMBER: " << f->getName().str() << " ( " ;

TypeSourceInfo * t = f->getTypeSourceInfo() ;

TypeLoc TL = t->getTypeLoc() ;

const QualType & q = TL.getType() ;

cout << “TYPE: " << q.getAsString() << " )” << endl ;

return true ;

I’d like to be able to check a few things about the types of the class/struct/union members that this finds:

  • ask if q is a basic type (pointer, reference, char, short, int, …)?
  • if q is an array type, what’s the underlying array type?

(motivation: I’d like to know what all the true dependencies of a given struct is, with an eventual goal of comparing the header file dependencies to the actual structure dependencies … ignoring for now dependencies that are introduced by inline member functions).

I’ve answered part of my own question, by grepping the clang source tree for ArrayTypeLoc, and found I can do something like:

TypeLoc TL = t->getTypeLoc() ;

// don’t care if it’s an array, just want the basic underlying type of the array.
if ( const ArrayTypeLoc *Arr = dyn_cast(&TL) )
TL = Arr->getElementLoc() ;

Am guessing I can probably do something similar to see if the type is a pointer or reference.

Is there any easy way to determine if the underlying type is one of the fundamental types: char, short, int, long (not caring about sign/const/volatile/… variations of these)?

Is you need to drill through the layers until you hit a BuiltinType...