1// RUN: %clang_cc1 -fsyntax-only -Wno-incomplete-implementation -verify -fblocks %s
2
3#define NS_DESIGNATED_INITIALIZER __attribute__((objc_designated_initializer))
4
5void fnfoo(void) NS_DESIGNATED_INITIALIZER; // expected-error {{only applies to init methods of interface or class extension declarations}}
6
7@protocol P1
8-(id)init NS_DESIGNATED_INITIALIZER; // expected-error {{only applies to init methods of interface or class extension declarations}}
9@end
10
11__attribute__((objc_root_class))
12@interface I1
13-(void)meth NS_DESIGNATED_INITIALIZER; // expected-error {{only applies to init methods of interface or class extension declarations}}
14-(id)init NS_DESIGNATED_INITIALIZER;
15+(id)init NS_DESIGNATED_INITIALIZER; // expected-error {{only applies to init methods of interface or class extension declarations}}
16@end
17
18@interface I1(cat)
19-(id)init2 NS_DESIGNATED_INITIALIZER; // expected-error {{only applies to init methods of interface or class extension declarations}}
20@end
21
22@interface I1()
23-(id)init3 NS_DESIGNATED_INITIALIZER;
24@end
25
26@implementation I1
27-(void)meth {}
28-(id)init NS_DESIGNATED_INITIALIZER { return 0; } // expected-error {{only applies to init methods of interface or class extension declarations}}
29+(id)init { return 0; }
30-(id)init3 { return 0; }
31-(id)init4 NS_DESIGNATED_INITIALIZER { return 0; } // expected-error {{only applies to init methods of interface or class extension declarations}} \
32									 			   // expected-warning {{convenience initializer missing a 'self' call to another initializer}}
33@end
34
35__attribute__((objc_root_class))
36@interface B1
37-(id)initB1 NS_DESIGNATED_INITIALIZER; // expected-note 6 {{method marked as designated initializer of the class here}}
38-(id)initB2;
39@end
40
41@interface B1()
42-(id)initB3 NS_DESIGNATED_INITIALIZER; // expected-note 4 {{method marked as designated initializer of the class here}}
43@end;
44
45@implementation B1
46-(id)initB1 { return 0; }
47-(id)initB2 { return 0; } // expected-warning {{convenience initializer missing a 'self' call to another initializer}}
48-(id)initB3 { return 0; }
49@end
50
51@interface S1 : B1
52-(id)initS1 NS_DESIGNATED_INITIALIZER; // expected-note {{method marked as designated initializer of the class here}}
53-(id)initS2 NS_DESIGNATED_INITIALIZER;
54-(id)initS3 NS_DESIGNATED_INITIALIZER; // expected-note 2 {{method marked as designated initializer of the class here}}
55-(id)initB1;
56@end
57
58@interface S1()
59-(id)initS4 NS_DESIGNATED_INITIALIZER; // expected-note 2 {{method marked as designated initializer of the class here}}
60@end
61
62@implementation S1
63-(id)initS1 { // expected-warning {{designated initializer missing a 'super' call to a designated initializer of the super class}}
64  return 0;
65}
66-(id)initS2 {
67  return [super initB1];
68}
69-(id)initS3 { // expected-warning {{designated initializer missing a 'super' call to a designated initializer of the super class}}
70  return [super initB2]; // expected-warning {{designated initializer invoked a non-designated initializer}}
71}
72-(id)initS4 { // expected-warning {{designated initializer missing a 'super' call to a designated initializer of the super class}}
73  return [self initB1]; // expected-warning {{designated initializer should only invoke a designated initializer on 'super'}}
74}
75-(id)initB1 {
76  return [self initS1];
77}
78-(id)initB3 {
79  return [self initS1];
80}
81@end
82
83@interface S2 : B1
84-(id)initB1;
85@end
86
87@interface SS2 : S2
88-(id)initSS1 NS_DESIGNATED_INITIALIZER;
89@end
90
91@implementation SS2 // expected-warning {{method override for the designated initializer of the superclass '-initB1' not found}} \
92                    // expected-warning {{method override for the designated initializer of the superclass '-initB3' not found}}
93-(id)initSS1 {
94  return [super initB1];
95}
96@end
97
98@interface S3 : B1
99-(id)initS1 NS_DESIGNATED_INITIALIZER; // expected-note {{method marked as designated initializer of the class here}}
100@end
101
102@interface SS3 : S3
103-(id)initSS1 NS_DESIGNATED_INITIALIZER; // expected-note 2 {{method marked as designated initializer of the class here}}
104@end
105
106@implementation SS3 // expected-warning {{method override for the designated initializer of the superclass '-initS1' not found}}
107-(id)initSS1 { // expected-warning {{designated initializer missing a 'super' call to a designated initializer of the super class}}
108  return [super initB1]; // expected-warning {{designated initializer invoked a non-designated initializer}}
109}
110@end
111
112@interface S4 : B1
113-(id)initB1;
114-(id)initB3;
115@end
116
117@implementation S4
118-(id)initB1 { // expected-warning {{designated initializer missing a 'super' call to a designated initializer of the super class}}
119  return 0;
120}
121-(id)initB3 {
122  return [super initB3];
123}
124@end
125
126@interface S5 : B1
127-(void)meth;
128@end
129
130@implementation S5
131-(id)initB1 { // expected-warning {{designated initializer missing a 'super' call to a designated initializer of the super class}}
132  return 0;
133}
134-(id)initB3 {
135  [self initB1]; // expected-warning {{designated initializer should only invoke a designated initializer on 'super'}}
136  S5 *s;
137  [s initB1];
138  [self meth];
139  void (^blk)(void) = ^{
140    [self initB1]; // expected-warning {{designated initializer should only invoke a designated initializer on 'super'}}
141  };
142  return [super initB3];
143}
144-(void)meth {}
145@end
146
147@interface S6 : B1
148-(id)initS1 NS_DESIGNATED_INITIALIZER;
149-(id)initS2;
150-(id)initS3;
151-(id)initS4;
152@end
153
154@implementation S6 // expected-warning {{method override for the designated initializer of the superclass '-initB1' not found}} \
155                   // expected-warning {{method override for the designated initializer of the superclass '-initB3' not found}}
156-(id)initS1 {
157  return [super initB1];
158}
159-(id)initS2 { // expected-warning {{convenience initializer missing a 'self' call to another initializer}}
160  return [super initB1]; // expected-warning {{convenience initializer should not invoke an initializer on 'super'}}
161}
162-(id)initS3 {
163  return [self initB1];
164}
165-(id)initS4 {
166  return [self initS1];
167}
168-(id)initS5 {
169  [super initB1]; // expected-warning {{convenience initializer should not invoke an initializer on 'super'}}
170  void (^blk)(void) = ^{
171    [super initB1]; // expected-warning {{convenience initializer should not invoke an initializer on 'super'}}
172  };
173  return [self initS1];
174}
175-(id)initS6 { // expected-warning {{convenience initializer missing a 'self' call to another initializer}}
176  S6 *s;
177  return [s initS1];
178}
179@end
180
181@interface SS4 : S4
182-(id)initB1;
183@end
184
185@implementation SS4
186-(id)initB1 { // expected-warning {{designated initializer missing a 'super' call to a designated initializer of the super class}}
187  return 0;
188}
189@end
190
191@interface S7 : B1
192-(id)initB1;
193-(id)initB3;
194-(id)initNewOne;
195@end
196
197@interface SS7 : S7
198-(id)initB1;
199@end
200
201@implementation SS7
202-(id)initB1 {
203  return 0;
204}
205@end
206
207__attribute__((objc_root_class))
208@interface B2
209-(id)init;
210@end
211
212@interface S8: B2
213-(id)initS8 NS_DESIGNATED_INITIALIZER;
214@end
215
216@implementation S8
217-(id)initS8
218{
219  return [super init];
220}
221@end
222
223@interface S9 : B1
224-(id)initB1;
225-(id)initB3;
226@end
227
228@interface S9(secondInit)
229-(id)initNewOne;
230@end
231
232@interface SS9 : S9
233-(id)initB1;
234@end
235
236@implementation SS9
237-(id)initB1 { // expected-warning {{designated initializer missing a 'super' call to a designated initializer of the super class}}
238  return 0;
239}
240@end
241
242// rdar://16261494
243@class GEOPDAnalyticMetadata; // expected-note {{forward declaration of class here}}
244
245@implementation GEOPDAnalyticMetadata (PlaceCardExtras) // expected-error {{cannot find interface declaration for 'GEOPDAnalyticMetadata'}}
246- (instancetype)initInProcess
247{
248    return ((void*)0);
249}
250@end
251
252// rdar://16305460
253__attribute__((objc_root_class))
254@interface MyObject
255- (instancetype)initWithStuff:(id)stuff __attribute__((objc_designated_initializer));
256- (instancetype)init __attribute__((unavailable));
257@end
258
259@implementation MyObject
260- (instancetype)init
261{
262   return ((void*)0);
263}
264@end
265
266// rdar://16323233
267__attribute__((objc_root_class))
268@interface B4
269-(id)initB4 NS_DESIGNATED_INITIALIZER; // expected-note 4 {{method marked as designated initializer of the class here}}
270-(id)initNonDI;
271@end
272
273@interface rdar16323233 : B4
274-(id)initS4 NS_DESIGNATED_INITIALIZER;
275@end
276
277@implementation rdar16323233
278-(id)initS4 {
279    static id sSharedObject = (void*)0;
280    (void)^(void) {
281        sSharedObject = [super initB4];
282    };
283    return 0;
284}
285-(id)initB4 {
286   return [self initS4];
287}
288@end
289
290@interface S1B4 : B4
291@end
292@implementation S1B4
293-(id)initB4 { // expected-warning {{designated initializer missing a 'super' call to a designated initializer of the super class}}
294   return [super initNonDI]; // expected-warning {{designated initializer invoked a non-designated initializer}}
295}
296@end
297
298@interface S2B4 : B4
299-(id)initB4;
300@end
301@implementation S2B4
302-(id)initB4 { // expected-warning {{designated initializer missing a 'super' call to a designated initializer of the super class}}
303   return [super initNonDI]; // expected-warning {{designated initializer invoked a non-designated initializer}}
304}
305@end
306
307@interface S3B4 : B4
308@end
309@implementation S3B4
310-(id)initNew {
311  return [super initB4];
312}
313-(id)initB4 {
314   return [self initNew];
315}
316@end
317
318@interface S4B4 : B4
319-(id)initNew;
320@end
321@implementation S4B4
322-(id)initNew {
323  return [super initB4];
324}
325-(id)initB4 {
326   return [self initNew];
327}
328@end
329
330@interface S5B4 : B4
331-(id)initB4;
332@end
333@implementation S5B4
334-(id)initNew {
335  return [super initB4];
336}
337-(id)initB4 {
338   return [self initNew];
339}
340@end
341
342@interface S6B4 : B4
343-(id)initNew;
344-(id)initB4;
345@end
346@implementation S6B4
347-(id)initNew {
348  return [super initB4];
349}
350-(id)initB4 {
351   return [self initNew];
352}
353@end
354
355__attribute__((objc_root_class))
356@interface NSObject
357-(instancetype) init NS_DESIGNATED_INITIALIZER; // expected-note {{method marked as designated initializer of the class here}}
358@end
359
360@interface Test3 : NSObject
361@end
362
363@implementation Test3
364-(instancetype) initWithBasePath:(id)path {
365  return [super init];
366}
367-(instancetype) init {
368  return [self initWithBasePath:0];
369}
370@end
371
372@interface Test1 : NSObject
373-(instancetype) init NS_DESIGNATED_INITIALIZER;
374@end
375@implementation Test1
376-(instancetype) init {
377  return self;
378}
379@end
380
381
382@interface Test2 : NSObject
383@end
384@interface SubTest2 : Test2
385@end
386@implementation SubTest2
387-(instancetype) init { // expected-warning {{designated initializer missing a 'super' call to a designated initializer of the super class}}
388  return self;
389}
390@end
391
392__attribute__((objc_root_class))
393@interface RootNoDI
394-(id)init;
395@end
396
397@interface Base : RootNoDI
398@end
399
400@implementation Base
401@end
402
403@interface Derived : Base
404- (instancetype)initWithInt:(int)n NS_DESIGNATED_INITIALIZER;
405@end
406
407@implementation Derived
408- (instancetype)initWithInt:(int)n
409{
410  return [super init];
411}
412@end
413