blob: d5151733430db1e2022d4102dabc2ebd2538553b [file] [log] [blame]
Anna Zaks9a70cdd2012-04-16 21:51:09 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=osx.cocoa.SelfInit -fobjc-default-synthesize-properties %s -verify
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +00002
3@class NSZone, NSCoder;
Anna Zaksf420fe32012-03-05 18:58:25 +00004@protocol NSObject- (id)self;
5@end
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +00006@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
Argyrios Kyrtzidiseaf969b2011-01-25 23:54:44 +000020@interface NSProxy <NSObject> {}
21@end
22
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000023//#import "Foundation/NSObject.h"
24typedef unsigned NSUInteger;
Anna Zaksf420fe32012-03-05 18:58:25 +000025typedef long NSInteger;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000026
Anna Zaksf420fe32012-03-05 18:58:25 +000027@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;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000033@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
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +000050@class MyObj;
Anna Zaks9a70cdd2012-04-16 21:51:09 +000051extern id _commonInit(MyObj *self);
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +000052
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000053@interface MyObj : NSObject {
54 id myivar;
55 int myint;
56}
57-(id)_init;
58-(id)initWithSomething:(int)x;
59-(void)doSomething;
Anna Zaks9a70cdd2012-04-16 21:51:09 +000060+(id)commonInitMember:(id)s;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000061@end
62
Argyrios Kyrtzidiseaf969b2011-01-25 23:54:44 +000063@interface MyProxyObj : NSProxy {}
64-(id)init;
65@end
66
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000067@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
Anna Zaks9a70cdd2012-04-16 21:51:09 +000092-(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
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000100- (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 {
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000117 [NSBundle loadNibNamed:@"Window" owner:myivar]; // no-warning
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000118 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 {
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000136 [self doSomething];
137 return self; // no-warning
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000138}
139
140-(id)init10 {
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000141 myivar = 0; // no-warning
142 return self;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000143}
144
145-(id)init11 {
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000146 return self; // no-warning
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000147}
148
149-(id)init12 {
150 [super init];
151 return self; // expected-warning {{Returning 'self'}}
152}
153
154-(id)init13 {
Argyrios Kyrtzidis0e2dc3a2011-02-01 18:24:22 +0000155 if (self == [super init]) {
Argyrios Kyrtzidis4717f162011-01-26 01:26:41 +0000156 myivar = 0; // expected-warning {{Instance variable used}}
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000157 }
158 return self; // expected-warning {{Returning 'self'}}
159}
160
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000161-(id)init14 {
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000162 if (!(self = _commonInit(self)))
163 return 0;
164 return self;
165}
166
Anna Zaks9a70cdd2012-04-16 21:51:09 +0000167-(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
Anna Zaks1efcc422012-02-04 02:31:37 +0000173-(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}}
Anna Zaks4f502fb2012-02-04 02:31:53 +0000189 return 0;
Anna Zaks1efcc422012-02-04 02:31:37 +0000190}
191
Anna Zaks9a70cdd2012-04-16 21:51:09 +0000192-(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
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000214-(void)doSomething {}
215
216@end
Argyrios Kyrtzidiseaf969b2011-01-25 23:54:44 +0000217
218@implementation MyProxyObj
219
220- (id)init { return self; }
221
222@end
Anna Zaksf420fe32012-03-05 18:58:25 +0000223
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
Anna Zaks9a70cdd2012-04-16 21:51:09 +0000239// 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