retain-release-gc-only.m revision 6a08469ebe3e15262733841a9c29e2e563d78ba5
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__((ns_returns_ownership)); 133- (void) myRetain:(id)__attribute__((ns_ownership_retain))obj; 134- (void) myCFRetain:(id)__attribute__((cf_ownership_retain))obj; 135- (void) myRelease:(id)__attribute__((ns_ownership_release))obj; 136- (void) myCFRelease:(id)__attribute__((cf_ownership_release))obj; 137@end 138 139void test_attr_1(TestOwnershipAttr *X) { 140 NSString *str = [X returnsAnOwnedString]; // no-warning 141} 142 143void test_attr_2(TestOwnershipAttr *X) { 144 NSString *str = [X returnsAnOwnedString]; // no-warning 145 [X myRetain:str]; 146 [str release]; 147} 148 149void test_attr_3(TestOwnershipAttr *X) { 150 NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}} 151 [X myCFRetain:str]; 152 [str release]; 153} 154 155void test_attr_4a(TestOwnershipAttr *X) { 156 NSString *str = [X returnsAnOwnedString]; // no-warning 157} 158 159void test_attr_4b(TestOwnershipAttr *X) { 160 NSString *str = [X returnsAnOwnedString]; // no-warning 161 [X myRelease:str]; 162} 163 164void test_attr_4c(TestOwnershipAttr *X) { 165 NSString *str = [X returnsAnOwnedString]; // no-warning 166 [X myRetain:str]; 167 [X myRelease:str]; 168} 169 170void test_attr_5a(TestOwnershipAttr *X) { 171 NSString *str = [X returnsAnOwnedString]; // no-waring 172} 173 174void test_attr_5b(TestOwnershipAttr *X) { 175 NSString *str = [X returnsAnOwnedString]; 176 [X myCFRelease:str]; // expected-warning{{Incorrect decrement of the reference count of an object is not owned at this point by the caller}} 177} 178 179void test_attr_5c(TestOwnershipAttr *X) { 180 NSString *str = [X returnsAnOwnedString]; // no-warning 181 [X myCFRetain:str]; 182 [X myCFRelease:str]; 183} 184 185void test_attr_6a(TestOwnershipAttr *X) { 186 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}} 187} 188 189void test_attr_6b(TestOwnershipAttr *X) { 190 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 191 CFMakeCollectable(A); 192} 193 194 195