<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="">Thank you!<div class=""><br class=""></div><div class="">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: <a href="http://clang-developers.42468.n3.nabble.com/abort-calling-getTypeSize-td4030616.html#a4030632" class="">http://clang-developers.42468.n3.nabble.com/abort-calling-getTypeSize-td4030616.html#a4030632</a></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Best,</div><div class=""><br class=""></div><div class="">.Chris.</div><div class=""><br class=""></div><div class=""><br class=""><div class=""><div class=""><div><blockquote type="cite" class=""><div class="">On Feb 8, 2016, at 8:00 AM, Manuel Klimek <<a href="mailto:klimek@google.com" class="">klimek@google.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div dir="ltr" class="">On Mon, Feb 8, 2016 at 1:29 PM Christian Schafmeister via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" 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></blockquote><div class=""><br class=""></div><div class="">It's not available any more at this point.</div><div class=""> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div 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 style="white-space:pre-wrap" class=""> </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></div>_______________________________________________<br class="">
cfe-dev mailing list<br class="">
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank" class="">cfe-dev@lists.llvm.org</a><br class="">
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br class="">
</blockquote></div></div>
</div></blockquote></div><br class=""></div></div></div></body></html>