blob: 019fdcd0c2bfa3c4bffbaa5e08e11b8a6e1c4fe7 [file] [log] [blame]
Ted Kremenek033a07e2011-08-03 23:14:55 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=experimental.osx.cocoa.SelfInit %s -verify
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +00002
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
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;
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
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +000045@class MyObj;
46static id _commonInit(MyObj *self) {
47 return self;
48}
49
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000050@interface MyObj : NSObject {
51 id myivar;
52 int myint;
53}
54-(id)_init;
55-(id)initWithSomething:(int)x;
56-(void)doSomething;
57@end
58
Argyrios Kyrtzidiseaf969b2011-01-25 23:54:44 +000059@interface MyProxyObj : NSProxy {}
60-(id)init;
61@end
62
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000063@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 {
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000105 [NSBundle loadNibNamed:@"Window" owner:myivar]; // no-warning
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000106 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 {
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000124 [self doSomething];
125 return self; // no-warning
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000126}
127
128-(id)init10 {
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000129 myivar = 0; // no-warning
130 return self;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000131}
132
133-(id)init11 {
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000134 return self; // no-warning
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000135}
136
137-(id)init12 {
138 [super init];
139 return self; // expected-warning {{Returning 'self'}}
140}
141
142-(id)init13 {
Argyrios Kyrtzidis0e2dc3a2011-02-01 18:24:22 +0000143 if (self == [super init]) {
Argyrios Kyrtzidis4717f162011-01-26 01:26:41 +0000144 myivar = 0; // expected-warning {{Instance variable used}}
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000145 }
146 return self; // expected-warning {{Returning 'self'}}
147}
148
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000149-(id)init14 {
150 if (!(self = [super init]))
151 return 0;
152 if (!(self = _commonInit(self)))
153 return 0;
154 return self;
155}
156
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000157-(void)doSomething {}
158
159@end
Argyrios Kyrtzidiseaf969b2011-01-25 23:54:44 +0000160
161@implementation MyProxyObj
162
163- (id)init { return self; }
164
165@end