Anna Zaks | 9a70cdd | 2012-04-16 21:51:09 +0000 | [diff] [blame^] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=osx.cocoa.SelfInit -fobjc-default-synthesize-properties %s -verify |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 2 | |
| 3 | @class NSZone, NSCoder; |
Anna Zaks | f420fe3 | 2012-03-05 18:58:25 +0000 | [diff] [blame] | 4 | @protocol NSObject- (id)self; |
| 5 | @end |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 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 Kyrtzidis | eaf969b | 2011-01-25 23:54:44 +0000 | [diff] [blame] | 20 | @interface NSProxy <NSObject> {} |
| 21 | @end |
| 22 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 23 | //#import "Foundation/NSObject.h" |
| 24 | typedef unsigned NSUInteger; |
Anna Zaks | f420fe3 | 2012-03-05 18:58:25 +0000 | [diff] [blame] | 25 | typedef long NSInteger; |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 26 | |
Anna Zaks | f420fe3 | 2012-03-05 18:58:25 +0000 | [diff] [blame] | 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; |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 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 |
| 41 | extern NSString * const NSConnectionReplyMode; |
| 42 | |
| 43 | @interface NSBundle : NSObject |
| 44 | +(id)loadNibNamed:(NSString*)s owner:(id)o; |
| 45 | @end |
| 46 | |
| 47 | void log(void *obj); |
| 48 | extern void *somePtr; |
| 49 | |
Argyrios Kyrtzidis | 0ca1040 | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 50 | @class MyObj; |
Anna Zaks | 9a70cdd | 2012-04-16 21:51:09 +0000 | [diff] [blame^] | 51 | extern id _commonInit(MyObj *self); |
Argyrios Kyrtzidis | 0ca1040 | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 52 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 53 | @interface MyObj : NSObject { |
| 54 | id myivar; |
| 55 | int myint; |
| 56 | } |
| 57 | -(id)_init; |
| 58 | -(id)initWithSomething:(int)x; |
| 59 | -(void)doSomething; |
Anna Zaks | 9a70cdd | 2012-04-16 21:51:09 +0000 | [diff] [blame^] | 60 | +(id)commonInitMember:(id)s; |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 61 | @end |
| 62 | |
Argyrios Kyrtzidis | eaf969b | 2011-01-25 23:54:44 +0000 | [diff] [blame] | 63 | @interface MyProxyObj : NSProxy {} |
| 64 | -(id)init; |
| 65 | @end |
| 66 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 67 | @implementation MyObj |
| 68 | |
| 69 | -(id)init { |
| 70 | 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); |
| 71 | return [self initWithSomething:0]; |
| 72 | } |
| 73 | |
| 74 | -(id)init2 { |
| 75 | self = [self initWithSomething:0]; |
| 76 | return self; |
| 77 | } |
| 78 | |
| 79 | -(id)init3 { |
| 80 | log([self class]); |
| 81 | return [self initWithSomething:0]; |
| 82 | } |
| 83 | |
| 84 | -(id)init4 { |
| 85 | self = [super init]; |
| 86 | if (self) { |
| 87 | log(&self); |
| 88 | } |
| 89 | return self; |
| 90 | } |
| 91 | |
Anna Zaks | 9a70cdd | 2012-04-16 21:51:09 +0000 | [diff] [blame^] | 92 | -(id)init4_w { |
| 93 | [super init]; |
| 94 | if (self) { |
| 95 | log(&self); |
| 96 | } |
| 97 | return self; // expected-warning {{Returning 'self' while it is not set to the result of '[(super or self) init...]'}} |
| 98 | } |
| 99 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 100 | - (id)initWithSomething:(int)x { |
| 101 | if ((self = [super init])) |
| 102 | myint = x; |
| 103 | return self; |
| 104 | } |
| 105 | |
| 106 | -(id)_init { |
| 107 | myivar = 0; |
| 108 | return self; |
| 109 | } |
| 110 | |
| 111 | -(id)init5 { |
| 112 | [NSBundle loadNibNamed:@"Window" owner:self]; |
| 113 | return [self initWithSomething:0]; |
| 114 | } |
| 115 | |
| 116 | -(id)init6 { |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 117 | [NSBundle loadNibNamed:@"Window" owner:myivar]; // no-warning |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 118 | return [self initWithSomething:0]; |
| 119 | } |
| 120 | |
| 121 | -(id)init7 { |
| 122 | if (0 != (self = [self _init])) |
| 123 | myivar = 0; |
| 124 | return self; |
| 125 | } |
| 126 | |
| 127 | -(id)init8 { |
| 128 | if ((self = [super init])) { |
| 129 | log(&self); |
| 130 | myivar = 0; |
| 131 | } |
| 132 | return self; |
| 133 | } |
| 134 | |
| 135 | -(id)init9 { |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 136 | [self doSomething]; |
| 137 | return self; // no-warning |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | -(id)init10 { |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 141 | myivar = 0; // no-warning |
| 142 | return self; |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | -(id)init11 { |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 146 | return self; // no-warning |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | -(id)init12 { |
| 150 | [super init]; |
| 151 | return self; // expected-warning {{Returning 'self'}} |
| 152 | } |
| 153 | |
| 154 | -(id)init13 { |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 155 | if (self == [super init]) { |
Argyrios Kyrtzidis | 4717f16 | 2011-01-26 01:26:41 +0000 | [diff] [blame] | 156 | myivar = 0; // expected-warning {{Instance variable used}} |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 157 | } |
| 158 | return self; // expected-warning {{Returning 'self'}} |
| 159 | } |
| 160 | |
Argyrios Kyrtzidis | 0ca1040 | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 161 | -(id)init14 { |
Argyrios Kyrtzidis | 0ca1040 | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 162 | if (!(self = _commonInit(self))) |
| 163 | return 0; |
| 164 | return self; |
| 165 | } |
| 166 | |
Anna Zaks | 9a70cdd | 2012-04-16 21:51:09 +0000 | [diff] [blame^] | 167 | -(id)init14_w { |
| 168 | [super init]; |
| 169 | self = _commonInit(self); |
| 170 | return self; // expected-warning {{Returning 'self' while it is not set to the result of '[(super or self) init...]'}} |
| 171 | } |
| 172 | |
Anna Zaks | 1efcc42 | 2012-02-04 02:31:37 +0000 | [diff] [blame] | 173 | -(id)init15 { |
| 174 | if (!(self = [super init])) |
| 175 | return 0; |
| 176 | return self; |
| 177 | } |
| 178 | |
| 179 | -(id)init16 { |
| 180 | somePtr = [super init]; |
| 181 | self = somePtr; |
| 182 | myivar = 0; |
| 183 | return self; |
| 184 | } |
| 185 | |
| 186 | -(id)init17 { |
| 187 | somePtr = [super init]; |
| 188 | myivar = 0; // expected-warning {{Instance variable used}} |
Anna Zaks | 4f502fb | 2012-02-04 02:31:53 +0000 | [diff] [blame] | 189 | return 0; |
Anna Zaks | 1efcc42 | 2012-02-04 02:31:37 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Anna Zaks | 9a70cdd | 2012-04-16 21:51:09 +0000 | [diff] [blame^] | 192 | -(id)init18 { |
| 193 | self = [super init]; |
| 194 | self = _commonInit(self); |
| 195 | return self; |
| 196 | } |
| 197 | |
| 198 | +(id)commonInitMember:(id)s { |
| 199 | return s; |
| 200 | } |
| 201 | |
| 202 | -(id)init19 { |
| 203 | self = [super init]; |
| 204 | self = [MyObj commonInitMember:self]; |
| 205 | return self; |
| 206 | } |
| 207 | |
| 208 | -(id)init19_w { |
| 209 | [super init]; |
| 210 | self = [MyObj commonInitMember:self]; |
| 211 | return self; // expected-warning {{Returning 'self'}} |
| 212 | } |
| 213 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 214 | -(void)doSomething {} |
| 215 | |
| 216 | @end |
Argyrios Kyrtzidis | eaf969b | 2011-01-25 23:54:44 +0000 | [diff] [blame] | 217 | |
| 218 | @implementation MyProxyObj |
| 219 | |
| 220 | - (id)init { return self; } |
| 221 | |
| 222 | @end |
Anna Zaks | f420fe3 | 2012-03-05 18:58:25 +0000 | [diff] [blame] | 223 | |
| 224 | |
| 225 | // Test for radar://10973514 : self should not be invalidated by a method call. |
| 226 | @interface Test : NSObject { |
| 227 | NSInvocation *invocation_; |
| 228 | } |
| 229 | @end |
| 230 | @implementation Test |
| 231 | -(id) initWithTarget:(id) rec selector:(SEL) cb { |
| 232 | if (self=[super init]) { |
| 233 | [invocation_ setArgument:&self atIndex:2]; |
| 234 | } |
| 235 | return self; |
| 236 | } |
| 237 | @end |
| 238 | |
Anna Zaks | 9a70cdd | 2012-04-16 21:51:09 +0000 | [diff] [blame^] | 239 | // Test radar:11235991 - passing self to a call to super. |
| 240 | @protocol MyDelegate |
| 241 | @end |
| 242 | @interface Object : NSObject |
| 243 | - (id) initWithObject: (id)i; |
| 244 | @end |
| 245 | @interface Derived: Object <MyDelegate> |
| 246 | - (id) initWithInt: (int)t; |
| 247 | @property (nonatomic, retain, readwrite) Object *size; |
| 248 | @end |
| 249 | @implementation Derived |
| 250 | - (id) initWithInt: (int)t { |
| 251 | if ((self = [super initWithObject:self])) { |
| 252 | _size = [[Object alloc] init]; |
| 253 | } |
| 254 | return self; |
| 255 | } |
| 256 | @end |