Clang Project

clang_source_code/test/SemaObjC/arc-decls.m
1// RUN: %clang_cc1 -fsyntax-only -fblocks -fobjc-arc -verify -Wno-objc-root-class %s
2
3// rdar://8843524
4
5struct A {
6  id x[4];
7  id y;
8};
9
10union u {
11    id u; // expected-error {{ARC forbids Objective-C objects in union}}
12};
13
14union 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
29@interface I {
30   struct A a; 
31   struct B {
32    id y[10][20];
33    id z;
34   } b;
35
36   union u c; 
37};
38@end
39
40// rdar://10260525
41struct r10260525 {
42  id (^block) ();
43};
44
45struct S { 
46    id __attribute__((objc_ownership(none))) i;
47    void * vp;
48    int i1;
49};
50
51// rdar://9046528
52
53@class NSError;
54
55__autoreleasing id X; // expected-error {{global variables cannot have __autoreleasing ownership}}
56__autoreleasing NSError *E; // expected-error {{global variables cannot have __autoreleasing ownership}}
57
58
59extern id __autoreleasing X1; // expected-error {{global variables cannot have __autoreleasing ownership}}
60
61void func()
62{
63    id X;
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}}
66
67}
68
69// rdar://9157348
70// rdar://15757510
71
72@interface J
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}}
76@property (copy, nonatomic) id new;
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}}
80@end
81
82@implementation J
83@synthesize newFoo;
84@synthesize copyBar;
85@synthesize allocBaz;
86@synthesize new;
87- new {return 0; };
88
89@dynamic newDFoo;
90@dynamic copyDBar; 
91@dynamic allocDBaz;
92@end
93
94
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
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));
135// rdar://12173491
136@property (copy, nonatomic) __attribute__((ns_returns_retained)) id (^fblock)(void);
137@end
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.
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}
149
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
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}}
163
164// rdar://15465916
165@property (readonly, weak) ControllerClass *weak_controller;
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}}
170
171@property (readwrite, weak) ControllerClass *weak_controller;
172@end
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
195