[cfe-dev] How do I get the Sema object from an ASTMatcher callback?

Christian Schafmeister via cfe-dev cfe-dev at lists.llvm.org
Mon Feb 8 05:18:49 PST 2016


Thank you!

I was asking the wrong question - I want to use getFieldOffset and I found that I need to test type->isDependentType() before I get ASTRecordLayout.  It is discussed in this discussion thread:  http://clang-developers.42468.n3.nabble.com/abort-calling-getTypeSize-td4030616.html#a4030632 <http://clang-developers.42468.n3.nabble.com/abort-calling-getTypeSize-td4030616.html#a4030632>


Best,

.Chris.


> On Feb 8, 2016, at 8:00 AM, Manuel Klimek <klimek at google.com> wrote:
> 
> On Mon, Feb 8, 2016 at 1:29 PM Christian Schafmeister via cfe-dev <cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>> wrote:
> 
> How do I get the Sema object within an ASTMatcher callback? 
> I’m trying to get field offsets within C++ classes/structs.
> 
> The code below works somewhat but crashes on some clang::RecordDecl's
> On the #llvm irc-channel I was advised to use RequireCompleteType to
> make sure the type of the RecordDecl is complete before I ask for the
> ASTRecordLayout.
> To do that I need a Sema object - how do I get that?
> 
> It's not available any more at this point.
>  
> 
> size_t getFieldOffset(clang::ASTContext* context, clang::RecordDecl* record, size_t fieldIndex)
> {
>   clang::SourceLocation loc = record->getLocStart();
>   const clang::Type* type = record->getTypeForDecl();
>   clang::QualType qt = type->getCanonicalTypeInternal();
>   // The next line is wrong - I need a Sema object
>   bool complete = clang::Sema::RequireCompleteType(loc,qt,9876);
>   if (!complete) return 0;
>   printf("getFieldOffset context = %p record = %p(%s) fieldIndex = %d\n",
> 	 context, record,record->getNameAsString().c_str(), fieldIndex);
>   printf("getFieldOffset isInjectedClassName = %d\n", record->isInjectedClassName() );
>   const clang::ASTRecordLayout& layout = context->getASTRecordLayout(record);
>   printf("  layout = %p\n", &layout );
>   size_t offset = layout.getFieldOffset(fieldIndex);
>   printf("Returning offset=%lu\n", offset);
>   return offset;
> }
> 
> 
> Christian Schafmeister
> Professor
> Chemistry Department
> Temple University
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev <http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160208/67b68773/attachment.html>


More information about the cfe-dev mailing list