RetainCountExamples.m revision 5a90193ad825656d4a03099cd5e9c928d1782b5e
1// RUN: %clang_cc1 -analyze -analyzer-checker=core,osx.cocoa.RetainCount -analyzer-ipa=dynamic-bifurcate -verify %s
2
3typedef signed char BOOL;
4typedef struct objc_class *Class;
5typedef struct objc_object {
6    Class isa;
7} *id;
8@protocol NSObject  - (BOOL)isEqual:(id)object; @end
9@interface NSObject <NSObject> {}
10+(id)alloc;
11+(id)new;
12- (oneway void)release;
13-(id)init;
14-(id)autorelease;
15-(id)copy;
16- (Class)class;
17-(id)retain;
18@end
19
20@interface SelfStaysLive : NSObject
21- (id)init;
22@end
23
24@implementation SelfStaysLive
25- (id)init {
26  return [super init];
27}
28@end
29
30void selfStaysLive() {
31    SelfStaysLive *foo = [[SelfStaysLive alloc] init]; 
32    [foo release];
33}
34
35// Test that retain release checker warns on leaks and use-after-frees when 
36// self init is not enabled.  
37// radar://12115830
38@interface ParentOfCell : NSObject
39- (id)initWithInt: (int)inInt;
40@end
41@interface Cell : ParentOfCell{
42  int x;
43}
44- (id)initWithInt: (int)inInt;
45+ (void)testOverRelease;
46+ (void)testLeak;
47@property int x;
48@end
49@implementation Cell
50@synthesize x;
51- (id) initWithInt: (int)inInt {
52  [super initWithInt: inInt];
53  self.x = inInt; // no-warning 
54  return self; // Self Init checker would produce a warning here.
55}
56+ (void) testOverRelease {
57  Cell *sharedCell3 = [[Cell alloc] initWithInt: 3];
58  [sharedCell3 release];
59  [sharedCell3 release]; // expected-warning {{Reference-counted object is used after it is released}}
60}
61+ (void) testLeak {
62  Cell *sharedCell4 = [[Cell alloc] initWithInt: 3]; // expected-warning {{leak}}
63}
64@end
65
66