self-init.m revision c4d2c9074be6eb2091086eddd6c8f052f3b245c8
1// RUN: %clang_cc1 -analyze -analyzer-checker=cocoa.SelfInit %s -verify
2
3@class NSZone, NSCoder;
4@protocol NSObject
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 int NSInteger;
26
27@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
28@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
29- (NSUInteger)length;
30+ (id)stringWithUTF8String:(const char *)nullTerminatedCString;
31@end extern NSString * const NSBundleDidLoadNotification;
32@interface NSAssertionHandler : NSObject {}
33+ (NSAssertionHandler *)currentHandler;
34- (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...;
35@end
36extern NSString * const NSConnectionReplyMode;
37
38@interface NSBundle : NSObject
39+(id)loadNibNamed:(NSString*)s owner:(id)o;
40@end
41
42void log(void *obj);
43extern void *somePtr;
44
45@class MyObj;
46static id _commonInit(MyObj *self) {
47  return self;
48}
49
50@interface MyObj : NSObject {
51	id myivar;
52	int myint;
53}
54-(id)_init;
55-(id)initWithSomething:(int)x;
56-(void)doSomething;
57@end
58
59@interface MyProxyObj : NSProxy {}
60-(id)init;
61@end
62
63@implementation MyObj
64
65-(id)init {
66  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);
67  return [self initWithSomething:0];
68}
69
70-(id)init2 {
71  self = [self initWithSomething:0];
72  return self;
73}
74
75-(id)init3 {
76	log([self class]);
77	return [self initWithSomething:0];
78}
79
80-(id)init4 {
81	self = [super init];
82	if (self) {
83		log(&self);
84	}
85	return self;
86}
87
88- (id)initWithSomething:(int)x {    
89	if ((self = [super init]))
90		myint = x;
91	return self;
92}
93
94-(id)_init {
95	myivar = 0;
96	return self;
97}
98
99-(id)init5 {
100  [NSBundle loadNibNamed:@"Window" owner:self];
101  return [self initWithSomething:0];
102}
103
104-(id)init6 {
105  [NSBundle loadNibNamed:@"Window" owner:myivar]; // no-warning
106  return [self initWithSomething:0];
107}
108
109-(id)init7 {
110  if (0 != (self = [self _init]))
111    myivar = 0;
112  return self;
113}
114
115-(id)init8 {
116    if ((self = [super init])) {
117		log(&self);
118		myivar = 0;
119    }
120    return self;
121}
122
123-(id)init9 {
124  [self doSomething];
125  return self; // no-warning
126}
127
128-(id)init10 {
129  myivar = 0; // no-warning
130  return self;
131}
132
133-(id)init11 {
134  return self; // no-warning
135}
136
137-(id)init12 {
138	[super init];
139	return self; // expected-warning {{Returning 'self'}}
140}
141
142-(id)init13 {
143	if (self == [super init]) {
144	  myivar = 0; // expected-warning {{Instance variable used}}
145	}
146	return self; // expected-warning {{Returning 'self'}}
147}
148
149-(id)init14 {
150  if (!(self = [super init]))
151    return 0;
152  if (!(self = _commonInit(self)))
153    return 0;
154  return self;
155}
156
157-(void)doSomething {}
158
159@end
160
161@implementation MyProxyObj
162
163- (id)init { return self; }
164
165@end
166