diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -11732,7 +11732,14 @@ auto CheckedHandleSizeof = [&](QualType Ty, CharUnits &Result) { if (Ty.isNull() || Ty->isIncompleteType() || Ty->isFunctionType()) return false; - return HandleSizeof(Info, ExprLoc, Ty, Result); + bool Ret = HandleSizeof(Info, ExprLoc, Ty, Result); + if (Ty->isStructureType() && + Ty->getAsStructureType()->getDecl()->hasFlexibleArrayMember()) { + const auto *VD = dyn_cast_or_null( + LVal.getLValueBase().dyn_cast()); + Result += VD->getFlexibleArrayInitChars(Info.Ctx); + } + return Ret; }; // We want to evaluate the size of the entire object. This is a valid fallback diff --git a/clang/test/CodeGen/object-size.c b/clang/test/CodeGen/object-size.c --- a/clang/test/CodeGen/object-size.c +++ b/clang/test/CodeGen/object-size.c @@ -531,9 +531,7 @@ .snd = { 0, 1, 2, }, }; unsigned long test32(void) { - // FIXME: GCC returns the sizeof the base type, plus the number of bytes from - // members of the initializer of the flexible array: 19. - // CHECK: ret i64 16 + // CHECK: ret i64 19 return __builtin_object_size(&D, 1); }