xref: /aosp_15_r20/external/clang/test/SemaObjC/arc-decls.m (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li// RUN: %clang_cc1 -fsyntax-only -fblocks -fobjc-arc -verify -Wno-objc-root-class %s
2*67e74705SXin Li
3*67e74705SXin Li// rdar://8843524
4*67e74705SXin Li
5*67e74705SXin Listruct A {
6*67e74705SXin Li    id x; // expected-error {{ARC forbids Objective-C objects in struct}}
7*67e74705SXin Li};
8*67e74705SXin Li
9*67e74705SXin Liunion u {
10*67e74705SXin Li    id u; // expected-error {{ARC forbids Objective-C objects in union}}
11*67e74705SXin Li};
12*67e74705SXin Li
13*67e74705SXin Li@interface I {
14*67e74705SXin Li   struct A a;
15*67e74705SXin Li   struct B {
16*67e74705SXin Li    id y[10][20]; // expected-error {{ARC forbids Objective-C objects in struct}}
17*67e74705SXin Li    id z;
18*67e74705SXin Li   } b;
19*67e74705SXin Li
20*67e74705SXin Li   union u c;
21*67e74705SXin Li};
22*67e74705SXin Li@end
23*67e74705SXin Li
24*67e74705SXin Li// rdar://10260525
25*67e74705SXin Listruct r10260525 {
26*67e74705SXin Li  id (^block) (); // expected-error {{ARC forbids blocks in struct}}
27*67e74705SXin Li};
28*67e74705SXin Li
29*67e74705SXin Listruct S {
30*67e74705SXin Li    id __attribute__((objc_ownership(none))) i;
31*67e74705SXin Li    void * vp;
32*67e74705SXin Li    int i1;
33*67e74705SXin Li};
34*67e74705SXin Li
35*67e74705SXin Li// rdar://9046528
36*67e74705SXin Li
37*67e74705SXin Li@class NSError;
38*67e74705SXin Li
39*67e74705SXin Li__autoreleasing id X; // expected-error {{global variables cannot have __autoreleasing ownership}}
40*67e74705SXin Li__autoreleasing NSError *E; // expected-error {{global variables cannot have __autoreleasing ownership}}
41*67e74705SXin Li
42*67e74705SXin Li
43*67e74705SXin Liextern id __autoreleasing X1; // expected-error {{global variables cannot have __autoreleasing ownership}}
44*67e74705SXin Li
45*67e74705SXin Livoid func()
46*67e74705SXin Li{
47*67e74705SXin Li    id X;
48*67e74705SXin Li    static id __autoreleasing X1; // expected-error {{global variables cannot have __autoreleasing ownership}}
49*67e74705SXin Li    extern id __autoreleasing E; // expected-error {{global variables cannot have __autoreleasing ownership}}
50*67e74705SXin Li
51*67e74705SXin Li}
52*67e74705SXin Li
53*67e74705SXin Li// rdar://9157348
54*67e74705SXin Li// rdar://15757510
55*67e74705SXin Li
56*67e74705SXin Li@interface J
57*67e74705SXin Li@property (retain) id newFoo; // expected-error {{property follows Cocoa naming convention for returning 'owned' objects}} expected-note{{explicitly declare getter '-newFoo' with '__attribute__((objc_method_family(none)))' to return an 'unowned' object}}
58*67e74705SXin Li@property (strong) id copyBar;  // expected-error {{property follows Cocoa naming convention for returning 'owned' objects}} expected-note{{explicitly declare getter '-copyBar' with '__attribute__((objc_method_family(none)))' to return an 'unowned' object}}
59*67e74705SXin Li@property (copy) id allocBaz; // expected-error {{property follows Cocoa naming convention for returning 'owned' objects}} expected-note{{explicitly declare getter '-allocBaz' with '__attribute__((objc_method_family(none)))' to return an 'unowned' object}}
60*67e74705SXin Li@property (copy, nonatomic) id new;
61*67e74705SXin Li@property (retain) id newDFoo; // expected-error {{property follows Cocoa naming convention for returning 'owned' objects}} expected-note{{explicitly declare getter '-newDFoo' with '__attribute__((objc_method_family(none)))' to return an 'unowned' object}}
62*67e74705SXin Li@property (strong) id copyDBar; // expected-error {{property follows Cocoa naming convention for returning 'owned' objects}} expected-note{{explicitly declare getter '-copyDBar' with '__attribute__((objc_method_family(none)))' to return an 'unowned' object}}
63*67e74705SXin Li@property (copy) id allocDBaz; // expected-error {{property follows Cocoa naming convention for returning 'owned' objects}} expected-note{{explicitly declare getter '-allocDBaz' with '__attribute__((objc_method_family(none)))' to return an 'unowned' object}}
64*67e74705SXin Li@end
65*67e74705SXin Li
66*67e74705SXin Li@implementation J
67*67e74705SXin Li@synthesize newFoo;
68*67e74705SXin Li@synthesize copyBar;
69*67e74705SXin Li@synthesize allocBaz;
70*67e74705SXin Li@synthesize new;
71*67e74705SXin Li- new {return 0; };
72*67e74705SXin Li
73*67e74705SXin Li@dynamic newDFoo;
74*67e74705SXin Li@dynamic copyDBar;
75*67e74705SXin Li@dynamic allocDBaz;
76*67e74705SXin Li@end
77*67e74705SXin Li
78*67e74705SXin Li
79*67e74705SXin Li@interface MethodFamilyDiags
80*67e74705SXin Li@property (retain) id newFoo; // expected-error {{property follows Cocoa naming convention for returning 'owned' objects}}
81*67e74705SXin Li- (id)newFoo; // expected-note {{explicitly declare getter '-newFoo' with '__attribute__((objc_method_family(none)))' to return an 'unowned' object}}
82*67e74705SXin Li
83*67e74705SXin Li#define OBJC_METHOD_FAMILY_NONE __attribute__((objc_method_family(none)))
84*67e74705SXin Li- (id)newBar; // expected-note {{explicitly declare getter '-newBar' with 'OBJC_METHOD_FAMILY_NONE' to return an 'unowned' object}}
85*67e74705SXin Li@property (retain) id newBar; // expected-error {{property follows Cocoa naming convention for returning 'owned' objects}}
86*67e74705SXin Li
87*67e74705SXin Li@property (retain) id newBaz; // expected-error {{property follows Cocoa naming convention for returning 'owned' objects}} expected-note {{explicitly declare getter '-newBaz' with 'OBJC_METHOD_FAMILY_NONE' to return an 'unowned' object}}
88*67e74705SXin Li#undef OBJC_METHOD_FAMILY_NONE
89*67e74705SXin Li
90*67e74705SXin Li@property (retain, readonly) id newGarply; // expected-error {{property follows Cocoa naming convention for returning 'owned' objects}} expected-note {{explicitly declare getter '-newGarply' with '__attribute__((objc_method_family(none)))' to return an 'unowned' object}}
91*67e74705SXin Li@end
92*67e74705SXin Li
93*67e74705SXin Li@interface MethodFamilyDiags (Redeclarations)
94*67e74705SXin Li- (id)newGarply; // no note here
95*67e74705SXin Li@end
96*67e74705SXin Li
97*67e74705SXin Li@implementation MethodFamilyDiags
98*67e74705SXin Li@synthesize newGarply;
99*67e74705SXin Li@end
100*67e74705SXin Li
101*67e74705SXin Li
102*67e74705SXin Li// rdar://10187884
103*67e74705SXin Li@interface Super
104*67e74705SXin Li- (void)bar:(id)b; // expected-note {{parameter declared here}}
105*67e74705SXin Li- (void)bar1:(id) __attribute((ns_consumed)) b;
106*67e74705SXin Li- (void)ok:(id) __attribute((ns_consumed)) b;
107*67e74705SXin Li- (id)ns_non; // expected-note {{method declared here}}
108*67e74705SXin Li- (id)not_ret:(id) b __attribute((ns_returns_not_retained)); // expected-note {{method declared here}}
109*67e74705SXin Li- (id)both__returns_not_retained:(id) b __attribute((ns_returns_not_retained));
110*67e74705SXin Li@end
111*67e74705SXin Li
112*67e74705SXin Li@interface Sub : Super
113*67e74705SXin Li- (void)bar:(id) __attribute((ns_consumed)) b; // expected-error {{overriding method has mismatched ns_consumed attribute on its parameter}}
114*67e74705SXin Li- (void)bar1:(id)b;
115*67e74705SXin Li- (void)ok:(id) __attribute((ns_consumed)) b;
116*67e74705SXin Li- (id)ns_non __attribute((ns_returns_not_retained)); // expected-error {{overriding method has mismatched ns_returns_not_retained attributes}}
117*67e74705SXin Li- (id)not_ret:(id) b __attribute((ns_returns_retained)); // expected-error {{overriding method has mismatched ns_returns_retained attributes}}
118*67e74705SXin Li- (id)both__returns_not_retained:(id) b __attribute((ns_returns_not_retained));
119*67e74705SXin Li// rdar://12173491
120*67e74705SXin Li@property (copy, nonatomic) __attribute__((ns_returns_retained)) id (^fblock)(void);
121*67e74705SXin Li@end
122*67e74705SXin Li
123*67e74705SXin Li// Test that we give a good diagnostic here that mentions the missing
124*67e74705SXin Li// ownership qualifier.  We don't want this to get suppressed because
125*67e74705SXin Li// of an invalid conversion.
126*67e74705SXin Livoid test7(void) {
127*67e74705SXin Li  id x;
128*67e74705SXin Li  id *px = &x; // expected-error {{pointer to non-const type 'id' with no explicit ownership}}
129*67e74705SXin Li
130*67e74705SXin Li  I *y;
131*67e74705SXin Li  J **py = &y; // expected-error {{pointer to non-const type 'J *' with no explicit ownership}} expected-warning {{incompatible pointer types initializing}}
132*67e74705SXin Li}
133*67e74705SXin Li
134*67e74705SXin Livoid func(void) __attribute__((objc_ownership(none)));  // expected-warning {{'objc_ownership' only applies to Objective-C object or block pointer types; type here is 'void (void)'}}
135*67e74705SXin Listruct __attribute__((objc_ownership(none))) S2 {}; // expected-error {{'objc_ownership' attribute only applies to variables}}
136*67e74705SXin Li@interface I2
137*67e74705SXin Li    @property __attribute__((objc_ownership(frob))) id i; // expected-warning {{'objc_ownership' attribute argument not supported: 'frob'}}
138*67e74705SXin Li@end
139*67e74705SXin Li
140*67e74705SXin Li// rdar://15304886
141*67e74705SXin Li@interface NSObject @end
142*67e74705SXin Li
143*67e74705SXin Li@interface ControllerClass : NSObject @end
144*67e74705SXin Li
145*67e74705SXin Li@interface SomeClassOwnedByController
146*67e74705SXin Li@property (readonly) ControllerClass *controller; // expected-note {{property declared here}}
147*67e74705SXin Li
148*67e74705SXin Li// rdar://15465916
149*67e74705SXin Li@property (readonly, weak) ControllerClass *weak_controller;
150*67e74705SXin Li@end
151*67e74705SXin Li
152*67e74705SXin Li@interface SomeClassOwnedByController ()
153*67e74705SXin Li@property (readwrite, weak) ControllerClass *controller; // expected-warning {{primary property declaration is implicitly strong while redeclaration in class extension is weak}}
154*67e74705SXin Li
155*67e74705SXin Li@property (readwrite, weak) ControllerClass *weak_controller;
156*67e74705SXin Li@end
157