retain-release-gc-only.m revision bf20dbdec6ab7d02e96fac937ad57b2b95c0675c
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 104//===----------------------------------------------------------------------===// 105// Test cases. 106//===----------------------------------------------------------------------===// 107 108void f1() { 109 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 110 id x = [(id) A autorelease]; 111 CFRelease((CFMutableArrayRef) x); 112} 113 114void f2() { 115 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}} 116 id x = [(id) A retain]; 117 [x release]; 118 [x release]; 119} 120 121void f3() { 122 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}} 123 CFMakeCollectable(A); 124 CFRetain(A); 125} 126 127//===----------------------------------------------------------------------===// 128// Tests of ownership attributes. 129//===----------------------------------------------------------------------===// 130 131@interface TestOwnershipAttr : NSObject 132- (NSString*) returnsAnOwnedString __attribute__((objc_ownership_returns)); 133- (void) myRetain:(id)__attribute__((objc_ownership_retain))obj; 134- (void) myCFRetain:(id)__attribute__((objc_ownership_cfretain))obj; 135- (void) myRelease:(id)__attribute__((objc_ownership_release))obj; 136- (void) myCFRelease:(id)__attribute__((objc_ownership_cfrelease))obj; 137- (void) makeCollectable:(id)__attribute__((objc_ownership_make_collectable))obj; 138@end 139 140void test_attr_1(TestOwnershipAttr *X) { 141 NSString *str = [X returnsAnOwnedString]; // no-warning 142} 143 144void test_attr_2(TestOwnershipAttr *X) { 145 NSString *str = [X returnsAnOwnedString]; // no-warning 146 [X myRetain:str]; 147 [str release]; 148} 149 150void test_attr_3(TestOwnershipAttr *X) { 151 NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}} 152 [X myCFRetain:str]; 153 [str release]; 154} 155 156void test_attr_4a(TestOwnershipAttr *X) { 157 NSString *str = [X returnsAnOwnedString]; // no-warning 158} 159 160void test_attr_4b(TestOwnershipAttr *X) { 161 NSString *str = [X returnsAnOwnedString]; // no-warning 162 [X myRelease:str]; 163} 164 165void test_attr_4c(TestOwnershipAttr *X) { 166 NSString *str = [X returnsAnOwnedString]; // no-warning 167 [X myRetain:str]; 168 [X myRelease:str]; 169} 170 171void test_attr_5a(TestOwnershipAttr *X) { 172 NSString *str = [X returnsAnOwnedString]; // no-waring 173} 174 175void test_attr_5b(TestOwnershipAttr *X) { 176 NSString *str = [X returnsAnOwnedString]; 177 [X myCFRelease:str]; // expected-warning{{Incorrect decrement of the reference count of an object is not owned at this point by the caller}} 178} 179 180void test_attr_5c(TestOwnershipAttr *X) { 181 NSString *str = [X returnsAnOwnedString]; // no-warning 182 [X myCFRetain:str]; 183 [X myCFRelease:str]; 184} 185 186void test_attr_6a(TestOwnershipAttr *X) { 187 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}} 188} 189 190void test_attr_6b(TestOwnershipAttr *X) { 191 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 192 [X makeCollectable:(id)A]; 193} 194 195 196