[clang] [Sema] Enforce parameter match for ownership_returns attribute (PR #192339)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 24 08:32:00 PDT 2026


================
@@ -1558,38 +1558,40 @@ static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
       // Cannot have two ownership attributes of different kinds for the same
       // index.
       if (I->getOwnKind() != K && llvm::is_contained(I->args(), Idx)) {
-          S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
-              << AL << I
-              << (AL.isRegularKeywordAttribute() ||
-                  I->isRegularKeywordAttribute());
-          return;
-      } else if (K == OwnershipAttr::Returns &&
-                 I->getOwnKind() == OwnershipAttr::Returns) {
-        // A returns attribute conflicts with any other returns attribute using
-        // a different index.
-        if (!llvm::is_contained(I->args(), Idx)) {
-          S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
-              << I->args_begin()->getSourceIndex();
-          if (I->args_size())
-            S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
-                << Idx.getSourceIndex() << Ex->getSourceRange();
-          return;
-        }
-      } else if (K == OwnershipAttr::Takes &&
-                 I->getOwnKind() == OwnershipAttr::Takes) {
-        if (I->getModule()->getName() != ModuleName) {
-          S.Diag(I->getLocation(), diag::err_ownership_takes_class_mismatch)
-              << I->getModule()->getName();
-          S.Diag(AL.getLoc(), diag::note_ownership_takes_class_mismatch)
-              << ModuleName << Ex->getSourceRange();
-
-          return;
-        }
+        S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
+            << AL << I
+            << (AL.isRegularKeywordAttribute() ||
+                I->isRegularKeywordAttribute());
+        return;
       }
     }
     OwnershipArgs.push_back(Idx);
   }
 
+  for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
----------------
AaronBallman wrote:

Can we handle this from the existing loop instead of walking the attributes twice?

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


More information about the cfe-commits mailing list