blob: 10b0c4da47b646be7ee91eb7ce5923eaef3be14a [file] [log] [blame]
Chad Rosier454393e2012-04-24 22:40:01 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=osx.cocoa.SelfInit -fobjc-default-synthesize-properties -fno-builtin %s -verify
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +00002
3@class NSZone, NSCoder;
Anna Zaks6a2a1862012-05-08 21:19:21 +00004@protocol NSObject
5- (id)self;
Anna Zaksf420fe32012-03-05 18:58:25 +00006@end
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +00007@protocol NSCopying - (id)copyWithZone:(NSZone *)zone;
8@end
9@protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone;
10@end
11@protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder;
12@end
13@interface NSObject <NSObject> {}
14+ (id)allocWithZone:(NSZone *)zone;
15+ (id)alloc;
16- (void)dealloc;
17-(id)class;
18-(id)init;
19-(id)release;
20@end
Argyrios Kyrtzidiseaf969b2011-01-25 23:54:44 +000021@interface NSProxy <NSObject> {}
22@end
23
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000024//#import "Foundation/NSObject.h"
25typedef unsigned NSUInteger;
Anna Zaksf420fe32012-03-05 18:58:25 +000026typedef long NSInteger;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000027
Anna Zaksf420fe32012-03-05 18:58:25 +000028@interface NSInvocation : NSObject {}
29- (void)getArgument:(void *)argumentLocation atIndex:(NSInteger)idx;
30- (void)setArgument:(void *)argumentLocation atIndex:(NSInteger)idx;
31@end
32
33@class NSMethodSignature, NSCoder, NSString, NSEnumerator;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000034@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
35- (NSUInteger)length;
36+ (id)stringWithUTF8String:(const char *)nullTerminatedCString;
37@end extern NSString * const NSBundleDidLoadNotification;
38@interface NSAssertionHandler : NSObject {}
39+ (NSAssertionHandler *)currentHandler;
40- (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...;
41@end
42extern NSString * const NSConnectionReplyMode;
43
44@interface NSBundle : NSObject
45+(id)loadNibNamed:(NSString*)s owner:(id)o;
46@end
47
48void log(void *obj);
49extern void *somePtr;
50
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +000051@class MyObj;
Anna Zaks9a70cdd2012-04-16 21:51:09 +000052extern id _commonInit(MyObj *self);
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +000053
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000054@interface MyObj : NSObject {
55 id myivar;
56 int myint;
57}
58-(id)_init;
59-(id)initWithSomething:(int)x;
60-(void)doSomething;
Anna Zaks9a70cdd2012-04-16 21:51:09 +000061+(id)commonInitMember:(id)s;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000062@end
63
Argyrios Kyrtzidiseaf969b2011-01-25 23:54:44 +000064@interface MyProxyObj : NSProxy {}
65-(id)init;
66@end
67
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000068@implementation MyObj
69
70-(id)init {
71 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);
72 return [self initWithSomething:0];
73}
74
75-(id)init2 {
76 self = [self initWithSomething:0];
77 return self;
78}
79
80-(id)init3 {
81 log([self class]);
82 return [self initWithSomething:0];
83}
84
85-(id)init4 {
86 self = [super init];
87 if (self) {
88 log(&self);
89 }
90 return self;
91}
92
Anna Zaks9a70cdd2012-04-16 21:51:09 +000093-(id)init4_w {
94 [super init];
95 if (self) {
96 log(&self);
97 }
98 return self; // expected-warning {{Returning 'self' while it is not set to the result of '[(super or self) init...]'}}
99}
100
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000101- (id)initWithSomething:(int)x {
102 if ((self = [super init]))
103 myint = x;
104 return self;
105}
106
107-(id)_init {
108 myivar = 0;
109 return self;
110}
111
112-(id)init5 {
113 [NSBundle loadNibNamed:@"Window" owner:self];
114 return [self initWithSomething:0];
115}
116
117-(id)init6 {
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000118 [NSBundle loadNibNamed:@"Window" owner:myivar]; // no-warning
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000119 return [self initWithSomething:0];
120}
121
122-(id)init7 {
123 if (0 != (self = [self _init]))
124 myivar = 0;
125 return self;
126}
127
128-(id)init8 {
129 if ((self = [super init])) {
130 log(&self);
131 myivar = 0;
132 }
133 return self;
134}
135
136-(id)init9 {
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000137 [self doSomething];
138 return self; // no-warning
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000139}
140
141-(id)init10 {
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000142 myivar = 0; // no-warning
143 return self;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000144}
145
146-(id)init11 {
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000147 return self; // no-warning
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000148}
149
150-(id)init12 {
151 [super init];
152 return self; // expected-warning {{Returning 'self'}}
153}
154
155-(id)init13 {
Argyrios Kyrtzidis0e2dc3a2011-02-01 18:24:22 +0000156 if (self == [super init]) {
Argyrios Kyrtzidis4717f162011-01-26 01:26:41 +0000157 myivar = 0; // expected-warning {{Instance variable used}}
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000158 }
159 return self; // expected-warning {{Returning 'self'}}
160}
161
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000162-(id)init14 {
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000163 if (!(self = _commonInit(self)))
164 return 0;
165 return self;
166}
167
Anna Zaks9a70cdd2012-04-16 21:51:09 +0000168-(id)init14_w {
169 [super init];
170 self = _commonInit(self);
171 return self; // expected-warning {{Returning 'self' while it is not set to the result of '[(super or self) init...]'}}
172}
173
Anna Zaks1efcc422012-02-04 02:31:37 +0000174-(id)init15 {
175 if (!(self = [super init]))
176 return 0;
177 return self;
178}
179
180-(id)init16 {
181 somePtr = [super init];
182 self = somePtr;
183 myivar = 0;
184 return self;
185}
186
187-(id)init17 {
188 somePtr = [super init];
189 myivar = 0; // expected-warning {{Instance variable used}}
Anna Zaks4f502fb2012-02-04 02:31:53 +0000190 return 0;
Anna Zaks1efcc422012-02-04 02:31:37 +0000191}
192
Anna Zaks9a70cdd2012-04-16 21:51:09 +0000193-(id)init18 {
194 self = [super init];
195 self = _commonInit(self);
196 return self;
197}
198
199+(id)commonInitMember:(id)s {
200 return s;
201}
202
203-(id)init19 {
204 self = [super init];
205 self = [MyObj commonInitMember:self];
206 return self;
207}
208
209-(id)init19_w {
210 [super init];
211 self = [MyObj commonInitMember:self];
212 return self; // expected-warning {{Returning 'self'}}
213}
214
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000215-(void)doSomething {}
216
217@end
Argyrios Kyrtzidiseaf969b2011-01-25 23:54:44 +0000218
219@implementation MyProxyObj
220
221- (id)init { return self; }
222
223@end
Anna Zaksf420fe32012-03-05 18:58:25 +0000224
225
226// Test for radar://10973514 : self should not be invalidated by a method call.
227@interface Test : NSObject {
228 NSInvocation *invocation_;
229}
230@end
231@implementation Test
232-(id) initWithTarget:(id) rec selector:(SEL) cb {
233 if (self=[super init]) {
234 [invocation_ setArgument:&self atIndex:2];
235 }
236 return self;
237}
238@end
239
Anna Zaks9a70cdd2012-04-16 21:51:09 +0000240// Test radar:11235991 - passing self to a call to super.
241@protocol MyDelegate
242@end
243@interface Object : NSObject
244- (id) initWithObject: (id)i;
245@end
246@interface Derived: Object <MyDelegate>
247- (id) initWithInt: (int)t;
248@property (nonatomic, retain, readwrite) Object *size;
249@end
250@implementation Derived
251- (id) initWithInt: (int)t {
252 if ((self = [super initWithObject:self])) {
253 _size = [[Object alloc] init];
254 }
255 return self;
256}
257@end
Anna Zaks6a2a1862012-05-08 21:19:21 +0000258
259// Test for radar://11125870: init constructing a special instance.
260typedef signed char BOOL;
261@interface MyClass : NSObject
262@end
263@implementation MyClass
264+ (id)specialInstance {
265 return [[MyClass alloc] init];
266}
267- (id)initSpecially:(BOOL)handleSpecially {
268 if ((self = [super init])) {
269 if (handleSpecially) {
270 self = [MyClass specialInstance];
271 }
272 }
273 return self;
274}
275- (id)initSelfSelf {
276 if ((self = [super init])) {
277 self = self;
278 }
279 return self;
280}
281@end
282