<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">How do I get the Sema object within an ASTMatcher callback? </div><div class="">I’m trying to get field offsets within C++ classes/structs.</div><div class=""><br class=""></div><div class="">The code below works somewhat but crashes on some clang::RecordDecl's<br class="">On the #llvm irc-channel I was advised to use RequireCompleteType to<br class="">make sure the type of the RecordDecl is complete before I ask for the<br class="">ASTRecordLayout.<br class="">To do that I need a Sema object - how do I get that?<br class=""></div><div class=""><br class=""></div><div class=""><div class=""><font face="Courier" class="">size_t getFieldOffset(clang::ASTContext* context, clang::RecordDecl* record, size_t fieldIndex)</font></div><div class=""><font face="Courier" class="">{</font></div><div class=""><font face="Courier" class=""> clang::SourceLocation loc = record->getLocStart();</font></div><div class=""><font face="Courier" class=""> const clang::Type* type = record->getTypeForDecl();</font></div><div class=""><font face="Courier" class=""> clang::QualType qt = type->getCanonicalTypeInternal();</font></div><div class=""><font face="Courier" class=""> // The next line is wrong - I need a Sema object</font></div><div class=""><font face="Courier" class=""> bool complete = clang::Sema::RequireCompleteType(loc,qt,9876);</font></div><div class=""><font face="Courier" class=""> if (!complete) return 0;</font></div><div class=""><font face="Courier" class=""> printf("getFieldOffset context = %p record = %p(%s) fieldIndex = %d\n",</font></div><div class=""><font face="Courier" class=""><span class="Apple-tab-span" style="white-space:pre"> </span> context, record,record->getNameAsString().c_str(), fieldIndex);</font></div><div class=""><font face="Courier" class=""> printf("getFieldOffset isInjectedClassName = %d\n", record->isInjectedClassName() );</font></div><div class=""><font face="Courier" class=""> const clang::ASTRecordLayout& layout = context->getASTRecordLayout(record);</font></div><div class=""><font face="Courier" class=""> printf(" layout = %p\n", &layout );</font></div><div class=""><font face="Courier" class=""> size_t offset = layout.getFieldOffset(fieldIndex);</font></div><div class=""><font face="Courier" class=""> printf("Returning offset=%lu\n", offset);</font></div><div class=""><font face="Courier" class=""> return offset;</font></div><div class=""><font face="Courier" class="">}</font></div></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class="">Christian Schafmeister</font></div><div class=""><font face="Courier" class="">Professor</font></div><div class=""><font face="Courier" class="">Chemistry Department</font></div><div class=""><font face="Courier" class="">Temple University</font></div></body></html>