[analyzer] Overhauling of the checker registration mechanism.
[clang.git] / test / Analysis / self-init.m
blobb8c2c3e8d9848ac8663d72671ce09226253354a3
1 // RUN: %clang_cc1 -analyze -analyzer-check-objc-mem -analyzer-checker=cocoa.SelfInit %s -verify
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
23 //#import "Foundation/NSObject.h"
24 typedef unsigned NSUInteger;
25 typedef int NSInteger;
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;
38 @interface NSBundle : NSObject
39 +(id)loadNibNamed:(NSString*)s owner:(id)o;
40 @end
42 void log(void *obj);
43 extern void *somePtr;
45 @class MyObj;
46 static id _commonInit(MyObj *self) {
47   return self;
50 @interface MyObj : NSObject {
51         id myivar;
52         int myint;
54 -(id)_init;
55 -(id)initWithSomething:(int)x;
56 -(void)doSomething;
57 @end
59 @interface MyProxyObj : NSProxy {}
60 -(id)init;
61 @end
63 @implementation MyObj
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];
70 -(id)init2 {
71   self = [self initWithSomething:0];
72   return self;
75 -(id)init3 {
76         log([self class]);
77         return [self initWithSomething:0];
80 -(id)init4 {
81         self = [super init];
82         if (self) {
83                 log(&self);
84         }
85         return self;
88 - (id)initWithSomething:(int)x {    
89         if ((self = [super init]))
90                 myint = x;
91         return self;
94 -(id)_init {
95         myivar = 0;
96         return self;
99 -(id)init5 {
100   [NSBundle loadNibNamed:@"Window" owner:self];
101   return [self initWithSomething:0];
104 -(id)init6 {
105   [NSBundle loadNibNamed:@"Window" owner:myivar]; // no-warning
106   return [self initWithSomething:0];
109 -(id)init7 {
110   if (0 != (self = [self _init]))
111     myivar = 0;
112   return self;
115 -(id)init8 {
116     if ((self = [super init])) {
117                 log(&self);
118                 myivar = 0;
119     }
120     return self;
123 -(id)init9 {
124   [self doSomething];
125   return self; // no-warning
128 -(id)init10 {
129   myivar = 0; // no-warning
130   return self;
133 -(id)init11 {
134   return self; // no-warning
137 -(id)init12 {
138         [super init];
139         return self; // expected-warning {{Returning 'self'}}
142 -(id)init13 {
143         if (self == [super init]) {
144           myivar = 0; // expected-warning {{Instance variable used}}
145         }
146         return self; // expected-warning {{Returning 'self'}}
149 -(id)init14 {
150   if (!(self = [super init]))
151     return 0;
152   if (!(self = _commonInit(self)))
153     return 0;
154   return self;
157 -(void)doSomething {}
159 @end
161 @implementation MyProxyObj
163 - (id)init { return self; }
165 @end