[clang] [APINotes] [NFC] Add tests for SWIFT_RETURNS_(UN)RETAINED for ObjC APIs (PR #122167)

via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 8 12:43:22 PST 2025


https://github.com/fahadnayyar created https://github.com/llvm/llvm-project/pull/122167

Adding test case to verify that SwiftReturnOwnership works correctly for ObjC functions and methods as well.

rdar://142504115

>From 3c8fc5525710cb3bed6e6b4035e5d62693d90ade Mon Sep 17 00:00:00 2001
From: Fahad Nayyar <f_nayyar at apple.com>
Date: Wed, 8 Jan 2025 12:39:15 -0800
Subject: [PATCH] [APINotes] [NFC] Add tests for SWIFT_RETURNS_(UN)RETAINED for
 ObjC APIs

rdar://142504115
---
 .../SwiftReturnOwnershipForObjC.apinotes        | 17 +++++++++++++++++
 .../Headers/SwiftReturnOwnershipForObjC.h       |  9 +++++++++
 .../APINotes/Inputs/Headers/module.modulemap    |  4 ++++
 clang/test/APINotes/swift-return-ownership.m    | 11 +++++++++++
 4 files changed, 41 insertions(+)
 create mode 100644 clang/test/APINotes/Inputs/Headers/SwiftReturnOwnershipForObjC.apinotes
 create mode 100644 clang/test/APINotes/Inputs/Headers/SwiftReturnOwnershipForObjC.h
 create mode 100644 clang/test/APINotes/swift-return-ownership.m

diff --git a/clang/test/APINotes/Inputs/Headers/SwiftReturnOwnershipForObjC.apinotes b/clang/test/APINotes/Inputs/Headers/SwiftReturnOwnershipForObjC.apinotes
new file mode 100644
index 00000000000000..4b749b40e026f2
--- /dev/null
+++ b/clang/test/APINotes/Inputs/Headers/SwiftReturnOwnershipForObjC.apinotes
@@ -0,0 +1,17 @@
+---
+Name: SwiftImportAsForObjC
+Classes:
+  - Name:            MethodTest
+    Methods:
+      - Selector:        getUnowned
+        MethodKind:      Instance
+        SwiftReturnOwnership: unretained
+      - Selector:        getOwned
+        MethodKind:      Instance
+        SwiftReturnOwnership: retained
+
+Functions:
+  - Name:            getObjCUnowned
+    SwiftReturnOwnership: unretained
+  - Name:            getObjCOwned
+    SwiftReturnOwnership: retained
diff --git a/clang/test/APINotes/Inputs/Headers/SwiftReturnOwnershipForObjC.h b/clang/test/APINotes/Inputs/Headers/SwiftReturnOwnershipForObjC.h
new file mode 100644
index 00000000000000..83e2535387caa5
--- /dev/null
+++ b/clang/test/APINotes/Inputs/Headers/SwiftReturnOwnershipForObjC.h
@@ -0,0 +1,9 @@
+struct RefCountedType { int value; };
+
+ at interface MethodTest
+- (struct RefCountedType *)getUnowned;
+- (struct RefCountedType *)getOwned;
+ at end
+
+struct RefCountedType * getObjCUnowned(void);
+struct RefCountedType * getObjCOwned(void);
diff --git a/clang/test/APINotes/Inputs/Headers/module.modulemap b/clang/test/APINotes/Inputs/Headers/module.modulemap
index 31f7d36356d83e..bedb7d505f794a 100644
--- a/clang/test/APINotes/Inputs/Headers/module.modulemap
+++ b/clang/test/APINotes/Inputs/Headers/module.modulemap
@@ -57,3 +57,7 @@ module Templates {
 module SwiftImportAs {
   header "SwiftImportAs.h"
 }
+
+module SwiftReturnOwnershipForObjC {
+  header "SwiftReturnOwnershipForObjC.h"
+}
diff --git a/clang/test/APINotes/swift-return-ownership.m b/clang/test/APINotes/swift-return-ownership.m
new file mode 100644
index 00000000000000..b1221d159e610f
--- /dev/null
+++ b/clang/test/APINotes/swift-return-ownership.m
@@ -0,0 +1,11 @@
+// RUN: rm -rf %t && mkdir -p %t
+// RUN: %clang_cc1 -fmodules -fblocks -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fdisable-module-hash -fapinotes-modules -fsyntax-only -I %S/Inputs/Headers %s
+// RUN: %clang_cc1 -ast-print %t/ModulesCache/SwiftReturnOwnershipForObjC.pcm | FileCheck %s
+#import <SwiftReturnOwnershipForObjC.h>
+
+// CHECK: @interface MethodTest
+// CHECK: - (struct RefCountedType *)getUnowned __attribute__((swift_attr("returns_unretained")));
+// CHECK: - (struct RefCountedType *)getOwned __attribute__((swift_attr("returns_retained")));
+// CHECK: @end
+// CHECK: __attribute__((swift_attr("returns_unretained"))) struct RefCountedType *getObjCUnowned(void);
+// CHECK: __attribute__((swift_attr("returns_retained"))) struct RefCountedType *getObjCOwned(void);



More information about the cfe-commits mailing list