[clang] [LifetimeSafety] Support allocating/freeing functions annotated `ownership_takes`/`ownership_returns` (PR #213439)

via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 6 06:51:14 PST 2026


================
@@ -1083,18 +1083,13 @@ void FactsGenerator::handleLifetimeCaptureBy(const FunctionDecl *FD,
 
 void FactsGenerator::handleFreeingCall(const Expr *Call, const FunctionDecl *FD,
                                        ArrayRef<const Expr *> Args) {
-  // FIXME: `ParamIdx` accounts for the implicit 'this' parameter of
-  // constructors, but VisitCXXConstructExpr passes only the explicit
-  // arguments, so the index mapping would be off by one.
-  if (isa<CXXConstructorDecl>(FD))
-    return;
   for (const OwnershipAttr *Attr : FD->specific_attrs<OwnershipAttr>()) {
     if (Attr->getOwnKind() != OwnershipAttr::Takes)
       continue;
     for (const ParamIdx &Idx : Attr->args()) {
-      // `getLLVMIndex` encodes zero-origin indices including any implicit
-      // 'this' parameter, matching the layout of the Args array.
-      unsigned ArgIndex = Idx.getLLVMIndex();
+      // Attribute indices count implicit 'this', but constructor Args omits it:
+      // the first explicit parameter (2) maps to Args[0], not Args[1].
+      unsigned ArgIndex = Idx.getLLVMIndex() - isa<CXXConstructorDecl>(FD);
----------------
NeKon69 wrote:

It can be zero, but not when we subtract 1. Sema rejects `ownership_takes(whatever, 1)` for constructors, so it shouldn't happen.
https://github.com/llvm/llvm-project/blob/abb4497c6af23e9d97bf9a2710948090060fe3f1/clang/test/SemaCXX/alloc-align-attr.cpp#L3-L5

https://github.com/llvm/llvm-project/pull/213439


More information about the cfe-commits mailing list