Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -fblocks -fobjc-arc -verify -Wno-objc-root-class %s |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2 | |
| 3 | // rdar://8843524 |
| 4 | |
| 5 | struct A { |
Akira Hatanaka | 5fbdccd | 2019-02-07 20:21:46 +0000 | [diff] [blame] | 6 | id x[4]; |
| 7 | id y; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8 | }; |
| 9 | |
| 10 | union u { |
Douglas Gregor | 0ad84b4 | 2013-01-28 20:13:44 +0000 | [diff] [blame] | 11 | id u; // expected-error {{ARC forbids Objective-C objects in union}} |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 12 | }; |
| 13 | |
Akira Hatanaka | 5fbdccd | 2019-02-07 20:21:46 +0000 | [diff] [blame] | 14 | union u_nontrivial_c { |
| 15 | struct A a; // expected-error {{non-trivial C types are disallowed in union}} |
| 16 | }; |
| 17 | |
| 18 | // Volatile fields are fine. |
| 19 | struct C { |
| 20 | volatile int x[4]; |
| 21 | volatile int y; |
| 22 | }; |
| 23 | |
| 24 | union u_trivial_c { |
| 25 | volatile int b; |
| 26 | struct C c; |
| 27 | }; |
| 28 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 29 | @interface I { |
| 30 | struct A a; |
| 31 | struct B { |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 32 | id y[10][20]; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 33 | id z; |
| 34 | } b; |
| 35 | |
| 36 | union u c; |
| 37 | }; |
| 38 | @end |
| 39 | |
Fariborz Jahanian | b989e6e | 2011-12-12 23:17:04 +0000 | [diff] [blame] | 40 | // rdar://10260525 |
| 41 | struct r10260525 { |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 42 | id (^block) (); |
Fariborz Jahanian | b989e6e | 2011-12-12 23:17:04 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 45 | struct S { |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 46 | id __attribute__((objc_ownership(none))) i; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 47 | void * vp; |
| 48 | int i1; |
| 49 | }; |
| 50 | |
| 51 | // rdar://9046528 |
| 52 | |
| 53 | @class NSError; |
| 54 | |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 55 | __autoreleasing id X; // expected-error {{global variables cannot have __autoreleasing ownership}} |
| 56 | __autoreleasing NSError *E; // expected-error {{global variables cannot have __autoreleasing ownership}} |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 57 | |
| 58 | |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 59 | extern id __autoreleasing X1; // expected-error {{global variables cannot have __autoreleasing ownership}} |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 60 | |
| 61 | void func() |
| 62 | { |
| 63 | id X; |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 64 | 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 McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 66 | |
| 67 | } |
| 68 | |
| 69 | // rdar://9157348 |
Fariborz Jahanian | 65b1377 | 2014-01-10 00:53:48 +0000 | [diff] [blame] | 70 | // rdar://15757510 |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 71 | |
| 72 | @interface J |
Jordan Rose | a34d04d | 2015-01-16 23:04:31 +0000 | [diff] [blame] | 73 | @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 McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 76 | @property (copy, nonatomic) id new; |
Jordan Rose | a34d04d | 2015-01-16 23:04:31 +0000 | [diff] [blame] | 77 | @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 McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 80 | @end |
| 81 | |
| 82 | @implementation J |
Fariborz Jahanian | 65b1377 | 2014-01-10 00:53:48 +0000 | [diff] [blame] | 83 | @synthesize newFoo; |
| 84 | @synthesize copyBar; |
| 85 | @synthesize allocBaz; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 86 | @synthesize new; |
| 87 | - new {return 0; }; |
Fariborz Jahanian | 65b1377 | 2014-01-10 00:53:48 +0000 | [diff] [blame] | 88 | |
| 89 | @dynamic newDFoo; |
| 90 | @dynamic copyDBar; |
| 91 | @dynamic allocDBaz; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 92 | @end |
| 93 | |
Fariborz Jahanian | ac8dbf0 | 2011-09-27 22:35:36 +0000 | [diff] [blame] | 94 | |
Jordan Rose | a34d04d | 2015-01-16 23:04:31 +0000 | [diff] [blame] | 95 | @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 Jahanian | ac8dbf0 | 2011-09-27 22:35:36 +0000 | [diff] [blame] | 118 | // 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 Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 135 | // rdar://12173491 |
| 136 | @property (copy, nonatomic) __attribute__((ns_returns_retained)) id (^fblock)(void); |
Fariborz Jahanian | ac8dbf0 | 2011-09-27 22:35:36 +0000 | [diff] [blame] | 137 | @end |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 138 | |
| 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. |
| 142 | void 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 Ballman | 5e13985 | 2013-09-01 19:11:23 +0000 | [diff] [blame] | 149 | |
| 150 | void 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)'}} |
| 151 | struct __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 Jahanian | 7196487 | 2013-10-26 00:35:39 +0000 | [diff] [blame] | 155 | |
| 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 Jahanian | 3b65982 | 2013-11-19 19:26:30 +0000 | [diff] [blame] | 163 | |
| 164 | // rdar://15465916 |
| 165 | @property (readonly, weak) ControllerClass *weak_controller; |
Fariborz Jahanian | 7196487 | 2013-10-26 00:35:39 +0000 | [diff] [blame] | 166 | @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 Jahanian | 3b65982 | 2013-11-19 19:26:30 +0000 | [diff] [blame] | 170 | |
| 171 | @property (readwrite, weak) ControllerClass *weak_controller; |
Fariborz Jahanian | 7196487 | 2013-10-26 00:35:39 +0000 | [diff] [blame] | 172 | @end |
Akira Hatanaka | 4c687f3 | 2018-02-06 23:44:40 +0000 | [diff] [blame] | 173 | |
| 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 |