retain-release-path-notes-gc.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 -fobjc-gc-only -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 -fobjc-gc-only -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 is for notes that only appear in a GC-enabled analysis. 
10Non-specific and ref-count-only notes should go in retain-release-path-notes.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 creationViaCFCreate () {
42  CFTypeRef leaked = CFCreateSomething(); // expected-warning{{leak}} expected-note{{Call to function 'CFCreateSomething' returns a Core Foundation object with a +1 retain count.  Core Foundation objects are not automatically garbage collected}}
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 makeCollectable () {
47  CFTypeRef leaked = CFCreateSomething(); // expected-warning{{leak}} expected-note{{Call to function 'CFCreateSomething' returns a Core Foundation object with a +1 retain count.  Core Foundation objects are not automatically garbage collected}}
48  CFRetain(leaked); // expected-note{{Reference count incremented. The object now has a +2 retain count}}
49  CFMakeCollectable(leaked); // expected-note{{In GC mode a call to 'CFMakeCollectable' decrements an object's retain count and registers the object with the garbage collector. An object must have a 0 retain count to be garbage collected. After this call its retain count is +1.}}
50  NSMakeCollectable(leaked); // expected-note{{In GC mode a call to 'NSMakeCollectable' decrements an object's retain count and registers the object with the garbage collector. Since it now has a 0 retain count the object can be automatically collected by the garbage collector}}
51  CFRetain(leaked); // expected-note{{Reference count incremented. The object now has a +1 retain count. The object is not eligible for garbage collection until the retain count reaches 0 again.}}
52  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}}
53}
54
55void retainReleaseIgnored () {
56  id object = [[NSObject alloc] init]; // expected-note{{Method returns an Objective-C object with a +0 retain count}}
57  [object retain]; // expected-note{{In GC mode the 'retain' message has no effect}}
58  [object release]; // expected-note{{In GC mode the 'release' message has no effect}}
59  [object autorelease]; // expected-note{{In GC mode an 'autorelease' has no effect}}
60  CFRelease((CFTypeRef)object); // expected-warning{{Incorrect decrement of the reference count of an object that is not owned at this point by the caller}} expected-note{{Incorrect decrement of the reference count of an object that is not owned at this point by the caller}}
61}
62
63@implementation Foo (FundamentalRuleUnderGC)
64- (id)getViolation {
65  id object = (id) CFCreateSomething(); // expected-warning{{leak}} expected-note{{Call to function 'CFCreateSomething' returns a Core Foundation object with a +1 retain count.  Core Foundation objects are not automatically garbage collected.}}
66  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' and returned from method 'getViolation' is potentially leaked when using garbage collection.  Callers of this method do not expect a returned object with a +1 retain count since they expect the object to be managed by the garbage collector}}
67}
68
69- (id)copyViolation {
70  id object = (id) CFCreateSomething(); // expected-warning{{leak}} expected-note{{Call to function 'CFCreateSomething' returns a Core Foundation object with a +1 retain count.  Core Foundation objects are not automatically garbage collected.}}
71  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' and returned from method 'copyViolation' is potentially leaked when using garbage collection.  Callers of this method do not expect a returned object with a +1 retain count since they expect the object to be managed by the garbage collector}}
72}
73@end
74
75