self-init.m revision 9a70cddef6850f302615b4f5d27f16ec45926ca6
1// RUN: %clang_cc1 -analyze -analyzer-checker=osx.cocoa.SelfInit -fobjc-default-synthesize-properties %s -verify
2
3@class NSZone, NSCoder;
4@protocol NSObject- (id)self;
5@end
6@protocol NSCopying  - (id)copyWithZone:(NSZone *)zone;
7@end 
8@protocol NSMutableCopying  - (id)mutableCopyWithZone:(NSZone *)zone;
9@end 
10@protocol NSCoding  - (void)encodeWithCoder:(NSCoder *)aCoder;
11@end
12@interface NSObject <NSObject> {}
13+ (id)allocWithZone:(NSZone *)zone;
14+ (id)alloc;
15- (void)dealloc;
16-(id)class;
17-(id)init;
18-(id)release;
19@end
20@interface NSProxy <NSObject> {}
21@end
22
23//#import "Foundation/NSObject.h"
24typedef unsigned NSUInteger;
25typedef long NSInteger;
26
27@interface NSInvocation : NSObject {}
28- (void)getArgument:(void *)argumentLocation atIndex:(NSInteger)idx;
29- (void)setArgument:(void *)argumentLocation atIndex:(NSInteger)idx;
30@end
31
32@class NSMethodSignature, NSCoder, NSString, NSEnumerator;
33@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
34- (NSUInteger)length;
35+ (id)stringWithUTF8String:(const char *)nullTerminatedCString;
36@end extern NSString * const NSBundleDidLoadNotification;
37@interface NSAssertionHandler : NSObject {}
38+ (NSAssertionHandler *)currentHandler;
39- (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...;
40@end
41extern NSString * const NSConnectionReplyMode;
42
43@interface NSBundle : NSObject
44+(id)loadNibNamed:(NSString*)s owner:(id)o;
45@end
46
47void log(void *obj);
48extern void *somePtr;
49
50@class MyObj;
51extern id _commonInit(MyObj *self);
52
53@interface MyObj : NSObject {
54	id myivar;
55	int myint;
56}
57-(id)_init;
58-(id)initWithSomething:(int)x;
59-(void)doSomething;
60+(id)commonInitMember:(id)s;
61@end
62
63@interface MyProxyObj : NSProxy {}
64-(id)init;
65@end
66
67@implementation MyObj
68
69-(id)init {
70  do { if (!((somePtr != 0))) { [[NSAssertionHandler currentHandler] handleFailureInMethod:_cmd object:self file:[NSString stringWithUTF8String:"init.m"] lineNumber:21 description:(@"Invalid parameter not satisfying: %s"), ("x != 0"), (0), (0), (0), (0)]; } } while(0);
71  return [self initWithSomething:0];
72}
73
74-(id)init2 {
75  self = [self initWithSomething:0];
76  return self;
77}
78
79-(id)init3 {
80	log([self class]);
81	return [self initWithSomething:0];
82}
83
84-(id)init4 {
85	self = [super init];
86	if (self) {
87		log(&self);
88	}
89	return self;
90}
91
92-(id)init4_w {
93  [super init];
94  if (self) {
95    log(&self);
96  }
97  return self; // expected-warning {{Returning 'self' while it is not set to the result of '[(super or self) init...]'}}
98}
99
100- (id)initWithSomething:(int)x {    
101	if ((self = [super init]))
102		myint = x;
103	return self;
104}
105
106-(id)_init {
107	myivar = 0;
108	return self;
109}
110
111-(id)init5 {
112  [NSBundle loadNibNamed:@"Window" owner:self];
113  return [self initWithSomething:0];
114}
115
116-(id)init6 {
117  [NSBundle loadNibNamed:@"Window" owner:myivar]; // no-warning
118  return [self initWithSomething:0];
119}
120
121-(id)init7 {
122  if (0 != (self = [self _init]))
123    myivar = 0;
124  return self;
125}
126
127-(id)init8 {
128    if ((self = [super init])) {
129		log(&self);
130		myivar = 0;
131    }
132    return self;
133}
134
135-(id)init9 {
136  [self doSomething];
137  return self; // no-warning
138}
139
140-(id)init10 {
141  myivar = 0; // no-warning
142  return self;
143}
144
145-(id)init11 {
146  return self; // no-warning
147}
148
149-(id)init12 {
150	[super init];
151	return self; // expected-warning {{Returning 'self'}}
152}
153
154-(id)init13 {
155	if (self == [super init]) {
156	  myivar = 0; // expected-warning {{Instance variable used}}
157	}
158	return self; // expected-warning {{Returning 'self'}}
159}
160
161-(id)init14 {
162  if (!(self = _commonInit(self)))
163    return 0;
164  return self;
165}
166
167-(id)init14_w {
168  [super init];
169  self = _commonInit(self);
170  return self; // expected-warning {{Returning 'self' while it is not set to the result of '[(super or self) init...]'}}
171}
172
173-(id)init15 {
174  if (!(self = [super init]))
175    return 0;
176  return self;
177}
178
179-(id)init16 {
180  somePtr = [super init];
181  self = somePtr;
182  myivar = 0; 
183  return self;
184}
185
186-(id)init17 {
187  somePtr = [super init];
188  myivar = 0; // expected-warning {{Instance variable used}}
189  return 0;
190}
191
192-(id)init18 {
193  self = [super init];
194  self = _commonInit(self);
195  return self;
196}
197
198+(id)commonInitMember:(id)s {
199  return s;
200}
201
202-(id)init19 {
203  self = [super init];
204  self = [MyObj commonInitMember:self];
205  return self;
206}
207
208-(id)init19_w {
209  [super init];
210  self = [MyObj commonInitMember:self];
211  return self; // expected-warning {{Returning 'self'}}
212}
213
214-(void)doSomething {}
215
216@end
217
218@implementation MyProxyObj
219
220- (id)init { return self; }
221
222@end
223
224
225// Test for radar://10973514 : self should not be invalidated by a method call.
226@interface Test : NSObject {
227    NSInvocation *invocation_;
228}
229@end
230@implementation Test
231-(id) initWithTarget:(id) rec selector:(SEL) cb {
232  if (self=[super init]) {
233    [invocation_ setArgument:&self atIndex:2];
234  }   
235  return self;
236}
237@end
238
239// Test radar:11235991 - passing self to a call to super.
240@protocol MyDelegate
241@end
242@interface Object : NSObject
243- (id) initWithObject: (id)i;
244@end
245@interface Derived: Object <MyDelegate>
246- (id) initWithInt: (int)t;
247@property (nonatomic, retain, readwrite) Object *size;
248@end
249@implementation Derived 
250- (id) initWithInt: (int)t {
251   if ((self = [super initWithObject:self])) {
252      _size = [[Object alloc] init];
253   }
254   return self;
255}
256@end
257