================
@@ -1131,63 +1151,98 @@ llvm::Value *CodeGenFunction::emitCountedByPointerSize(
// cmp = (cmp && index > 0)
// return cmp ? result : 0;
- auto GetElementBaseSize = [&](QualType ElementTy) {
- CharUnits ElementSize =
- getContext().getTypeSizeInChars(ElementTy->getPointeeType());
-
- if (ElementSize.isZero()) {
- // This might be a __sized_by (or __counted_by) on a
- // 'void *', which counts bytes, not elements.
- [[maybe_unused]] auto *CAT = ElementTy->getAs<CountAttributedType>();
- assert(CAT && "must have an CountAttributedType");
-
- ElementSize = CharUnits::One();
+ auto GetPointeeSize = [&](QualType PtrTy) -> CharUnits {
+ assert(!PtrTy.isNull());
+ QualType PointeeTy = PtrTy->getPointeeType();
+ assert(!PointeeTy.isNull() &&
+ (PointeeTy->isVoidType() || !PointeeTy->isIncompleteType()) &&
+ "pointee type must have a computable size");
+
+ CharUnits PointeeSize = getContext().getTypeSizeInChars(PointeeTy);
+ if (PointeeSize.isZero()) {
+ // Support GNU extension of treating `void` having size 1.
+ PointeeSize = CharUnits::One();
}
- return std::optional<CharUnits>(ElementSize);
+ return PointeeSize;
};
- // Get the sizes of the original array element and the casted array element,
- // if different.
- std::optional<CharUnits> ArrayElementBaseSize =
- GetElementBaseSize(ArrayBaseFD->getType());
- if (!ArrayElementBaseSize)
- return nullptr;
-
- std::optional<CharUnits> CastedArrayElementBaseSize = ArrayElementBaseSize;
- if (!CastedArrayElementTy.isNull() && CastedArrayElementTy->isPointerType())
{
- CastedArrayElementBaseSize = GetElementBaseSize(CastedArrayElementTy);
- if (!CastedArrayElementBaseSize)
- return nullptr;
- }
-
bool IsSigned = CountFD->getType()->isSignedIntegerType();
+ const auto *CAT = ArrayBaseFD->getType()->getAs<CountAttributedType>();
+ assert(CAT && "the field's type is a CountAttributedType");
+ const bool CountInBytes = CAT->isCountInBytes();
----------------
delcypher wrote:
I've inlined `CAT->isCountInBytes();`
https://github.com/llvm/llvm-project/pull/213794
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits