retain-release-gc-only.m revision b9d8db86ab1d9c95c09083d8e9792414ae9fcd6d
1// RUN: clang-cc -analyze -checker-cfref -verify -fobjc-gc-only %s && 2// RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -fobjc-gc-only -verify %s 3 4//===----------------------------------------------------------------------===// 5// Header stuff. 6//===----------------------------------------------------------------------===// 7 8typedef unsigned int __darwin_natural_t; 9typedef struct {} div_t; 10typedef unsigned long UInt32; 11typedef signed long CFIndex; 12typedef const void * CFTypeRef; 13typedef const struct __CFString * CFStringRef; 14typedef const struct __CFAllocator * CFAllocatorRef; 15extern const CFAllocatorRef kCFAllocatorDefault; 16extern CFTypeRef CFRetain(CFTypeRef cf); 17extern void CFRelease(CFTypeRef cf); 18typedef struct { 19} 20CFArrayCallBacks; 21extern const CFArrayCallBacks kCFTypeArrayCallBacks; 22typedef const struct __CFArray * CFArrayRef; 23typedef struct __CFArray * CFMutableArrayRef; 24extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks); 25extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx); 26extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value); 27typedef const struct __CFDictionary * CFDictionaryRef; 28typedef UInt32 CFStringEncoding; 29enum { 30kCFStringEncodingMacRoman = 0, kCFStringEncodingWindowsLatin1 = 0x0500, kCFStringEncodingISOLatin1 = 0x0201, kCFStringEncodingNextStepLatin = 0x0B01, kCFStringEncodingASCII = 0x0600, kCFStringEncodingUnicode = 0x0100, kCFStringEncodingUTF8 = 0x08000100, kCFStringEncodingNonLossyASCII = 0x0BFF , kCFStringEncodingUTF16 = 0x0100, kCFStringEncodingUTF16BE = 0x10000100, kCFStringEncodingUTF16LE = 0x14000100, kCFStringEncodingUTF32 = 0x0c000100, kCFStringEncodingUTF32BE = 0x18000100, kCFStringEncodingUTF32LE = 0x1c000100 }; 31extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding); 32typedef double CFTimeInterval; 33typedef CFTimeInterval CFAbsoluteTime; 34extern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void); 35typedef const struct __CFDate * CFDateRef; 36extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at); 37extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate); 38typedef __darwin_natural_t natural_t; 39typedef natural_t mach_port_name_t; 40typedef mach_port_name_t mach_port_t; 41typedef struct { 42} 43CFRunLoopObserverContext; 44typedef signed char BOOL; 45typedef unsigned int NSUInteger; 46@class NSString, Protocol; 47extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); 48typedef struct _NSZone NSZone; 49@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; 50@protocol NSObject - (BOOL)isEqual:(id)object; 51- (id)retain; 52- (oneway void)release; 53- (id)autorelease; 54@end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; 55@end @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; 56@end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; 57@end 58@interface NSObject <NSObject> {} 59+ (id)alloc; 60+ (id)allocWithZone:(NSZone *)zone; 61@end typedef float CGFloat; 62@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; 63- (const char *)UTF8String; 64- (id)initWithUTF8String:(const char *)nullTerminatedCString; 65+ (id)stringWithUTF8String:(const char *)nullTerminatedCString; 66- (id)init; 67- (void)dealloc; 68@end extern NSString * const NSCurrentLocaleDidChangeNotification ; 69@protocol NSLocking - (void)lock; 70@end extern NSString * const NSUndoManagerCheckpointNotification; 71typedef enum { 72ACL_READ_DATA = (1<<1), ACL_LIST_DIRECTORY = (1<<1), ACL_WRITE_DATA = (1<<2), ACL_ADD_FILE = (1<<2), ACL_EXECUTE = (1<<3), ACL_SEARCH = (1<<3), ACL_DELETE = (1<<4), ACL_APPEND_DATA = (1<<5), ACL_ADD_SUBDIRECTORY = (1<<5), ACL_DELETE_CHILD = (1<<6), ACL_READ_ATTRIBUTES = (1<<7), ACL_WRITE_ATTRIBUTES = (1<<8), ACL_READ_EXTATTRIBUTES = (1<<9), ACL_WRITE_EXTATTRIBUTES = (1<<10), ACL_READ_SECURITY = (1<<11), ACL_WRITE_SECURITY = (1<<12), ACL_CHANGE_OWNER = (1<<13) } 73acl_entry_id_t; 74typedef int kern_return_t; 75typedef kern_return_t mach_error_t; 76typedef mach_port_t io_object_t; 77typedef io_object_t io_service_t; 78typedef struct __DASession * DASessionRef; 79extern DASessionRef DASessionCreate( CFAllocatorRef allocator ); 80typedef struct __DADisk * DADiskRef; 81extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name ); 82extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media ); 83extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk ); 84extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk ); 85@interface NSResponder : NSObject <NSCoding> { 86} 87@end @class NSColor, NSFont, NSNotification; 88typedef struct __CFlags { 89} 90_CFlags; 91@interface NSCell : NSObject <NSCopying, NSCoding> { 92} 93@end @class NSDate, NSDictionary, NSError, NSException, NSNotification; 94@interface NSManagedObjectContext : NSObject <NSCoding, NSLocking> { 95} 96@end enum { 97kDAReturnSuccess = 0, kDAReturnError = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x01, kDAReturnBusy = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x02, kDAReturnBadArgument = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x03, kDAReturnExclusiveAccess = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x04, kDAReturnNoResources = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x05, kDAReturnNotFound = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x06, kDAReturnNotMounted = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x07, kDAReturnNotPermitted = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x08, kDAReturnNotPrivileged = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x09, kDAReturnNotReady = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0A, kDAReturnNotWritable = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0B, kDAReturnUnsupported = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0C }; 98typedef mach_error_t DAReturn; 99typedef const struct __DADissenter * DADissenterRef; 100extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string ); 101 102CFTypeRef CFMakeCollectable(CFTypeRef cf) ; 103 104static __inline__ __attribute__((always_inline)) id NSMakeCollectable(CFTypeRef 105cf) { 106 return cf ? (id)CFMakeCollectable(cf) : ((void*)0); 107} 108 109//===----------------------------------------------------------------------===// 110// Test cases. 111//===----------------------------------------------------------------------===// 112 113void f1() { 114 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 115 id x = [(id) A autorelease]; 116 CFRelease((CFMutableArrayRef) x); 117} 118 119void f2() { 120 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}} 121 id x = [(id) A retain]; 122 [x release]; 123 [x release]; 124} 125 126void f3() { 127 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}} 128 CFMakeCollectable(A); 129 CFRetain(A); 130} 131 132void f3b() { 133 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 134 CFMakeCollectable(A); 135} 136 137 138void f4() { 139 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}} 140 NSMakeCollectable(A); 141 CFRetain(A); 142} 143 144void f4b() { 145 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 146 NSMakeCollectable(A); 147} 148 149void f5() { 150 id x = [NSMakeCollectable(CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks)) autorelease]; // no-warning 151} 152 153void f5b() { 154 id x = [(id) CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks) autorelease]; // expected-warning{{leak}} 155} 156 157// Test return of non-owned objects in contexts where an owned object 158// is expected. 159@interface TestReturnNotOwnedWhenExpectedOwned 160- (NSString*)newString; 161- (CFMutableArrayRef)newArray; 162@end 163 164@implementation TestReturnNotOwnedWhenExpectedOwned 165- (NSString*)newString { 166 NSString *s = [NSString stringWithUTF8String:"hello"]; // expected-warning{{Potential leak (when using garbage collection) of an object allocated}} 167 CFRetain(s); 168 return s; 169} 170- (CFMutableArrayRef)newArray{ 171 return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 172} 173@end 174 175//===----------------------------------------------------------------------===// 176// Tests of ownership attributes. 177//===----------------------------------------------------------------------===// 178 179@interface TestOwnershipAttr : NSObject 180- (NSString*) returnsAnOwnedString __attribute__((ns_returns_retained)); 181- (NSString*) returnsAnOwnedCFString __attribute__((cf_returns_retained)); 182@end 183 184void test_attr_1(TestOwnershipAttr *X) { 185 NSString *str = [X returnsAnOwnedString]; // no-warning 186} 187 188void test_attr_1b(TestOwnershipAttr *X) { 189 NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}} 190} 191 192@interface MyClassTestCFAttr : NSObject {} 193- (NSDate*) returnsCFRetained __attribute__((cf_returns_retained)); 194- (NSDate*) alsoReturnsRetained; 195- (NSDate*) returnsNSRetained __attribute__((ns_returns_retained)); 196@end 197 198__attribute__((cf_returns_retained)) 199CFDateRef returnsRetainedCFDate() { 200 return CFDateCreate(0, CFAbsoluteTimeGetCurrent()); 201} 202 203@implementation MyClassTestCFAttr 204- (NSDate*) returnsCFRetained { 205 return (NSDate*) returnsRetainedCFDate(); // No leak. 206} 207 208- (NSDate*) alsoReturnsRetained { 209 return (NSDate*) returnsRetainedCFDate(); // expected-warning{{leak}} 210} 211 212- (NSDate*) returnsNSRetained { 213 return (NSDate*) returnsRetainedCFDate(); // expected-warning{{leak}} 214} 215@end