How do I get the Sema object from an ASTMatcher callback?

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?

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

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.

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

Best,

.Chris.