summaryrefslogtreecommitdiffstats
path: root/test/Analysis/self-init.m
blob: d5151733430db1e2022d4102dabc2ebd2538553b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// RUN: %clang_cc1 -analyze -analyzer-checker=osx.cocoa.SelfInit -fobjc-default-synthesize-properties %s -verify

@class NSZone, NSCoder;
@protocol NSObject- (id)self;
@end
@protocol NSCopying  - (id)copyWithZone:(NSZone *)zone;
@end 
@protocol NSMutableCopying  - (id)mutableCopyWithZone:(NSZone *)zone;
@end 
@protocol NSCoding  - (void)encodeWithCoder:(NSCoder *)aCoder;
@end
@interface NSObject <NSObject> {}
+ (id)allocWithZone:(NSZone *)zone;
+ (id)alloc;
- (void)dealloc;
-(id)class;
-(id)init;
-(id)release;
@end
@interface NSProxy <NSObject> {}
@end

//#import "Foundation/NSObject.h"
typedef unsigned NSUInteger;
typedef long NSInteger;

@interface NSInvocation : NSObject {}
- (void)getArgument:(void *)argumentLocation atIndex:(NSInteger)idx;
- (void)setArgument:(void *)argumentLocation atIndex:(NSInteger)idx;
@end

@class NSMethodSignature, NSCoder, NSString, NSEnumerator;
@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
- (NSUInteger)length;
+ (id)stringWithUTF8String:(const char *)nullTerminatedCString;
@end extern NSString * const NSBundleDidLoadNotification;
@interface NSAssertionHandler : NSObject {}
+ (NSAssertionHandler *)currentHandler;
- (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...;
@end
extern NSString * const NSConnectionReplyMode;

@interface NSBundle : NSObject
+(id)loadNibNamed:(NSString*)s owner:(id)o;
@end

void log(void *obj);
extern void *somePtr;

@class MyObj;
extern id _commonInit(MyObj *self);

@interface MyObj : NSObject {
	id myivar;
	int myint;
}
-(id)_init;
-(id)initWithSomething:(int)x;
-(void)doSomething;
+(id)commonInitMember:(id)s;
@end

@interface MyProxyObj : NSProxy {}
-(id)init;
@end

@implementation MyObj

-(id)init {
  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);
  return [self initWithSomething:0];
}

-(id)init2 {
  self = [self initWithSomething:0];
  return self;
}

-(id)init3 {
	log([self class]);
	return [self initWithSomething:0];
}

-(id)init4 {
	self = [super init];
	if (self) {
		log(&self);
	}
	return self;
}

-(id)init4_w {
  [super init];
  if (self) {
    log(&self);
  }
  return self; // expected-warning {{Returning 'self' while it is not set to the result of '[(super or self) init...]'}}
}

- (id)initWithSomething:(int)x {    
	if ((self = [super init]))
		myint = x;
	return self;
}

-(id)_init {
	myivar = 0;
	return self;
}

-(id)init5 {
  [NSBundle loadNibNamed:@"Window" owner:self];
  return [self initWithSomething:0];
}

-(id)init6 {
  [NSBundle loadNibNamed:@"Window" owner:myivar]; // no-warning
  return [self initWithSomething:0];
}

-(id)init7 {
  if (0 != (self = [self _init]))
    myivar = 0;
  return self;
}

-(id)init8 {
    if ((self = [super init])) {
		log(&self);
		myivar = 0;
    }
    return self;
}

-(id)init9 {
  [self doSomething];
  return self; // no-warning
}

-(id)init10 {
  myivar = 0; // no-warning
  return self;
}

-(id)init11 {
  return self; // no-warning
}

-(id)init12 {
	[super init];
	return self; // expected-warning {{Returning 'self'}}
}

-(id)init13 {
	if (self == [super init]) {
	  myivar = 0; // expected-warning {{Instance variable used}}
	}
	return self; // expected-warning {{Returning 'self'}}
}

-(id)init14 {
  if (!(self = _commonInit(self)))
    return 0;
  return self;
}

-(id)init14_w {
  [super init];
  self = _commonInit(self);
  return self; // expected-warning {{Returning 'self' while it is not set to the result of '[(super or self) init...]'}}
}

-(id)init15 {
  if (!(self = [super init]))
    return 0;
  return self;
}

-(id)init16 {
  somePtr = [super init];
  self = somePtr;
  myivar = 0; 
  return self;
}

-(id)init17 {
  somePtr = [super init];
  myivar = 0; // expected-warning {{Instance variable used}}
  return 0;
}

-(id)init18 {
  self = [super init];
  self = _commonInit(self);
  return self;
}

+(id)commonInitMember:(id)s {
  return s;
}

-(id)init19 {
  self = [super init];
  self = [MyObj commonInitMember:self];
  return self;
}

-(id)init19_w {
  [super init];
  self = [MyObj commonInitMember:self];
  return self; // expected-warning {{Returning 'self'}}
}

-(void)doSomething {}

@end

@implementation MyProxyObj

- (id)init { return self; }

@end


// Test for radar://10973514 : self should not be invalidated by a method call.
@interface Test : NSObject {
    NSInvocation *invocation_;
}
@end
@implementation Test
-(id) initWithTarget:(id) rec selector:(SEL) cb {
  if (self=[super init]) {
    [invocation_ setArgument:&self atIndex:2];
  }   
  return self;
}
@end

// Test radar:11235991 - passing self to a call to super.
@protocol MyDelegate
@end
@interface Object : NSObject
- (id) initWithObject: (id)i;
@end
@interface Derived: Object <MyDelegate>
- (id) initWithInt: (int)t;
@property (nonatomic, retain, readwrite) Object *size;
@end
@implementation Derived 
- (id) initWithInt: (int)t {
   if ((self = [super initWithObject:self])) {
      _size = [[Object alloc] init];
   }
   return self;
}
@end