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 @@ -11736,9 +11736,9 @@ bool Ret = HandleSizeof(Info, ExprLoc, Ty, Result); if (Ty->isStructureType() && Ty->getAsStructureType()->getDecl()->hasFlexibleArrayMember()) { - const auto *VD = - cast(LVal.getLValueBase().get()); - Result += VD->getFlexibleArrayInitChars(Info.Ctx); + if (const auto *V = LVal.getLValueBase().dyn_cast()) + if (const auto *VD = dyn_cast(V)) + Result += VD->getFlexibleArrayInitChars(Info.Ctx); } return Ret; }; 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 @@ -551,6 +551,10 @@ // CHECK: ret i64 16 return OBJECT_SIZE_BUILTIN(&D34, 1); } +unsigned long test35(void) { + // CHECK: ret i64 16 + return OBJECT_SIZE_BUILTIN(&(struct DynStructVar){}, 1); +} // CHECK-LABEL: @PR30346 void PR30346(void) {