1 // RUN: %clang_analyze_cc1 -analyzer-checker=osx.cocoa.SelfInit -analyzer-config ipa=dynamic -fno-builtin %s -verify
2 // RUN: %clang_analyze_cc1 -analyzer-checker=osx.cocoa.SelfInit -fno-builtin %s -verify
4 @class NSZone, NSCoder;
8 @protocol NSCopying - (id)copyWithZone:(NSZone *)zone;
10 @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone;
12 @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder;
14 @interface NSObject <NSObject> {}
15 + (id)allocWithZone:(NSZone *)zone;
22 @interface NSProxy <NSObject> {}
25 //#import "Foundation/NSObject.h"
26 typedef unsigned NSUInteger;
27 typedef long NSInteger;
29 @interface NSInvocation : NSObject {}
30 - (void)getArgument:(void *)argumentLocation atIndex:(NSInteger)idx;
31 - (void)setArgument:(void *)argumentLocation atIndex:(NSInteger)idx;
34 @class NSMethodSignature, NSCoder, NSString, NSEnumerator;
35 @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
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,...;
43 extern NSString * const NSConnectionReplyMode;
45 @interface NSBundle : NSObject
46 +(id)loadNibNamed:(NSString*)s owner:(id)o;
53 extern id _commonInit(MyObj *self);
55 @interface MyObj : NSObject {
60 -(id)initWithSomething:(int)x;
62 +(id)commonInitMember:(id)s;
65 @interface MyProxyObj : NSProxy {}
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];
77 self = [self initWithSomething:0];
83 return [self initWithSomething:0];
99 return self; // expected-warning {{Returning 'self' while it is not set to the result of '[(super or self) init...]'}}
102 - (id)initWithSomething:(int)x {
103 if ((self = [super init]))
114 [NSBundle loadNibNamed:@"Window" owner:self];
115 return [self initWithSomething:0];
119 [NSBundle loadNibNamed:@"Window" owner:myivar]; // no-warning
120 return [self initWithSomething:0];
124 if (0 != (self = [self _init]))
130 if ((self = [super init])) {
139 return self; // no-warning
143 myivar = 0; // no-warning
148 return self; // no-warning
153 return self; // expected-warning {{Returning 'self'}}
157 if (self == [super init]) {
158 myivar = 0; // expected-warning {{Instance variable used}}
160 return self; // expected-warning {{Returning 'self'}}
164 if (!(self = _commonInit(self)))
171 self = _commonInit(self);
172 return self; // expected-warning {{Returning 'self' while it is not set to the result of '[(super or self) init...]'}}
176 if (!(self = [super init]))
182 somePtr = [super init];
189 somePtr = [super init];
190 myivar = 0; // expected-warning {{Instance variable used}}
196 self = _commonInit(self);
200 +(id)commonInitMember:(id)s {
206 self = [MyObj commonInitMember:self];
212 self = [MyObj commonInitMember:self];
213 return self; // expected-warning {{Returning 'self'}}
216 -(void)doSomething {}
220 @implementation MyProxyObj
222 - (id)init { return self; }
227 // Test for radar://10973514 : self should not be invalidated by a method call.
228 @interface Test : NSObject {
229 NSInvocation *invocation_;
233 -(id) initWithTarget:(id) rec selector:(SEL) cb {
234 if (self=[super init]) {
235 [invocation_ setArgument:&self atIndex:2];
241 // Test radar:11235991 - passing self to a call to super.
244 @interface Object : NSObject
245 - (id) initWithObject: (id)i;
247 @interface Derived: Object <MyDelegate>
248 - (id) initWithInt: (int)t;
249 @property (nonatomic, retain, readwrite) Object *size;
251 @implementation Derived
252 - (id) initWithInt: (int)t {
253 if ((self = [super initWithObject:self])) {
254 _size = [[Object alloc] init];
260 // Test for radar://11125870: init constructing a special instance.
261 typedef signed char BOOL;
262 @interface MyClass : NSObject
264 @implementation MyClass
265 + (id)specialInstance {
266 return [[MyClass alloc] init];
268 - (id)initSpecially:(BOOL)handleSpecially {
269 if ((self = [super init])) {
270 if (handleSpecially) {
271 self = [MyClass specialInstance];
277 if ((self = [super init])) {
284 // Test for radar://12838705.
285 @interface ABCClass : NSObject
286 @property (nonatomic, strong) NSString *foo;
287 @property (nonatomic, strong) NSString *bar;
288 @property (nonatomic, strong) NSString *baz;
291 @implementation ABCClass
292 @synthesize foo = foo_;
293 @synthesize bar = bar_;
294 @synthesize baz = baz_;
296 - (id)initWithABC:(ABCClass *)abc {
302 - (ABCClass *)abcWithFoo:(NSString *)foo {
303 ABCClass *copy = [[ABCClass alloc] initWithABC:self];