Chad Rosier | 454393e | 2012-04-24 22:40:01 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=osx.cocoa.SelfInit -fobjc-default-synthesize-properties -fno-builtin %s -verify |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 2 | |
| 3 | @class NSZone, NSCoder; |
Anna Zaks | 6a2a186 | 2012-05-08 21:19:21 +0000 | [diff] [blame^] | 4 | @protocol NSObject |
| 5 | - (id)self; |
Anna Zaks | f420fe3 | 2012-03-05 18:58:25 +0000 | [diff] [blame] | 6 | @end |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 7 | @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; |
| 8 | @end |
| 9 | @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; |
| 10 | @end |
| 11 | @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; |
| 12 | @end |
| 13 | @interface NSObject <NSObject> {} |
| 14 | + (id)allocWithZone:(NSZone *)zone; |
| 15 | + (id)alloc; |
| 16 | - (void)dealloc; |
| 17 | -(id)class; |
| 18 | -(id)init; |
| 19 | -(id)release; |
| 20 | @end |
Argyrios Kyrtzidis | eaf969b | 2011-01-25 23:54:44 +0000 | [diff] [blame] | 21 | @interface NSProxy <NSObject> {} |
| 22 | @end |
| 23 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 24 | //#import "Foundation/NSObject.h" |
| 25 | typedef unsigned NSUInteger; |
Anna Zaks | f420fe3 | 2012-03-05 18:58:25 +0000 | [diff] [blame] | 26 | typedef long NSInteger; |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 27 | |
Anna Zaks | f420fe3 | 2012-03-05 18:58:25 +0000 | [diff] [blame] | 28 | @interface NSInvocation : NSObject {} |
| 29 | - (void)getArgument:(void *)argumentLocation atIndex:(NSInteger)idx; |
| 30 | - (void)setArgument:(void *)argumentLocation atIndex:(NSInteger)idx; |
| 31 | @end |
| 32 | |
| 33 | @class NSMethodSignature, NSCoder, NSString, NSEnumerator; |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 34 | @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> |
| 35 | - (NSUInteger)length; |
| 36 | + (id)stringWithUTF8String:(const char *)nullTerminatedCString; |
| 37 | @end extern NSString * const NSBundleDidLoadNotification; |
| 38 | @interface NSAssertionHandler : NSObject {} |
| 39 | + (NSAssertionHandler *)currentHandler; |
| 40 | - (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...; |
| 41 | @end |
| 42 | extern NSString * const NSConnectionReplyMode; |
| 43 | |
| 44 | @interface NSBundle : NSObject |
| 45 | +(id)loadNibNamed:(NSString*)s owner:(id)o; |
| 46 | @end |
| 47 | |
| 48 | void log(void *obj); |
| 49 | extern void *somePtr; |
| 50 | |
Argyrios Kyrtzidis | 0ca1040 | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 51 | @class MyObj; |
Anna Zaks | 9a70cdd | 2012-04-16 21:51:09 +0000 | [diff] [blame] | 52 | extern id _commonInit(MyObj *self); |
Argyrios Kyrtzidis | 0ca1040 | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 53 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 54 | @interface MyObj : NSObject { |
| 55 | id myivar; |
| 56 | int myint; |
| 57 | } |
| 58 | -(id)_init; |
| 59 | -(id)initWithSomething:(int)x; |
| 60 | -(void)doSomething; |
Anna Zaks | 9a70cdd | 2012-04-16 21:51:09 +0000 | [diff] [blame] | 61 | +(id)commonInitMember:(id)s; |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 62 | @end |
| 63 | |
Argyrios Kyrtzidis | eaf969b | 2011-01-25 23:54:44 +0000 | [diff] [blame] | 64 | @interface MyProxyObj : NSProxy {} |
| 65 | -(id)init; |
| 66 | @end |
| 67 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 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 | |
Anna Zaks | 9a70cdd | 2012-04-16 21:51:09 +0000 | [diff] [blame] | 93 | -(id)init4_w { |
| 94 | [super init]; |
| 95 | if (self) { |
| 96 | log(&self); |
| 97 | } |
| 98 | return self; // expected-warning {{Returning 'self' while it is not set to the result of '[(super or self) init...]'}} |
| 99 | } |
| 100 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 101 | - (id)initWithSomething:(int)x { |
| 102 | if ((self = [super init])) |
| 103 | myint = x; |
| 104 | return self; |
| 105 | } |
| 106 | |
| 107 | -(id)_init { |
| 108 | myivar = 0; |
| 109 | return self; |
| 110 | } |
| 111 | |
| 112 | -(id)init5 { |
| 113 | [NSBundle loadNibNamed:@"Window" owner:self]; |
| 114 | return [self initWithSomething:0]; |
| 115 | } |
| 116 | |
| 117 | -(id)init6 { |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 118 | [NSBundle loadNibNamed:@"Window" owner:myivar]; // no-warning |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 119 | return [self initWithSomething:0]; |
| 120 | } |
| 121 | |
| 122 | -(id)init7 { |
| 123 | if (0 != (self = [self _init])) |
| 124 | myivar = 0; |
| 125 | return self; |
| 126 | } |
| 127 | |
| 128 | -(id)init8 { |
| 129 | if ((self = [super init])) { |
| 130 | log(&self); |
| 131 | myivar = 0; |
| 132 | } |
| 133 | return self; |
| 134 | } |
| 135 | |
| 136 | -(id)init9 { |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 137 | [self doSomething]; |
| 138 | return self; // no-warning |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | -(id)init10 { |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 142 | myivar = 0; // no-warning |
| 143 | return self; |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | -(id)init11 { |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 147 | return self; // no-warning |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | -(id)init12 { |
| 151 | [super init]; |
| 152 | return self; // expected-warning {{Returning 'self'}} |
| 153 | } |
| 154 | |
| 155 | -(id)init13 { |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 156 | if (self == [super init]) { |
Argyrios Kyrtzidis | 4717f16 | 2011-01-26 01:26:41 +0000 | [diff] [blame] | 157 | myivar = 0; // expected-warning {{Instance variable used}} |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 158 | } |
| 159 | return self; // expected-warning {{Returning 'self'}} |
| 160 | } |
| 161 | |
Argyrios Kyrtzidis | 0ca1040 | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 162 | -(id)init14 { |
Argyrios Kyrtzidis | 0ca1040 | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 163 | if (!(self = _commonInit(self))) |
| 164 | return 0; |
| 165 | return self; |
| 166 | } |
| 167 | |
Anna Zaks | 9a70cdd | 2012-04-16 21:51:09 +0000 | [diff] [blame] | 168 | -(id)init14_w { |
| 169 | [super init]; |
| 170 | self = _commonInit(self); |
| 171 | return self; // expected-warning {{Returning 'self' while it is not set to the result of '[(super or self) init...]'}} |
| 172 | } |
| 173 | |
Anna Zaks | 1efcc42 | 2012-02-04 02:31:37 +0000 | [diff] [blame] | 174 | -(id)init15 { |
| 175 | if (!(self = [super init])) |
| 176 | return 0; |
| 177 | return self; |
| 178 | } |
| 179 | |
| 180 | -(id)init16 { |
| 181 | somePtr = [super init]; |
| 182 | self = somePtr; |
| 183 | myivar = 0; |
| 184 | return self; |
| 185 | } |
| 186 | |
| 187 | -(id)init17 { |
| 188 | somePtr = [super init]; |
| 189 | myivar = 0; // expected-warning {{Instance variable used}} |
Anna Zaks | 4f502fb | 2012-02-04 02:31:53 +0000 | [diff] [blame] | 190 | return 0; |
Anna Zaks | 1efcc42 | 2012-02-04 02:31:37 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Anna Zaks | 9a70cdd | 2012-04-16 21:51:09 +0000 | [diff] [blame] | 193 | -(id)init18 { |
| 194 | self = [super init]; |
| 195 | self = _commonInit(self); |
| 196 | return self; |
| 197 | } |
| 198 | |
| 199 | +(id)commonInitMember:(id)s { |
| 200 | return s; |
| 201 | } |
| 202 | |
| 203 | -(id)init19 { |
| 204 | self = [super init]; |
| 205 | self = [MyObj commonInitMember:self]; |
| 206 | return self; |
| 207 | } |
| 208 | |
| 209 | -(id)init19_w { |
| 210 | [super init]; |
| 211 | self = [MyObj commonInitMember:self]; |
| 212 | return self; // expected-warning {{Returning 'self'}} |
| 213 | } |
| 214 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 215 | -(void)doSomething {} |
| 216 | |
| 217 | @end |
Argyrios Kyrtzidis | eaf969b | 2011-01-25 23:54:44 +0000 | [diff] [blame] | 218 | |
| 219 | @implementation MyProxyObj |
| 220 | |
| 221 | - (id)init { return self; } |
| 222 | |
| 223 | @end |
Anna Zaks | f420fe3 | 2012-03-05 18:58:25 +0000 | [diff] [blame] | 224 | |
| 225 | |
| 226 | // Test for radar://10973514 : self should not be invalidated by a method call. |
| 227 | @interface Test : NSObject { |
| 228 | NSInvocation *invocation_; |
| 229 | } |
| 230 | @end |
| 231 | @implementation Test |
| 232 | -(id) initWithTarget:(id) rec selector:(SEL) cb { |
| 233 | if (self=[super init]) { |
| 234 | [invocation_ setArgument:&self atIndex:2]; |
| 235 | } |
| 236 | return self; |
| 237 | } |
| 238 | @end |
| 239 | |
Anna Zaks | 9a70cdd | 2012-04-16 21:51:09 +0000 | [diff] [blame] | 240 | // Test radar:11235991 - passing self to a call to super. |
| 241 | @protocol MyDelegate |
| 242 | @end |
| 243 | @interface Object : NSObject |
| 244 | - (id) initWithObject: (id)i; |
| 245 | @end |
| 246 | @interface Derived: Object <MyDelegate> |
| 247 | - (id) initWithInt: (int)t; |
| 248 | @property (nonatomic, retain, readwrite) Object *size; |
| 249 | @end |
| 250 | @implementation Derived |
| 251 | - (id) initWithInt: (int)t { |
| 252 | if ((self = [super initWithObject:self])) { |
| 253 | _size = [[Object alloc] init]; |
| 254 | } |
| 255 | return self; |
| 256 | } |
| 257 | @end |
Anna Zaks | 6a2a186 | 2012-05-08 21:19:21 +0000 | [diff] [blame^] | 258 | |
| 259 | // Test for radar://11125870: init constructing a special instance. |
| 260 | typedef signed char BOOL; |
| 261 | @interface MyClass : NSObject |
| 262 | @end |
| 263 | @implementation MyClass |
| 264 | + (id)specialInstance { |
| 265 | return [[MyClass alloc] init]; |
| 266 | } |
| 267 | - (id)initSpecially:(BOOL)handleSpecially { |
| 268 | if ((self = [super init])) { |
| 269 | if (handleSpecially) { |
| 270 | self = [MyClass specialInstance]; |
| 271 | } |
| 272 | } |
| 273 | return self; |
| 274 | } |
| 275 | - (id)initSelfSelf { |
| 276 | if ((self = [super init])) { |
| 277 | self = self; |
| 278 | } |
| 279 | return self; |
| 280 | } |
| 281 | @end |
| 282 | |