self-init.m revision eaf969bf4b657f0c4577f38a39f8c4ef1d9272fc
1// RUN: %clang_cc1 -analyze -analyzer-check-objc-mem %s -verify 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 20@interface NSProxy <NSObject> {} 21@end 22 23//#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 45@interface MyObj : NSObject { 46 id myivar; 47 int myint; 48} 49-(id)_init; 50-(id)initWithSomething:(int)x; 51-(void)doSomething; 52@end 53 54@interface MyProxyObj : NSProxy {} 55-(id)init; 56@end 57 58@implementation MyObj 59 60-(id)init { 61 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); 62 return [self initWithSomething:0]; 63} 64 65-(id)init2 { 66 self = [self initWithSomething:0]; 67 return self; 68} 69 70-(id)init3 { 71 log([self class]); 72 return [self initWithSomething:0]; 73} 74 75-(id)init4 { 76 self = [super init]; 77 if (self) { 78 log(&self); 79 } 80 return self; 81} 82 83- (id)initWithSomething:(int)x { 84 if ((self = [super init])) 85 myint = x; 86 return self; 87} 88 89-(id)_init { 90 myivar = 0; 91 return self; 92} 93 94-(id)init5 { 95 [NSBundle loadNibNamed:@"Window" owner:self]; 96 return [self initWithSomething:0]; 97} 98 99-(id)init6 { 100 [NSBundle loadNibNamed:@"Window" owner:myivar]; // expected-warning {{Using an ivar}} 101 return [self initWithSomething:0]; 102} 103 104-(id)init7 { 105 if (0 != (self = [self _init])) 106 myivar = 0; 107 return self; 108} 109 110-(id)init8 { 111 if ((self = [super init])) { 112 log(&self); 113 myivar = 0; 114 } 115 return self; 116} 117 118-(id)init9 { 119 [self doSomething]; 120 return self; // expected-warning {{Returning 'self'}} 121} 122 123-(id)init10 { 124 myivar = 0; // expected-warning {{Using an ivar}} 125 return self; 126} 127 128-(id)init11 { 129 return self; // expected-warning {{Returning 'self'}} 130} 131 132-(id)init12 { 133 [super init]; 134 return self; // expected-warning {{Returning 'self'}} 135} 136 137-(id)init13 { 138 if ((self == [super init])) { 139 myivar = 0; // expected-warning {{Using an ivar}} 140 } 141 return self; // expected-warning {{Returning 'self'}} 142} 143 144-(void)doSomething {} 145 146@end 147 148@implementation MyProxyObj 149 150- (id)init { return self; } 151 152@end 153