blob: 0abd45dac33e2b4c1defa6138d62e1c63239a74a [file] [log] [blame]
Patrick Beardacfbe9e2012-04-06 18:12:22 +00001// RUN: %clang_cc1 -fsyntax-only -fblocks -fobjc-arc -verify -Wno-objc-root-class %s
John McCall31168b02011-06-15 23:02:42 +00002
3// rdar://8843524
4
5struct A {
Akira Hatanaka5fbdccd2019-02-07 20:21:46 +00006 id x[4];
7 id y;
John McCall31168b02011-06-15 23:02:42 +00008};
9
10union u {
Douglas Gregor0ad84b42013-01-28 20:13:44 +000011 id u; // expected-error {{ARC forbids Objective-C objects in union}}
John McCall31168b02011-06-15 23:02:42 +000012};
13
Akira Hatanaka5fbdccd2019-02-07 20:21:46 +000014union u_nontrivial_c {
15 struct A a; // expected-error {{non-trivial C types are disallowed in union}}
16};
17
18// Volatile fields are fine.
19struct C {
20 volatile int x[4];
21 volatile int y;
22};
23
24union u_trivial_c {
25 volatile int b;
26 struct C c;
27};
28
John McCall31168b02011-06-15 23:02:42 +000029@interface I {
30 struct A a;
31 struct B {
Akira Hatanaka7275da02018-02-28 07:15:55 +000032 id y[10][20];
John McCall31168b02011-06-15 23:02:42 +000033 id z;
34 } b;
35
36 union u c;
37};
38@end
39
Fariborz Jahanianb989e6e2011-12-12 23:17:04 +000040// rdar://10260525
41struct r10260525 {
Akira Hatanaka7275da02018-02-28 07:15:55 +000042 id (^block) ();
Fariborz Jahanianb989e6e2011-12-12 23:17:04 +000043};
44
John McCall31168b02011-06-15 23:02:42 +000045struct S {
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +000046 id __attribute__((objc_ownership(none))) i;
John McCall31168b02011-06-15 23:02:42 +000047 void * vp;
48 int i1;
49};
50
51// rdar://9046528
52
53@class NSError;
54
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +000055__autoreleasing id X; // expected-error {{global variables cannot have __autoreleasing ownership}}
56__autoreleasing NSError *E; // expected-error {{global variables cannot have __autoreleasing ownership}}
John McCall31168b02011-06-15 23:02:42 +000057
58
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +000059extern id __autoreleasing X1; // expected-error {{global variables cannot have __autoreleasing ownership}}
John McCall31168b02011-06-15 23:02:42 +000060
61void func()
62{
63 id X;
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +000064 static id __autoreleasing X1; // expected-error {{global variables cannot have __autoreleasing ownership}}
65 extern id __autoreleasing E; // expected-error {{global variables cannot have __autoreleasing ownership}}
John McCall31168b02011-06-15 23:02:42 +000066
67}
68
69// rdar://9157348
Fariborz Jahanian65b13772014-01-10 00:53:48 +000070// rdar://15757510
John McCall31168b02011-06-15 23:02:42 +000071
72@interface J
Jordan Rosea34d04d2015-01-16 23:04:31 +000073@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}}
74@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}}
75@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}}
John McCall31168b02011-06-15 23:02:42 +000076@property (copy, nonatomic) id new;
Jordan Rosea34d04d2015-01-16 23:04:31 +000077@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}}
78@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}}
79@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}}
John McCall31168b02011-06-15 23:02:42 +000080@end
81
82@implementation J
Fariborz Jahanian65b13772014-01-10 00:53:48 +000083@synthesize newFoo;
84@synthesize copyBar;
85@synthesize allocBaz;
John McCall31168b02011-06-15 23:02:42 +000086@synthesize new;
87- new {return 0; };
Fariborz Jahanian65b13772014-01-10 00:53:48 +000088
89@dynamic newDFoo;
90@dynamic copyDBar;
91@dynamic allocDBaz;
John McCall31168b02011-06-15 23:02:42 +000092@end
93
Fariborz Jahanianac8dbf02011-09-27 22:35:36 +000094
Jordan Rosea34d04d2015-01-16 23:04:31 +000095@interface MethodFamilyDiags
96@property (retain) id newFoo; // expected-error {{property follows Cocoa naming convention for returning 'owned' objects}}
97- (id)newFoo; // expected-note {{explicitly declare getter '-newFoo' with '__attribute__((objc_method_family(none)))' to return an 'unowned' object}}
98
99#define OBJC_METHOD_FAMILY_NONE __attribute__((objc_method_family(none)))
100- (id)newBar; // expected-note {{explicitly declare getter '-newBar' with 'OBJC_METHOD_FAMILY_NONE' to return an 'unowned' object}}
101@property (retain) id newBar; // expected-error {{property follows Cocoa naming convention for returning 'owned' objects}}
102
103@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}}
104#undef OBJC_METHOD_FAMILY_NONE
105
106@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}}
107@end
108
109@interface MethodFamilyDiags (Redeclarations)
110- (id)newGarply; // no note here
111@end
112
113@implementation MethodFamilyDiags
114@synthesize newGarply;
115@end
116
117
Fariborz Jahanianac8dbf02011-09-27 22:35:36 +0000118// rdar://10187884
119@interface Super
120- (void)bar:(id)b; // expected-note {{parameter declared here}}
121- (void)bar1:(id) __attribute((ns_consumed)) b;
122- (void)ok:(id) __attribute((ns_consumed)) b;
123- (id)ns_non; // expected-note {{method declared here}}
124- (id)not_ret:(id) b __attribute((ns_returns_not_retained)); // expected-note {{method declared here}}
125- (id)both__returns_not_retained:(id) b __attribute((ns_returns_not_retained));
126@end
127
128@interface Sub : Super
129- (void)bar:(id) __attribute((ns_consumed)) b; // expected-error {{overriding method has mismatched ns_consumed attribute on its parameter}}
130- (void)bar1:(id)b;
131- (void)ok:(id) __attribute((ns_consumed)) b;
132- (id)ns_non __attribute((ns_returns_not_retained)); // expected-error {{overriding method has mismatched ns_returns_not_retained attributes}}
133- (id)not_ret:(id) b __attribute((ns_returns_retained)); // expected-error {{overriding method has mismatched ns_returns_retained attributes}}
134- (id)both__returns_not_retained:(id) b __attribute((ns_returns_not_retained));
Fariborz Jahanian272b7dc2012-08-28 22:26:21 +0000135// rdar://12173491
136@property (copy, nonatomic) __attribute__((ns_returns_retained)) id (^fblock)(void);
Fariborz Jahanianac8dbf02011-09-27 22:35:36 +0000137@end
John McCall18a962b2012-01-26 20:04:03 +0000138
139// Test that we give a good diagnostic here that mentions the missing
140// ownership qualifier. We don't want this to get suppressed because
141// of an invalid conversion.
142void test7(void) {
143 id x;
144 id *px = &x; // expected-error {{pointer to non-const type 'id' with no explicit ownership}}
145
146 I *y;
147 J **py = &y; // expected-error {{pointer to non-const type 'J *' with no explicit ownership}} expected-warning {{incompatible pointer types initializing}}
148}
Aaron Ballman5e139852013-09-01 19:11:23 +0000149
150void 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)'}}
151struct __attribute__((objc_ownership(none))) S2 {}; // expected-error {{'objc_ownership' attribute only applies to variables}}
152@interface I2
153 @property __attribute__((objc_ownership(frob))) id i; // expected-warning {{'objc_ownership' attribute argument not supported: 'frob'}}
154@end
Fariborz Jahanian71964872013-10-26 00:35:39 +0000155
156// rdar://15304886
157@interface NSObject @end
158
159@interface ControllerClass : NSObject @end
160
161@interface SomeClassOwnedByController
162@property (readonly) ControllerClass *controller; // expected-note {{property declared here}}
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000163
164// rdar://15465916
165@property (readonly, weak) ControllerClass *weak_controller;
Fariborz Jahanian71964872013-10-26 00:35:39 +0000166@end
167
168@interface SomeClassOwnedByController ()
169@property (readwrite, weak) ControllerClass *controller; // expected-warning {{primary property declaration is implicitly strong while redeclaration in class extension is weak}}
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000170
171@property (readwrite, weak) ControllerClass *weak_controller;
Fariborz Jahanian71964872013-10-26 00:35:39 +0000172@end
Akira Hatanaka4c687f32018-02-06 23:44:40 +0000173
174@interface I3
175@end
176
177@interface D3 : I3
178@end
179
180@interface D3 (Cat1)
181- (id)method;
182@end
183
184@interface I3 (Cat2)
185// FIXME: clang should diagnose mismatch between methods in D3(Cat1) and
186// I3(Cat2).
187- (id)method __attribute__((ns_returns_retained));
188@end
189
190@implementation D3
191- (id)method {
192 return (id)0;
193}
194@end