Skip to content

Instantly share code, notes, and snippets.

@trolldbois
Last active December 11, 2015 00:18
Show Gist options
  • Save trolldbois/4515426 to your computer and use it in GitHub Desktop.
Save trolldbois/4515426 to your computer and use it in GitHub Desktop.
patch for clang.cindex
// FIXME:
// JAL: 2013
// TODO CXString clang_getRecordFieldOffset(CXTranslationUnit TU, CXCursor cursor, int num) {
// TODO CharUnits getAlignment()
int64_t clang_getRecordSize(CXTranslationUnit TU, CXCursor cursor) {
assert(clang_isDeclaration(cursor.kind) && "isDeclaration == FALSE");
assert( ((cursor.kind == CXCursor_StructDecl) or
(cursor.kind == CXCursor_UnionDecl) or
(cursor.kind == CXCursor_ClassDecl)) && "bad declaration kind");
// get the context
ASTUnit *CXXUnit = static_cast<ASTUnit*>(TU->TUData);
ASTContext &context = CXXUnit->getASTContext();
Decl *D = getCursorDecl(cursor); // RecordDecl is not Decl
assert(D && "no cursor decl");
assert(classof(static_cast<RecordDecl*>(D)) && "Invalid Kind, not a RecordDecl!");
// get the layout for a record
RecordDecl *RD = static_cast<RecordDecl*>(D);
const ASTRecordLayout &layout = context.getASTRecordLayout(RD);
CharUnits size = layout.getSize();
return size.getQuantity();
}
int64_t clang_getRecordAlignement(CXTranslationUnit TU, CXCursor cursor) {
assert(clang_isDeclaration(cursor.kind) && "isDeclaration == FALSE");
assert( ((cursor.kind == CXCursor_StructDecl) or
(cursor.kind == CXCursor_UnionDecl) or
(cursor.kind == CXCursor_ClassDecl)) && "bad declaration kind");
// get the context
ASTUnit *CXXUnit = static_cast<ASTUnit*>(TU->TUData);
ASTContext &context = CXXUnit->getASTContext();
Decl *D = getCursorDecl(cursor); // RecordDecl is not Decl
assert(D && "no cursor decl");
assert(classof(static_cast<RecordDecl*>(D)) && "Invalid Kind, not a RecordDecl!");
// get the layout for a record
RecordDecl *RD = static_cast<RecordDecl*>(D);
const ASTRecordLayout &layout = context.getASTRecordLayout(RD);
CharUnits align = layout.getAlignment();
return align.getQuantity();
}
int64_t clang_getRecordFieldOffset(CXTranslationUnit TU, CXCursor cursor) {
assert(clang_isDeclaration(cursor.kind) && "isDeclaration == FALSE");
assert( (cursor.kind == CXCursor_FieldDecl) && "bad declaration kind");
// get the context
ASTUnit *CXXUnit = static_cast<ASTUnit*>(TU->TUData);
ASTContext &context = CXXUnit->getASTContext();
Decl *D = getCursorDecl(cursor); // RecordDecl is not Decl
assert(D && "no cursor decl");
assert(classof(static_cast<FieldDecl*>(D)) && "Invalid Kind, not a FieldDecl!");
// get the layout for a record
FieldDecl *F = static_cast<FieldDecl*>(D);
unsigned FieldNo = F->getFieldIndex();
const ASTRecordLayout &layout = context.getASTRecordLayout(F->getParent());
return layout.getFieldOffset( FieldNo );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment