blob: 3db42e9cf5acf298dc052b9750aae3a850801813 [file] [log] [blame]
Anna Zaks52a38882012-02-04 02:31:57 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=osx.cocoa.SelfInit %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;
51static id _commonInit(MyObj *self) {
52 return self;
53}
54
Argyrios Kyrtzidisd7a31ba2011-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;
62@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
93- (id)initWithSomething:(int)x {
94 if ((self = [super init]))
95 myint = x;
96 return self;
97}
98
99-(id)_init {
100 myivar = 0;
101 return self;
102}
103
104-(id)init5 {
105 [NSBundle loadNibNamed:@"Window" owner:self];
106 return [self initWithSomething:0];
107}
108
109-(id)init6 {
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000110 [NSBundle loadNibNamed:@"Window" owner:myivar]; // no-warning
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000111 return [self initWithSomething:0];
112}
113
114-(id)init7 {
115 if (0 != (self = [self _init]))
116 myivar = 0;
117 return self;
118}
119
120-(id)init8 {
121 if ((self = [super init])) {
122 log(&self);
123 myivar = 0;
124 }
125 return self;
126}
127
128-(id)init9 {
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000129 [self doSomething];
130 return self; // no-warning
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000131}
132
133-(id)init10 {
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000134 myivar = 0; // no-warning
135 return self;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000136}
137
138-(id)init11 {
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000139 return self; // no-warning
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000140}
141
142-(id)init12 {
143 [super init];
144 return self; // expected-warning {{Returning 'self'}}
145}
146
147-(id)init13 {
Argyrios Kyrtzidis0e2dc3a2011-02-01 18:24:22 +0000148 if (self == [super init]) {
Argyrios Kyrtzidis4717f162011-01-26 01:26:41 +0000149 myivar = 0; // expected-warning {{Instance variable used}}
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000150 }
151 return self; // expected-warning {{Returning 'self'}}
152}
153
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000154-(id)init14 {
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000155 if (!(self = _commonInit(self)))
156 return 0;
157 return self;
158}
159
Anna Zaks1efcc422012-02-04 02:31:37 +0000160-(id)init15 {
161 if (!(self = [super init]))
162 return 0;
163 return self;
164}
165
166-(id)init16 {
167 somePtr = [super init];
168 self = somePtr;
169 myivar = 0;
170 return self;
171}
172
173-(id)init17 {
174 somePtr = [super init];
175 myivar = 0; // expected-warning {{Instance variable used}}
Anna Zaks4f502fb2012-02-04 02:31:53 +0000176 return 0;
Anna Zaks1efcc422012-02-04 02:31:37 +0000177}
178
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000179-(void)doSomething {}
180
181@end
Argyrios Kyrtzidiseaf969b2011-01-25 23:54:44 +0000182
183@implementation MyProxyObj
184
185- (id)init { return self; }
186
187@end
Anna Zaksf420fe32012-03-05 18:58:25 +0000188
189
190// Test for radar://10973514 : self should not be invalidated by a method call.
191@interface Test : NSObject {
192 NSInvocation *invocation_;
193}
194@end
195@implementation Test
196-(id) initWithTarget:(id) rec selector:(SEL) cb {
197 if (self=[super init]) {
198 [invocation_ setArgument:&self atIndex:2];
199 }
200 return self;
201}
202@end
203