[cfe-dev] want to call getTypeSize() in AST visitor, but skip incomplete types

Peeter Joot peeter.joot at gmail.com
Fri Feb 22 08:41:24 PST 2013


With the following AST visitor:

bool VisitFieldDecl( FieldDecl * f )
{
   RecordDecl * r = f->getParent() ;
   const QualType & theMembersClassType = m_context.getRecordType( r ) ;

   TypeSourceInfo * pThisFieldSourceInfo = f->getTypeSourceInfo() ;

   TypeLoc thisFieldTypeLoc = pThisFieldSourceInfo->getTypeLoc() ;

   const QualType & thisFieldQualType = thisFieldTypeLoc.getType() ;

   size_t szInBits = m_context.getTypeSize( thisFieldQualType ) ;
   size_t offsetInBits = m_context.getFieldOffset( f ) ;

   cout
      << "[" << offsetInBits/8 << "]:\t"
      << theMembersClassType.getAsString( m_pp ) << "::" <<
thisFieldQualType.getAsString( m_pp ) << "\t" << f->getNameAsString()
      << "\tsize: " << szInBits/8 << endl ;
      ;

   return true ;
}


I'm able to dump the sizeof and offsetof the members of a my C++
structures.  I have trouble with input like so:

#include <signal.h>

struct signalDisposition
{
   struct sigaction sigact ;
   int signum ;
} ;

getting the following assert:

(gdb) run asample8.cpp
Starting program:
/home/hotellnx94/peeterj/.physicsplay/rewriter/memberdumper asample8.cpp
Failed to read a valid object file image from memory.
[Thread debugging using libthread_db enabled]
asample8.cpp:1:10: fatal error: 'signal.h' file not found
#include <signal.h>
         ^
memberdumper:
/home/hotellnx94/peeterj/clang/sources/llvm/tools/clang/lib/AST/RecordLayoutBuilder.cpp:2470:
const clang::ASTRecordLayout& clang::ASTContext::getASTRecordLayout(const
clang::RecordDecl*) const: Assertion `D && "Cannot get layout of forward
declarations!"' failed.

Program received signal SIGABRT, Aborted.
0x00002aaaab39ef45 in raise () from /lib64/libc.so.6
(gdb) where
#0  0x00002aaaab39ef45 in raise () from /lib64/libc.so.6
#1  0x00002aaaab3a0340 in abort () from /lib64/libc.so.6
#2  0x00002aaaab398486 in __assert_fail () from /lib64/libc.so.6
#3  0x0000000000b29c80 in
clang::ASTContext::getASTRecordLayout(clang::RecordDecl const*) const ()
#4  0x0000000000a34f99 in clang::ASTContext::getTypeInfoImpl(clang::Type
const*) const ()
#5  0x0000000000a32892 in clang::ASTContext::getTypeInfo(clang::Type
const*) const ()
#6  0x0000000000a348b9 in clang::ASTContext::getTypeInfoImpl(clang::Type
const*) const ()
#7  0x0000000000a32892 in clang::ASTContext::getTypeInfo(clang::Type
const*) const ()
#8  0x000000000046409d in clang::ASTContext::getTypeInfo (this=0x1366ff0,
T=...)
    at
/home/hotellnx94/peeterj/clang/optimized48rt/include/clang/AST/ASTContext.h:1489
#9  0x0000000000464022 in clang::ASTContext::getTypeSize (this=0x1366ff0,
T=...)
    at
/home/hotellnx94/peeterj/clang/optimized48rt/include/clang/AST/ASTContext.h:1494
#10 0x0000000000463e6a in MyASTVisitor::VisitFieldDecl
(this=0x7fffffffc3b8, f=0x136f390) at ./memberdumper.h:68


I understand the root cause for the assert: I've been unable to coerse my
AST visitor into properly setting up the paths for my system headers. That
appears to be a difficult problem.  I've had some advise in the past from
this list on how to do so, that I've been able to use with only partial
success.

For now, I'd just like to skip such an incomplete type.  How could I code a
check that the type of the FieldDecl is complete so that I don't hit the
forward reference assert above?

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


More information about the cfe-dev mailing list