[clang] [clang][bytecode] Improve __builtin_{,dynamic_}object_size implementation (PR #153601)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 24 01:39:50 PDT 2025
================
@@ -2170,29 +2170,32 @@ static bool interp__builtin_memchr(InterpState &S, CodePtr OpPC,
return true;
}
-static unsigned computeFullDescSize(const ASTContext &ASTCtx,
- const Descriptor *Desc) {
-
+static std::optional<unsigned> computeFullDescSize(const ASTContext &ASTCtx,
+ const Descriptor *Desc) {
if (Desc->isPrimitive())
return ASTCtx.getTypeSizeInChars(Desc->getType()).getQuantity();
-
if (Desc->isArray())
return ASTCtx.getTypeSizeInChars(Desc->getElemQualType()).getQuantity() *
Desc->getNumElems();
+ if (Desc->isRecord()) {
+ // Can't use Descriptor::getType() as that may return a pointer type. Look
+ // at the decl directly.
+ return ASTCtx
+ .getTypeSizeInChars(
+ ASTCtx.getCanonicalTagType(Desc->ElemRecord->getDecl()))
+ .getQuantity();
+ }
- if (Desc->isRecord())
- return ASTCtx.getTypeSizeInChars(Desc->getType()).getQuantity();
-
- llvm_unreachable("Unhandled descriptor type");
- return 0;
+ return std::nullopt;
}
+/// Compute the byte offset of \p Ptr in the full declaration.
static unsigned computePointerOffset(const ASTContext &ASTCtx,
const Pointer &Ptr) {
unsigned Result = 0;
Pointer P = Ptr;
- while (P.isArrayElement() || P.isField()) {
+ while (P.isField() || P.isArrayElement()) {
----------------
tbaederr wrote:
No, the previous order just caused some assertion failures in `isArrayElement` IIRC. This version works for more cases.
https://github.com/llvm/llvm-project/pull/153601
More information about the cfe-commits
mailing list