retain-release-gc-only.m revision a834fb43fddcf611ad248722fff1aa5b19807bed
1// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -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 unsigned long uintptr_t; 10typedef unsigned int uint32_t; 11typedef unsigned long long uint64_t; 12typedef unsigned int UInt32; 13typedef signed long CFIndex; 14typedef struct { 15 CFIndex location; 16 CFIndex length; 17} CFRange; 18static __inline__ __attribute__((always_inline)) CFRange CFRangeMake(CFIndex loc, CFIndex len) { 19 CFRange range; 20 range.location = loc; 21 range.length = len; 22 return range; 23} 24typedef const void * CFTypeRef; 25typedef const struct __CFString * CFStringRef; 26typedef const struct __CFAllocator * CFAllocatorRef; 27extern const CFAllocatorRef kCFAllocatorDefault; 28extern CFTypeRef CFRetain(CFTypeRef cf); 29extern void CFRelease(CFTypeRef cf); 30typedef struct { 31} 32CFArrayCallBacks; 33extern const CFArrayCallBacks kCFTypeArrayCallBacks; 34typedef const struct __CFArray * CFArrayRef; 35typedef struct __CFArray * CFMutableArrayRef; 36extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks); 37extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx); 38extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value); 39typedef struct { 40} 41CFDictionaryKeyCallBacks; 42extern const CFDictionaryKeyCallBacks kCFTypeDictionaryKeyCallBacks; 43typedef struct { 44} 45CFDictionaryValueCallBacks; 46extern const CFDictionaryValueCallBacks kCFTypeDictionaryValueCallBacks; 47typedef const struct __CFDictionary * CFDictionaryRef; 48typedef struct __CFDictionary * CFMutableDictionaryRef; 49extern CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFDictionaryKeyCallBacks *keyCallBacks, const CFDictionaryValueCallBacks *valueCallBacks); 50typedef UInt32 CFStringEncoding; 51enum { 52kCFStringEncodingMacRoman = 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 }; 53extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding); 54typedef double CFTimeInterval; 55typedef CFTimeInterval CFAbsoluteTime; 56extern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void); 57typedef const struct __CFDate * CFDateRef; 58extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at); 59extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate); 60typedef __darwin_natural_t natural_t; 61typedef natural_t mach_port_name_t; 62typedef mach_port_name_t mach_port_t; 63typedef int kern_return_t; 64typedef kern_return_t mach_error_t; 65enum { 66kCFNumberSInt8Type = 1, kCFNumberSInt16Type = 2, kCFNumberSInt32Type = 3, kCFNumberSInt64Type = 4, kCFNumberFloat32Type = 5, kCFNumberFloat64Type = 6, kCFNumberCharType = 7, kCFNumberShortType = 8, kCFNumberIntType = 9, kCFNumberLongType = 10, kCFNumberLongLongType = 11, kCFNumberFloatType = 12, kCFNumberDoubleType = 13, kCFNumberCFIndexType = 14, kCFNumberNSIntegerType = 15, kCFNumberCGFloatType = 16, kCFNumberMaxType = 16 }; 67typedef CFIndex CFNumberType; 68typedef const struct __CFNumber * CFNumberRef; 69extern CFNumberRef CFNumberCreate(CFAllocatorRef allocator, CFNumberType theType, const void *valuePtr); 70typedef const struct __CFAttributedString *CFAttributedStringRef; 71typedef struct __CFAttributedString *CFMutableAttributedStringRef; 72extern CFAttributedStringRef CFAttributedStringCreate(CFAllocatorRef alloc, CFStringRef str, CFDictionaryRef attributes) ; 73extern CFMutableAttributedStringRef CFAttributedStringCreateMutableCopy(CFAllocatorRef alloc, CFIndex maxLength, CFAttributedStringRef aStr) ; 74extern void CFAttributedStringSetAttribute(CFMutableAttributedStringRef aStr, CFRange range, CFStringRef attrName, CFTypeRef value) ; 75typedef signed char BOOL; 76typedef unsigned long NSUInteger; 77@class NSString, Protocol; 78extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); 79typedef struct _NSZone NSZone; 80@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; 81@protocol NSObject 82- (BOOL)isEqual:(id)object; 83- (id)retain; 84- (oneway void)release; 85- (id)autorelease; 86- (Class)class; 87@end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; 88@end @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; 89@end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; 90@end 91@interface NSObject <NSObject> {} 92+ (id)allocWithZone:(NSZone *)zone; 93+ (id)alloc; 94- (void)dealloc; 95@end 96@interface NSObject (NSCoderMethods) 97- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder; 98@end 99extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); 100typedef struct { 101} 102NSFastEnumerationState; 103@protocol NSFastEnumeration - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len; 104@end @class NSString, NSDictionary; 105@interface NSValue : NSObject <NSCopying, NSCoding> - (void)getValue:(void *)value; 106@end @interface NSNumber : NSValue - (char)charValue; 107- (id)initWithInt:(int)value; 108@end @class NSString; 109@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count; 110@end @interface NSArray (NSArrayCreation) + (id)array; 111@end @interface NSAutoreleasePool : NSObject { 112} 113- (void)drain; 114@end extern NSString * const NSBundleDidLoadNotification; 115typedef double NSTimeInterval; 116@interface NSDate : NSObject <NSCopying, NSCoding> - (NSTimeInterval)timeIntervalSinceReferenceDate; 117@end typedef unsigned short unichar; 118@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; 119- ( const char *)UTF8String; 120- (id)initWithUTF8String:(const char *)nullTerminatedCString; 121+ (id)stringWithUTF8String:(const char *)nullTerminatedCString; 122@end @class NSString, NSURL, NSError; 123@interface NSData : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; 124+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length; 125+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b; 126@end @class NSLocale, NSDate, NSCalendar, NSTimeZone, NSError, NSArray, NSMutableDictionary; 127@interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count; 128@end @interface NSMutableDictionary : NSDictionary - (void)removeObjectForKey:(id)aKey; 129- (void)setObject:(id)anObject forKey:(id)aKey; 130@end @interface NSMutableDictionary (NSMutableDictionaryCreation) + (id)dictionaryWithCapacity:(NSUInteger)numItems; 131@end typedef double CGFloat; 132struct CGSize { 133}; 134typedef struct CGSize CGSize; 135struct CGRect { 136}; 137typedef struct CGRect CGRect; 138typedef mach_port_t io_object_t; 139typedef char io_name_t[128]; 140typedef io_object_t io_iterator_t; 141typedef io_object_t io_service_t; 142typedef struct IONotificationPort * IONotificationPortRef; 143typedef void (*IOServiceMatchingCallback)( void * refcon, io_iterator_t iterator ); 144io_service_t IOServiceGetMatchingService( mach_port_t masterPort, CFDictionaryRef matching ); 145kern_return_t IOServiceGetMatchingServices( mach_port_t masterPort, CFDictionaryRef matching, io_iterator_t * existing ); 146kern_return_t IOServiceAddNotification( mach_port_t masterPort, const io_name_t notificationType, CFDictionaryRef matching, mach_port_t wakePort, uintptr_t reference, io_iterator_t * notification ) __attribute__((deprecated)); 147kern_return_t IOServiceAddMatchingNotification( IONotificationPortRef notifyPort, const io_name_t notificationType, CFDictionaryRef matching, IOServiceMatchingCallback callback, void * refCon, io_iterator_t * notification ); 148CFMutableDictionaryRef IOServiceMatching( const char * name ); 149CFMutableDictionaryRef IOServiceNameMatching( const char * name ); 150CFMutableDictionaryRef IOBSDNameMatching( mach_port_t masterPort, uint32_t options, const char * bsdName ); 151CFMutableDictionaryRef IOOpenFirmwarePathMatching( mach_port_t masterPort, uint32_t options, const char * path ); 152CFMutableDictionaryRef IORegistryEntryIDMatching( uint64_t entryID ); 153typedef struct __DASession * DASessionRef; 154extern DASessionRef DASessionCreate( CFAllocatorRef allocator ); 155typedef struct __DADisk * DADiskRef; 156extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name ); 157extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media ); 158extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk ); 159extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk ); 160@interface NSTask : NSObject - (id)init; 161@end typedef struct CGColorSpace *CGColorSpaceRef; 162typedef struct CGImage *CGImageRef; 163typedef struct CGLayer *CGLayerRef; 164@interface NSResponder : NSObject <NSCoding> { 165} 166@end @protocol NSAnimatablePropertyContainer - (id)animator; 167@end extern NSString *NSAnimationTriggerOrderIn ; 168@interface NSView : NSResponder <NSAnimatablePropertyContainer> { 169} 170@end @protocol NSValidatedUserInterfaceItem - (SEL)action; 171@end @protocol NSUserInterfaceValidations - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem; 172@end @class NSDate, NSDictionary, NSError, NSException, NSNotification; 173@interface NSApplication : NSResponder <NSUserInterfaceValidations> { 174} 175@end enum { 176NSTerminateCancel = 0, NSTerminateNow = 1, NSTerminateLater = 2 }; 177typedef NSUInteger NSApplicationTerminateReply; 178@protocol NSApplicationDelegate <NSObject> @optional - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender; 179@end @class NSAttributedString, NSEvent, NSFont, NSFormatter, NSImage, NSMenu, NSText, NSView, NSTextView; 180@interface NSCell : NSObject <NSCopying, NSCoding> { 181} 182@end @class NSTextField, NSPanel, NSArray, NSWindow, NSImage, NSButton, NSError; 183typedef struct { 184} 185CVTimeStamp; 186@interface CIImage : NSObject <NSCoding, NSCopying> { 187} 188typedef int CIFormat; 189@end enum { 190kDAReturnSuccess = 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 }; 191typedef mach_error_t DAReturn; 192typedef const struct __DADissenter * DADissenterRef; 193extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string ); 194@interface CIContext: NSObject { 195} 196- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r; 197- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r format:(CIFormat)f colorSpace:(CGColorSpaceRef)cs; 198- (CGLayerRef)createCGLayerWithSize:(CGSize)size info:(CFDictionaryRef)d; 199@end extern NSString* const QCRendererEventKey; 200@protocol QCCompositionRenderer - (NSDictionary*) attributes; 201@end @interface QCRenderer : NSObject <QCCompositionRenderer> { 202} 203- (id) createSnapshotImageOfType:(NSString*)type; 204@end extern NSString* const QCViewDidStartRenderingNotification; 205@interface QCView : NSView <QCCompositionRenderer> { 206} 207- (id) createSnapshotImageOfType:(NSString*)type; 208@end enum { 209ICEXIFOrientation1 = 1, ICEXIFOrientation2 = 2, ICEXIFOrientation3 = 3, ICEXIFOrientation4 = 4, ICEXIFOrientation5 = 5, ICEXIFOrientation6 = 6, ICEXIFOrientation7 = 7, ICEXIFOrientation8 = 8, }; 210@class ICDevice; 211@protocol ICDeviceDelegate <NSObject> @required - (void)didRemoveDevice:(ICDevice*)device; 212@end extern NSString *const ICScannerStatusWarmingUp; 213@class ICScannerDevice; 214@protocol ICScannerDeviceDelegate <ICDeviceDelegate> @optional - (void)scannerDeviceDidBecomeAvailable:(ICScannerDevice*)scanner; 215@end 216CFTypeRef CFMakeCollectable(CFTypeRef cf) ; 217 218static __inline__ __attribute__((always_inline)) id NSMakeCollectable(CFTypeRef 219cf) { 220 return cf ? (id)CFMakeCollectable(cf) : ((void*)0); 221} 222 223//===----------------------------------------------------------------------===// 224// Test cases. 225//===----------------------------------------------------------------------===// 226 227void f1() { 228 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 229 id x = [(id) A autorelease]; 230 CFRelease((CFMutableArrayRef) x); 231} 232 233void f2() { 234 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}} 235 id x = [(id) A retain]; 236 [x release]; 237 [x release]; 238} 239 240void f3() { 241 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}} 242 CFMakeCollectable(A); 243 CFRetain(A); 244} 245 246void f3b() { 247 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 248 CFMakeCollectable(A); 249} 250 251 252void f4() { 253 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}} 254 NSMakeCollectable(A); 255 CFRetain(A); 256} 257 258void f4b() { 259 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 260 NSMakeCollectable(A); 261} 262 263void f5() { 264 id x = [NSMakeCollectable(CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks)) autorelease]; // no-warning 265} 266 267void f5b() { 268 id x = [(id) CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks) autorelease]; // expected-warning{{leak}} 269} 270 271// Test return of non-owned objects in contexts where an owned object 272// is expected. 273@interface TestReturnNotOwnedWhenExpectedOwned 274- (NSString*)newString; 275- (CFMutableArrayRef)newArray; 276@end 277 278@implementation TestReturnNotOwnedWhenExpectedOwned 279- (NSString*)newString { 280 NSString *s = [NSString stringWithUTF8String:"hello"]; // expected-warning{{Potential leak (when using garbage collection) of an object allocated}} 281 CFRetain(s); 282 return s; 283} 284- (CFMutableArrayRef)newArray{ 285 return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 286} 287@end 288 289//===----------------------------------------------------------------------===// 290// <rdar://problem/6948053> False positive: object substitution during -init* 291// methods warns about returning +0 when using -fobjc-gc-only 292//===----------------------------------------------------------------------===// 293 294@interface MyClassRdar6948053 : NSObject 295- (id) init; 296+ (id) shared; 297@end 298 299@implementation MyClassRdar6948053 300+(id) shared { 301 return (id) 0; 302} 303- (id) init 304{ 305 Class myClass = [self class]; 306 [self release]; 307 return [[myClass shared] retain]; // no-warning 308} 309@end 310 311//===----------------------------------------------------------------------===// 312// <rdar://problem/7174400> 'ciContext createCGImage:outputImage fromRect:' returns a retained CF object (not GC'ed)//===----------------------------------------------------------------------===// 313//===----------------------------------------------------------------------===// 314 315void rdar_7174400(QCView *view, QCRenderer *renderer, CIContext *context, 316 NSString *str, CIImage *img, CGRect rect, 317 CIFormat form, CGColorSpaceRef cs) { 318 [view createSnapshotImageOfType:str]; // no-warning 319 [renderer createSnapshotImageOfType:str]; // no-warning 320 [context createCGImage:img fromRect:rect]; // expected-warning{{leak}} 321 [context createCGImage:img fromRect:rect format:form colorSpace:cs]; // expected-warning{{leak}} 322} 323 324//===----------------------------------------------------------------------===// 325// Tests of ownership attributes. 326//===----------------------------------------------------------------------===// 327 328@interface TestOwnershipAttr : NSObject 329- (NSString*) returnsAnOwnedString __attribute__((ns_returns_retained)); 330- (NSString*) returnsAnOwnedCFString __attribute__((cf_returns_retained)); 331@end 332 333void test_attr_1(TestOwnershipAttr *X) { 334 NSString *str = [X returnsAnOwnedString]; // no-warning 335} 336 337void test_attr_1b(TestOwnershipAttr *X) { 338 NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}} 339} 340 341@interface MyClassTestCFAttr : NSObject {} 342- (NSDate*) returnsCFRetained __attribute__((cf_returns_retained)); 343- (NSDate*) alsoReturnsRetained; 344- (NSDate*) returnsNSRetained __attribute__((ns_returns_retained)); 345@end 346 347__attribute__((cf_returns_retained)) 348CFDateRef returnsRetainedCFDate() { 349 return CFDateCreate(0, CFAbsoluteTimeGetCurrent()); 350} 351 352@implementation MyClassTestCFAttr 353- (NSDate*) returnsCFRetained { 354 return (NSDate*) returnsRetainedCFDate(); // No leak. 355} 356 357- (NSDate*) alsoReturnsRetained { 358 return (NSDate*) returnsRetainedCFDate(); // expected-warning{{leak}} 359} 360 361- (NSDate*) returnsNSRetained { 362 return (NSDate*) returnsRetainedCFDate(); // expected-warning{{leak}} 363} 364@end