nickdesaulniers updated this revision to Diff 523480. nickdesaulniers added a comment.
- moar tests Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D150892/new/ https://reviews.llvm.org/D150892 Files: clang/docs/ReleaseNotes.rst clang/lib/AST/ExprConstant.cpp clang/test/CodeGen/object-size.c Index: clang/test/CodeGen/object-size.c =================================================================== --- clang/test/CodeGen/object-size.c +++ clang/test/CodeGen/object-size.c @@ -525,6 +525,33 @@ gi = OBJECT_SIZE_BUILTIN(&dsv[9].snd[0], 1); } +// CHECK-LABEL: @test32 +static struct DynStructVar D32 = { + .fst = {}, + .snd = { 0, 1, 2, }, +}; +unsigned long test32(void) { + // CHECK: ret i64 19 + return __builtin_object_size(&D32, 1); +} +// CHECK-LABEL: @test33 +static struct DynStructVar D33 = { + .fst = {}, + .snd = {}, +}; +unsigned long test33(void) { + // CHECK: ret i64 16 + return __builtin_object_size(&D33, 1); +} +// CHECK-LABEL: @test34 +static struct DynStructVar D34 = { + .fst = {}, +}; +unsigned long test34(void) { + // CHECK: ret i64 16 + return __builtin_object_size(&D34, 1); +} + // CHECK-LABEL: @PR30346 void PR30346(void) { struct sa_family_t {}; Index: clang/lib/AST/ExprConstant.cpp =================================================================== --- clang/lib/AST/ExprConstant.cpp +++ 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 = + cast<VarDecl>(LVal.getLValueBase().get<const ValueDecl *>()); + Result += VD->getFlexibleArrayInitChars(Info.Ctx); + } + return Ret; }; // We want to evaluate the size of the entire object. This is a valid fallback Index: clang/docs/ReleaseNotes.rst =================================================================== --- clang/docs/ReleaseNotes.rst +++ clang/docs/ReleaseNotes.rst @@ -51,6 +51,10 @@ foo: asm goto ("# %0 %1"::"i"(&&foo)::foo); +- ``__builtin_object_size`` now adds the ``sizeof`` the elements specified in + designated initializers of flexible array members. This change is more + consistent with the behavior of GCC. + C++ Specific Potentially Breaking Changes ----------------------------------------- - Clang won't search for coroutine_traits in std::experimental namespace any more. @@ -403,6 +407,8 @@ when it had been instantiated from a partial template specialization with different template arguments on the containing class. This fixes: (`#60778 <https://github.com/llvm/llvm-project/issues/60778>`_). +- Match GCC's behavior for __builtin_object_size on flexible array members. + (`#62789 <https://github.com/llvm/llvm-project/issues/62789>`_). Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Index: clang/test/CodeGen/object-size.c =================================================================== --- clang/test/CodeGen/object-size.c +++ clang/test/CodeGen/object-size.c @@ -525,6 +525,33 @@ gi = OBJECT_SIZE_BUILTIN(&dsv[9].snd[0], 1); } +// CHECK-LABEL: @test32 +static struct DynStructVar D32 = { + .fst = {}, + .snd = { 0, 1, 2, }, +}; +unsigned long test32(void) { + // CHECK: ret i64 19 + return __builtin_object_size(&D32, 1); +} +// CHECK-LABEL: @test33 +static struct DynStructVar D33 = { + .fst = {}, + .snd = {}, +}; +unsigned long test33(void) { + // CHECK: ret i64 16 + return __builtin_object_size(&D33, 1); +} +// CHECK-LABEL: @test34 +static struct DynStructVar D34 = { + .fst = {}, +}; +unsigned long test34(void) { + // CHECK: ret i64 16 + return __builtin_object_size(&D34, 1); +} + // CHECK-LABEL: @PR30346 void PR30346(void) { struct sa_family_t {}; Index: clang/lib/AST/ExprConstant.cpp =================================================================== --- clang/lib/AST/ExprConstant.cpp +++ 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 = + cast<VarDecl>(LVal.getLValueBase().get<const ValueDecl *>()); + Result += VD->getFlexibleArrayInitChars(Info.Ctx); + } + return Ret; }; // We want to evaluate the size of the entire object. This is a valid fallback Index: clang/docs/ReleaseNotes.rst =================================================================== --- clang/docs/ReleaseNotes.rst +++ clang/docs/ReleaseNotes.rst @@ -51,6 +51,10 @@ foo: asm goto ("# %0 %1"::"i"(&&foo)::foo); +- ``__builtin_object_size`` now adds the ``sizeof`` the elements specified in + designated initializers of flexible array members. This change is more + consistent with the behavior of GCC. + C++ Specific Potentially Breaking Changes ----------------------------------------- - Clang won't search for coroutine_traits in std::experimental namespace any more. @@ -403,6 +407,8 @@ when it had been instantiated from a partial template specialization with different template arguments on the containing class. This fixes: (`#60778 <https://github.com/llvm/llvm-project/issues/60778>`_). +- Match GCC's behavior for __builtin_object_size on flexible array members. + (`#62789 <https://github.com/llvm/llvm-project/issues/62789>`_). Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits