[cfe-dev] printing name of owning anonymous structure in RecursiveASTVisitor::VisitFieldDecl?

Peeter Joot peeter.joot at gmail.com
Tue Dec 11 08:17:11 PST 2012


With an AST field visitor of the following form:

   bool VisitFieldDecl( FieldDecl * f )
   {
      RecordDecl * r = f->getParent() ;

      TypeSourceInfo * t = f->getTypeSourceInfo() ;

      TypeLoc TL = t->getTypeLoc() ;

      if ( const ArrayTypeLoc *Arr = dyn_cast<ArrayTypeLoc>(&TL) )
      {
         TL = Arr->getElementLoc() ;
      }

      const QualType & q = TL.getType() ;

      cout
         << r->getName().str()
         << " : " << q.getAsString() << endl ;

      return true ;
   }

I get a printout of

<classname> : type-dependence.  Example:

struct Test
{
   int a, b ;
} ;

gives:

Test : int
Test : int

For anonymous structs like:

struct TestA
{
   struct {
      int a, b ;
   } m ;
} ;

I do get a "name" for the dependent type m:

TestA : struct <anonymous struct at asample2.cpp:8:4>

However, when the AST visitor is called for the a, b fields in this
anonymous struct, my r->getName().str() call returns an empty string.

I'm guessing I could get the "name" above, if I could get the QualType from
the 'RecordDecl * r' variable, but am unsure how to do so, because
getTypeSourceInfo() doesn't appear to be available to a RecordDecl object.
 How can I get the RecordDecl's QualType?

-- 
Peeter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20121211/b4f39e27/attachment.html>


More information about the cfe-dev mailing list