blob: b0c51a2b37e19f47f534bc6c6340865737616cff [file] [log] [blame]
Anna Zaks5808eb82012-07-30 23:48:36 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=osx.cocoa.SelfInit -fobjc-default-synthesize-properties -analyzer-ipa=dynamic -fno-builtin %s -verify
Chad Rosier155005c2012-04-24 22:40:01 +00002// RUN: %clang_cc1 -analyze -analyzer-checker=osx.cocoa.SelfInit -fobjc-default-synthesize-properties -fno-builtin %s -verify
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +00003
4@class NSZone, NSCoder;
Anna Zaks66843482012-05-08 21:19:21 +00005@protocol NSObject
6- (id)self;
Anna Zaks53a0b6c2012-03-05 18:58:25 +00007@end
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +00008@protocol NSCopying - (id)copyWithZone:(NSZone *)zone;
9@end
10@protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone;
11@end
12@protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder;
13@end
14@interface NSObject <NSObject> {}
15+ (id)allocWithZone:(NSZone *)zone;
16+ (id)alloc;
17- (void)dealloc;
18-(id)class;
19-(id)init;
20-(id)release;
21@end
Argyrios Kyrtzidis3ae681e2011-01-25 23:54:44 +000022@interface NSProxy <NSObject> {}
23@end
24
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +000025//#import "Foundation/NSObject.h"
26typedef unsigned NSUInteger;
Anna Zaks53a0b6c2012-03-05 18:58:25 +000027typedef long NSInteger;
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +000028
Anna Zaks53a0b6c2012-03-05 18:58:25 +000029@interface NSInvocation : NSObject {}
30- (void)getArgument:(void *)argumentLocation atIndex:(NSInteger)idx;
31- (void)setArgument:(void *)argumentLocation atIndex:(NSInteger)idx;
32@end
33
34@class NSMethodSignature, NSCoder, NSString, NSEnumerator;
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +000035@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
36- (NSUInteger)length;
37+ (id)stringWithUTF8String:(const char *)nullTerminatedCString;
38@end extern NSString * const NSBundleDidLoadNotification;
39@interface NSAssertionHandler : NSObject {}
40+ (NSAssertionHandler *)currentHandler;
41- (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...;
42@end
43extern NSString * const NSConnectionReplyMode;
44
45@interface NSBundle : NSObject
46+(id)loadNibNamed:(NSString*)s owner:(id)o;
47@end
48
49void log(void *obj);
50extern void *somePtr;
51
Argyrios Kyrtzidisdd03d8d2011-02-05 05:54:53 +000052@class MyObj;
Anna Zaks51244c22012-04-16 21:51:09 +000053extern id _commonInit(MyObj *self);
Argyrios Kyrtzidisdd03d8d2011-02-05 05:54:53 +000054
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +000055@interface MyObj : NSObject {
56 id myivar;
57 int myint;
58}
59-(id)_init;
60-(id)initWithSomething:(int)x;
61-(void)doSomething;
Anna Zaks51244c22012-04-16 21:51:09 +000062+(id)commonInitMember:(id)s;
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +000063@end
64
Argyrios Kyrtzidis3ae681e2011-01-25 23:54:44 +000065@interface MyProxyObj : NSProxy {}
66-(id)init;
67@end
68
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +000069@implementation MyObj
70
71-(id)init {
72 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);
73 return [self initWithSomething:0];
74}
75
76-(id)init2 {
77 self = [self initWithSomething:0];
78 return self;
79}
80
81-(id)init3 {
82 log([self class]);
83 return [self initWithSomething:0];
84}
85
86-(id)init4 {
87 self = [super init];
88 if (self) {
89 log(&self);
90 }
91 return self;
92}
93
Anna Zaks51244c22012-04-16 21:51:09 +000094-(id)init4_w {
95 [super init];
96 if (self) {
97 log(&self);
98 }
99 return self; // expected-warning {{Returning 'self' while it is not set to the result of '[(super or self) init...]'}}
100}
101
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000102- (id)initWithSomething:(int)x {
103 if ((self = [super init]))
104 myint = x;
105 return self;
106}
107
108-(id)_init {
109 myivar = 0;
110 return self;
111}
112
113-(id)init5 {
114 [NSBundle loadNibNamed:@"Window" owner:self];
115 return [self initWithSomething:0];
116}
117
118-(id)init6 {
Ted Kremenek70aeefa2011-02-12 03:03:54 +0000119 [NSBundle loadNibNamed:@"Window" owner:myivar]; // no-warning
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000120 return [self initWithSomething:0];
121}
122
123-(id)init7 {
124 if (0 != (self = [self _init]))
125 myivar = 0;
126 return self;
127}
128
129-(id)init8 {
130 if ((self = [super init])) {
131 log(&self);
132 myivar = 0;
133 }
134 return self;
135}
136
137-(id)init9 {
Ted Kremenek70aeefa2011-02-12 03:03:54 +0000138 [self doSomething];
139 return self; // no-warning
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000140}
141
142-(id)init10 {
Ted Kremenek70aeefa2011-02-12 03:03:54 +0000143 myivar = 0; // no-warning
144 return self;
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000145}
146
147-(id)init11 {
Ted Kremenek70aeefa2011-02-12 03:03:54 +0000148 return self; // no-warning
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000149}
150
151-(id)init12 {
152 [super init];
153 return self; // expected-warning {{Returning 'self'}}
154}
155
156-(id)init13 {
Argyrios Kyrtzidis8b6ec682011-02-01 18:24:22 +0000157 if (self == [super init]) {
Argyrios Kyrtzidis8781b7d2011-01-26 01:26:41 +0000158 myivar = 0; // expected-warning {{Instance variable used}}
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000159 }
160 return self; // expected-warning {{Returning 'self'}}
161}
162
Argyrios Kyrtzidisdd03d8d2011-02-05 05:54:53 +0000163-(id)init14 {
Argyrios Kyrtzidisdd03d8d2011-02-05 05:54:53 +0000164 if (!(self = _commonInit(self)))
165 return 0;
166 return self;
167}
168
Anna Zaks51244c22012-04-16 21:51:09 +0000169-(id)init14_w {
170 [super init];
171 self = _commonInit(self);
172 return self; // expected-warning {{Returning 'self' while it is not set to the result of '[(super or self) init...]'}}
173}
174
Anna Zaks00790d92012-02-04 02:31:37 +0000175-(id)init15 {
176 if (!(self = [super init]))
177 return 0;
178 return self;
179}
180
181-(id)init16 {
182 somePtr = [super init];
183 self = somePtr;
184 myivar = 0;
185 return self;
186}
187
188-(id)init17 {
189 somePtr = [super init];
190 myivar = 0; // expected-warning {{Instance variable used}}
Anna Zaks922b86e2012-02-04 02:31:53 +0000191 return 0;
Anna Zaks00790d92012-02-04 02:31:37 +0000192}
193
Anna Zaks51244c22012-04-16 21:51:09 +0000194-(id)init18 {
195 self = [super init];
196 self = _commonInit(self);
197 return self;
198}
199
200+(id)commonInitMember:(id)s {
201 return s;
202}
203
204-(id)init19 {
205 self = [super init];
206 self = [MyObj commonInitMember:self];
207 return self;
208}
209
210-(id)init19_w {
211 [super init];
212 self = [MyObj commonInitMember:self];
213 return self; // expected-warning {{Returning 'self'}}
214}
215
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000216-(void)doSomething {}
217
218@end
Argyrios Kyrtzidis3ae681e2011-01-25 23:54:44 +0000219
220@implementation MyProxyObj
221
222- (id)init { return self; }
223
224@end
Anna Zaks53a0b6c2012-03-05 18:58:25 +0000225
226
227// Test for radar://10973514 : self should not be invalidated by a method call.
228@interface Test : NSObject {
229 NSInvocation *invocation_;
230}
231@end
232@implementation Test
233-(id) initWithTarget:(id) rec selector:(SEL) cb {
234 if (self=[super init]) {
235 [invocation_ setArgument:&self atIndex:2];
236 }
237 return self;
238}
239@end
240
Anna Zaks51244c22012-04-16 21:51:09 +0000241// Test radar:11235991 - passing self to a call to super.
242@protocol MyDelegate
243@end
244@interface Object : NSObject
245- (id) initWithObject: (id)i;
246@end
247@interface Derived: Object <MyDelegate>
248- (id) initWithInt: (int)t;
249@property (nonatomic, retain, readwrite) Object *size;
250@end
251@implementation Derived
252- (id) initWithInt: (int)t {
253 if ((self = [super initWithObject:self])) {
254 _size = [[Object alloc] init];
255 }
256 return self;
257}
258@end
Anna Zaks66843482012-05-08 21:19:21 +0000259
260// Test for radar://11125870: init constructing a special instance.
261typedef signed char BOOL;
262@interface MyClass : NSObject
263@end
264@implementation MyClass
265+ (id)specialInstance {
266 return [[MyClass alloc] init];
267}
268- (id)initSpecially:(BOOL)handleSpecially {
269 if ((self = [super init])) {
270 if (handleSpecially) {
271 self = [MyClass specialInstance];
272 }
273 }
274 return self;
275}
276- (id)initSelfSelf {
277 if ((self = [super init])) {
278 self = self;
279 }
280 return self;
281}
282@end
283