Skip to content

Conversation

@fahadnayyar
Copy link
Contributor

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

rdar://142504115

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Jan 8, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 8, 2025

@llvm/pr-subscribers-clang

Author: None (fahadnayyar)

Changes

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 --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; };
+
+@interface MethodTest
+- (struct RefCountedType *)getUnowned;
+- (struct RefCountedType *)getOwned;
+@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);

@fahadnayyar fahadnayyar requested a review from ahatanak January 8, 2025 20:47
@fahadnayyar fahadnayyar merged commit 43e663d into llvm:main Jan 9, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants