1*67e74705SXin Li// RUN: rm -rf %t 2*67e74705SXin Li// RUN: %clang_cc1 -fblocks -objcmt-migrate-annotation -objcmt-migrate-instancetype -objcmt-migrate-readwrite-property -mt-migrate-directory %t %s -x objective-c -triple x86_64-apple-darwin11 3*67e74705SXin Li// RUN: c-arcmt-test -mt-migrate-directory %t | arcmt-test -verify-transformed-files %s.result 4*67e74705SXin Li// RUN: %clang_cc1 -fblocks -triple x86_64-apple-darwin10 -fsyntax-only -x objective-c %s.result 5*67e74705SXin Li 6*67e74705SXin Li#ifndef CF_IMPLICIT_BRIDGING_ENABLED 7*67e74705SXin Li#if __has_feature(arc_cf_code_audited) 8*67e74705SXin Li#define CF_IMPLICIT_BRIDGING_ENABLED _Pragma("clang arc_cf_code_audited begin") 9*67e74705SXin Li#else 10*67e74705SXin Li#define CF_IMPLICIT_BRIDGING_ENABLED 11*67e74705SXin Li#endif 12*67e74705SXin Li#endif 13*67e74705SXin Li 14*67e74705SXin Li#ifndef CF_IMPLICIT_BRIDGING_DISABLED 15*67e74705SXin Li#if __has_feature(arc_cf_code_audited) 16*67e74705SXin Li#define CF_IMPLICIT_BRIDGING_DISABLED _Pragma("clang arc_cf_code_audited end") 17*67e74705SXin Li#else 18*67e74705SXin Li#define CF_IMPLICIT_BRIDGING_DISABLED 19*67e74705SXin Li#endif 20*67e74705SXin Li#endif 21*67e74705SXin Li 22*67e74705SXin Li#if __has_feature(attribute_ns_returns_retained) 23*67e74705SXin Li#define NS_RETURNS_RETAINED __attribute__((ns_returns_retained)) 24*67e74705SXin Li#endif 25*67e74705SXin Li#if __has_feature(attribute_cf_returns_retained) 26*67e74705SXin Li#define CF_RETURNS_RETAINED __attribute__((cf_returns_retained)) 27*67e74705SXin Li#endif 28*67e74705SXin Li#if __has_feature(attribute_ns_returns_not_retained) 29*67e74705SXin Li#define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained)) 30*67e74705SXin Li#endif 31*67e74705SXin Li#if __has_feature(attribute_cf_returns_not_retained) 32*67e74705SXin Li#define CF_RETURNS_NOT_RETAINED __attribute__((cf_returns_not_retained)) 33*67e74705SXin Li#endif 34*67e74705SXin Li#if __has_feature(attribute_ns_consumes_self) 35*67e74705SXin Li#define NS_CONSUMES_SELF __attribute__((ns_consumes_self)) 36*67e74705SXin Li#endif 37*67e74705SXin Li#if __has_feature(attribute_ns_consumed) 38*67e74705SXin Li#define NS_CONSUMED __attribute__((ns_consumed)) 39*67e74705SXin Li#endif 40*67e74705SXin Li#if __has_feature(attribute_cf_consumed) 41*67e74705SXin Li#define CF_CONSUMED __attribute__((cf_consumed)) 42*67e74705SXin Li#endif 43*67e74705SXin Li#if __has_attribute(ns_returns_autoreleased) 44*67e74705SXin Li#define NS_RETURNS_AUTORELEASED __attribute__((ns_returns_autoreleased)) 45*67e74705SXin Li#endif 46*67e74705SXin Li 47*67e74705SXin Li//===----------------------------------------------------------------------===// 48*67e74705SXin Li// The following code is reduced using delta-debugging from Mac OS X headers: 49*67e74705SXin Li// 50*67e74705SXin Li// #include <Cocoa/Cocoa.h> 51*67e74705SXin Li// #include <CoreFoundation/CoreFoundation.h> 52*67e74705SXin Li// #include <DiskArbitration/DiskArbitration.h> 53*67e74705SXin Li// #include <QuartzCore/QuartzCore.h> 54*67e74705SXin Li// #include <Quartz/Quartz.h> 55*67e74705SXin Li// #include <IOKit/IOKitLib.h> 56*67e74705SXin Li// 57*67e74705SXin Li// It includes the basic definitions for the test cases below. 58*67e74705SXin Li//===----------------------------------------------------------------------===// 59*67e74705SXin Li 60*67e74705SXin Litypedef unsigned int __darwin_natural_t; 61*67e74705SXin Litypedef unsigned long uintptr_t; 62*67e74705SXin Litypedef unsigned int uint32_t; 63*67e74705SXin Litypedef unsigned long long uint64_t; 64*67e74705SXin Litypedef unsigned int UInt32; 65*67e74705SXin Litypedef signed long CFIndex; 66*67e74705SXin Litypedef CFIndex CFByteOrder; 67*67e74705SXin Litypedef struct { 68*67e74705SXin Li CFIndex location; 69*67e74705SXin Li CFIndex length; 70*67e74705SXin Li} CFRange; 71*67e74705SXin Listatic __inline__ __attribute__((always_inline)) CFRange CFRangeMake(CFIndex loc, CFIndex len) { 72*67e74705SXin Li CFRange range; 73*67e74705SXin Li range.location = loc; 74*67e74705SXin Li range.length = len; 75*67e74705SXin Li return range; 76*67e74705SXin Li} 77*67e74705SXin Litypedef const void * CFTypeRef; 78*67e74705SXin Litypedef const struct __CFString * CFStringRef; 79*67e74705SXin Litypedef const struct __CFAllocator * CFAllocatorRef; 80*67e74705SXin Liextern const CFAllocatorRef kCFAllocatorDefault; 81*67e74705SXin Liextern CFTypeRef CFRetain(CFTypeRef cf); 82*67e74705SXin Liextern void CFRelease(CFTypeRef cf); 83*67e74705SXin Liextern CFTypeRef CFMakeCollectable(CFTypeRef cf); 84*67e74705SXin Litypedef struct { 85*67e74705SXin Li} 86*67e74705SXin LiCFArrayCallBacks; 87*67e74705SXin Liextern const CFArrayCallBacks kCFTypeArrayCallBacks; 88*67e74705SXin Litypedef const struct __CFArray * CFArrayRef; 89*67e74705SXin Litypedef struct __CFArray * CFMutableArrayRef; 90*67e74705SXin Liextern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks); 91*67e74705SXin Liextern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx); 92*67e74705SXin Liextern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value); 93*67e74705SXin Litypedef struct { 94*67e74705SXin Li} 95*67e74705SXin LiCFDictionaryKeyCallBacks; 96*67e74705SXin Liextern const CFDictionaryKeyCallBacks kCFTypeDictionaryKeyCallBacks; 97*67e74705SXin Litypedef struct { 98*67e74705SXin Li} 99*67e74705SXin LiCFDictionaryValueCallBacks; 100*67e74705SXin Liextern const CFDictionaryValueCallBacks kCFTypeDictionaryValueCallBacks; 101*67e74705SXin Litypedef const struct __CFDictionary * CFDictionaryRef; 102*67e74705SXin Litypedef struct __CFDictionary * CFMutableDictionaryRef; 103*67e74705SXin Liextern CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFDictionaryKeyCallBacks *keyCallBacks, const CFDictionaryValueCallBacks *valueCallBacks); 104*67e74705SXin Litypedef UInt32 CFStringEncoding; 105*67e74705SXin Lienum { 106*67e74705SXin LikCFStringEncodingMacRoman = 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 }; 107*67e74705SXin Liextern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding); 108*67e74705SXin Litypedef double CFTimeInterval; 109*67e74705SXin Litypedef CFTimeInterval CFAbsoluteTime; 110*67e74705SXin Liextern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void); 111*67e74705SXin Litypedef const struct __CFDate * CFDateRef; 112*67e74705SXin Liextern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at); 113*67e74705SXin Liextern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate); 114*67e74705SXin Litypedef __darwin_natural_t natural_t; 115*67e74705SXin Litypedef natural_t mach_port_name_t; 116*67e74705SXin Litypedef mach_port_name_t mach_port_t; 117*67e74705SXin Litypedef int kern_return_t; 118*67e74705SXin Litypedef kern_return_t mach_error_t; 119*67e74705SXin Lienum { 120*67e74705SXin LikCFNumberSInt8Type = 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 }; 121*67e74705SXin Litypedef CFIndex CFNumberType; 122*67e74705SXin Litypedef const struct __CFNumber * CFNumberRef; 123*67e74705SXin Liextern CFNumberRef CFNumberCreate(CFAllocatorRef allocator, CFNumberType theType, const void *valuePtr); 124*67e74705SXin Litypedef const struct __CFAttributedString *CFAttributedStringRef; 125*67e74705SXin Litypedef struct __CFAttributedString *CFMutableAttributedStringRef; 126*67e74705SXin Liextern CFAttributedStringRef CFAttributedStringCreate(CFAllocatorRef alloc, CFStringRef str, CFDictionaryRef attributes) ; 127*67e74705SXin Liextern CFMutableAttributedStringRef CFAttributedStringCreateMutableCopy(CFAllocatorRef alloc, CFIndex maxLength, CFAttributedStringRef aStr) ; 128*67e74705SXin Liextern void CFAttributedStringSetAttribute(CFMutableAttributedStringRef aStr, CFRange range, CFStringRef attrName, CFTypeRef value) ; 129*67e74705SXin Litypedef signed char BOOL; 130*67e74705SXin Litypedef unsigned long NSUInteger; 131*67e74705SXin Li@class NSString, Protocol; 132*67e74705SXin Liextern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); 133*67e74705SXin Litypedef struct _NSZone NSZone; 134*67e74705SXin Li@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; 135*67e74705SXin Li@protocol NSObject 136*67e74705SXin Li- (BOOL)isEqual:(id)object; 137*67e74705SXin Li- (id)retain; 138*67e74705SXin Li- (oneway void)release; 139*67e74705SXin Li- (id)autorelease; 140*67e74705SXin Li- (NSString *)description; 141*67e74705SXin Li- (id)init; 142*67e74705SXin Li@end 143*67e74705SXin Li@protocol NSCopying 144*67e74705SXin Li- (id)copyWithZone:(NSZone *)zone; 145*67e74705SXin Li@end 146*67e74705SXin Li@protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; 147*67e74705SXin Li@end 148*67e74705SXin Li@protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; 149*67e74705SXin Li@end 150*67e74705SXin Li@interface NSObject <NSObject> {} 151*67e74705SXin Li+ (id)allocWithZone:(NSZone *)zone; 152*67e74705SXin Li+ (id)alloc; 153*67e74705SXin Li+ (id)new; 154*67e74705SXin Li- (void)dealloc; 155*67e74705SXin Li@end 156*67e74705SXin Li@interface NSObject (NSCoderMethods) 157*67e74705SXin Li- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder; 158*67e74705SXin Li@end 159*67e74705SXin Liextern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); 160*67e74705SXin Litypedef struct { 161*67e74705SXin Li} 162*67e74705SXin LiNSFastEnumerationState; 163*67e74705SXin Li@protocol NSFastEnumeration 164*67e74705SXin Li- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len; 165*67e74705SXin Li@end 166*67e74705SXin Li@class NSString, NSDictionary; 167*67e74705SXin Li@interface NSValue : NSObject <NSCopying, NSCoding> - (void)getValue:(void *)value; 168*67e74705SXin Li@end 169*67e74705SXin Li@interface NSNumber : NSValue 170*67e74705SXin Li- (char)charValue; 171*67e74705SXin Li- (id)initWithInt:(int)value; 172*67e74705SXin Li+ (NSNumber *)numberWithInt:(int)value; 173*67e74705SXin Li@end 174*67e74705SXin Li@class NSString; 175*67e74705SXin Li@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> 176*67e74705SXin Li- (NSUInteger)count; 177*67e74705SXin Li- (id)initWithObjects:(const id [])objects count:(NSUInteger)cnt; 178*67e74705SXin Li+ (id)arrayWithObject:(id)anObject; 179*67e74705SXin Li+ (id)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt; 180*67e74705SXin Li+ (id)arrayWithObjects:(id)firstObj, ... __attribute__((sentinel(0,1))); 181*67e74705SXin Li- (id)initWithObjects:(id)firstObj, ... __attribute__((sentinel(0,1))); 182*67e74705SXin Li- (id)initWithArray:(NSArray *)array; 183*67e74705SXin Li@end @interface NSArray (NSArrayCreation) + (id)array; 184*67e74705SXin Li@end @interface NSAutoreleasePool : NSObject { 185*67e74705SXin Li} 186*67e74705SXin Li- (void)drain; 187*67e74705SXin Li@end extern NSString * const NSBundleDidLoadNotification; 188*67e74705SXin Litypedef double NSTimeInterval; 189*67e74705SXin Li@interface NSDate : NSObject <NSCopying, NSCoding> - (NSTimeInterval)timeIntervalSinceReferenceDate; 190*67e74705SXin Li@end typedef unsigned short unichar; 191*67e74705SXin Li@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> 192*67e74705SXin Li- (NSUInteger)length; 193*67e74705SXin Li- (NSString *)stringByAppendingString:(NSString *)aString; 194*67e74705SXin Li- ( const char *)UTF8String; 195*67e74705SXin Li- (id)initWithUTF8String:(const char *)nullTerminatedCString; 196*67e74705SXin Li+ (id)stringWithUTF8String:(const char *)nullTerminatedCString; 197*67e74705SXin Li@end @class NSString, NSURL, NSError; 198*67e74705SXin Li@interface NSData : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; 199*67e74705SXin Li+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length; 200*67e74705SXin Li+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b; 201*67e74705SXin Li@end @class NSLocale, NSDate, NSCalendar, NSTimeZone, NSError, NSArray, NSMutableDictionary; 202*67e74705SXin Li@interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> 203*67e74705SXin Li- (NSUInteger)count; 204*67e74705SXin Li+ (id)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys; 205*67e74705SXin Li+ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id <NSCopying> [])keys count:(NSUInteger)cnt; 206*67e74705SXin Li@end 207*67e74705SXin Li@interface NSMutableDictionary : NSDictionary - (void)removeObjectForKey:(id)aKey; 208*67e74705SXin Li- (void)setObject:(id)anObject forKey:(id)aKey; 209*67e74705SXin Li@end @interface NSMutableDictionary (NSMutableDictionaryCreation) + (id)dictionaryWithCapacity:(NSUInteger)numItems; 210*67e74705SXin Li@end typedef double CGFloat; 211*67e74705SXin Listruct CGSize { 212*67e74705SXin Li}; 213*67e74705SXin Litypedef struct CGSize CGSize; 214*67e74705SXin Listruct CGRect { 215*67e74705SXin Li}; 216*67e74705SXin Litypedef struct CGRect CGRect; 217*67e74705SXin Litypedef mach_port_t io_object_t; 218*67e74705SXin Litypedef char io_name_t[128]; 219*67e74705SXin Litypedef io_object_t io_iterator_t; 220*67e74705SXin Litypedef io_object_t io_service_t; 221*67e74705SXin Litypedef struct IONotificationPort * IONotificationPortRef; 222*67e74705SXin Litypedef void (*IOServiceMatchingCallback)( void * refcon, io_iterator_t iterator ); 223*67e74705SXin Liio_service_t IOServiceGetMatchingService( mach_port_t masterPort, CFDictionaryRef matching ); 224*67e74705SXin Likern_return_t IOServiceGetMatchingServices( mach_port_t masterPort, CFDictionaryRef matching, io_iterator_t * existing ); 225*67e74705SXin Likern_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)); // expected-note {{'IOServiceAddNotification' declared here}} 226*67e74705SXin Likern_return_t IOServiceAddMatchingNotification( IONotificationPortRef notifyPort, const io_name_t notificationType, CFDictionaryRef matching, IOServiceMatchingCallback callback, void * refCon, io_iterator_t * notification ); 227*67e74705SXin LiCFMutableDictionaryRef IOServiceMatching( const char * name ); 228*67e74705SXin LiCFMutableDictionaryRef IOServiceNameMatching( const char * name ); 229*67e74705SXin LiCFMutableDictionaryRef IOBSDNameMatching( mach_port_t masterPort, uint32_t options, const char * bsdName ); 230*67e74705SXin LiCFMutableDictionaryRef IOOpenFirmwarePathMatching( mach_port_t masterPort, uint32_t options, const char * path ); 231*67e74705SXin LiCFMutableDictionaryRef IORegistryEntryIDMatching( uint64_t entryID ); 232*67e74705SXin Litypedef struct __DASession * DASessionRef; 233*67e74705SXin Liextern DASessionRef DASessionCreate( CFAllocatorRef allocator ); 234*67e74705SXin Litypedef struct __DADisk * DADiskRef; 235*67e74705SXin Liextern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name ); 236*67e74705SXin Liextern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media ); 237*67e74705SXin Liextern CFDictionaryRef DADiskCopyDescription( DADiskRef disk ); 238*67e74705SXin Liextern DADiskRef DADiskCopyWholeDisk( DADiskRef disk ); 239*67e74705SXin Li@interface NSTask : NSObject - (id)init; 240*67e74705SXin Li@end typedef struct CGColorSpace *CGColorSpaceRef; 241*67e74705SXin Litypedef struct CGImage *CGImageRef; 242*67e74705SXin Litypedef struct CGLayer *CGLayerRef; 243*67e74705SXin Li@interface NSResponder : NSObject <NSCoding> { 244*67e74705SXin Li} 245*67e74705SXin Li@end @protocol NSAnimatablePropertyContainer - (id)animator; 246*67e74705SXin Li@end extern NSString *NSAnimationTriggerOrderIn ; 247*67e74705SXin Li@interface NSView : NSResponder <NSAnimatablePropertyContainer> { 248*67e74705SXin Li} 249*67e74705SXin Li@end @protocol NSValidatedUserInterfaceItem - (SEL)action; 250*67e74705SXin Li@end @protocol NSUserInterfaceValidations - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem; 251*67e74705SXin Li@end @class NSDate, NSDictionary, NSError, NSException, NSNotification; 252*67e74705SXin Li@class NSTextField, NSPanel, NSArray, NSWindow, NSImage, NSButton, NSError; 253*67e74705SXin Li@interface NSApplication : NSResponder <NSUserInterfaceValidations> { 254*67e74705SXin Li} 255*67e74705SXin Li- (void)beginSheet:(NSWindow *)sheet modalForWindow:(NSWindow *)docWindow modalDelegate:(id)modalDelegate didEndSelector:(SEL)didEndSelector contextInfo:(void *)contextInfo; 256*67e74705SXin Li@end enum { 257*67e74705SXin LiNSTerminateCancel = 0, NSTerminateNow = 1, NSTerminateLater = 2 }; 258*67e74705SXin Litypedef NSUInteger NSApplicationTerminateReply; 259*67e74705SXin Li@protocol NSApplicationDelegate <NSObject> @optional - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender; 260*67e74705SXin Li@end @class NSAttributedString, NSEvent, NSFont, NSFormatter, NSImage, NSMenu, NSText, NSView, NSTextView; 261*67e74705SXin Li@interface NSCell : NSObject <NSCopying, NSCoding> { 262*67e74705SXin Li} 263*67e74705SXin Li@end 264*67e74705SXin Litypedef struct { 265*67e74705SXin Li} 266*67e74705SXin LiCVTimeStamp; 267*67e74705SXin Li@interface CIImage : NSObject <NSCoding, NSCopying> { 268*67e74705SXin Li} 269*67e74705SXin Litypedef int CIFormat; 270*67e74705SXin Li@end enum { 271*67e74705SXin LikDAReturnSuccess = 0, kDAReturnError = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x01, kDAReturnBusy = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x02, kDAReturnBadArgument = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x03, kDAReturnExclusiveAccess = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x04, kDAReturnNoResources = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x05, kDAReturnNotFound = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x06, kDAReturnNotMounted = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x07, kDAReturnNotPermitted = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x08, kDAReturnNotPrivileged = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x09, kDAReturnNotReady = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0A, kDAReturnNotWritable = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0B, kDAReturnUnsupported = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0C }; 272*67e74705SXin Litypedef mach_error_t DAReturn; 273*67e74705SXin Litypedef const struct __DADissenter * DADissenterRef; 274*67e74705SXin Liextern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string ); 275*67e74705SXin Li@interface CIContext: NSObject { 276*67e74705SXin Li} 277*67e74705SXin Li- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r; 278*67e74705SXin Li- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r format:(CIFormat)f colorSpace:(CGColorSpaceRef)cs; 279*67e74705SXin Li- (CGLayerRef)createCGLayerWithSize:(CGSize)size info:(CFDictionaryRef)d; 280*67e74705SXin Li@end extern NSString* const QCRendererEventKey; 281*67e74705SXin Li@protocol QCCompositionRenderer - (NSDictionary*) attributes; 282*67e74705SXin Li@end @interface QCRenderer : NSObject <QCCompositionRenderer> { 283*67e74705SXin Li} 284*67e74705SXin Li- (id) createSnapshotImageOfType:(NSString*)type; 285*67e74705SXin Li@end extern NSString* const QCViewDidStartRenderingNotification; 286*67e74705SXin Li@interface QCView : NSView <QCCompositionRenderer> { 287*67e74705SXin Li} 288*67e74705SXin Li- (id) createSnapshotImageOfType:(NSString*)type; 289*67e74705SXin Li@end enum { 290*67e74705SXin LiICEXIFOrientation1 = 1, ICEXIFOrientation2 = 2, ICEXIFOrientation3 = 3, ICEXIFOrientation4 = 4, ICEXIFOrientation5 = 5, ICEXIFOrientation6 = 6, ICEXIFOrientation7 = 7, ICEXIFOrientation8 = 8, }; 291*67e74705SXin Li@class ICDevice; 292*67e74705SXin Li@protocol ICDeviceDelegate <NSObject> @required - (void)didRemoveDevice:(ICDevice*)device; 293*67e74705SXin Li@end extern NSString *const ICScannerStatusWarmingUp; 294*67e74705SXin Li@class ICScannerDevice; 295*67e74705SXin Li@protocol ICScannerDeviceDelegate <ICDeviceDelegate> @optional - (void)scannerDeviceDidBecomeAvailable:(ICScannerDevice*)scanner; 296*67e74705SXin Li@end 297*67e74705SXin Li 298*67e74705SXin Litypedef long unsigned int __darwin_size_t; 299*67e74705SXin Litypedef __darwin_size_t size_t; 300*67e74705SXin Litypedef unsigned long CFTypeID; 301*67e74705SXin Listruct CGPoint { 302*67e74705SXin Li CGFloat x; 303*67e74705SXin Li CGFloat y; 304*67e74705SXin Li}; 305*67e74705SXin Litypedef struct CGPoint CGPoint; 306*67e74705SXin Litypedef struct CGGradient *CGGradientRef; 307*67e74705SXin Litypedef uint32_t CGGradientDrawingOptions; 308*67e74705SXin Liextern CFTypeID CGGradientGetTypeID(void); 309*67e74705SXin Liextern CGGradientRef CGGradientCreateWithColorComponents(CGColorSpaceRef 310*67e74705SXin Li space, const CGFloat components[], const CGFloat locations[], size_t count); 311*67e74705SXin Liextern CGGradientRef CGGradientCreateWithColors(CGColorSpaceRef space, 312*67e74705SXin Li CFArrayRef colors, const CGFloat locations[]); 313*67e74705SXin Liextern CGGradientRef CGGradientRetain(CGGradientRef gradient); 314*67e74705SXin Liextern void CGGradientRelease(CGGradientRef gradient); 315*67e74705SXin Litypedef struct CGContext *CGContextRef; 316*67e74705SXin Liextern void CGContextDrawLinearGradient(CGContextRef context, 317*67e74705SXin Li CGGradientRef gradient, CGPoint startPoint, CGPoint endPoint, 318*67e74705SXin Li CGGradientDrawingOptions options); 319*67e74705SXin Liextern CGColorSpaceRef CGColorSpaceCreateDeviceRGB(void); 320*67e74705SXin Li 321*67e74705SXin Li@interface NSMutableArray : NSObject 322*67e74705SXin Li- (void)addObject:(id)object; 323*67e74705SXin Li+ (id)array; 324*67e74705SXin Li@end 325*67e74705SXin Li 326*67e74705SXin Li// This is how NSMakeCollectable is declared in the OS X 10.8 headers. 327*67e74705SXin Liid NSMakeCollectable(CFTypeRef __attribute__((cf_consumed))) __attribute__((ns_returns_retained)); 328*67e74705SXin Li 329*67e74705SXin Litypedef const struct __CFUUID * CFUUIDRef; 330*67e74705SXin Li 331*67e74705SXin Liextern 332*67e74705SXin Livoid *CFPlugInInstanceCreate(CFAllocatorRef allocator, CFUUIDRef factoryUUID, CFUUIDRef typeUUID); 333*67e74705SXin Li 334*67e74705SXin Li//===----------------------------------------------------------------------===// 335*67e74705SXin Li// Test cases. 336*67e74705SXin Li//===----------------------------------------------------------------------===// 337*67e74705SXin Li 338*67e74705SXin LiCFAbsoluteTime f1() { 339*67e74705SXin Li CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); 340*67e74705SXin Li CFDateRef date = CFDateCreate(0, t); 341*67e74705SXin Li CFRetain(date); 342*67e74705SXin Li CFRelease(date); 343*67e74705SXin Li CFDateGetAbsoluteTime(date); // no-warning 344*67e74705SXin Li CFRelease(date); 345*67e74705SXin Li t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released}} 346*67e74705SXin Li return t; 347*67e74705SXin Li} 348*67e74705SXin Li 349*67e74705SXin LiCFAbsoluteTime f2() { 350*67e74705SXin Li CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); 351*67e74705SXin Li CFDateRef date = CFDateCreate(0, t); 352*67e74705SXin Li [((NSDate*) date) retain]; 353*67e74705SXin Li CFRelease(date); 354*67e74705SXin Li CFDateGetAbsoluteTime(date); // no-warning 355*67e74705SXin Li [((NSDate*) date) release]; 356*67e74705SXin Li t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released}} 357*67e74705SXin Li return t; 358*67e74705SXin Li} 359*67e74705SXin Li 360*67e74705SXin Li 361*67e74705SXin LiNSDate* global_x; 362*67e74705SXin Li 363*67e74705SXin Li// Test to see if we suppress an error when we store the pointer 364*67e74705SXin Li// to a global. 365*67e74705SXin Li 366*67e74705SXin LiCFAbsoluteTime f3() { 367*67e74705SXin Li CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); 368*67e74705SXin Li CFDateRef date = CFDateCreate(0, t); 369*67e74705SXin Li [((NSDate*) date) retain]; 370*67e74705SXin Li CFRelease(date); 371*67e74705SXin Li CFDateGetAbsoluteTime(date); // no-warning 372*67e74705SXin Li global_x = (NSDate*) date; 373*67e74705SXin Li [((NSDate*) date) release]; 374*67e74705SXin Li t = CFDateGetAbsoluteTime(date); // no-warning 375*67e74705SXin Li return t; 376*67e74705SXin Li} 377*67e74705SXin Li 378*67e74705SXin Li//--------------------------------------------------------------------------- 379*67e74705SXin Li// Test case 'f4' differs for region store and basic store. See 380*67e74705SXin Li// retain-release-region-store.m and retain-release-basic-store.m. 381*67e74705SXin Li//--------------------------------------------------------------------------- 382*67e74705SXin Li 383*67e74705SXin Li// Test a leak. 384*67e74705SXin Li 385*67e74705SXin LiCFAbsoluteTime f5(int x) { 386*67e74705SXin Li CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); 387*67e74705SXin Li CFDateRef date = CFDateCreate(0, t); // expected-warning{{leak}} 388*67e74705SXin Li 389*67e74705SXin Li if (x) 390*67e74705SXin Li CFRelease(date); 391*67e74705SXin Li 392*67e74705SXin Li return t; 393*67e74705SXin Li} 394*67e74705SXin Li 395*67e74705SXin Li// Test a leak involving the return. 396*67e74705SXin Li 397*67e74705SXin LiCFDateRef f6(int x) { 398*67e74705SXin Li CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // expected-warning{{leak}} 399*67e74705SXin Li CFRetain(date); 400*67e74705SXin Li return date; 401*67e74705SXin Li} 402*67e74705SXin Li 403*67e74705SXin Li// Test a leak involving an overwrite. 404*67e74705SXin Li 405*67e74705SXin LiCFDateRef f7() { 406*67e74705SXin Li CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); //expected-warning{{leak}} 407*67e74705SXin Li CFRetain(date); 408*67e74705SXin Li date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // expected-warning {{leak}} 409*67e74705SXin Li return date; 410*67e74705SXin Li} 411*67e74705SXin Li 412*67e74705SXin Li// Generalization of Create rule. MyDateCreate returns a CFXXXTypeRef, and 413*67e74705SXin Li// has the word create. 414*67e74705SXin LiCFDateRef MyDateCreate(); 415*67e74705SXin Li 416*67e74705SXin LiCFDateRef f8() { 417*67e74705SXin Li CFDateRef date = MyDateCreate(); // expected-warning{{leak}} 418*67e74705SXin Li CFRetain(date); 419*67e74705SXin Li return date; 420*67e74705SXin Li} 421*67e74705SXin Li 422*67e74705SXin Li__attribute__((cf_returns_retained)) CFDateRef f9() { 423*67e74705SXin Li CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // no-warning 424*67e74705SXin Li int *p = 0; 425*67e74705SXin Li // When allocations fail, CFDateCreate can return null. 426*67e74705SXin Li if (!date) *p = 1; // expected-warning{{null}} 427*67e74705SXin Li return date; 428*67e74705SXin Li} 429*67e74705SXin Li 430*67e74705SXin Li// Handle DiskArbitration API: 431*67e74705SXin Li// 432*67e74705SXin Li// http://developer.apple.com/DOCUMENTATION/DARWIN/Reference/DiscArbitrationFramework/ 433*67e74705SXin Li// 434*67e74705SXin Livoid f10(io_service_t media, DADiskRef d, CFStringRef s) { 435*67e74705SXin Li DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, 0, "hello"); // expected-warning{{leak}} 436*67e74705SXin Li if (disk) NSLog(@"ok"); 437*67e74705SXin Li 438*67e74705SXin Li disk = DADiskCreateFromIOMedia(kCFAllocatorDefault, 0, media); // expected-warning{{leak}} 439*67e74705SXin Li if (disk) NSLog(@"ok"); 440*67e74705SXin Li 441*67e74705SXin Li CFDictionaryRef dict = DADiskCopyDescription(d); // expected-warning{{leak}} 442*67e74705SXin Li if (dict) NSLog(@"ok"); 443*67e74705SXin Li 444*67e74705SXin Li disk = DADiskCopyWholeDisk(d); // expected-warning{{leak}} 445*67e74705SXin Li if (disk) NSLog(@"ok"); 446*67e74705SXin Li 447*67e74705SXin Li DADissenterRef dissenter = DADissenterCreate(kCFAllocatorDefault, // expected-warning{{leak}} 448*67e74705SXin Li kDAReturnSuccess, s); 449*67e74705SXin Li if (dissenter) NSLog(@"ok"); 450*67e74705SXin Li 451*67e74705SXin Li DASessionRef session = DASessionCreate(kCFAllocatorDefault); // expected-warning{{leak}} 452*67e74705SXin Li if (session) NSLog(@"ok"); 453*67e74705SXin Li} 454*67e74705SXin Li 455*67e74705SXin Li// Test retain/release checker with CFString and CFMutableArray. 456*67e74705SXin Livoid f11() { 457*67e74705SXin Li // Create the array. 458*67e74705SXin Li CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); 459*67e74705SXin Li 460*67e74705SXin Li // Create a string. 461*67e74705SXin Li CFStringRef s1 = CFStringCreateWithCString(0, "hello world", 462*67e74705SXin Li kCFStringEncodingUTF8); 463*67e74705SXin Li 464*67e74705SXin Li // Add the string to the array. 465*67e74705SXin Li CFArrayAppendValue(A, s1); 466*67e74705SXin Li 467*67e74705SXin Li // Decrement the reference count. 468*67e74705SXin Li CFRelease(s1); // no-warning 469*67e74705SXin Li 470*67e74705SXin Li // Get the string. We don't own it. 471*67e74705SXin Li s1 = (CFStringRef) CFArrayGetValueAtIndex(A, 0); 472*67e74705SXin Li 473*67e74705SXin Li // Release the array. 474*67e74705SXin Li CFRelease(A); // no-warning 475*67e74705SXin Li 476*67e74705SXin Li // Release the string. This is a bug. 477*67e74705SXin Li CFRelease(s1); // expected-warning{{Incorrect decrement of the reference count}} 478*67e74705SXin Li} 479*67e74705SXin Li 480*67e74705SXin Li// PR 3337: Handle functions declared using typedefs. 481*67e74705SXin Litypedef CFTypeRef CREATEFUN(); 482*67e74705SXin LiCFTypeRef MyCreateFun(); 483*67e74705SXin Li 484*67e74705SXin Livoid f12() { 485*67e74705SXin Li CFTypeRef o = MyCreateFun(); // expected-warning {{leak}} 486*67e74705SXin Li} 487*67e74705SXin Li 488*67e74705SXin Livoid f13_autorelease() { 489*67e74705SXin Li CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 490*67e74705SXin Li [(id) A autorelease]; // no-warning 491*67e74705SXin Li} 492*67e74705SXin Li 493*67e74705SXin Livoid f13_autorelease_b() { 494*67e74705SXin Li CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); 495*67e74705SXin Li [(id) A autorelease]; 496*67e74705SXin Li [(id) A autorelease]; 497*67e74705SXin Li} // expected-warning{{Object autoreleased too many times}} 498*67e74705SXin Li 499*67e74705SXin LiCFMutableArrayRef f13_autorelease_c() { 500*67e74705SXin Li CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); 501*67e74705SXin Li [(id) A autorelease]; 502*67e74705SXin Li [(id) A autorelease]; 503*67e74705SXin Li return A; // expected-warning{{Object autoreleased too many times}} 504*67e74705SXin Li} 505*67e74705SXin Li 506*67e74705SXin LiCFMutableArrayRef f13_autorelease_d() { 507*67e74705SXin Li CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); 508*67e74705SXin Li [(id) A autorelease]; 509*67e74705SXin Li [(id) A autorelease]; 510*67e74705SXin Li CFMutableArrayRef B = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{Object autoreleased too many times}} 511*67e74705SXin Li CFRelease(B); // no-warning 512*67e74705SXin Li while (1) {} 513*67e74705SXin Li} 514*67e74705SXin Li 515*67e74705SXin Li 516*67e74705SXin Li// This case exercises the logic where the leak site is the same as the allocation site. 517*67e74705SXin Livoid f14_leakimmediately() { 518*67e74705SXin Li CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}} 519*67e74705SXin Li} 520*67e74705SXin Li 521*67e74705SXin Li// Test that we track an allocated object beyond the point where the *name* 522*67e74705SXin Li// of the variable storing the reference is no longer live. 523*67e74705SXin Livoid f15() { 524*67e74705SXin Li // Create the array. 525*67e74705SXin Li CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); 526*67e74705SXin Li CFMutableArrayRef *B = &A; 527*67e74705SXin Li // At this point, the name 'A' is no longer live. 528*67e74705SXin Li CFRelease(*B); // no-warning 529*67e74705SXin Li} 530*67e74705SXin Li 531*67e74705SXin Li// Test when we pass NULL to CFRetain/CFRelease/CFMakeCollectable/CFAutorelease. 532*67e74705SXin Livoid f16(int x, CFTypeRef p) { 533*67e74705SXin Li if (p) 534*67e74705SXin Li return; 535*67e74705SXin Li 536*67e74705SXin Li switch (x) { 537*67e74705SXin Li case 0: 538*67e74705SXin Li CFRelease(p); 539*67e74705SXin Li break; 540*67e74705SXin Li case 1: 541*67e74705SXin Li CFRetain(p); 542*67e74705SXin Li break; 543*67e74705SXin Li case 2: 544*67e74705SXin Li CFMakeCollectable(p); 545*67e74705SXin Li break; 546*67e74705SXin Li case 3: 547*67e74705SXin Li CFAutorelease(p); 548*67e74705SXin Li break; 549*67e74705SXin Li default: 550*67e74705SXin Li break; 551*67e74705SXin Li } 552*67e74705SXin Li} 553*67e74705SXin Li 554*67e74705SXin Li// Test that an object is non-null after CFRetain/CFRelease/CFMakeCollectable/CFAutorelease. 555*67e74705SXin Livoid f17(int x, CFTypeRef p) { 556*67e74705SXin Li switch (x) { 557*67e74705SXin Li case 0: 558*67e74705SXin Li CFRelease(p); 559*67e74705SXin Li if (!p) 560*67e74705SXin Li CFRelease(0); // no-warning 561*67e74705SXin Li break; 562*67e74705SXin Li case 1: 563*67e74705SXin Li CFRetain(p); 564*67e74705SXin Li if (!p) 565*67e74705SXin Li CFRetain(0); // no-warning 566*67e74705SXin Li break; 567*67e74705SXin Li case 2: 568*67e74705SXin Li CFMakeCollectable(p); 569*67e74705SXin Li if (!p) 570*67e74705SXin Li CFMakeCollectable(0); // no-warning 571*67e74705SXin Li break; 572*67e74705SXin Li case 3: 573*67e74705SXin Li CFAutorelease(p); 574*67e74705SXin Li if (!p) 575*67e74705SXin Li CFAutorelease(0); // no-warning 576*67e74705SXin Li break; 577*67e74705SXin Li default: 578*67e74705SXin Li break; 579*67e74705SXin Li } 580*67e74705SXin Li} 581*67e74705SXin Li 582*67e74705SXin Li// Test basic tracking of ivars associated with 'self'. For the retain/release 583*67e74705SXin Li// checker we currently do not want to flag leaks associated with stores 584*67e74705SXin Li// of tracked objects to ivars. 585*67e74705SXin Li@interface SelfIvarTest : NSObject { 586*67e74705SXin Li id myObj; 587*67e74705SXin Li} 588*67e74705SXin Li- (void)test_self_tracking; 589*67e74705SXin Li@end 590*67e74705SXin Li 591*67e74705SXin Li@implementation SelfIvarTest 592*67e74705SXin Li- (void)test_self_tracking { 593*67e74705SXin Li myObj = (id) CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 594*67e74705SXin Li} 595*67e74705SXin Li@end 596*67e74705SXin Li 597*67e74705SXin Li// Test return of non-owned objects in contexts where an owned object 598*67e74705SXin Li// is expected. 599*67e74705SXin Li@interface TestReturnNotOwnedWhenExpectedOwned 600*67e74705SXin Li- (NSString*)newString; 601*67e74705SXin Li@end 602*67e74705SXin Li 603*67e74705SXin Li@implementation TestReturnNotOwnedWhenExpectedOwned 604*67e74705SXin Li- (NSString*)newString { 605*67e74705SXin Li NSString *s = [NSString stringWithUTF8String:"hello"]; 606*67e74705SXin Li return s; // expected-warning{{Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected}} 607*67e74705SXin Li} 608*67e74705SXin Li@end 609*67e74705SXin Li 610*67e74705SXin Li// <rdar://problem/6659160> 611*67e74705SXin Liint isFoo(char c); 612*67e74705SXin Li 613*67e74705SXin Listatic void rdar_6659160(char *inkind, char *inname) 614*67e74705SXin Li{ 615*67e74705SXin Li // We currently expect that [NSObject alloc] cannot fail. This 616*67e74705SXin Li // will be a toggled flag in the future. It can indeed return null, but 617*67e74705SXin Li // Cocoa programmers generally aren't expected to reason about out-of-memory 618*67e74705SXin Li // conditions. 619*67e74705SXin Li NSString *kind = [[NSString alloc] initWithUTF8String:inkind]; // expected-warning{{leak}} 620*67e74705SXin Li 621*67e74705SXin Li // We do allow stringWithUTF8String to fail. This isn't really correct, as 622*67e74705SXin Li // far as returning 0. In most error conditions it will throw an exception. 623*67e74705SXin Li // If allocation fails it could return 0, but again this 624*67e74705SXin Li // isn't expected. 625*67e74705SXin Li NSString *name = [NSString stringWithUTF8String:inname]; 626*67e74705SXin Li if(!name) 627*67e74705SXin Li return; 628*67e74705SXin Li 629*67e74705SXin Li const char *kindC = 0; 630*67e74705SXin Li const char *nameC = 0; 631*67e74705SXin Li 632*67e74705SXin Li // In both cases, we cannot reach a point down below where we 633*67e74705SXin Li // dereference kindC or nameC with either being null. This is because 634*67e74705SXin Li // we assume that [NSObject alloc] doesn't fail and that we have the guard 635*67e74705SXin Li // up above. 636*67e74705SXin Li 637*67e74705SXin Li if(kind) 638*67e74705SXin Li kindC = [kind UTF8String]; 639*67e74705SXin Li if(name) 640*67e74705SXin Li nameC = [name UTF8String]; 641*67e74705SXin Li if(!isFoo(kindC[0])) // expected-warning{{null}} 642*67e74705SXin Li return; 643*67e74705SXin Li if(!isFoo(nameC[0])) // no-warning 644*67e74705SXin Li return; 645*67e74705SXin Li 646*67e74705SXin Li [kind release]; 647*67e74705SXin Li [name release]; // expected-warning{{Incorrect decrement of the reference count}} 648*67e74705SXin Li} 649*67e74705SXin Li 650*67e74705SXin Li// PR 3677 - 'allocWithZone' should be treated as following the Cocoa naming 651*67e74705SXin Li// conventions with respect to 'return'ing ownership. 652*67e74705SXin Li@interface PR3677: NSObject @end 653*67e74705SXin Li@implementation PR3677 654*67e74705SXin Li+ (id)allocWithZone:(NSZone *)inZone { 655*67e74705SXin Li return [super allocWithZone:inZone]; // no-warning 656*67e74705SXin Li} 657*67e74705SXin Li@end 658*67e74705SXin Li 659*67e74705SXin Li// PR 3820 - Reason about calls to -dealloc 660*67e74705SXin Livoid pr3820_DeallocInsteadOfRelease(void) 661*67e74705SXin Li{ 662*67e74705SXin Li id foo = [[NSString alloc] init]; // no-warning 663*67e74705SXin Li [foo dealloc]; 664*67e74705SXin Li // foo is not leaked, since it has been deallocated. 665*67e74705SXin Li} 666*67e74705SXin Li 667*67e74705SXin Livoid pr3820_ReleaseAfterDealloc(void) 668*67e74705SXin Li{ 669*67e74705SXin Li id foo = [[NSString alloc] init]; 670*67e74705SXin Li [foo dealloc]; 671*67e74705SXin Li [foo release]; // expected-warning{{used after it is release}} 672*67e74705SXin Li // NSInternalInconsistencyException: message sent to deallocated object 673*67e74705SXin Li} 674*67e74705SXin Li 675*67e74705SXin Livoid pr3820_DeallocAfterRelease(void) 676*67e74705SXin Li{ 677*67e74705SXin Li NSLog(@"\n\n[%s]", __FUNCTION__); 678*67e74705SXin Li id foo = [[NSString alloc] init]; 679*67e74705SXin Li [foo release]; 680*67e74705SXin Li [foo dealloc]; // expected-warning{{used after it is released}} 681*67e74705SXin Li // message sent to released object 682*67e74705SXin Li} 683*67e74705SXin Li 684*67e74705SXin Li// From <rdar://problem/6704930>. The problem here is that 'length' binds to 685*67e74705SXin Li// '($0 - 1)' after '--length', but SimpleConstraintManager doesn't know how to 686*67e74705SXin Li// reason about '($0 - 1) > constant'. As a temporary hack, we drop the value 687*67e74705SXin Li// of '($0 - 1)' and conjure a new symbol. 688*67e74705SXin Livoid rdar6704930(unsigned char *s, unsigned int length) { 689*67e74705SXin Li NSString* name = 0; 690*67e74705SXin Li if (s != 0) { 691*67e74705SXin Li if (length > 0) { 692*67e74705SXin Li while (length > 0) { 693*67e74705SXin Li if (*s == ':') { 694*67e74705SXin Li ++s; 695*67e74705SXin Li --length; 696*67e74705SXin Li name = [[NSString alloc] init]; // no-warning 697*67e74705SXin Li break; 698*67e74705SXin Li } 699*67e74705SXin Li ++s; 700*67e74705SXin Li --length; 701*67e74705SXin Li } 702*67e74705SXin Li if ((length == 0) && (name != 0)) { 703*67e74705SXin Li [name release]; 704*67e74705SXin Li name = 0; 705*67e74705SXin Li } 706*67e74705SXin Li if (length == 0) { // no ':' found -> use it all as name 707*67e74705SXin Li name = [[NSString alloc] init]; // no-warning 708*67e74705SXin Li } 709*67e74705SXin Li } 710*67e74705SXin Li } 711*67e74705SXin Li 712*67e74705SXin Li if (name != 0) { 713*67e74705SXin Li [name release]; 714*67e74705SXin Li } 715*67e74705SXin Li} 716*67e74705SXin Li 717*67e74705SXin Li//===----------------------------------------------------------------------===// 718*67e74705SXin Li// <rdar://problem/6833332> 719*67e74705SXin Li// One build of the analyzer accidentally stopped tracking the allocated 720*67e74705SXin Li// object after the 'retain'. 721*67e74705SXin Li//===----------------------------------------------------------------------===// 722*67e74705SXin Li 723*67e74705SXin Li@interface rdar_6833332 : NSObject <NSApplicationDelegate> { 724*67e74705SXin Li NSWindow *window; 725*67e74705SXin Li} 726*67e74705SXin Li@property (nonatomic, retain) NSWindow *window; 727*67e74705SXin Li@end 728*67e74705SXin Li 729*67e74705SXin Li@implementation rdar_6833332 730*67e74705SXin Li@synthesize window; 731*67e74705SXin Li- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 732*67e74705SXin Li NSMutableDictionary *dict = [[NSMutableDictionary dictionaryWithCapacity:4] retain]; // expected-warning{{leak}} 733*67e74705SXin Li 734*67e74705SXin Li [dict setObject:@"foo" forKey:@"bar"]; 735*67e74705SXin Li 736*67e74705SXin Li NSLog(@"%@", dict); 737*67e74705SXin Li} 738*67e74705SXin Li- (void)dealloc { 739*67e74705SXin Li [window release]; 740*67e74705SXin Li [super dealloc]; 741*67e74705SXin Li} 742*67e74705SXin Li 743*67e74705SXin Li- (void)radar10102244 { 744*67e74705SXin Li NSMutableDictionary *dict = [[NSMutableDictionary dictionaryWithCapacity:4] retain]; // expected-warning{{leak}} 745*67e74705SXin Li if (window) 746*67e74705SXin Li NSLog(@"%@", window); 747*67e74705SXin Li} 748*67e74705SXin Li@end 749*67e74705SXin Li 750*67e74705SXin Li//===----------------------------------------------------------------------===// 751*67e74705SXin Li// <rdar://problem/6257780> clang checker fails to catch use-after-release 752*67e74705SXin Li//===----------------------------------------------------------------------===// 753*67e74705SXin Li 754*67e74705SXin Liint rdar_6257780_Case1() { 755*67e74705SXin Li NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 756*67e74705SXin Li NSArray *array = [NSArray array]; 757*67e74705SXin Li [array release]; // expected-warning{{Incorrect decrement of the reference count of an object that is not owned at this point by the caller}} 758*67e74705SXin Li [pool drain]; 759*67e74705SXin Li return 0; 760*67e74705SXin Li} 761*67e74705SXin Li 762*67e74705SXin Li//===----------------------------------------------------------------------===// 763*67e74705SXin Li// <rdar://problem/10640253> Analyzer is confused about NSAutoreleasePool -allocWithZone:. 764*67e74705SXin Li//===----------------------------------------------------------------------===// 765*67e74705SXin Li 766*67e74705SXin Livoid rdar_10640253_autorelease_allocWithZone() { 767*67e74705SXin Li NSAutoreleasePool *pool = [[NSAutoreleasePool allocWithZone:(NSZone*)0] init]; 768*67e74705SXin Li (void) pool; 769*67e74705SXin Li} 770*67e74705SXin Li 771*67e74705SXin Li//===----------------------------------------------------------------------===// 772*67e74705SXin Li// <rdar://problem/6866843> Checker should understand new/setObject:/release constructs 773*67e74705SXin Li//===----------------------------------------------------------------------===// 774*67e74705SXin Li 775*67e74705SXin Livoid rdar_6866843() { 776*67e74705SXin Li NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 777*67e74705SXin Li NSMutableDictionary* dictionary = [[NSMutableDictionary alloc] init]; 778*67e74705SXin Li NSArray* array = [[NSArray alloc] init]; 779*67e74705SXin Li [dictionary setObject:array forKey:@"key"]; 780*67e74705SXin Li [array release]; 781*67e74705SXin Li // Using 'array' here should be fine 782*67e74705SXin Li NSLog(@"array = %@\n", array); // no-warning 783*67e74705SXin Li // Now the array is released 784*67e74705SXin Li [dictionary release]; 785*67e74705SXin Li [pool drain]; 786*67e74705SXin Li} 787*67e74705SXin Li 788*67e74705SXin Li 789*67e74705SXin Li//===----------------------------------------------------------------------===// 790*67e74705SXin Li// <rdar://problem/6877235> Classes typedef-ed to CF objects should get the same treatment as CF objects 791*67e74705SXin Li//===----------------------------------------------------------------------===// 792*67e74705SXin Li 793*67e74705SXin Litypedef CFTypeRef OtherRef; 794*67e74705SXin Li 795*67e74705SXin Li@interface RDar6877235 : NSObject {} 796*67e74705SXin Li- (CFTypeRef)_copyCFTypeRef; 797*67e74705SXin Li- (OtherRef)_copyOtherRef; 798*67e74705SXin Li@end 799*67e74705SXin Li 800*67e74705SXin Li@implementation RDar6877235 801*67e74705SXin Li- (CFTypeRef)_copyCFTypeRef { 802*67e74705SXin Li return [[NSString alloc] init]; // no-warning 803*67e74705SXin Li} 804*67e74705SXin Li- (OtherRef)_copyOtherRef { 805*67e74705SXin Li return [[NSString alloc] init]; // no-warning 806*67e74705SXin Li} 807*67e74705SXin Li@end 808*67e74705SXin Li 809*67e74705SXin Li//===----------------------------------------------------------------------===// 810*67e74705SXin Li// <rdar://problem/6320065> false positive - init method returns an object 811*67e74705SXin Li// owned by caller 812*67e74705SXin Li//===----------------------------------------------------------------------===// 813*67e74705SXin Li 814*67e74705SXin Li@interface RDar6320065 : NSObject { 815*67e74705SXin Li NSString *_foo; 816*67e74705SXin Li} 817*67e74705SXin Li- (id)initReturningNewClass; 818*67e74705SXin Li- (id)_initReturningNewClassBad; 819*67e74705SXin Li- (id)initReturningNewClassBad2; 820*67e74705SXin Li@end 821*67e74705SXin Li 822*67e74705SXin Li@interface RDar6320065Subclass : RDar6320065 823*67e74705SXin Li@end 824*67e74705SXin Li 825*67e74705SXin Li@implementation RDar6320065 826*67e74705SXin Li- (id)initReturningNewClass { 827*67e74705SXin Li [self release]; 828*67e74705SXin Li self = [[RDar6320065Subclass alloc] init]; // no-warning 829*67e74705SXin Li return self; 830*67e74705SXin Li} 831*67e74705SXin Li- (id)_initReturningNewClassBad { 832*67e74705SXin Li [self release]; 833*67e74705SXin Li [[RDar6320065Subclass alloc] init]; // expected-warning {{leak}} 834*67e74705SXin Li return self; 835*67e74705SXin Li} 836*67e74705SXin Li- (id)initReturningNewClassBad2 { 837*67e74705SXin Li [self release]; 838*67e74705SXin Li self = [[RDar6320065Subclass alloc] init]; 839*67e74705SXin Li return [self autorelease]; // expected-warning{{Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected}} 840*67e74705SXin Li} 841*67e74705SXin Li 842*67e74705SXin Li@end 843*67e74705SXin Li 844*67e74705SXin Li@implementation RDar6320065Subclass 845*67e74705SXin Li@end 846*67e74705SXin Li 847*67e74705SXin Liint RDar6320065_test() { 848*67e74705SXin Li RDar6320065 *test = [[RDar6320065 alloc] init]; // no-warning 849*67e74705SXin Li [test release]; 850*67e74705SXin Li return 0; 851*67e74705SXin Li} 852*67e74705SXin Li 853*67e74705SXin Li//===----------------------------------------------------------------------===// 854*67e74705SXin Li// <rdar://problem/7129086> -awakeAfterUsingCoder: returns an owned object 855*67e74705SXin Li// and claims the receiver 856*67e74705SXin Li//===----------------------------------------------------------------------===// 857*67e74705SXin Li 858*67e74705SXin Li@interface RDar7129086 : NSObject {} @end 859*67e74705SXin Li@implementation RDar7129086 860*67e74705SXin Li- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder { 861*67e74705SXin Li [self release]; // no-warning 862*67e74705SXin Li return [NSString alloc]; // no-warning 863*67e74705SXin Li} 864*67e74705SXin Li@end 865*67e74705SXin Li 866*67e74705SXin Li//===----------------------------------------------------------------------===// 867*67e74705SXin Li// <rdar://problem/6859457> [NSData dataWithBytesNoCopy] does not return a 868*67e74705SXin Li// retained object 869*67e74705SXin Li//===----------------------------------------------------------------------===// 870*67e74705SXin Li 871*67e74705SXin Li@interface RDar6859457 : NSObject {} 872*67e74705SXin Li- (NSString*) NoCopyString; 873*67e74705SXin Li- (NSString*) noCopyString; 874*67e74705SXin Li@end 875*67e74705SXin Li 876*67e74705SXin Li@implementation RDar6859457 877*67e74705SXin Li- (NSString*) NoCopyString { return [[NSString alloc] init]; } // expected-warning{{leak}} 878*67e74705SXin Li- (NSString*) noCopyString { return [[NSString alloc] init]; } // expected-warning{{leak}} 879*67e74705SXin Li@end 880*67e74705SXin Li 881*67e74705SXin Livoid test_RDar6859457(RDar6859457 *x, void *bytes, NSUInteger dataLength) { 882*67e74705SXin Li [x NoCopyString]; // expected-warning{{leak}} 883*67e74705SXin Li [x noCopyString]; // expected-warning{{leak}} 884*67e74705SXin Li [NSData dataWithBytesNoCopy:bytes length:dataLength]; // no-warning 885*67e74705SXin Li [NSData dataWithBytesNoCopy:bytes length:dataLength freeWhenDone:1]; // no-warning 886*67e74705SXin Li} 887*67e74705SXin Li 888*67e74705SXin Li//===----------------------------------------------------------------------===// 889*67e74705SXin Li// PR 4230 - an autorelease pool is not necessarily leaked during a premature 890*67e74705SXin Li// return 891*67e74705SXin Li//===----------------------------------------------------------------------===// 892*67e74705SXin Li 893*67e74705SXin Listatic void PR4230(void) 894*67e74705SXin Li{ 895*67e74705SXin Li NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // no-warning 896*67e74705SXin Li NSString *object = [[[NSString alloc] init] autorelease]; // no-warning 897*67e74705SXin Li return; 898*67e74705SXin Li} 899*67e74705SXin Li 900*67e74705SXin Listatic void PR4230_new(void) 901*67e74705SXin Li{ 902*67e74705SXin Li NSAutoreleasePool *pool = [NSAutoreleasePool new]; // no-warning 903*67e74705SXin Li NSString *object = [[[NSString alloc] init] autorelease]; // no-warning 904*67e74705SXin Li return; 905*67e74705SXin Li} 906*67e74705SXin Li 907*67e74705SXin Li//===----------------------------------------------------------------------===// 908*67e74705SXin Li// Method name that has a null IdentifierInfo* for its first selector slot. 909*67e74705SXin Li// This test just makes sure that we handle it. 910*67e74705SXin Li//===----------------------------------------------------------------------===// 911*67e74705SXin Li 912*67e74705SXin Li@interface TestNullIdentifier 913*67e74705SXin Li@end 914*67e74705SXin Li 915*67e74705SXin Li@implementation TestNullIdentifier 916*67e74705SXin Li+ (id):(int)x, ... { 917*67e74705SXin Li return [[NSString alloc] init]; // expected-warning{{leak}} 918*67e74705SXin Li} 919*67e74705SXin Li@end 920*67e74705SXin Li 921*67e74705SXin Li//===----------------------------------------------------------------------===// 922*67e74705SXin Li// <rdar://problem/6893565> don't flag leaks for return types that cannot be 923*67e74705SXin Li// determined to be CF types 924*67e74705SXin Li//===----------------------------------------------------------------------===// 925*67e74705SXin Li 926*67e74705SXin Li// We don't know if 'struct s6893565' represents a Core Foundation type, so 927*67e74705SXin Li// we shouldn't emit an error here. 928*67e74705SXin Litypedef struct s6893565* TD6893565; 929*67e74705SXin Li 930*67e74705SXin Li@interface RDar6893565 {} 931*67e74705SXin Li-(TD6893565)newThing; 932*67e74705SXin Li@end 933*67e74705SXin Li 934*67e74705SXin Li@implementation RDar6893565 935*67e74705SXin Li-(TD6893565)newThing { 936*67e74705SXin Li return (TD6893565) [[NSString alloc] init]; // no-warning 937*67e74705SXin Li} 938*67e74705SXin Li@end 939*67e74705SXin Li 940*67e74705SXin Li//===----------------------------------------------------------------------===// 941*67e74705SXin Li// <rdar://problem/6902710> clang: false positives w/QC and CoreImage methods 942*67e74705SXin Li//===----------------------------------------------------------------------===// 943*67e74705SXin Li 944*67e74705SXin Livoid rdar6902710(QCView *view, QCRenderer *renderer, CIContext *context, 945*67e74705SXin Li NSString *str, CIImage *img, CGRect rect, 946*67e74705SXin Li CIFormat form, CGColorSpaceRef cs) { 947*67e74705SXin Li [view createSnapshotImageOfType:str]; // expected-warning{{leak}} 948*67e74705SXin Li [renderer createSnapshotImageOfType:str]; // expected-warning{{leak}} 949*67e74705SXin Li [context createCGImage:img fromRect:rect]; // expected-warning{{leak}} 950*67e74705SXin Li [context createCGImage:img fromRect:rect format:form colorSpace:cs]; // expected-warning{{leak}} 951*67e74705SXin Li} 952*67e74705SXin Li 953*67e74705SXin Li//===----------------------------------------------------------------------===// 954*67e74705SXin Li// <rdar://problem/6945561> -[CIContext createCGLayerWithSize:info:] 955*67e74705SXin Li// misinterpreted by clang scan-build 956*67e74705SXin Li//===----------------------------------------------------------------------===// 957*67e74705SXin Li 958*67e74705SXin Livoid rdar6945561(CIContext *context, CGSize size, CFDictionaryRef d) { 959*67e74705SXin Li [context createCGLayerWithSize:size info:d]; // expected-warning{{leak}} 960*67e74705SXin Li} 961*67e74705SXin Li 962*67e74705SXin Li//===----------------------------------------------------------------------===// 963*67e74705SXin Li// <rdar://problem/6961230> add knowledge of IOKit functions to retain/release 964*67e74705SXin Li// checker 965*67e74705SXin Li//===----------------------------------------------------------------------===// 966*67e74705SXin Li 967*67e74705SXin Livoid IOBSDNameMatching_wrapper(mach_port_t masterPort, uint32_t options, const char * bsdName) { 968*67e74705SXin Li IOBSDNameMatching(masterPort, options, bsdName); // expected-warning{{leak}} 969*67e74705SXin Li} 970*67e74705SXin Li 971*67e74705SXin Livoid IOServiceMatching_wrapper(const char * name) { 972*67e74705SXin Li IOServiceMatching(name); // expected-warning{{leak}} 973*67e74705SXin Li} 974*67e74705SXin Li 975*67e74705SXin Livoid IOServiceNameMatching_wrapper(const char * name) { 976*67e74705SXin Li IOServiceNameMatching(name); // expected-warning{{leak}} 977*67e74705SXin Li} 978*67e74705SXin Li 979*67e74705SXin LiCF_RETURNS_RETAINED CFDictionaryRef CreateDict(); 980*67e74705SXin Li 981*67e74705SXin Livoid IOServiceAddNotification_wrapper(mach_port_t masterPort, const io_name_t notificationType, 982*67e74705SXin Li mach_port_t wakePort, uintptr_t reference, io_iterator_t * notification ) { 983*67e74705SXin Li 984*67e74705SXin Li CFDictionaryRef matching = CreateDict(); 985*67e74705SXin Li CFRelease(matching); 986*67e74705SXin Li IOServiceAddNotification(masterPort, notificationType, matching, // expected-warning{{used after it is released}} expected-warning{{deprecated}} 987*67e74705SXin Li wakePort, reference, notification); 988*67e74705SXin Li} 989*67e74705SXin Li 990*67e74705SXin Livoid IORegistryEntryIDMatching_wrapper(uint64_t entryID ) { 991*67e74705SXin Li IORegistryEntryIDMatching(entryID); // expected-warning{{leak}} 992*67e74705SXin Li} 993*67e74705SXin Li 994*67e74705SXin Livoid IOOpenFirmwarePathMatching_wrapper(mach_port_t masterPort, uint32_t options, 995*67e74705SXin Li const char * path) { 996*67e74705SXin Li IOOpenFirmwarePathMatching(masterPort, options, path); // expected-warning{{leak}} 997*67e74705SXin Li} 998*67e74705SXin Li 999*67e74705SXin Livoid IOServiceGetMatchingService_wrapper(mach_port_t masterPort) { 1000*67e74705SXin Li CFDictionaryRef matching = CreateDict(); 1001*67e74705SXin Li IOServiceGetMatchingService(masterPort, matching); 1002*67e74705SXin Li CFRelease(matching); // expected-warning{{used after it is released}} 1003*67e74705SXin Li} 1004*67e74705SXin Li 1005*67e74705SXin Livoid IOServiceGetMatchingServices_wrapper(mach_port_t masterPort, io_iterator_t *existing) { 1006*67e74705SXin Li CFDictionaryRef matching = CreateDict(); 1007*67e74705SXin Li IOServiceGetMatchingServices(masterPort, matching, existing); 1008*67e74705SXin Li CFRelease(matching); // expected-warning{{used after it is released}} 1009*67e74705SXin Li} 1010*67e74705SXin Li 1011*67e74705SXin Livoid IOServiceAddMatchingNotification_wrapper(IONotificationPortRef notifyPort, const io_name_t notificationType, 1012*67e74705SXin Li IOServiceMatchingCallback callback, void * refCon, io_iterator_t * notification) { 1013*67e74705SXin Li 1014*67e74705SXin Li CFDictionaryRef matching = CreateDict(); 1015*67e74705SXin Li IOServiceAddMatchingNotification(notifyPort, notificationType, matching, callback, refCon, notification); 1016*67e74705SXin Li CFRelease(matching); // expected-warning{{used after it is released}} 1017*67e74705SXin Li} 1018*67e74705SXin Li 1019*67e74705SXin Li//===----------------------------------------------------------------------===// 1020*67e74705SXin Li// Test of handling objects whose references "escape" to containers. 1021*67e74705SXin Li//===----------------------------------------------------------------------===// 1022*67e74705SXin Li 1023*67e74705SXin Livoid CFDictionaryAddValue(CFMutableDictionaryRef, void *, void *); 1024*67e74705SXin Li 1025*67e74705SXin Li// <rdar://problem/6539791> 1026*67e74705SXin Livoid rdar_6539791(CFMutableDictionaryRef y, void* key, void* val_key) { 1027*67e74705SXin Li CFMutableDictionaryRef x = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); 1028*67e74705SXin Li CFDictionaryAddValue(y, key, x); 1029*67e74705SXin Li CFRelease(x); // the dictionary keeps a reference, so the object isn't deallocated yet 1030*67e74705SXin Li signed z = 1; 1031*67e74705SXin Li CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); 1032*67e74705SXin Li if (value) { 1033*67e74705SXin Li CFDictionaryAddValue(x, val_key, (void*)value); // no-warning 1034*67e74705SXin Li CFRelease(value); 1035*67e74705SXin Li CFDictionaryAddValue(y, val_key, (void*)value); // no-warning 1036*67e74705SXin Li } 1037*67e74705SXin Li} 1038*67e74705SXin Li 1039*67e74705SXin Li// <rdar://problem/6560661> 1040*67e74705SXin Li// Same issue, except with "AppendValue" functions. 1041*67e74705SXin Livoid rdar_6560661(CFMutableArrayRef x) { 1042*67e74705SXin Li signed z = 1; 1043*67e74705SXin Li CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); 1044*67e74705SXin Li // CFArrayAppendValue keeps a reference to value. 1045*67e74705SXin Li CFArrayAppendValue(x, value); 1046*67e74705SXin Li CFRelease(value); 1047*67e74705SXin Li CFRetain(value); 1048*67e74705SXin Li CFRelease(value); // no-warning 1049*67e74705SXin Li} 1050*67e74705SXin Li 1051*67e74705SXin Li// <rdar://problem/7152619> 1052*67e74705SXin Li// Same issue, excwept with "CFAttributeStringSetAttribute". 1053*67e74705SXin Livoid rdar_7152619(CFStringRef str) { 1054*67e74705SXin Li CFAttributedStringRef string = CFAttributedStringCreate(kCFAllocatorDefault, str, 0); 1055*67e74705SXin Li CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutableCopy(kCFAllocatorDefault, 100, string); 1056*67e74705SXin Li CFRelease(string); 1057*67e74705SXin Li NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}} 1058*67e74705SXin Li CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 1), str, number); 1059*67e74705SXin Li [number release]; 1060*67e74705SXin Li [number retain]; 1061*67e74705SXin Li CFRelease(attrString); 1062*67e74705SXin Li} 1063*67e74705SXin Li 1064*67e74705SXin Li//===----------------------------------------------------------------------===// 1065*67e74705SXin Li// Test of handling CGGradientXXX functions. 1066*67e74705SXin Li//===----------------------------------------------------------------------===// 1067*67e74705SXin Li 1068*67e74705SXin Livoid rdar_7184450(CGContextRef myContext, CGFloat x, CGPoint myStartPoint, 1069*67e74705SXin Li CGPoint myEndPoint) { 1070*67e74705SXin Li size_t num_locations = 6; 1071*67e74705SXin Li CGFloat locations[6] = { 0.0, 0.265, 0.28, 0.31, 0.36, 1.0 }; 1072*67e74705SXin Li CGFloat components[28] = { 239.0/256.0, 167.0/256.0, 170.0/256.0, 1073*67e74705SXin Li x, // Start color 1074*67e74705SXin Li 207.0/255.0, 39.0/255.0, 39.0/255.0, x, 1075*67e74705SXin Li 147.0/255.0, 21.0/255.0, 22.0/255.0, x, 1076*67e74705SXin Li 175.0/255.0, 175.0/255.0, 175.0/255.0, x, 1077*67e74705SXin Li 255.0/255.0,255.0/255.0, 255.0/255.0, x, 1078*67e74705SXin Li 255.0/255.0,255.0/255.0, 255.0/255.0, x 1079*67e74705SXin Li }; // End color 1080*67e74705SXin Li 1081*67e74705SXin Li CGGradientRef myGradient = 1082*67e74705SXin Li CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), // expected-warning{{leak}} 1083*67e74705SXin Li components, locations, num_locations); 1084*67e74705SXin Li 1085*67e74705SXin Li CGContextDrawLinearGradient(myContext, myGradient, myStartPoint, myEndPoint, 1086*67e74705SXin Li 0); 1087*67e74705SXin Li CGGradientRelease(myGradient); 1088*67e74705SXin Li} 1089*67e74705SXin Li 1090*67e74705SXin Livoid rdar_7184450_pos(CGContextRef myContext, CGFloat x, CGPoint myStartPoint, 1091*67e74705SXin Li CGPoint myEndPoint) { 1092*67e74705SXin Li size_t num_locations = 6; 1093*67e74705SXin Li CGFloat locations[6] = { 0.0, 0.265, 0.28, 0.31, 0.36, 1.0 }; 1094*67e74705SXin Li CGFloat components[28] = { 239.0/256.0, 167.0/256.0, 170.0/256.0, 1095*67e74705SXin Li x, // Start color 1096*67e74705SXin Li 207.0/255.0, 39.0/255.0, 39.0/255.0, x, 1097*67e74705SXin Li 147.0/255.0, 21.0/255.0, 22.0/255.0, x, 1098*67e74705SXin Li 175.0/255.0, 175.0/255.0, 175.0/255.0, x, 1099*67e74705SXin Li 255.0/255.0,255.0/255.0, 255.0/255.0, x, 1100*67e74705SXin Li 255.0/255.0,255.0/255.0, 255.0/255.0, x 1101*67e74705SXin Li }; // End color 1102*67e74705SXin Li 1103*67e74705SXin Li CGGradientRef myGradient = 1104*67e74705SXin Li CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), components, locations, num_locations); // expected-warning 2 {{leak}} 1105*67e74705SXin Li 1106*67e74705SXin Li CGContextDrawLinearGradient(myContext, myGradient, myStartPoint, myEndPoint, 1107*67e74705SXin Li 0); 1108*67e74705SXin Li} 1109*67e74705SXin Li 1110*67e74705SXin Li//===----------------------------------------------------------------------===// 1111*67e74705SXin Li// <rdar://problem/7299394> clang false positive: retained instance passed to 1112*67e74705SXin Li// thread in pthread_create marked as leak 1113*67e74705SXin Li// 1114*67e74705SXin Li// Until we have full IPA, the analyzer should stop tracking the reference 1115*67e74705SXin Li// count of objects passed to pthread_create. 1116*67e74705SXin Li// 1117*67e74705SXin Li//===----------------------------------------------------------------------===// 1118*67e74705SXin Li 1119*67e74705SXin Listruct _opaque_pthread_t {}; 1120*67e74705SXin Listruct _opaque_pthread_attr_t {}; 1121*67e74705SXin Litypedef struct _opaque_pthread_t *__darwin_pthread_t; 1122*67e74705SXin Litypedef struct _opaque_pthread_attr_t __darwin_pthread_attr_t; 1123*67e74705SXin Litypedef __darwin_pthread_t pthread_t; 1124*67e74705SXin Litypedef __darwin_pthread_attr_t pthread_attr_t; 1125*67e74705SXin Litypedef unsigned long __darwin_pthread_key_t; 1126*67e74705SXin Litypedef __darwin_pthread_key_t pthread_key_t; 1127*67e74705SXin Li 1128*67e74705SXin Liint pthread_create(pthread_t *, const pthread_attr_t *, 1129*67e74705SXin Li void *(*)(void *), void *); 1130*67e74705SXin Li 1131*67e74705SXin Liint pthread_setspecific(pthread_key_t key, const void *value); 1132*67e74705SXin Li 1133*67e74705SXin Livoid *rdar_7299394_start_routine(void *p) { 1134*67e74705SXin Li [((id) p) release]; 1135*67e74705SXin Li return 0; 1136*67e74705SXin Li} 1137*67e74705SXin Livoid rdar_7299394(pthread_attr_t *attr, pthread_t *thread, void *args) { 1138*67e74705SXin Li NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning 1139*67e74705SXin Li pthread_create(thread, attr, rdar_7299394_start_routine, number); 1140*67e74705SXin Li} 1141*67e74705SXin Livoid rdar_7299394_positive(pthread_attr_t *attr, pthread_t *thread) { 1142*67e74705SXin Li NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}} 1143*67e74705SXin Li} 1144*67e74705SXin Li 1145*67e74705SXin Li//===----------------------------------------------------------------------===// 1146*67e74705SXin Li// <rdar://problem/11282706> false positive with not understanding thread 1147*67e74705SXin Li// local storage 1148*67e74705SXin Li//===----------------------------------------------------------------------===// 1149*67e74705SXin Li 1150*67e74705SXin Livoid rdar11282706(pthread_key_t key) { 1151*67e74705SXin Li NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning 1152*67e74705SXin Li pthread_setspecific(key, (void*) number); 1153*67e74705SXin Li} 1154*67e74705SXin Li 1155*67e74705SXin Li//===----------------------------------------------------------------------===// 1156*67e74705SXin Li// <rdar://problem/7283567> False leak associated with call to 1157*67e74705SXin Li// CVPixelBufferCreateWithBytes () 1158*67e74705SXin Li// 1159*67e74705SXin Li// According to the Core Video Reference (ADC), CVPixelBufferCreateWithBytes and 1160*67e74705SXin Li// CVPixelBufferCreateWithPlanarBytes can release (via a callback) the 1161*67e74705SXin Li// pixel buffer object. These test cases show how the analyzer stops tracking 1162*67e74705SXin Li// the reference count for the objects passed for this argument. This 1163*67e74705SXin Li// could be made smarter. 1164*67e74705SXin Li//===----------------------------------------------------------------------===// 1165*67e74705SXin Li 1166*67e74705SXin Litypedef int int32_t; 1167*67e74705SXin Litypedef UInt32 FourCharCode; 1168*67e74705SXin Litypedef FourCharCode OSType; 1169*67e74705SXin Litypedef uint64_t CVOptionFlags; 1170*67e74705SXin Litypedef int32_t CVReturn; 1171*67e74705SXin Litypedef struct __CVBuffer *CVBufferRef; 1172*67e74705SXin Litypedef CVBufferRef CVImageBufferRef; 1173*67e74705SXin Litypedef CVImageBufferRef CVPixelBufferRef; 1174*67e74705SXin Litypedef void (*CVPixelBufferReleaseBytesCallback)( void *releaseRefCon, const void *baseAddress ); 1175*67e74705SXin Li 1176*67e74705SXin Liextern CVReturn CVPixelBufferCreateWithBytes(CFAllocatorRef allocator, 1177*67e74705SXin Li size_t width, 1178*67e74705SXin Li size_t height, 1179*67e74705SXin Li OSType pixelFormatType, 1180*67e74705SXin Li void *baseAddress, 1181*67e74705SXin Li size_t bytesPerRow, 1182*67e74705SXin Li CVPixelBufferReleaseBytesCallback releaseCallback, 1183*67e74705SXin Li void *releaseRefCon, 1184*67e74705SXin Li CFDictionaryRef pixelBufferAttributes, 1185*67e74705SXin Li CVPixelBufferRef *pixelBufferOut) ; 1186*67e74705SXin Li 1187*67e74705SXin Litypedef void (*CVPixelBufferReleasePlanarBytesCallback)( void *releaseRefCon, const void *dataPtr, size_t dataSize, size_t numberOfPlanes, const void *planeAddresses[] ); 1188*67e74705SXin Li 1189*67e74705SXin Liextern CVReturn CVPixelBufferCreateWithPlanarBytes(CFAllocatorRef allocator, 1190*67e74705SXin Li size_t width, 1191*67e74705SXin Li size_t height, 1192*67e74705SXin Li OSType pixelFormatType, 1193*67e74705SXin Li void *dataPtr, 1194*67e74705SXin Li size_t dataSize, 1195*67e74705SXin Li size_t numberOfPlanes, 1196*67e74705SXin Li void *planeBaseAddress[], 1197*67e74705SXin Li size_t planeWidth[], 1198*67e74705SXin Li size_t planeHeight[], 1199*67e74705SXin Li size_t planeBytesPerRow[], 1200*67e74705SXin Li CVPixelBufferReleasePlanarBytesCallback releaseCallback, 1201*67e74705SXin Li void *releaseRefCon, 1202*67e74705SXin Li CFDictionaryRef pixelBufferAttributes, 1203*67e74705SXin Li CVPixelBufferRef *pixelBufferOut) ; 1204*67e74705SXin Li 1205*67e74705SXin Liextern CVReturn CVPixelBufferCreateWithBytes(CFAllocatorRef allocator, 1206*67e74705SXin Li size_t width, 1207*67e74705SXin Li size_t height, 1208*67e74705SXin Li OSType pixelFormatType, 1209*67e74705SXin Li void *baseAddress, 1210*67e74705SXin Li size_t bytesPerRow, 1211*67e74705SXin Li CVPixelBufferReleaseBytesCallback releaseCallback, 1212*67e74705SXin Li void *releaseRefCon, 1213*67e74705SXin Li CFDictionaryRef pixelBufferAttributes, 1214*67e74705SXin Li CVPixelBufferRef *pixelBufferOut) ; 1215*67e74705SXin Li 1216*67e74705SXin LiCVReturn rdar_7283567(CFAllocatorRef allocator, size_t width, size_t height, 1217*67e74705SXin Li OSType pixelFormatType, void *baseAddress, 1218*67e74705SXin Li size_t bytesPerRow, 1219*67e74705SXin Li CVPixelBufferReleaseBytesCallback releaseCallback, 1220*67e74705SXin Li CFDictionaryRef pixelBufferAttributes, 1221*67e74705SXin Li CVPixelBufferRef *pixelBufferOut) { 1222*67e74705SXin Li 1223*67e74705SXin Li // For the allocated object, it doesn't really matter what type it is 1224*67e74705SXin Li // for the purpose of this test. All we want to show is that 1225*67e74705SXin Li // this is freed later by the callback. 1226*67e74705SXin Li NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning 1227*67e74705SXin Li 1228*67e74705SXin Li return CVPixelBufferCreateWithBytes(allocator, width, height, pixelFormatType, 1229*67e74705SXin Li baseAddress, bytesPerRow, releaseCallback, 1230*67e74705SXin Li number, // potentially released by callback 1231*67e74705SXin Li pixelBufferAttributes, pixelBufferOut) ; 1232*67e74705SXin Li} 1233*67e74705SXin Li 1234*67e74705SXin LiCVReturn rdar_7283567_2(CFAllocatorRef allocator, size_t width, size_t height, 1235*67e74705SXin Li OSType pixelFormatType, void *dataPtr, size_t dataSize, 1236*67e74705SXin Li size_t numberOfPlanes, void *planeBaseAddress[], 1237*67e74705SXin Li size_t planeWidth[], size_t planeHeight[], size_t planeBytesPerRow[], 1238*67e74705SXin Li CVPixelBufferReleasePlanarBytesCallback releaseCallback, 1239*67e74705SXin Li CFDictionaryRef pixelBufferAttributes, 1240*67e74705SXin Li CVPixelBufferRef *pixelBufferOut) { 1241*67e74705SXin Li 1242*67e74705SXin Li // For the allocated object, it doesn't really matter what type it is 1243*67e74705SXin Li // for the purpose of this test. All we want to show is that 1244*67e74705SXin Li // this is freed later by the callback. 1245*67e74705SXin Li NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning 1246*67e74705SXin Li 1247*67e74705SXin Li return CVPixelBufferCreateWithPlanarBytes(allocator, 1248*67e74705SXin Li width, height, pixelFormatType, dataPtr, dataSize, 1249*67e74705SXin Li numberOfPlanes, planeBaseAddress, planeWidth, 1250*67e74705SXin Li planeHeight, planeBytesPerRow, releaseCallback, 1251*67e74705SXin Li number, // potentially released by callback 1252*67e74705SXin Li pixelBufferAttributes, pixelBufferOut) ; 1253*67e74705SXin Li} 1254*67e74705SXin Li 1255*67e74705SXin Li//===----------------------------------------------------------------------===// 1256*67e74705SXin Li// <rdar://problem/7358899> False leak associated with 1257*67e74705SXin Li// CGBitmapContextCreateWithData 1258*67e74705SXin Li//===----------------------------------------------------------------------===// 1259*67e74705SXin Litypedef uint32_t CGBitmapInfo; 1260*67e74705SXin Litypedef void (*CGBitmapContextReleaseDataCallback)(void *releaseInfo, void *data); 1261*67e74705SXin Li 1262*67e74705SXin LiCGContextRef CGBitmapContextCreateWithData(void *data, 1263*67e74705SXin Li size_t width, size_t height, size_t bitsPerComponent, 1264*67e74705SXin Li size_t bytesPerRow, CGColorSpaceRef space, CGBitmapInfo bitmapInfo, 1265*67e74705SXin Li CGBitmapContextReleaseDataCallback releaseCallback, void *releaseInfo); 1266*67e74705SXin Li 1267*67e74705SXin Livoid rdar_7358899(void *data, 1268*67e74705SXin Li size_t width, size_t height, size_t bitsPerComponent, 1269*67e74705SXin Li size_t bytesPerRow, CGColorSpaceRef space, CGBitmapInfo bitmapInfo, 1270*67e74705SXin Li CGBitmapContextReleaseDataCallback releaseCallback) { 1271*67e74705SXin Li 1272*67e74705SXin Li // For the allocated object, it doesn't really matter what type it is 1273*67e74705SXin Li // for the purpose of this test. All we want to show is that 1274*67e74705SXin Li // this is freed later by the callback. 1275*67e74705SXin Li NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning 1276*67e74705SXin Li 1277*67e74705SXin Li CGBitmapContextCreateWithData(data, width, height, bitsPerComponent, // expected-warning{{leak}} 1278*67e74705SXin Li bytesPerRow, space, bitmapInfo, releaseCallback, number); 1279*67e74705SXin Li} 1280*67e74705SXin Li 1281*67e74705SXin Li//===----------------------------------------------------------------------===// 1282*67e74705SXin Li// <rdar://problem/7265711> allow 'new', 'copy', 'alloc', 'init' prefix to 1283*67e74705SXin Li// start before '_' when determining Cocoa fundamental rule 1284*67e74705SXin Li// 1285*67e74705SXin Li// Previously the retain/release checker just skipped prefixes before the 1286*67e74705SXin Li// first '_' entirely. Now the checker honors the prefix if it results in a 1287*67e74705SXin Li// recognizable naming convention (e.g., 'new', 'init'). 1288*67e74705SXin Li//===----------------------------------------------------------------------===// 1289*67e74705SXin Li 1290*67e74705SXin Li@interface RDar7265711 {} 1291*67e74705SXin Li- (id) new_stuff; 1292*67e74705SXin Li@end 1293*67e74705SXin Li 1294*67e74705SXin Livoid rdar7265711_a(RDar7265711 *x) { 1295*67e74705SXin Li id y = [x new_stuff]; // expected-warning{{leak}} 1296*67e74705SXin Li} 1297*67e74705SXin Li 1298*67e74705SXin Livoid rdar7265711_b(RDar7265711 *x) { 1299*67e74705SXin Li id y = [x new_stuff]; // no-warning 1300*67e74705SXin Li [y release]; 1301*67e74705SXin Li} 1302*67e74705SXin Li 1303*67e74705SXin Li//===----------------------------------------------------------------------===// 1304*67e74705SXin Li// <rdar://problem/7306898> clang thinks [NSCursor dragCopyCursor] returns a 1305*67e74705SXin Li// retained reference 1306*67e74705SXin Li//===----------------------------------------------------------------------===// 1307*67e74705SXin Li 1308*67e74705SXin Li@interface NSCursor : NSObject 1309*67e74705SXin Li+ (NSCursor *)dragCopyCursor; 1310*67e74705SXin Li@end 1311*67e74705SXin Li 1312*67e74705SXin Livoid rdar7306898(void) { 1313*67e74705SXin Li // 'dragCopyCursor' does not follow Cocoa's fundamental rule. It is a noun, not an sentence 1314*67e74705SXin Li // implying a 'copy' of something. 1315*67e74705SXin Li NSCursor *c = [NSCursor dragCopyCursor]; // no-warning 1316*67e74705SXin Li NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}} 1317*67e74705SXin Li} 1318*67e74705SXin Li 1319*67e74705SXin Li//===----------------------------------------------------------------------===// 1320*67e74705SXin Li// <rdar://problem/7252064> sending 'release', 'retain', etc. to a Class 1321*67e74705SXin Li// directly is not likely what the user intended 1322*67e74705SXin Li//===----------------------------------------------------------------------===// 1323*67e74705SXin Li 1324*67e74705SXin Li@interface RDar7252064 : NSObject @end 1325*67e74705SXin Livoid rdar7252064(void) { 1326*67e74705SXin Li [RDar7252064 release]; // expected-warning{{The 'release' message should be sent to instances of class 'RDar7252064' and not the class directly}} 1327*67e74705SXin Li [RDar7252064 retain]; // expected-warning{{The 'retain' message should be sent to instances of class 'RDar7252064' and not the class directly}} 1328*67e74705SXin Li [RDar7252064 autorelease]; // expected-warning{{The 'autorelease' message should be sent to instances of class 'RDar7252064' and not the class directly}} 1329*67e74705SXin Li [NSAutoreleasePool drain]; // expected-warning{{method '+drain' not found}} expected-warning{{The 'drain' message should be sent to instances of class 'NSAutoreleasePool' and not the class directly}} 1330*67e74705SXin Li} 1331*67e74705SXin Li 1332*67e74705SXin Li//===----------------------------------------------------------------------===// 1333*67e74705SXin Li// Tests of ownership attributes. 1334*67e74705SXin Li//===----------------------------------------------------------------------===// 1335*67e74705SXin Li 1336*67e74705SXin Litypedef NSString* MyStringTy; 1337*67e74705SXin Li 1338*67e74705SXin Li@protocol FooP; 1339*67e74705SXin Li 1340*67e74705SXin Li@interface TestOwnershipAttr : NSObject 1341*67e74705SXin Li- (NSString*) returnsAnOwnedString NS_RETURNS_RETAINED; // no-warning 1342*67e74705SXin Li- (NSString*) returnsAnOwnedCFString CF_RETURNS_RETAINED; // no-warning 1343*67e74705SXin Li- (MyStringTy) returnsAnOwnedTypedString NS_RETURNS_RETAINED; // no-warning 1344*67e74705SXin Li- (NSString*) newString NS_RETURNS_NOT_RETAINED; // no-warning 1345*67e74705SXin Li- (NSString*) newString_auto NS_RETURNS_AUTORELEASED; // no-warning 1346*67e74705SXin Li- (NSString*) newStringNoAttr; 1347*67e74705SXin Li- (int) returnsAnOwnedInt NS_RETURNS_RETAINED; // expected-warning{{'ns_returns_retained' attribute only applies to methods that return an Objective-C object}} 1348*67e74705SXin Li- (id) pseudoInit NS_CONSUMES_SELF NS_RETURNS_RETAINED; 1349*67e74705SXin Li+ (void) consume:(id) NS_CONSUMED x; 1350*67e74705SXin Li+ (void) consume2:(id) CF_CONSUMED x; 1351*67e74705SXin Li@end 1352*67e74705SXin Li 1353*67e74705SXin Listatic int ownership_attribute_doesnt_go_here NS_RETURNS_RETAINED; // expected-warning{{'ns_returns_retained' attribute only applies to functions and methods}} 1354*67e74705SXin Li 1355*67e74705SXin Livoid test_attr_1(TestOwnershipAttr *X) { 1356*67e74705SXin Li NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}} 1357*67e74705SXin Li} 1358*67e74705SXin Li 1359*67e74705SXin Livoid test_attr_1b(TestOwnershipAttr *X) { 1360*67e74705SXin Li NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}} 1361*67e74705SXin Li} 1362*67e74705SXin Li 1363*67e74705SXin Livoid test_attr1c(TestOwnershipAttr *X) { 1364*67e74705SXin Li NSString *str = [X newString]; // no-warning 1365*67e74705SXin Li NSString *str2 = [X newStringNoAttr]; // expected-warning{{leak}} 1366*67e74705SXin Li NSString *str3 = [X newString_auto]; // no-warning 1367*67e74705SXin Li NSString *str4 = [[X newString_auto] retain]; // expected-warning {{leak}} 1368*67e74705SXin Li} 1369*67e74705SXin Li 1370*67e74705SXin Livoid testattr2_a() { 1371*67e74705SXin Li TestOwnershipAttr *x = [TestOwnershipAttr alloc]; // expected-warning{{leak}} 1372*67e74705SXin Li} 1373*67e74705SXin Li 1374*67e74705SXin Livoid testattr2_b() { 1375*67e74705SXin Li TestOwnershipAttr *x = [[TestOwnershipAttr alloc] pseudoInit]; // expected-warning{{leak}} 1376*67e74705SXin Li} 1377*67e74705SXin Li 1378*67e74705SXin Livoid testattr2_b_11358224_self_assign_looses_the_leak() { 1379*67e74705SXin Li TestOwnershipAttr *x = [[TestOwnershipAttr alloc] pseudoInit];// expected-warning{{leak}} 1380*67e74705SXin Li x = x; 1381*67e74705SXin Li} 1382*67e74705SXin Li 1383*67e74705SXin Livoid testattr2_c() { 1384*67e74705SXin Li TestOwnershipAttr *x = [[TestOwnershipAttr alloc] pseudoInit]; // no-warning 1385*67e74705SXin Li [x release]; 1386*67e74705SXin Li} 1387*67e74705SXin Li 1388*67e74705SXin Livoid testattr3() { 1389*67e74705SXin Li TestOwnershipAttr *x = [TestOwnershipAttr alloc]; // no-warning 1390*67e74705SXin Li [TestOwnershipAttr consume:x]; 1391*67e74705SXin Li TestOwnershipAttr *y = [TestOwnershipAttr alloc]; // no-warning 1392*67e74705SXin Li [TestOwnershipAttr consume2:y]; 1393*67e74705SXin Li} 1394*67e74705SXin Li 1395*67e74705SXin Livoid consume_ns(id NS_CONSUMED x); 1396*67e74705SXin Livoid consume_cf(id CF_CONSUMED x); 1397*67e74705SXin Li 1398*67e74705SXin Livoid testattr4() { 1399*67e74705SXin Li TestOwnershipAttr *x = [TestOwnershipAttr alloc]; // no-warning 1400*67e74705SXin Li consume_ns(x); 1401*67e74705SXin Li TestOwnershipAttr *y = [TestOwnershipAttr alloc]; // no-warning 1402*67e74705SXin Li consume_cf(y); 1403*67e74705SXin Li} 1404*67e74705SXin Li 1405*67e74705SXin Li@interface TestOwnershipAttr2 : NSObject 1406*67e74705SXin Li- (NSString*) newString NS_RETURNS_NOT_RETAINED; // no-warning 1407*67e74705SXin Li@end 1408*67e74705SXin Li 1409*67e74705SXin Li@implementation TestOwnershipAttr2 1410*67e74705SXin Li- (NSString*) newString { 1411*67e74705SXin Li return [NSString alloc]; // expected-warning {{Potential leak of an object}} 1412*67e74705SXin Li} 1413*67e74705SXin Li@end 1414*67e74705SXin Li 1415*67e74705SXin Li@interface MyClassTestCFAttr : NSObject {} 1416*67e74705SXin Li- (NSDate*) returnsCFRetained CF_RETURNS_RETAINED; 1417*67e74705SXin Li- (CFDateRef) returnsCFRetainedAsCF CF_RETURNS_RETAINED; 1418*67e74705SXin Li- (CFDateRef) newCFRetainedAsCF CF_RETURNS_NOT_RETAINED; 1419*67e74705SXin Li- (CFDateRef) newCFRetainedAsCFNoAttr; 1420*67e74705SXin Li- (NSDate*) alsoReturnsRetained; 1421*67e74705SXin Li- (CFDateRef) alsoReturnsRetainedAsCF; 1422*67e74705SXin Li- (NSDate*) returnsNSRetained NS_RETURNS_RETAINED; 1423*67e74705SXin Li@end 1424*67e74705SXin Li 1425*67e74705SXin LiCF_RETURNS_RETAINED 1426*67e74705SXin LiCFDateRef returnsRetainedCFDate() { 1427*67e74705SXin Li return CFDateCreate(0, CFAbsoluteTimeGetCurrent()); 1428*67e74705SXin Li} 1429*67e74705SXin Li 1430*67e74705SXin Li@implementation MyClassTestCFAttr 1431*67e74705SXin Li- (NSDate*) returnsCFRetained { 1432*67e74705SXin Li return (NSDate*) returnsRetainedCFDate(); // No leak. 1433*67e74705SXin Li} 1434*67e74705SXin Li 1435*67e74705SXin Li- (CFDateRef) returnsCFRetainedAsCF { 1436*67e74705SXin Li return returnsRetainedCFDate(); // No leak. 1437*67e74705SXin Li} 1438*67e74705SXin Li 1439*67e74705SXin Li- (CFDateRef) newCFRetainedAsCF { 1440*67e74705SXin Li return (CFDateRef)[(id)[self returnsCFRetainedAsCF] autorelease]; 1441*67e74705SXin Li} 1442*67e74705SXin Li 1443*67e74705SXin Li- (CFDateRef) newCFRetainedAsCFNoAttr { 1444*67e74705SXin Li return (CFDateRef)[(id)[self returnsCFRetainedAsCF] autorelease]; // expected-warning{{Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected}} 1445*67e74705SXin Li} 1446*67e74705SXin Li 1447*67e74705SXin Li- (NSDate*) alsoReturnsRetained { 1448*67e74705SXin Li return (NSDate*) returnsRetainedCFDate(); // expected-warning{{leak}} 1449*67e74705SXin Li} 1450*67e74705SXin Li 1451*67e74705SXin Li- (CFDateRef) alsoReturnsRetainedAsCF { 1452*67e74705SXin Li return returnsRetainedCFDate(); // expected-warning{{leak}} 1453*67e74705SXin Li} 1454*67e74705SXin Li 1455*67e74705SXin Li 1456*67e74705SXin Li- (NSDate*) returnsNSRetained { 1457*67e74705SXin Li return (NSDate*) returnsRetainedCFDate(); // no-warning 1458*67e74705SXin Li} 1459*67e74705SXin Li@end 1460*67e74705SXin Li 1461*67e74705SXin Li//===----------------------------------------------------------------------===// 1462*67e74705SXin Li// Test that leaks post-dominated by "panic" functions are not reported. 1463*67e74705SXin Li// 1464*67e74705SXin Li// <rdar://problem/5905851> do not report a leak when post-dominated by a call 1465*67e74705SXin Li// to a noreturn or panic function 1466*67e74705SXin Li//===----------------------------------------------------------------------===// 1467*67e74705SXin Li 1468*67e74705SXin Livoid panic() __attribute__((noreturn)); 1469*67e74705SXin Livoid panic_not_in_hardcoded_list() __attribute__((noreturn)); 1470*67e74705SXin Li 1471*67e74705SXin Livoid test_panic_negative() { 1472*67e74705SXin Li signed z = 1; 1473*67e74705SXin Li CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // expected-warning{{leak}} 1474*67e74705SXin Li} 1475*67e74705SXin Li 1476*67e74705SXin Livoid test_panic_positive() { 1477*67e74705SXin Li signed z = 1; 1478*67e74705SXin Li CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // no-warning 1479*67e74705SXin Li panic(); 1480*67e74705SXin Li} 1481*67e74705SXin Li 1482*67e74705SXin Livoid test_panic_neg_2(int x) { 1483*67e74705SXin Li signed z = 1; 1484*67e74705SXin Li CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // expected-warning{{leak}} 1485*67e74705SXin Li if (x) 1486*67e74705SXin Li panic(); 1487*67e74705SXin Li} 1488*67e74705SXin Li 1489*67e74705SXin Livoid test_panic_pos_2(int x) { 1490*67e74705SXin Li signed z = 1; 1491*67e74705SXin Li CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // no-warning 1492*67e74705SXin Li if (x) 1493*67e74705SXin Li panic(); 1494*67e74705SXin Li if (!x) { 1495*67e74705SXin Li // This showed up in <rdar://problem/7796563>, where we silently missed checking 1496*67e74705SXin Li // the function type for noreturn. "panic()" is a hard-coded known panic function 1497*67e74705SXin Li // that isn't always noreturn. 1498*67e74705SXin Li panic_not_in_hardcoded_list(); 1499*67e74705SXin Li } 1500*67e74705SXin Li} 1501*67e74705SXin Li 1502*67e74705SXin Li//===----------------------------------------------------------------------===// 1503*67e74705SXin Li// Test uses of blocks (closures) 1504*67e74705SXin Li//===----------------------------------------------------------------------===// 1505*67e74705SXin Li 1506*67e74705SXin Livoid test_blocks_1_pos(void) { 1507*67e74705SXin Li NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}} 1508*67e74705SXin Li ^{}(); 1509*67e74705SXin Li} 1510*67e74705SXin Li 1511*67e74705SXin Livoid test_blocks_1_indirect_release(void) { 1512*67e74705SXin Li NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning 1513*67e74705SXin Li ^{ [number release]; }(); 1514*67e74705SXin Li} 1515*67e74705SXin Li 1516*67e74705SXin Livoid test_blocks_1_indirect_retain(void) { 1517*67e74705SXin Li // Eventually this should be reported as a leak. 1518*67e74705SXin Li NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning 1519*67e74705SXin Li ^{ [number retain]; }(); 1520*67e74705SXin Li} 1521*67e74705SXin Li 1522*67e74705SXin Livoid test_blocks_1_indirect_release_via_call(void) { 1523*67e74705SXin Li NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning 1524*67e74705SXin Li ^(NSObject *o){ [o release]; }(number); 1525*67e74705SXin Li} 1526*67e74705SXin Li 1527*67e74705SXin Livoid test_blocks_1_indirect_retain_via_call(void) { 1528*67e74705SXin Li NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning {{leak}} 1529*67e74705SXin Li ^(NSObject *o){ [o retain]; }(number); 1530*67e74705SXin Li} 1531*67e74705SXin Li 1532*67e74705SXin Li//===--------------------------------------------------------------------===// 1533*67e74705SXin Li// Test sending message to super that returns an object alias. Previously 1534*67e74705SXin Li// this caused a crash in the analyzer. 1535*67e74705SXin Li//===--------------------------------------------------------------------===// 1536*67e74705SXin Li 1537*67e74705SXin Li@interface Rdar8015556 : NSObject {} @end 1538*67e74705SXin Li@implementation Rdar8015556 1539*67e74705SXin Li- (id)retain { 1540*67e74705SXin Li return [super retain]; 1541*67e74705SXin Li} 1542*67e74705SXin Li@end 1543*67e74705SXin Li 1544*67e74705SXin Li// <rdar://problem/8272168> - Correcly handle Class<...> in Cocoa Conventions 1545*67e74705SXin Li// detector. 1546*67e74705SXin Li 1547*67e74705SXin Li@protocol Prot_R8272168 @end 1548*67e74705SXin LiClass <Prot_R8272168> GetAClassThatImplementsProt_R8272168(); 1549*67e74705SXin Livoid r8272168() { 1550*67e74705SXin Li GetAClassThatImplementsProt_R8272168(); 1551*67e74705SXin Li} 1552*67e74705SXin Li 1553*67e74705SXin Li// Test case for <rdar://problem/8356342>, which in the past triggered 1554*67e74705SXin Li// a false positive. 1555*67e74705SXin Li@interface RDar8356342 1556*67e74705SXin Li- (NSDate*) rdar8356342:(NSDate *)inValue; 1557*67e74705SXin Li@end 1558*67e74705SXin Li 1559*67e74705SXin Li@implementation RDar8356342 1560*67e74705SXin Li- (NSDate*) rdar8356342:(NSDate*)inValue { 1561*67e74705SXin Li NSDate *outValue = inValue; 1562*67e74705SXin Li if (outValue == 0) 1563*67e74705SXin Li outValue = [[NSDate alloc] init]; // no-warning 1564*67e74705SXin Li 1565*67e74705SXin Li if (outValue != inValue) 1566*67e74705SXin Li [outValue autorelease]; 1567*67e74705SXin Li 1568*67e74705SXin Li return outValue; 1569*67e74705SXin Li} 1570*67e74705SXin Li@end 1571*67e74705SXin Li 1572*67e74705SXin Li// <rdar://problem/8724287> - This test case previously crashed because 1573*67e74705SXin Li// of a bug in BugReporter. 1574*67e74705SXin Liextern const void *CFDictionaryGetValue(CFDictionaryRef theDict, const void *key); 1575*67e74705SXin Litypedef struct __CFError * CFErrorRef; 1576*67e74705SXin Liextern const CFStringRef kCFErrorUnderlyingErrorKey; 1577*67e74705SXin Liextern CFDictionaryRef CFErrorCopyUserInfo(CFErrorRef err); 1578*67e74705SXin Listatic void rdar_8724287(CFErrorRef error) 1579*67e74705SXin Li{ 1580*67e74705SXin Li CFErrorRef error_to_dump; 1581*67e74705SXin Li 1582*67e74705SXin Li error_to_dump = error; 1583*67e74705SXin Li while (error_to_dump != ((void*)0)) { 1584*67e74705SXin Li CFDictionaryRef info; 1585*67e74705SXin Li 1586*67e74705SXin Li info = CFErrorCopyUserInfo(error_to_dump); // expected-warning{{Potential leak of an object}} 1587*67e74705SXin Li 1588*67e74705SXin Li if (info != ((void*)0)) { 1589*67e74705SXin Li } 1590*67e74705SXin Li 1591*67e74705SXin Li error_to_dump = (CFErrorRef) CFDictionaryGetValue(info, kCFErrorUnderlyingErrorKey); 1592*67e74705SXin Li } 1593*67e74705SXin Li} 1594*67e74705SXin Li 1595*67e74705SXin Li// <rdar://problem/9234108> - Make sure the model applies cf_consumed 1596*67e74705SXin Li// correctly in argument positions besides the first. 1597*67e74705SXin Liextern void *CFStringCreate(void); 1598*67e74705SXin Liextern void rdar_9234108_helper(void *key, void * CF_CONSUMED value); 1599*67e74705SXin Livoid rdar_9234108() { 1600*67e74705SXin Li rdar_9234108_helper(0, CFStringCreate()); 1601*67e74705SXin Li} 1602*67e74705SXin Li 1603*67e74705SXin Li// <rdar://problem/9726279> - Make sure that objc_method_family works 1604*67e74705SXin Li// to override naming conventions. 1605*67e74705SXin Listruct TwoDoubles { 1606*67e74705SXin Li double one; 1607*67e74705SXin Li double two; 1608*67e74705SXin Li}; 1609*67e74705SXin Litypedef struct TwoDoubles TwoDoubles; 1610*67e74705SXin Li 1611*67e74705SXin Li@interface NSValue (Mine) 1612*67e74705SXin Li- (id)_prefix_initWithTwoDoubles:(TwoDoubles)twoDoubles __attribute__((objc_method_family(init))); 1613*67e74705SXin Li@end 1614*67e74705SXin Li 1615*67e74705SXin Li@implementation NSValue (Mine) 1616*67e74705SXin Li- (id)_prefix_initWithTwoDoubles:(TwoDoubles)twoDoubles 1617*67e74705SXin Li{ 1618*67e74705SXin Li return [self init]; 1619*67e74705SXin Li} 1620*67e74705SXin Li@end 1621*67e74705SXin Li 1622*67e74705SXin Livoid rdar9726279() { 1623*67e74705SXin Li TwoDoubles twoDoubles = { 0.0, 0.0 }; 1624*67e74705SXin Li NSValue *value = [[NSValue alloc] _prefix_initWithTwoDoubles:twoDoubles]; 1625*67e74705SXin Li [value release]; 1626*67e74705SXin Li} 1627*67e74705SXin Li 1628*67e74705SXin Li// <rdar://problem/9732321> 1629*67e74705SXin Li// Test camelcase support for CF conventions. While Core Foundation APIs 1630*67e74705SXin Li// don't use camel casing, other code is allowed to use it. 1631*67e74705SXin LiCFArrayRef camelcase_create_1() { 1632*67e74705SXin Li return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 1633*67e74705SXin Li} 1634*67e74705SXin Li 1635*67e74705SXin LiCFArrayRef camelcase_createno() { 1636*67e74705SXin Li return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning {{leak}} 1637*67e74705SXin Li} 1638*67e74705SXin Li 1639*67e74705SXin LiCFArrayRef camelcase_copy() { 1640*67e74705SXin Li return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 1641*67e74705SXin Li} 1642*67e74705SXin Li 1643*67e74705SXin LiCFArrayRef camelcase_copying() { 1644*67e74705SXin Li return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning {{leak}} 1645*67e74705SXin Li} 1646*67e74705SXin Li 1647*67e74705SXin LiCFArrayRef copyCamelCase() { 1648*67e74705SXin Li return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 1649*67e74705SXin Li} 1650*67e74705SXin Li 1651*67e74705SXin LiCFArrayRef __copyCamelCase() { 1652*67e74705SXin Li return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 1653*67e74705SXin Li} 1654*67e74705SXin Li 1655*67e74705SXin LiCFArrayRef __createCamelCase() { 1656*67e74705SXin Li return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 1657*67e74705SXin Li} 1658*67e74705SXin Li 1659*67e74705SXin LiCFArrayRef camel_create() { 1660*67e74705SXin Li return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 1661*67e74705SXin Li} 1662*67e74705SXin Li 1663*67e74705SXin Li 1664*67e74705SXin LiCFArrayRef camel_creat() { 1665*67e74705SXin Li return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning {{leak}} 1666*67e74705SXin Li} 1667*67e74705SXin Li 1668*67e74705SXin LiCFArrayRef camel_copy() { 1669*67e74705SXin Li return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 1670*67e74705SXin Li} 1671*67e74705SXin Li 1672*67e74705SXin LiCFArrayRef camel_copyMachine() { 1673*67e74705SXin Li return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning 1674*67e74705SXin Li} 1675*67e74705SXin Li 1676*67e74705SXin LiCFArrayRef camel_copymachine() { 1677*67e74705SXin Li return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning {{leak}} 1678*67e74705SXin Li} 1679*67e74705SXin Li 1680*67e74705SXin Li// rdar://problem/8024350 1681*67e74705SXin Li@protocol F18P 1682*67e74705SXin Li- (id) clone; 1683*67e74705SXin Li@end 1684*67e74705SXin Li@interface F18 : NSObject<F18P> @end 1685*67e74705SXin Li@interface F18(Cat) 1686*67e74705SXin Li- (id) clone NS_RETURNS_RETAINED; 1687*67e74705SXin Li@end 1688*67e74705SXin Li 1689*67e74705SXin Li@implementation F18 1690*67e74705SXin Li- (id) clone { 1691*67e74705SXin Li return [F18 alloc]; 1692*67e74705SXin Li} 1693*67e74705SXin Li@end 1694*67e74705SXin Li 1695*67e74705SXin Li// Radar 6582778. 1696*67e74705SXin Livoid rdar6582778(void) { 1697*67e74705SXin Li CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); 1698*67e74705SXin Li CFTypeRef vals[] = { CFDateCreate(0, t) }; // expected-warning {{leak}} 1699*67e74705SXin Li} 1700*67e74705SXin Li 1701*67e74705SXin LiCFTypeRef global; 1702*67e74705SXin Li 1703*67e74705SXin Livoid rdar6582778_2(void) { 1704*67e74705SXin Li CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); 1705*67e74705SXin Li global = CFDateCreate(0, t); // no-warning 1706*67e74705SXin Li} 1707*67e74705SXin Li 1708*67e74705SXin Li// <rdar://problem/10232019> - Test that objects passed to containers 1709*67e74705SXin Li// are marked "escaped". 1710*67e74705SXin Li 1711*67e74705SXin Livoid rdar10232019() { 1712*67e74705SXin Li NSMutableArray *array = [NSMutableArray array]; 1713*67e74705SXin Li 1714*67e74705SXin Li NSString *string = [[NSString alloc] initWithUTF8String:"foo"]; 1715*67e74705SXin Li [array addObject:string]; 1716*67e74705SXin Li [string release]; 1717*67e74705SXin Li 1718*67e74705SXin Li NSString *otherString = [string stringByAppendingString:@"bar"]; // no-warning 1719*67e74705SXin Li NSLog(@"%@", otherString); 1720*67e74705SXin Li} 1721*67e74705SXin Li 1722*67e74705SXin Livoid rdar10232019_positive() { 1723*67e74705SXin Li NSMutableArray *array = [NSMutableArray array]; 1724*67e74705SXin Li 1725*67e74705SXin Li NSString *string = [[NSString alloc] initWithUTF8String:"foo"]; 1726*67e74705SXin Li [string release]; 1727*67e74705SXin Li 1728*67e74705SXin Li NSString *otherString = [string stringByAppendingString:@"bar"]; // expected-warning {{Reference-counted object is used after it is release}} 1729*67e74705SXin Li NSLog(@"%@", otherString); 1730*67e74705SXin Li} 1731*67e74705SXin Li 1732*67e74705SXin Li// RetainCountChecker support for XPC. 1733*67e74705SXin Li// <rdar://problem/9658496> 1734*67e74705SXin Litypedef void * xpc_object_t; 1735*67e74705SXin Lixpc_object_t _CFXPCCreateXPCObjectFromCFObject(CFTypeRef cf); 1736*67e74705SXin Livoid xpc_release(xpc_object_t object); 1737*67e74705SXin Li 1738*67e74705SXin Livoid rdar9658496() { 1739*67e74705SXin Li CFStringRef cf; 1740*67e74705SXin Li xpc_object_t xpc; 1741*67e74705SXin Li cf = CFStringCreateWithCString( ((CFAllocatorRef)0), "test", kCFStringEncodingUTF8 ); // no-warning 1742*67e74705SXin Li xpc = _CFXPCCreateXPCObjectFromCFObject( cf ); 1743*67e74705SXin Li CFRelease(cf); 1744*67e74705SXin Li xpc_release(xpc); 1745*67e74705SXin Li} 1746*67e74705SXin Li 1747*67e74705SXin Li// Support annotations with method families. 1748*67e74705SXin Li@interface RDar10824732 : NSObject 1749*67e74705SXin Li- (id)initWithObj:(id CF_CONSUMED)obj; 1750*67e74705SXin Li@end 1751*67e74705SXin Li 1752*67e74705SXin Li@implementation RDar10824732 1753*67e74705SXin Li- (id)initWithObj:(id)obj { 1754*67e74705SXin Li [obj release]; 1755*67e74705SXin Li return [super init]; 1756*67e74705SXin Li} 1757*67e74705SXin Li@end 1758*67e74705SXin Li 1759*67e74705SXin Livoid rdar_10824732() { 1760*67e74705SXin Li @autoreleasepool { 1761*67e74705SXin Li NSString *obj = @"test"; 1762*67e74705SXin Li RDar10824732 *foo = [[RDar10824732 alloc] initWithObj:obj]; // no-warning 1763*67e74705SXin Li [foo release]; 1764*67e74705SXin Li } 1765*67e74705SXin Li} 1766*67e74705SXin Li 1767*67e74705SXin Li// Stop tracking objects passed to functions, which take callbacks as parameters. 1768*67e74705SXin Li// radar://10973977 1769*67e74705SXin Litypedef int (*CloseCallback) (void *); 1770*67e74705SXin Livoid ReaderForIO(CloseCallback ioclose, void *ioctx); 1771*67e74705SXin Liint IOClose(void *context); 1772*67e74705SXin Li 1773*67e74705SXin Li@protocol SInS <NSObject> 1774*67e74705SXin Li@end 1775*67e74705SXin Li 1776*67e74705SXin Li@interface radar10973977 : NSObject 1777*67e74705SXin Li- (id<SInS>)inputS; 1778*67e74705SXin Li- (void)reader; 1779*67e74705SXin Li@end 1780*67e74705SXin Li 1781*67e74705SXin Li@implementation radar10973977 1782*67e74705SXin Li- (void)reader 1783*67e74705SXin Li{ 1784*67e74705SXin Li id<SInS> inputS = [[self inputS] retain]; 1785*67e74705SXin Li ReaderForIO(IOClose, inputS); 1786*67e74705SXin Li} 1787*67e74705SXin Li- (id<SInS>)inputS 1788*67e74705SXin Li{ 1789*67e74705SXin Li return 0; 1790*67e74705SXin Li} 1791*67e74705SXin Li@end 1792*67e74705SXin Li 1793*67e74705SXin Li// Object escapes through a selector callback: radar://11398514 1794*67e74705SXin Liextern id NSApp; 1795*67e74705SXin Li@interface MySheetController 1796*67e74705SXin Li- (id<SInS>)inputS; 1797*67e74705SXin Li- (void)showDoSomethingSheetAction:(id)action; 1798*67e74705SXin Li- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo; 1799*67e74705SXin Li@end 1800*67e74705SXin Li 1801*67e74705SXin Li@implementation MySheetController 1802*67e74705SXin Li- (id<SInS>)inputS { 1803*67e74705SXin Li return 0; 1804*67e74705SXin Li} 1805*67e74705SXin Li- (void)showDoSomethingSheetAction:(id)action { 1806*67e74705SXin Li id<SInS> inputS = [[self inputS] retain]; 1807*67e74705SXin Li [NSApp beginSheet:0 1808*67e74705SXin Li modalForWindow:0 1809*67e74705SXin Li modalDelegate:0 1810*67e74705SXin Li didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) 1811*67e74705SXin Li contextInfo:(void *)inputS]; // no - warning 1812*67e74705SXin Li} 1813*67e74705SXin Li- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo { 1814*67e74705SXin Li 1815*67e74705SXin Li id contextObject = (id)contextInfo; 1816*67e74705SXin Li [contextObject release]; 1817*67e74705SXin Li} 1818*67e74705SXin Li 1819*67e74705SXin Li- (id)copyAutoreleaseRadar13081402 { 1820*67e74705SXin Li id x = [[[NSString alloc] initWithUTF8String:"foo"] autorelease]; 1821*67e74705SXin Li [x retain]; 1822*67e74705SXin Li return x; // no warning 1823*67e74705SXin Li} 1824*67e74705SXin Li 1825*67e74705SXin Li@end 1826*67e74705SXin Li//===----------------------------------------------------------------------===// 1827*67e74705SXin Li// Test returning allocated memory in a struct. 1828*67e74705SXin Li// 1829*67e74705SXin Li// We currently don't have a general way to track pointers that "escape". 1830*67e74705SXin Li// Here we test that RetainCountChecker doesn't get excited about returning 1831*67e74705SXin Li// allocated CF objects in struct fields. 1832*67e74705SXin Li//===----------------------------------------------------------------------===// 1833*67e74705SXin Livoid *malloc(size_t); 1834*67e74705SXin Listruct rdar11104566 { CFStringRef myStr; }; 1835*67e74705SXin Listruct rdar11104566 test_rdar11104566() { 1836*67e74705SXin Li CFStringRef cf = CFStringCreateWithCString( ((CFAllocatorRef)0), "test", kCFStringEncodingUTF8 ); // no-warning 1837*67e74705SXin Li struct rdar11104566 V; 1838*67e74705SXin Li V.myStr = cf; 1839*67e74705SXin Li return V; // no-warning 1840*67e74705SXin Li} 1841*67e74705SXin Li 1842*67e74705SXin Listruct rdar11104566 *test_2_rdar11104566() { 1843*67e74705SXin Li CFStringRef cf = CFStringCreateWithCString( ((CFAllocatorRef)0), "test", kCFStringEncodingUTF8 ); // no-warning 1844*67e74705SXin Li struct rdar11104566 *V = (struct rdar11104566 *) malloc(sizeof(*V)); 1845*67e74705SXin Li V->myStr = cf; 1846*67e74705SXin Li return V; // no-warning 1847*67e74705SXin Li} 1848*67e74705SXin Li 1849*67e74705SXin Li//===----------------------------------------------------------------------===// 1850*67e74705SXin Li// ObjC literals support. 1851*67e74705SXin Li//===----------------------------------------------------------------------===// 1852*67e74705SXin Li 1853*67e74705SXin Livoid test_objc_arrays() { 1854*67e74705SXin Li { // CASE ONE -- OBJECT IN ARRAY CREATED DIRECTLY 1855*67e74705SXin Li NSObject *o = [[NSObject alloc] init]; 1856*67e74705SXin Li NSArray *a = [[NSArray alloc] initWithObjects:o, (void*)0]; // expected-warning {{leak}} 1857*67e74705SXin Li [o release]; 1858*67e74705SXin Li [a description]; 1859*67e74705SXin Li [o description]; 1860*67e74705SXin Li } 1861*67e74705SXin Li 1862*67e74705SXin Li { // CASE TWO -- OBJECT IN ARRAY CREATED BY DUPING AUTORELEASED ARRAY 1863*67e74705SXin Li NSObject *o = [[NSObject alloc] init]; 1864*67e74705SXin Li NSArray *a1 = [NSArray arrayWithObjects:o, (void*)0]; 1865*67e74705SXin Li NSArray *a2 = [[NSArray alloc] initWithArray:a1]; // expected-warning {{leak}} 1866*67e74705SXin Li [o release]; 1867*67e74705SXin Li [a2 description]; 1868*67e74705SXin Li [o description]; 1869*67e74705SXin Li } 1870*67e74705SXin Li 1871*67e74705SXin Li { // CASE THREE -- OBJECT IN RETAINED @[] 1872*67e74705SXin Li NSObject *o = [[NSObject alloc] init]; 1873*67e74705SXin Li NSArray *a3 = [@[o] retain]; // expected-warning {{leak}} 1874*67e74705SXin Li [o release]; 1875*67e74705SXin Li [a3 description]; 1876*67e74705SXin Li [o description]; 1877*67e74705SXin Li } 1878*67e74705SXin Li 1879*67e74705SXin Li { // CASE FOUR -- OBJECT IN ARRAY CREATED BY DUPING @[] 1880*67e74705SXin Li NSObject *o = [[NSObject alloc] init]; 1881*67e74705SXin Li NSArray *a = [[NSArray alloc] initWithArray:@[o]]; // expected-warning {{leak}} 1882*67e74705SXin Li [o release]; 1883*67e74705SXin Li 1884*67e74705SXin Li [a description]; 1885*67e74705SXin Li [o description]; 1886*67e74705SXin Li } 1887*67e74705SXin Li 1888*67e74705SXin Li { // CASE FIVE -- OBJECT IN RETAINED @{} 1889*67e74705SXin Li NSValue *o = [[NSValue alloc] init]; 1890*67e74705SXin Li NSDictionary *a = [@{o : o} retain]; // expected-warning {{leak}} 1891*67e74705SXin Li [o release]; 1892*67e74705SXin Li 1893*67e74705SXin Li [a description]; 1894*67e74705SXin Li [o description]; 1895*67e74705SXin Li } 1896*67e74705SXin Li} 1897*67e74705SXin Li 1898*67e74705SXin Livoid test_objc_integer_literals() { 1899*67e74705SXin Li id value = [@1 retain]; // expected-warning {{leak}} 1900*67e74705SXin Li [value description]; 1901*67e74705SXin Li} 1902*67e74705SXin Li 1903*67e74705SXin Livoid test_objc_boxed_expressions(int x, const char *y) { 1904*67e74705SXin Li id value = [@(x) retain]; // expected-warning {{leak}} 1905*67e74705SXin Li [value description]; 1906*67e74705SXin Li 1907*67e74705SXin Li value = [@(y) retain]; // expected-warning {{leak}} 1908*67e74705SXin Li [value description]; 1909*67e74705SXin Li} 1910*67e74705SXin Li 1911*67e74705SXin Li// Test NSLog doesn't escape tracked objects. 1912*67e74705SXin Livoid rdar11400885(int y) 1913*67e74705SXin Li{ 1914*67e74705SXin Li @autoreleasepool { 1915*67e74705SXin Li NSString *printString; 1916*67e74705SXin Li if(y > 2) 1917*67e74705SXin Li printString = [[NSString alloc] init]; 1918*67e74705SXin Li else 1919*67e74705SXin Li printString = [[NSString alloc] init]; 1920*67e74705SXin Li NSLog(@"Once %@", printString); 1921*67e74705SXin Li [printString release]; 1922*67e74705SXin Li NSLog(@"Again: %@", printString); // expected-warning {{Reference-counted object is used after it is released}} 1923*67e74705SXin Li } 1924*67e74705SXin Li} 1925*67e74705SXin Li 1926*67e74705SXin Liid makeCollectableNonLeak() { 1927*67e74705SXin Li extern CFTypeRef CFCreateSomething(); 1928*67e74705SXin Li 1929*67e74705SXin Li CFTypeRef object = CFCreateSomething(); // +1 1930*67e74705SXin Li CFRetain(object); // +2 1931*67e74705SXin Li id objCObject = NSMakeCollectable(object); // +2 1932*67e74705SXin Li [objCObject release]; // +1 1933*67e74705SXin Li return [objCObject autorelease]; // +0 1934*67e74705SXin Li} 1935*67e74705SXin Li 1936*67e74705SXin Li 1937*67e74705SXin Livoid consumeAndStopTracking(id NS_CONSUMED obj, void (^callback)(void)); 1938*67e74705SXin Livoid CFConsumeAndStopTracking(CFTypeRef CF_CONSUMED obj, void (^callback)(void)); 1939*67e74705SXin Li 1940*67e74705SXin Livoid testConsumeAndStopTracking() { 1941*67e74705SXin Li id retained = [@[] retain]; // +1 1942*67e74705SXin Li consumeAndStopTracking(retained, ^{}); // no-warning 1943*67e74705SXin Li 1944*67e74705SXin Li id doubleRetained = [[@[] retain] retain]; // +2 1945*67e74705SXin Li consumeAndStopTracking(doubleRetained, ^{ 1946*67e74705SXin Li [doubleRetained release]; 1947*67e74705SXin Li }); // no-warning 1948*67e74705SXin Li 1949*67e74705SXin Li id unretained = @[]; // +0 1950*67e74705SXin Li consumeAndStopTracking(unretained, ^{}); // expected-warning {{Incorrect decrement of the reference count of an object that is not owned at this point by the caller}} 1951*67e74705SXin Li} 1952*67e74705SXin Li 1953*67e74705SXin Livoid testCFConsumeAndStopTracking() { 1954*67e74705SXin Li id retained = [@[] retain]; // +1 1955*67e74705SXin Li CFConsumeAndStopTracking((CFTypeRef)retained, ^{}); // no-warning 1956*67e74705SXin Li 1957*67e74705SXin Li id doubleRetained = [[@[] retain] retain]; // +2 1958*67e74705SXin Li CFConsumeAndStopTracking((CFTypeRef)doubleRetained, ^{ 1959*67e74705SXin Li [doubleRetained release]; 1960*67e74705SXin Li }); // no-warning 1961*67e74705SXin Li 1962*67e74705SXin Li id unretained = @[]; // +0 1963*67e74705SXin Li CFConsumeAndStopTracking((CFTypeRef)unretained, ^{}); // expected-warning {{Incorrect decrement of the reference count of an object that is not owned at this point by the caller}} 1964*67e74705SXin Li} 1965*67e74705SXin Li//===----------------------------------------------------------------------===// 1966*67e74705SXin Li// Test 'pragma clang arc_cf_code_audited' support. 1967*67e74705SXin Li//===----------------------------------------------------------------------===// 1968*67e74705SXin Li 1969*67e74705SXin Litypedef void *MyCFType; 1970*67e74705SXin Li#pragma clang arc_cf_code_audited begin 1971*67e74705SXin LiMyCFType CreateMyCFType(); 1972*67e74705SXin Li#pragma clang arc_cf_code_audited end 1973*67e74705SXin Li 1974*67e74705SXin Livoid test_custom_cf() { 1975*67e74705SXin Li MyCFType x = CreateMyCFType(); // expected-warning {{leak of an object stored into 'x'}} 1976*67e74705SXin Li} 1977*67e74705SXin Li 1978*67e74705SXin Li//===----------------------------------------------------------------------===// 1979*67e74705SXin Li// Test calling CFPlugInInstanceCreate, which appears in CF but doesn't 1980*67e74705SXin Li// return a CF object. 1981*67e74705SXin Li//===----------------------------------------------------------------------===// 1982*67e74705SXin Li 1983*67e74705SXin Livoid test_CFPlugInInstanceCreate(CFUUIDRef factoryUUID, CFUUIDRef typeUUID) { 1984*67e74705SXin Li CFPlugInInstanceCreate(kCFAllocatorDefault, factoryUUID, typeUUID); // no-warning 1985*67e74705SXin Li} 1986*67e74705SXin Li 1987*67e74705SXin Li//===----------------------------------------------------------------------===// 1988*67e74705SXin Li// PR14927: -drain only has retain-count semantics on NSAutoreleasePool. 1989*67e74705SXin Li//===----------------------------------------------------------------------===// 1990*67e74705SXin Li 1991*67e74705SXin Li@interface PR14927 : NSObject 1992*67e74705SXin Li- (void)drain; 1993*67e74705SXin Li@end 1994*67e74705SXin Li 1995*67e74705SXin Livoid test_drain() { 1996*67e74705SXin Li PR14927 *obj = [[PR14927 alloc] init]; 1997*67e74705SXin Li [obj drain]; 1998*67e74705SXin Li [obj release]; // no-warning 1999*67e74705SXin Li} 2000*67e74705SXin Li 2001*67e74705SXin Li//===----------------------------------------------------------------------===// 2002*67e74705SXin Li// Allow cf_returns_retained and cf_returns_not_retained to mark a return 2003*67e74705SXin Li// value as tracked, even if the object isn't a known CF type. 2004*67e74705SXin Li//===----------------------------------------------------------------------===// 2005*67e74705SXin Li 2006*67e74705SXin LiMyCFType getCustom() __attribute__((cf_returns_not_retained)); 2007*67e74705SXin LiMyCFType makeCustom() __attribute__((cf_returns_retained)); 2008*67e74705SXin Li 2009*67e74705SXin Livoid testCustomReturnsRetained() { 2010*67e74705SXin Li MyCFType obj = makeCustom(); // expected-warning {{leak of an object stored into 'obj'}} 2011*67e74705SXin Li} 2012*67e74705SXin Li 2013*67e74705SXin Livoid testCustomReturnsNotRetained() { 2014*67e74705SXin Li CFRelease(getCustom()); // expected-warning {{Incorrect decrement of the reference count of an object that is not owned at this point by the caller}} 2015*67e74705SXin Li} 2016*67e74705SXin Li 2017*67e74705SXin Li//===----------------------------------------------------------------------===// 2018*67e74705SXin Li// Don't print variables which are out of the current scope. 2019*67e74705SXin Li//===----------------------------------------------------------------------===// 2020*67e74705SXin Li@interface MyObj12706177 : NSObject 2021*67e74705SXin Li-(id)initX; 2022*67e74705SXin Li+(void)test12706177; 2023*67e74705SXin Li@end 2024*67e74705SXin Listatic int Cond; 2025*67e74705SXin Li@implementation MyObj12706177 2026*67e74705SXin Li-(id)initX { 2027*67e74705SXin Li if (Cond) 2028*67e74705SXin Li return 0; 2029*67e74705SXin Li self = [super init]; 2030*67e74705SXin Li return self; 2031*67e74705SXin Li} 2032*67e74705SXin Li+(void)test12706177 { 2033*67e74705SXin Li id x = [[MyObj12706177 alloc] initX]; //expected-warning {{Potential leak of an object}} 2034*67e74705SXin Li [x release]; 2035*67e74705SXin Li} 2036*67e74705SXin Li@end 2037*67e74705SXin Li 2038*67e74705SXin Li//===----------------------------------------------------------------------===// 2039*67e74705SXin Li// <rdar://problem/13783514> xpc_connection_set_finalizer_f 2040*67e74705SXin Li//===----------------------------------------------------------------------===// 2041*67e74705SXin Li 2042*67e74705SXin Litypedef xpc_object_t xpc_connection_t; 2043*67e74705SXin Litypedef void (*xpc_finalizer_t)(void *value); 2044*67e74705SXin Livoid xpc_connection_set_context(xpc_connection_t connection, void *ctx); 2045*67e74705SXin Livoid xpc_connection_set_finalizer_f(xpc_connection_t connection, 2046*67e74705SXin Li xpc_finalizer_t finalizer); 2047*67e74705SXin Livoid releaseAfterXPC(void *context) { 2048*67e74705SXin Li [(NSArray *)context release]; 2049*67e74705SXin Li} 2050*67e74705SXin Li 2051*67e74705SXin Livoid rdar13783514(xpc_connection_t connection) { 2052*67e74705SXin Li xpc_connection_set_context(connection, [[NSMutableArray alloc] init]); 2053*67e74705SXin Li xpc_connection_set_finalizer_f(connection, releaseAfterXPC); 2054*67e74705SXin Li} // no-warning 2055*67e74705SXin Li 2056*67e74705SXin LiCFAttributedStringRef CFAttributedCreate(void *CFObj CF_CONSUMED) CF_RETURNS_RETAINED; 2057*67e74705SXin Li 2058*67e74705SXin Li@interface Action 2059*67e74705SXin Li- (SEL)action; 2060*67e74705SXin Li- (void)setAction:(SEL)aSelector; 2061*67e74705SXin Li- (id) target; 2062*67e74705SXin Li- (void)setTarget:(id)aTarget; 2063*67e74705SXin Li@end 2064