[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:58 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (fahadnayyar)

<details>
<summary>Changes</summary>

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

rdar://142504115

---
Full diff: https://github.com/llvm/llvm-project/pull/122167.diff


4 Files Affected:

- (added) clang/test/APINotes/Inputs/Headers/SwiftReturnOwnershipForObjC.apinotes (+17) 
- (added) clang/test/APINotes/Inputs/Headers/SwiftReturnOwnershipForObjC.h (+9) 
- (modified) clang/test/APINotes/Inputs/Headers/module.modulemap (+4) 
- (added) clang/test/APINotes/swift-return-ownership.m (+11) 


``````````diff
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);

``````````

</details>


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


More information about the cfe-commits mailing list