[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:53:01 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:

I could add that test case to `clang/test/Sema/attr-ownership.cpp` to ensure that this can't happen.

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


More information about the cfe-commits mailing list