@@ -877,14 +877,14 @@ CodeGenFunction::emitBuiltinObjectSize(const Expr *E, unsigned Type,
877
877
// 2) bdos of the whole struct, including the flexible array:
878
878
//
879
879
// __builtin_dynamic_object_size(p, 1) ==
880
- // sizeof(*p ) + p->count * sizeof(*p->array)
880
+ // offsetof(struct s, array ) + p->count * sizeof(*p->array)
881
881
//
882
882
if (const ValueDecl *CountedByFD = FindCountedByField(E)) {
883
883
// Find the flexible array member.
884
+ ASTContext &Ctx = getContext();
884
885
const RecordDecl *OuterRD =
885
886
CountedByFD->getDeclContext()->getOuterLexicalRecordContext();
886
- const ValueDecl *FAM =
887
- FindFlexibleArrayMemberField(getContext(), OuterRD);
887
+ const ValueDecl *FAM = FindFlexibleArrayMemberField(Ctx, OuterRD);
888
888
889
889
// Find the outer struct expr (i.e. p in p->a.b.c.d).
890
890
const Expr *CountedByExpr = BuildCountedByFieldExpr(E, CountedByFD);
@@ -894,9 +894,8 @@ CodeGenFunction::emitBuiltinObjectSize(const Expr *E, unsigned Type,
894
894
EmitAnyExprToTemp(CountedByExpr).getScalarVal();
895
895
896
896
// Get the size of the flexible array member's base type.
897
- const auto *ArrayTy = getContext().getAsArrayType(FAM->getType());
898
- CharUnits Size =
899
- getContext().getTypeSizeInChars(ArrayTy->getElementType());
897
+ const auto *ArrayTy = Ctx.getAsArrayType(FAM->getType());
898
+ CharUnits Size = Ctx.getTypeSizeInChars(ArrayTy->getElementType());
900
899
llvm::Constant *ArraySize =
901
900
llvm::ConstantInt::get(CountedByInstr->getType(), Size.getQuantity());
902
901
@@ -905,10 +904,10 @@ CodeGenFunction::emitBuiltinObjectSize(const Expr *E, unsigned Type,
905
904
906
905
if (const auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) {
907
906
// The whole struct is specificed in the __bdos.
908
- QualType StructTy = DRE->getType()->getPointeeType( );
909
- llvm::Value *StructSize = ConstantInt::get(
910
- ResType, getContext().getTypeSizeInChars(StructTy). getQuantity(),
911
- true);
907
+ CharUnits FAMOffset = Ctx.toCharUnitsFromBits(Ctx.getFieldOffset(FAM) );
908
+ llvm::Value *StructSize =
909
+ ConstantInt::get( ResType, FAMOffset. getQuantity(), true);
910
+
912
911
ObjectSize = Builder.CreateAdd(StructSize, ObjectSize);
913
912
}
914
913
0 commit comments