[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 04:28:16 PST 2016


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160208/94ab43ac/attachment.html>


More information about the cfe-dev mailing list