self-init.m revision f420fe35dc3a7b7b53809b615fb28379e5694c22
1// RUN: %clang_cc1 -analyze -analyzer-checker=osx.cocoa.SelfInit %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;
51static id _commonInit(MyObj *self) {
52  return self;
53}
54
55@interface MyObj : NSObject {
56	id myivar;
57	int myint;
58}
59-(id)_init;
60-(id)initWithSomething:(int)x;
61-(void)doSomething;
62@end
63
64@interface MyProxyObj : NSProxy {}
65-(id)init;
66@end
67
68@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 {
110  [NSBundle loadNibNamed:@"Window" owner:myivar]; // no-warning
111  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 {
129  [self doSomething];
130  return self; // no-warning
131}
132
133-(id)init10 {
134  myivar = 0; // no-warning
135  return self;
136}
137
138-(id)init11 {
139  return self; // no-warning
140}
141
142-(id)init12 {
143	[super init];
144	return self; // expected-warning {{Returning 'self'}}
145}
146
147-(id)init13 {
148	if (self == [super init]) {
149	  myivar = 0; // expected-warning {{Instance variable used}}
150	}
151	return self; // expected-warning {{Returning 'self'}}
152}
153
154-(id)init14 {
155  if (!(self = _commonInit(self)))
156    return 0;
157  return self;
158}
159
160-(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}}
176  return 0;
177}
178
179-(void)doSomething {}
180
181@end
182
183@implementation MyProxyObj
184
185- (id)init { return self; }
186
187@end
188
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
204