Anna Zaks | 52a3888 | 2012-02-04 02:31:57 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=osx.cocoa.SelfInit %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; |
| 51 | static id _commonInit(MyObj *self) { |
| 52 | return self; |
| 53 | } |
| 54 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 55 | @interface MyObj : NSObject { |
| 56 | id myivar; |
| 57 | int myint; |
| 58 | } |
| 59 | -(id)_init; |
| 60 | -(id)initWithSomething:(int)x; |
| 61 | -(void)doSomething; |
| 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 | |
| 93 | - (id)initWithSomething:(int)x { |
| 94 | if ((self = [super init])) |
| 95 | myint = x; |
| 96 | return self; |
| 97 | } |
| 98 | |
| 99 | -(id)_init { |
| 100 | myivar = 0; |
| 101 | return self; |
| 102 | } |
| 103 | |
| 104 | -(id)init5 { |
| 105 | [NSBundle loadNibNamed:@"Window" owner:self]; |
| 106 | return [self initWithSomething:0]; |
| 107 | } |
| 108 | |
| 109 | -(id)init6 { |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 110 | [NSBundle loadNibNamed:@"Window" owner:myivar]; // no-warning |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 111 | return [self initWithSomething:0]; |
| 112 | } |
| 113 | |
| 114 | -(id)init7 { |
| 115 | if (0 != (self = [self _init])) |
| 116 | myivar = 0; |
| 117 | return self; |
| 118 | } |
| 119 | |
| 120 | -(id)init8 { |
| 121 | if ((self = [super init])) { |
| 122 | log(&self); |
| 123 | myivar = 0; |
| 124 | } |
| 125 | return self; |
| 126 | } |
| 127 | |
| 128 | -(id)init9 { |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 129 | [self doSomething]; |
| 130 | return self; // no-warning |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | -(id)init10 { |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 134 | myivar = 0; // no-warning |
| 135 | return self; |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | -(id)init11 { |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 139 | return self; // no-warning |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | -(id)init12 { |
| 143 | [super init]; |
| 144 | return self; // expected-warning {{Returning 'self'}} |
| 145 | } |
| 146 | |
| 147 | -(id)init13 { |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 148 | if (self == [super init]) { |
Argyrios Kyrtzidis | 4717f16 | 2011-01-26 01:26:41 +0000 | [diff] [blame] | 149 | myivar = 0; // expected-warning {{Instance variable used}} |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 150 | } |
| 151 | return self; // expected-warning {{Returning 'self'}} |
| 152 | } |
| 153 | |
Argyrios Kyrtzidis | 0ca1040 | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 154 | -(id)init14 { |
Argyrios Kyrtzidis | 0ca1040 | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 155 | if (!(self = _commonInit(self))) |
| 156 | return 0; |
| 157 | return self; |
| 158 | } |
| 159 | |
Anna Zaks | 1efcc42 | 2012-02-04 02:31:37 +0000 | [diff] [blame] | 160 | -(id)init15 { |
| 161 | if (!(self = [super init])) |
| 162 | return 0; |
| 163 | return self; |
| 164 | } |
| 165 | |
| 166 | -(id)init16 { |
| 167 | somePtr = [super init]; |
| 168 | self = somePtr; |
| 169 | myivar = 0; |
| 170 | return self; |
| 171 | } |
| 172 | |
| 173 | -(id)init17 { |
| 174 | somePtr = [super init]; |
| 175 | myivar = 0; // expected-warning {{Instance variable used}} |
Anna Zaks | 4f502fb | 2012-02-04 02:31:53 +0000 | [diff] [blame] | 176 | return 0; |
Anna Zaks | 1efcc42 | 2012-02-04 02:31:37 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 179 | -(void)doSomething {} |
| 180 | |
| 181 | @end |
Argyrios Kyrtzidis | eaf969b | 2011-01-25 23:54:44 +0000 | [diff] [blame] | 182 | |
| 183 | @implementation MyProxyObj |
| 184 | |
| 185 | - (id)init { return self; } |
| 186 | |
| 187 | @end |
Anna Zaks | f420fe3 | 2012-03-05 18:58:25 +0000 | [diff] [blame] | 188 | |
| 189 | |
| 190 | // Test for radar://10973514 : self should not be invalidated by a method call. |
| 191 | @interface Test : NSObject { |
| 192 | NSInvocation *invocation_; |
| 193 | } |
| 194 | @end |
| 195 | @implementation Test |
| 196 | -(id) initWithTarget:(id) rec selector:(SEL) cb { |
| 197 | if (self=[super init]) { |
| 198 | [invocation_ setArgument:&self atIndex:2]; |
| 199 | } |
| 200 | return self; |
| 201 | } |
| 202 | @end |
| 203 | |