retain-release-path-notes.m revision 4b9c2d235fb9449e249d74f48ecfec601650de93
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 -verify %s 2 3// This actually still works after the pseudo-object refactor, it just 4// uses messages that say 'method' instead of 'property'. Ted wanted 5// this xfailed and filed as a bug. rdar://problem/10402993 6// XFAIL: * 7 8/*** 9This file is for testing the path-sensitive notes for retain/release errors. 10Its goal is to have simple branch coverage of any path-based diagnostics, 11not to actually check all possible retain/release errors. 12 13This file includes notes that only appear in a ref-counted analysis. 14GC-specific notes should go in retain-release-path-notes-gc.m. 15***/ 16 17@interface NSObject 18+ (id)alloc; 19- (id)init; 20- (void)dealloc; 21 22- (Class)class; 23 24- (id)retain; 25- (void)release; 26- (void)autorelease; 27@end 28 29@interface Foo : NSObject 30- (id)methodWithValue; 31@property(retain) id propertyValue; 32@end 33 34typedef struct CFType *CFTypeRef; 35CFTypeRef CFRetain(CFTypeRef); 36void CFRelease(CFTypeRef); 37 38id NSMakeCollectable(CFTypeRef); 39CFTypeRef CFMakeCollectable(CFTypeRef); 40 41CFTypeRef CFCreateSomething(); 42CFTypeRef CFGetSomething(); 43 44 45void creationViaAlloc () { 46 id leaked = [[NSObject alloc] init]; // expected-warning{{leak}} expected-note{{Method returns an Objective-C object with a +1 retain count}} 47 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}} 48} 49 50void creationViaCFCreate () { 51 CFTypeRef leaked = CFCreateSomething(); // expected-warning{{leak}} expected-note{{Call to function 'CFCreateSomething' returns a Core Foundation object with a +1 retain count}} 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 acquisitionViaMethod (Foo *foo) { 56 id leaked = [foo methodWithValue]; // expected-warning{{leak}} expected-note{{Method returns an Objective-C object with a +0 retain count}} 57 [leaked retain]; // expected-note{{Reference count incremented. The object now has a +1 retain count}} 58 [leaked retain]; // expected-note{{Reference count incremented. The object now has a +2 retain count}} 59 [leaked release]; // expected-note{{Reference count decremented. The object now has a +1 retain count}} 60 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}} 61} 62 63void acquisitionViaProperty (Foo *foo) { 64 id leaked = foo.propertyValue; // expected-warning{{leak}} expected-note{{Property returns an Objective-C object with a +0 retain count}} 65 [leaked retain]; // expected-note{{Reference count incremented. The object now has a +1 retain count}} 66 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}} 67} 68 69void acquisitionViaCFFunction () { 70 CFTypeRef leaked = CFGetSomething(); // expected-warning{{leak}} expected-note{{Call to function 'CFGetSomething' returns a Core Foundation object with a +0 retain count}} 71 CFRetain(leaked); // expected-note{{Reference count incremented. The object now has a +1 retain count}} 72 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}} 73} 74 75void explicitDealloc () { 76 id object = [[NSObject alloc] init]; // expected-note{{Method returns an Objective-C object with a +1 retain count}} 77 [object dealloc]; // expected-note{{Object released by directly sending the '-dealloc' message}} 78 [object class]; // expected-warning{{Reference-counted object is used after it is released}} // expected-note{{Reference-counted object is used after it is released}} 79} 80 81void implicitDealloc () { 82 id object = [[NSObject alloc] init]; // expected-note{{Method returns an Objective-C object with a +1 retain count}} 83 [object release]; // expected-note{{Object released}} 84 [object class]; // expected-warning{{Reference-counted object is used after it is released}} // expected-note{{Reference-counted object is used after it is released}} 85} 86 87void overAutorelease () { 88 id object = [[NSObject alloc] init]; // expected-note{{Method returns an Objective-C object with a +1 retain count}} 89 [object autorelease]; // expected-note{{Object sent -autorelease message}} 90 [object autorelease]; // expected-note{{Object sent -autorelease message}} 91 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}} 92} 93 94void autoreleaseUnowned (Foo *foo) { 95 id object = foo.propertyValue; // expected-note{{Property returns an Objective-C object with a +0 retain count}} 96 [object autorelease]; // expected-note{{Object sent -autorelease message}} 97 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}} 98} 99 100void makeCollectableIgnored () { 101 CFTypeRef leaked = CFCreateSomething(); // expected-warning{{leak}} expected-note{{Call to function 'CFCreateSomething' returns a Core Foundation object with a +1 retain count}} 102 CFMakeCollectable(leaked); // expected-note{{When GC is not enabled a call to 'CFMakeCollectable' has no effect on its argument}} 103 NSMakeCollectable(leaked); // expected-note{{When GC is not enabled a call to 'NSMakeCollectable' has no effect on its argument}} 104 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}} 105} 106 107CFTypeRef CFCopyRuleViolation () { 108 CFTypeRef object = CFGetSomething(); // expected-note{{Call to function 'CFGetSomething' returns a Core Foundation object with a +0 retain counte}} 109 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}} 110} 111 112CFTypeRef CFGetRuleViolation () { 113 CFTypeRef object = CFCreateSomething(); // expected-warning{{leak}} expected-note{{Call to function 'CFCreateSomething' returns a Core Foundation object with a +1 retain counte}} 114 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}} 115} 116 117@implementation Foo (FundamentalMemoryManagementRules) 118- (id)copyViolation { 119 id result = self.propertyValue; // expected-note{{Property returns an Objective-C object with a +0 retain count}} 120 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}} 121} 122 123- (id)getViolation { 124 id result = [[Foo alloc] init]; // expected-warning{{leak}} expected-note{{Method returns an Objective-C object with a +1 retain count}} 125 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}} 126} 127@end 128