retain-release-path-notes.m revision 5b5402bbdadcf7d8e4aa83a803b6f33b03458c24
1// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,osx.coreFoundation.CFRetainRelease,osx.cocoa.ClassRelease -analyzer-store=basic -analyzer-output=text -verify %s
2// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,osx.coreFoundation.CFRetainRelease,osx.cocoa.ClassRelease -analyzer-store=region -analyzer-output=text -verify %s
3
4/***
5This file is for testing the path-sensitive notes for retain/release errors.
6Its goal is to have simple branch coverage of any path-based diagnostics,
7not to actually check all possible retain/release errors.
8
9This file includes notes that only appear in a ref-counted analysis. 
10GC-specific notes should go in retain-release-path-notes-gc.m.
11***/
12
13@interface NSObject
14+ (id)alloc;
15- (id)init;
16- (void)dealloc;
17
18- (Class)class;
19
20- (id)retain;
21- (void)release;
22- (void)autorelease;
23@end
24
25@interface Foo : NSObject
26- (id)methodWithValue;
27@property(retain) id propertyValue;
28@end
29
30typedef struct CFType *CFTypeRef;
31CFTypeRef CFRetain(CFTypeRef);
32void CFRelease(CFTypeRef);
33
34id NSMakeCollectable(CFTypeRef);
35CFTypeRef CFMakeCollectable(CFTypeRef);
36
37CFTypeRef CFCreateSomething();
38CFTypeRef CFGetSomething();
39
40
41void creationViaAlloc () {
42  id leaked = [[NSObject alloc] init]; // expected-warning{{leak}} expected-note{{Method returns an Objective-C object with a +1 retain count}}
43  return; // expected-note{{Object leaked: object allocated and stored into 'leaked' is not referenced later in this execution path and has a retain count of +1}}
44}
45
46void creationViaCFCreate () {
47  CFTypeRef leaked = CFCreateSomething(); // expected-warning{{leak}} expected-note{{Call to function 'CFCreateSomething' returns a Core Foundation object with a +1 retain count}}
48  return; // expected-note{{Object leaked: object allocated and stored into 'leaked' is not referenced later in this execution path and has a retain count of +1}}
49}
50
51void acquisitionViaMethod (Foo *foo) {
52  id leaked = [foo methodWithValue]; // expected-warning{{leak}} expected-note{{Method returns an Objective-C object with a +0 retain count}}
53  [leaked retain]; // expected-note{{Reference count incremented. The object now has a +1 retain count}}
54  [leaked retain]; // expected-note{{Reference count incremented. The object now has a +2 retain count}}
55  [leaked release]; // expected-note{{Reference count decremented. The object now has a +1 retain count}}
56  return; // expected-note{{Object leaked: object allocated and stored into 'leaked' is not referenced later in this execution path and has a retain count of +1}}
57}
58
59void acquisitionViaProperty (Foo *foo) {
60  id leaked = foo.propertyValue; // expected-warning{{leak}} expected-note{{Property returns an Objective-C object with a +0 retain count}}
61  [leaked retain]; // expected-note{{Reference count incremented. The object now has a +1 retain count}}
62  return; // expected-note{{Object leaked: object allocated and stored into 'leaked' is not referenced later in this execution path and has a retain count of +1}}
63}
64
65void acquisitionViaCFFunction () {
66  CFTypeRef leaked = CFGetSomething(); // expected-warning{{leak}} expected-note{{Call to function 'CFGetSomething' returns a Core Foundation object with a +0 retain count}}
67  CFRetain(leaked); // expected-note{{Reference count incremented. The object now has a +1 retain count}}
68  return; // expected-note{{Object leaked: object allocated and stored into 'leaked' is not referenced later in this execution path and has a retain count of +1}}
69}
70
71void explicitDealloc () {
72  id object = [[NSObject alloc] init]; // expected-note{{Method returns an Objective-C object with a +1 retain count}}
73  [object dealloc]; // expected-note{{Object released by directly sending the '-dealloc' message}}
74  [object class]; // expected-warning{{Reference-counted object is used after it is released}} // expected-note{{Reference-counted object is used after it is released}}
75}
76
77void implicitDealloc () {
78  id object = [[NSObject alloc] init]; // expected-note{{Method returns an Objective-C object with a +1 retain count}}
79  [object release]; // expected-note{{Object released}}
80  [object class]; // expected-warning{{Reference-counted object is used after it is released}} // expected-note{{Reference-counted object is used after it is released}}
81}
82
83void overAutorelease () {
84  id object = [[NSObject alloc] init]; // expected-note{{Method returns an Objective-C object with a +1 retain count}}
85  [object autorelease]; // expected-note{{Object sent -autorelease message}}
86  [object autorelease]; // expected-note{{Object sent -autorelease message}} 
87  return; // expected-warning{{Object sent -autorelease too many times}} expected-note{{Object over-autoreleased: object was sent -autorelease 2 times but the object has a +1 retain count}} 
88}
89
90void autoreleaseUnowned (Foo *foo) {
91  id object = foo.propertyValue; // expected-note{{Property returns an Objective-C object with a +0 retain count}}
92  [object autorelease]; // expected-note{{Object sent -autorelease message}} 
93  return; // expected-warning{{Object sent -autorelease too many times}} expected-note{{Object over-autoreleased: object was sent -autorelease but the object has a +0 retain count}}
94}
95
96void makeCollectableIgnored () {
97  CFTypeRef leaked = CFCreateSomething(); // expected-warning{{leak}} expected-note{{Call to function 'CFCreateSomething' returns a Core Foundation object with a +1 retain count}}
98  CFMakeCollectable(leaked); // expected-note{{When GC is not enabled a call to 'CFMakeCollectable' has no effect on its argument}}
99  NSMakeCollectable(leaked); // expected-note{{When GC is not enabled a call to 'NSMakeCollectable' has no effect on its argument}}
100  return; // expected-note{{Object leaked: object allocated and stored into 'leaked' is not referenced later in this execution path and has a retain count of +1}}
101}
102
103CFTypeRef CFCopyRuleViolation () {
104  CFTypeRef object = CFGetSomething(); // expected-note{{Call to function 'CFGetSomething' returns a Core Foundation object with a +0 retain counte}}
105  return object; // expected-warning{{Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected}} expected-note{{Object returned to caller with a +0 retain count}} expected-note{{Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected}}
106}
107
108CFTypeRef CFGetRuleViolation () {
109  CFTypeRef object = CFCreateSomething(); // expected-warning{{leak}} expected-note{{Call to function 'CFCreateSomething' returns a Core Foundation object with a +1 retain counte}}
110  return object; // expected-note{{Object returned to caller as an owning reference (single retain count transferred to caller)}} expected-note{{Object leaked: object allocated and stored into 'object' is return from a function whose name ('CFGetRuleViolation') does not contain 'Copy' or 'Create'.  This violates the naming convention rules given the Memory Management Guide for Core Foundation}}
111}
112
113@implementation Foo (FundamentalMemoryManagementRules)
114- (id)copyViolation {
115  id result = self.propertyValue; // expected-note{{Property returns an Objective-C object with a +0 retain count}}
116  return result; // expected-warning{{Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected}} expected-note{{Object returned to caller with a +0 retain count}} expected-note{{Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected}}
117}
118
119- (id)getViolation {
120  id result = [[Foo alloc] init]; // expected-warning{{leak}} expected-note{{Method returns an Objective-C object with a +1 retain count}}
121  return result; // expected-note{{Object returned to caller as an owning reference (single retain count transferred to caller)}} expected-note{{Object leaked: object allocated and stored into 'result' is returned from a method whose name ('getViolation') does not start with 'copy', 'mutableCopy', 'alloc' or 'new'.  This violates the naming convention rules given in the Memory Management Guide for Cocoa}}
122}
123@end
124