[clang] [CIR] Handle FunctionToPointerDecay casts (#153657) (PR #154060)
Morris Hafner via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 18 08:40:54 PDT 2025
================
@@ -73,21 +73,59 @@ Address CIRGenFunction::emitPointerWithAlignment(const Expr *expr,
// Casts:
if (auto const *ce = dyn_cast<CastExpr>(expr)) {
- if (isa<ExplicitCastExpr>(ce)) {
- cgm.errorNYI(expr->getSourceRange(),
- "emitPointerWithAlignment: explicit cast");
- return Address::invalid();
- }
+ if (const auto *ece = dyn_cast<ExplicitCastExpr>(ce))
+ cgm.emitExplicitCastExprType(ece);
switch (ce->getCastKind()) {
// Non-converting casts (but not C's implicit conversion from void*).
case CK_BitCast:
case CK_NoOp:
case CK_AddressSpaceConversion: {
- cgm.errorNYI(expr->getSourceRange(),
- "emitPointerWithAlignment: noop cast");
- return Address::invalid();
- } break;
+ if (const auto *ptrTy =
+ ce->getSubExpr()->getType()->getAs<PointerType>()) {
+ if (ptrTy->getPointeeType()->isVoidType())
+ break;
+
+ LValueBaseInfo innerBaseInfo;
+ Address addr =
+ emitPointerWithAlignment(ce->getSubExpr(), &innerBaseInfo);
+ if (baseInfo)
+ *baseInfo = innerBaseInfo;
+
+ if (isa<ExplicitCastExpr>(ce)) {
+ LValueBaseInfo targetTypeBaseInfo;
+
+ const QualType pointeeType = expr->getType()->getPointeeType();
+ const CharUnits align =
+ cgm.getNaturalTypeAlignment(pointeeType, &targetTypeBaseInfo);
+
+ // If the source l-value is opaque, honor the alignment of the
+ // casted-to type.
+ if (innerBaseInfo.getAlignmentSource() != AlignmentSource::Decl) {
+ if (baseInfo)
+ baseInfo->mergeForCast(targetTypeBaseInfo);
+ addr = Address(addr.getPointer(), addr.getElementType(), align);
+ }
+ }
+
+ if (sanOpts.has(SanitizerKind::CFIUnrelatedCast) &&
+ ce->getCastKind() == CK_BitCast) {
+ if (expr->getType()->getAs<PointerType>())
+ llvm_unreachable("NYI");
+ }
+
+ const auto eltTy = convertTypeForMem(expr->getType()->getPointeeType());
+ addr = getBuilder().createElementBitCast(getLoc(expr->getSourceRange()),
+ addr, eltTy);
+ if (ce->getCastKind() == CK_AddressSpaceConversion) {
+ assert(!cir::MissingFeatures::addressSpace());
+ llvm_unreachable("NYI");
+ }
----------------
mmha wrote:
```suggestion
assert(ce->getCastKind() != CK_AddressSpaceConversion && !cir::MissingFeatures::addressSpace());
```
https://github.com/llvm/llvm-project/pull/154060
More information about the cfe-commits
mailing list