retain-release-path-notes-gc.m revision 17a38e2636a8b1ce473fc6504c4b16cb09db29f4
1// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,osx.coreFoundation.CFRetainRelease,osx.cocoa.ClassRelease,osx.cocoa.RetainCount -analyzer-store=region -analyzer-output=text -fobjc-gc-only -verify %s
2
3/***
4This file is for testing the path-sensitive notes for retain/release errors.
5Its goal is to have simple branch coverage of any path-based diagnostics,
6not to actually check all possible retain/release errors.
7
8This file is for notes that only appear in a GC-enabled analysis. 
9Non-specific and ref-count-only notes should go in retain-release-path-notes.m.
10***/
11
12@interface NSObject
13+ (id)alloc;
14- (id)init;
15- (void)dealloc;
16
17- (Class)class;
18
19- (id)retain;
20- (void)release;
21- (void)autorelease;
22@end
23
24@interface Foo : NSObject
25- (id)methodWithValue;
26@property(retain) id propertyValue;
27@end
28
29typedef struct CFType *CFTypeRef;
30CFTypeRef CFRetain(CFTypeRef);
31void CFRelease(CFTypeRef);
32
33id NSMakeCollectable(CFTypeRef);
34CFTypeRef CFMakeCollectable(CFTypeRef);
35
36CFTypeRef CFCreateSomething();
37CFTypeRef CFGetSomething();
38
39
40void creationViaCFCreate () {
41  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}}
42  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}}
43}
44
45void makeCollectable () {
46  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}}
47  CFRetain(leaked); // expected-note{{Reference count incremented. The object now has a +2 retain count}}
48  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.}}
49  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}}
50  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.}}
51  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}}
52}
53
54void retainReleaseIgnored () {
55  id object = [[NSObject alloc] init]; // expected-note{{Method returns an Objective-C object with a +0 retain count}}
56  [object retain]; // expected-note{{In GC mode the 'retain' message has no effect}}
57  [object release]; // expected-note{{In GC mode the 'release' message has no effect}}
58  [object autorelease]; // expected-note{{In GC mode an 'autorelease' has no effect}}
59  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}}
60}
61
62@implementation Foo (FundamentalRuleUnderGC)
63- (id)getViolation {
64  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.}}
65  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}}
66}
67
68- (id)copyViolation {
69  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.}}
70  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}}
71}
72@end
73
74