Ted Kremenek | 033a07e | 2011-08-03 23:14:55 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=experimental.osx.cocoa.SelfInit %s -verify |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 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 |
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; |
| 25 | typedef 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 |
| 36 | extern NSString * const NSConnectionReplyMode; |
| 37 | |
| 38 | @interface NSBundle : NSObject |
| 39 | +(id)loadNibNamed:(NSString*)s owner:(id)o; |
| 40 | @end |
| 41 | |
| 42 | void log(void *obj); |
| 43 | extern void *somePtr; |
| 44 | |
Argyrios Kyrtzidis | 0ca1040 | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 45 | @class MyObj; |
| 46 | static id _commonInit(MyObj *self) { |
| 47 | return self; |
| 48 | } |
| 49 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 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 | |
Argyrios Kyrtzidis | eaf969b | 2011-01-25 23:54:44 +0000 | [diff] [blame] | 59 | @interface MyProxyObj : NSProxy {} |
| 60 | -(id)init; |
| 61 | @end |
| 62 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 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 { |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 105 | [NSBundle loadNibNamed:@"Window" owner:myivar]; // no-warning |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 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 { |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 124 | [self doSomething]; |
| 125 | return self; // no-warning |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | -(id)init10 { |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 129 | myivar = 0; // no-warning |
| 130 | return self; |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | -(id)init11 { |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 134 | return self; // no-warning |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | -(id)init12 { |
| 138 | [super init]; |
| 139 | return self; // expected-warning {{Returning 'self'}} |
| 140 | } |
| 141 | |
| 142 | -(id)init13 { |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 143 | if (self == [super init]) { |
Argyrios Kyrtzidis | 4717f16 | 2011-01-26 01:26:41 +0000 | [diff] [blame] | 144 | myivar = 0; // expected-warning {{Instance variable used}} |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 145 | } |
| 146 | return self; // expected-warning {{Returning 'self'}} |
| 147 | } |
| 148 | |
Argyrios Kyrtzidis | 0ca1040 | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 149 | -(id)init14 { |
| 150 | if (!(self = [super init])) |
| 151 | return 0; |
| 152 | if (!(self = _commonInit(self))) |
| 153 | return 0; |
| 154 | return self; |
| 155 | } |
| 156 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 157 | -(void)doSomething {} |
| 158 | |
| 159 | @end |
Argyrios Kyrtzidis | eaf969b | 2011-01-25 23:54:44 +0000 | [diff] [blame] | 160 | |
| 161 | @implementation MyProxyObj |
| 162 | |
| 163 | - (id)init { return self; } |
| 164 | |
| 165 | @end |