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

Manuel Klimek via cfe-dev cfe-dev at lists.llvm.org
Mon Feb 8 05:00:41 PST 2016


On Mon, Feb 8, 2016 at 1:29 PM Christian Schafmeister via cfe-dev <
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
> 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/93b1034f/attachment.html>


More information about the cfe-dev mailing list