1*67e74705SXin Li// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,osx.coreFoundation.CFRetainRelease,osx.cocoa.ClassRelease,osx.cocoa.RetainCount -fblocks -verify %s 2*67e74705SXin Li 3*67e74705SXin Li//===----------------------------------------------------------------------===// 4*67e74705SXin Li// The following code is reduced using delta-debugging from Mac OS X headers: 5*67e74705SXin Li// 6*67e74705SXin Li// #include <Cocoa/Cocoa.h> 7*67e74705SXin Li// #include <CoreFoundation/CoreFoundation.h> 8*67e74705SXin Li// #include <DiskArbitration/DiskArbitration.h> 9*67e74705SXin Li// #include <QuartzCore/QuartzCore.h> 10*67e74705SXin Li// #include <Quartz/Quartz.h> 11*67e74705SXin Li// #include <IOKit/IOKitLib.h> 12*67e74705SXin Li// 13*67e74705SXin Li// It includes the basic definitions for the test cases below. 14*67e74705SXin Li//===----------------------------------------------------------------------===// 15*67e74705SXin Li 16*67e74705SXin Litypedef unsigned int __darwin_natural_t; 17*67e74705SXin Litypedef unsigned long uintptr_t; 18*67e74705SXin Litypedef unsigned int uint32_t; 19*67e74705SXin Litypedef unsigned long long uint64_t; 20*67e74705SXin Litypedef unsigned int UInt32; 21*67e74705SXin Litypedef signed long CFIndex; 22*67e74705SXin Litypedef CFIndex CFByteOrder; 23*67e74705SXin Litypedef struct { 24*67e74705SXin Li CFIndex location; 25*67e74705SXin Li CFIndex length; 26*67e74705SXin Li} CFRange; 27*67e74705SXin Listatic __inline__ __attribute__((always_inline)) CFRange CFRangeMake(CFIndex loc, CFIndex len) { 28*67e74705SXin Li CFRange range; 29*67e74705SXin Li range.location = loc; 30*67e74705SXin Li range.length = len; 31*67e74705SXin Li return range; 32*67e74705SXin Li} 33*67e74705SXin Litypedef const void * CFTypeRef; 34*67e74705SXin Litypedef const struct __CFString * CFStringRef; 35*67e74705SXin Litypedef const struct __CFAllocator * CFAllocatorRef; 36*67e74705SXin Liextern const CFAllocatorRef kCFAllocatorDefault; 37*67e74705SXin Liextern CFTypeRef CFRetain(CFTypeRef cf); 38*67e74705SXin Liextern void CFRelease(CFTypeRef cf); 39*67e74705SXin Litypedef struct { 40*67e74705SXin Li} 41*67e74705SXin LiCFArrayCallBacks; 42*67e74705SXin Liextern const CFArrayCallBacks kCFTypeArrayCallBacks; 43*67e74705SXin Litypedef const struct __CFArray * CFArrayRef; 44*67e74705SXin Litypedef struct __CFArray * CFMutableArrayRef; 45*67e74705SXin Liextern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks); 46*67e74705SXin Liextern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx); 47*67e74705SXin Liextern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value); 48*67e74705SXin Litypedef struct { 49*67e74705SXin Li} 50*67e74705SXin LiCFDictionaryKeyCallBacks; 51*67e74705SXin Liextern const CFDictionaryKeyCallBacks kCFTypeDictionaryKeyCallBacks; 52*67e74705SXin Litypedef struct { 53*67e74705SXin Li} 54*67e74705SXin LiCFDictionaryValueCallBacks; 55*67e74705SXin Liextern const CFDictionaryValueCallBacks kCFTypeDictionaryValueCallBacks; 56*67e74705SXin Litypedef const struct __CFDictionary * CFDictionaryRef; 57*67e74705SXin Litypedef struct __CFDictionary * CFMutableDictionaryRef; 58*67e74705SXin Liextern CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFDictionaryKeyCallBacks *keyCallBacks, const CFDictionaryValueCallBacks *valueCallBacks); 59*67e74705SXin Litypedef UInt32 CFStringEncoding; 60*67e74705SXin Lienum { 61*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 }; 62*67e74705SXin Liextern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding); 63*67e74705SXin Litypedef double CFTimeInterval; 64*67e74705SXin Litypedef CFTimeInterval CFAbsoluteTime; 65*67e74705SXin Liextern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void); 66*67e74705SXin Litypedef const struct __CFDate * CFDateRef; 67*67e74705SXin Liextern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at); 68*67e74705SXin Liextern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate); 69*67e74705SXin Litypedef __darwin_natural_t natural_t; 70*67e74705SXin Litypedef natural_t mach_port_name_t; 71*67e74705SXin Litypedef mach_port_name_t mach_port_t; 72*67e74705SXin Litypedef int kern_return_t; 73*67e74705SXin Litypedef kern_return_t mach_error_t; 74*67e74705SXin Lienum { 75*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 }; 76*67e74705SXin Litypedef CFIndex CFNumberType; 77*67e74705SXin Litypedef const struct __CFNumber * CFNumberRef; 78*67e74705SXin Liextern CFNumberRef CFNumberCreate(CFAllocatorRef allocator, CFNumberType theType, const void *valuePtr); 79*67e74705SXin Litypedef const struct __CFAttributedString *CFAttributedStringRef; 80*67e74705SXin Litypedef struct __CFAttributedString *CFMutableAttributedStringRef; 81*67e74705SXin Liextern CFAttributedStringRef CFAttributedStringCreate(CFAllocatorRef alloc, CFStringRef str, CFDictionaryRef attributes) ; 82*67e74705SXin Liextern CFMutableAttributedStringRef CFAttributedStringCreateMutableCopy(CFAllocatorRef alloc, CFIndex maxLength, CFAttributedStringRef aStr) ; 83*67e74705SXin Liextern void CFAttributedStringSetAttribute(CFMutableAttributedStringRef aStr, CFRange range, CFStringRef attrName, CFTypeRef value) ; 84*67e74705SXin Litypedef signed char BOOL; 85*67e74705SXin Litypedef unsigned long NSUInteger; 86*67e74705SXin Li@class NSString, Protocol; 87*67e74705SXin Liextern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); 88*67e74705SXin Litypedef struct _NSZone NSZone; 89*67e74705SXin Li@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; 90*67e74705SXin Li@protocol NSObject 91*67e74705SXin Li- (BOOL)isEqual:(id)object; 92*67e74705SXin Li- (id)retain; 93*67e74705SXin Li- (oneway void)release; 94*67e74705SXin Li- (id)autorelease; 95*67e74705SXin Li- (id)init; 96*67e74705SXin Li@end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; 97*67e74705SXin Li@end @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; 98*67e74705SXin Li@end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; 99*67e74705SXin Li@end 100*67e74705SXin Li@interface NSObject <NSObject> {} 101*67e74705SXin Li+ (id)allocWithZone:(NSZone *)zone; 102*67e74705SXin Li+ (id)alloc; 103*67e74705SXin Li- (void)dealloc; 104*67e74705SXin Li@end 105*67e74705SXin Li@interface NSObject (NSCoderMethods) 106*67e74705SXin Li- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder; 107*67e74705SXin Li@end 108*67e74705SXin Liextern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); 109*67e74705SXin Litypedef struct { 110*67e74705SXin Li} 111*67e74705SXin LiNSFastEnumerationState; 112*67e74705SXin Li@protocol NSFastEnumeration - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len; 113*67e74705SXin Li@end @class NSString, NSDictionary; 114*67e74705SXin Li@interface NSValue : NSObject <NSCopying, NSCoding> - (void)getValue:(void *)value; 115*67e74705SXin Li@end @interface NSNumber : NSValue - (char)charValue; 116*67e74705SXin Li- (id)initWithInt:(int)value; 117*67e74705SXin Li@end @class NSString; 118*67e74705SXin Li@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count; 119*67e74705SXin Li@end @interface NSArray (NSArrayCreation) + (id)array; 120*67e74705SXin Li@end @interface NSAutoreleasePool : NSObject { 121*67e74705SXin Li} 122*67e74705SXin Li- (void)drain; 123*67e74705SXin Li@end extern NSString * const NSBundleDidLoadNotification; 124*67e74705SXin Litypedef double NSTimeInterval; 125*67e74705SXin Li@interface NSDate : NSObject <NSCopying, NSCoding> - (NSTimeInterval)timeIntervalSinceReferenceDate; 126*67e74705SXin Li@end typedef unsigned short unichar; 127*67e74705SXin Li@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> 128*67e74705SXin Li- (NSUInteger)length; 129*67e74705SXin Li- (NSString *)stringByAppendingString:(NSString *)aString; 130*67e74705SXin Li- ( const char *)UTF8String; 131*67e74705SXin Li- (id)initWithUTF8String:(const char *)nullTerminatedCString; 132*67e74705SXin Li+ (id)stringWithUTF8String:(const char *)nullTerminatedCString; 133*67e74705SXin Li@end @class NSString, NSURL, NSError; 134*67e74705SXin Li@interface NSData : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; 135*67e74705SXin Li+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length; 136*67e74705SXin Li+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b; 137*67e74705SXin Li@end @class NSLocale, NSDate, NSCalendar, NSTimeZone, NSError, NSArray, NSMutableDictionary; 138*67e74705SXin Li@interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count; 139*67e74705SXin Li@end @interface NSMutableDictionary : NSDictionary - (void)removeObjectForKey:(id)aKey; 140*67e74705SXin Li- (void)setObject:(id)anObject forKey:(id)aKey; 141*67e74705SXin Li@end @interface NSMutableDictionary (NSMutableDictionaryCreation) + (id)dictionaryWithCapacity:(NSUInteger)numItems; 142*67e74705SXin Li@end typedef double CGFloat; 143*67e74705SXin Listruct CGSize { 144*67e74705SXin Li}; 145*67e74705SXin Litypedef struct CGSize CGSize; 146*67e74705SXin Listruct CGRect { 147*67e74705SXin Li}; 148*67e74705SXin Litypedef struct CGRect CGRect; 149*67e74705SXin Litypedef mach_port_t io_object_t; 150*67e74705SXin Litypedef char io_name_t[128]; 151*67e74705SXin Litypedef io_object_t io_iterator_t; 152*67e74705SXin Litypedef io_object_t io_service_t; 153*67e74705SXin Litypedef struct IONotificationPort * IONotificationPortRef; 154*67e74705SXin Litypedef void (*IOServiceMatchingCallback)( void * refcon, io_iterator_t iterator ); 155*67e74705SXin Liio_service_t IOServiceGetMatchingService( mach_port_t masterPort, CFDictionaryRef matching ); 156*67e74705SXin Likern_return_t IOServiceGetMatchingServices( mach_port_t masterPort, CFDictionaryRef matching, io_iterator_t * existing ); 157*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)); 158*67e74705SXin Likern_return_t IOServiceAddMatchingNotification( IONotificationPortRef notifyPort, const io_name_t notificationType, CFDictionaryRef matching, IOServiceMatchingCallback callback, void * refCon, io_iterator_t * notification ); 159*67e74705SXin LiCFMutableDictionaryRef IOServiceMatching( const char * name ); 160*67e74705SXin LiCFMutableDictionaryRef IOServiceNameMatching( const char * name ); 161*67e74705SXin LiCFMutableDictionaryRef IOBSDNameMatching( mach_port_t masterPort, uint32_t options, const char * bsdName ); 162*67e74705SXin LiCFMutableDictionaryRef IOOpenFirmwarePathMatching( mach_port_t masterPort, uint32_t options, const char * path ); 163*67e74705SXin LiCFMutableDictionaryRef IORegistryEntryIDMatching( uint64_t entryID ); 164*67e74705SXin Litypedef struct __DASession * DASessionRef; 165*67e74705SXin Liextern DASessionRef DASessionCreate( CFAllocatorRef allocator ); 166*67e74705SXin Litypedef struct __DADisk * DADiskRef; 167*67e74705SXin Liextern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name ); 168*67e74705SXin Liextern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media ); 169*67e74705SXin Liextern CFDictionaryRef DADiskCopyDescription( DADiskRef disk ); 170*67e74705SXin Liextern DADiskRef DADiskCopyWholeDisk( DADiskRef disk ); 171*67e74705SXin Li@interface NSTask : NSObject - (id)init; 172*67e74705SXin Li@end typedef struct CGColorSpace *CGColorSpaceRef; 173*67e74705SXin Litypedef struct CGImage *CGImageRef; 174*67e74705SXin Litypedef struct CGLayer *CGLayerRef; 175*67e74705SXin Li@interface NSResponder : NSObject <NSCoding> { 176*67e74705SXin Li} 177*67e74705SXin Li@end @protocol NSAnimatablePropertyContainer - (id)animator; 178*67e74705SXin Li@end extern NSString *NSAnimationTriggerOrderIn ; 179*67e74705SXin Li@interface NSView : NSResponder <NSAnimatablePropertyContainer> { 180*67e74705SXin Li} 181*67e74705SXin Li@end @protocol NSValidatedUserInterfaceItem - (SEL)action; 182*67e74705SXin Li@end @protocol NSUserInterfaceValidations - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem; 183*67e74705SXin Li@end @class NSDate, NSDictionary, NSError, NSException, NSNotification; 184*67e74705SXin Li@interface NSApplication : NSResponder <NSUserInterfaceValidations> { 185*67e74705SXin Li} 186*67e74705SXin Li@end enum { 187*67e74705SXin LiNSTerminateCancel = 0, NSTerminateNow = 1, NSTerminateLater = 2 }; 188*67e74705SXin Litypedef NSUInteger NSApplicationTerminateReply; 189*67e74705SXin Li@protocol NSApplicationDelegate <NSObject> @optional - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender; 190*67e74705SXin Li@end @class NSAttributedString, NSEvent, NSFont, NSFormatter, NSImage, NSMenu, NSText, NSView, NSTextView; 191*67e74705SXin Li@interface NSCell : NSObject <NSCopying, NSCoding> { 192*67e74705SXin Li} 193*67e74705SXin Li@end @class NSTextField, NSPanel, NSArray, NSWindow, NSImage, NSButton, NSError; 194*67e74705SXin Litypedef struct { 195*67e74705SXin Li} 196*67e74705SXin LiCVTimeStamp; 197*67e74705SXin Li@interface CIImage : NSObject <NSCoding, NSCopying> { 198*67e74705SXin Li} 199*67e74705SXin Litypedef int CIFormat; 200*67e74705SXin Li@end enum { 201*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 }; 202*67e74705SXin Litypedef mach_error_t DAReturn; 203*67e74705SXin Litypedef const struct __DADissenter * DADissenterRef; 204*67e74705SXin Liextern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string ); 205*67e74705SXin Li@interface CIContext: NSObject { 206*67e74705SXin Li} 207*67e74705SXin Li- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r; 208*67e74705SXin Li- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r format:(CIFormat)f colorSpace:(CGColorSpaceRef)cs; 209*67e74705SXin Li- (CGLayerRef)createCGLayerWithSize:(CGSize)size info:(CFDictionaryRef)d; 210*67e74705SXin Li@end extern NSString* const QCRendererEventKey; 211*67e74705SXin Li@protocol QCCompositionRenderer - (NSDictionary*) attributes; 212*67e74705SXin Li@end @interface QCRenderer : NSObject <QCCompositionRenderer> { 213*67e74705SXin Li} 214*67e74705SXin Li- (id) createSnapshotImageOfType:(NSString*)type; 215*67e74705SXin Li@end extern NSString* const QCViewDidStartRenderingNotification; 216*67e74705SXin Li@interface QCView : NSView <QCCompositionRenderer> { 217*67e74705SXin Li} 218*67e74705SXin Li- (id) createSnapshotImageOfType:(NSString*)type; 219*67e74705SXin Li@end enum { 220*67e74705SXin LiICEXIFOrientation1 = 1, ICEXIFOrientation2 = 2, ICEXIFOrientation3 = 3, ICEXIFOrientation4 = 4, ICEXIFOrientation5 = 5, ICEXIFOrientation6 = 6, ICEXIFOrientation7 = 7, ICEXIFOrientation8 = 8, }; 221*67e74705SXin Li@class ICDevice; 222*67e74705SXin Li@protocol ICDeviceDelegate <NSObject> @required - (void)didRemoveDevice:(ICDevice*)device; 223*67e74705SXin Li@end extern NSString *const ICScannerStatusWarmingUp; 224*67e74705SXin Li@class ICScannerDevice; 225*67e74705SXin Li@protocol ICScannerDeviceDelegate <ICDeviceDelegate> @optional - (void)scannerDeviceDidBecomeAvailable:(ICScannerDevice*)scanner; 226*67e74705SXin Li@end 227*67e74705SXin Li 228*67e74705SXin Litypedef long unsigned int __darwin_size_t; 229*67e74705SXin Litypedef __darwin_size_t size_t; 230*67e74705SXin Litypedef unsigned long CFTypeID; 231*67e74705SXin Listruct CGPoint { 232*67e74705SXin Li CGFloat x; 233*67e74705SXin Li CGFloat y; 234*67e74705SXin Li}; 235*67e74705SXin Litypedef struct CGPoint CGPoint; 236*67e74705SXin Litypedef struct CGGradient *CGGradientRef; 237*67e74705SXin Litypedef uint32_t CGGradientDrawingOptions; 238*67e74705SXin Liextern CFTypeID CGGradientGetTypeID(void); 239*67e74705SXin Liextern CGGradientRef CGGradientCreateWithColorComponents(CGColorSpaceRef 240*67e74705SXin Li space, const CGFloat components[], const CGFloat locations[], size_t count); 241*67e74705SXin Liextern CGGradientRef CGGradientCreateWithColors(CGColorSpaceRef space, 242*67e74705SXin Li CFArrayRef colors, const CGFloat locations[]); 243*67e74705SXin Liextern CGGradientRef CGGradientRetain(CGGradientRef gradient); 244*67e74705SXin Liextern void CGGradientRelease(CGGradientRef gradient); 245*67e74705SXin Litypedef struct CGContext *CGContextRef; 246*67e74705SXin Liextern void CGContextDrawLinearGradient(CGContextRef context, 247*67e74705SXin Li CGGradientRef gradient, CGPoint startPoint, CGPoint endPoint, 248*67e74705SXin Li CGGradientDrawingOptions options); 249*67e74705SXin Liextern CGColorSpaceRef CGColorSpaceCreateDeviceRGB(void); 250*67e74705SXin Li 251*67e74705SXin Li@interface NSMutableArray : NSObject 252*67e74705SXin Li- (void)addObject:(id)object; 253*67e74705SXin Li+ (id)array; 254*67e74705SXin Li@end 255*67e74705SXin Li 256*67e74705SXin Lienum { 257*67e74705SXin Li NSASCIIStringEncoding = 1, 258*67e74705SXin Li NSNEXTSTEPStringEncoding = 2, 259*67e74705SXin Li NSJapaneseEUCStringEncoding = 3, 260*67e74705SXin Li NSUTF8StringEncoding = 4, 261*67e74705SXin Li NSISOLatin1StringEncoding = 5, 262*67e74705SXin Li NSSymbolStringEncoding = 6, 263*67e74705SXin Li NSNonLossyASCIIStringEncoding = 7, 264*67e74705SXin Li}; 265*67e74705SXin Litypedef struct __CFString * CFMutableStringRef; 266*67e74705SXin Litypedef NSUInteger NSStringEncoding; 267*67e74705SXin Li 268*67e74705SXin Liextern CFStringRef CFStringCreateWithCStringNoCopy(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding, CFAllocatorRef contentsDeallocator); 269*67e74705SXin Li 270*67e74705SXin Li//===----------------------------------------------------------------------===// 271*67e74705SXin Li// Test cases. 272*67e74705SXin Li//===----------------------------------------------------------------------===// 273*67e74705SXin Li 274*67e74705SXin Livoid foo(id x) { 275*67e74705SXin Li [x retain]; 276*67e74705SXin Li} 277*67e74705SXin Li 278*67e74705SXin Livoid bar(id x) { 279*67e74705SXin Li [x release]; 280*67e74705SXin Li} 281*67e74705SXin Li 282*67e74705SXin Livoid test() { 283*67e74705SXin Li NSString *s = [[NSString alloc] init]; // expected-warning {{Potential leak}} 284*67e74705SXin Li foo(s); 285*67e74705SXin Li foo(s); 286*67e74705SXin Li bar(s); 287*67e74705SXin Li} 288*67e74705SXin Livoid test_neg() { 289*67e74705SXin Li NSString *s = [[NSString alloc] init]; // no-warning 290*67e74705SXin Li foo(s); 291*67e74705SXin Li foo(s); 292*67e74705SXin Li bar(s); 293*67e74705SXin Li bar(s); 294*67e74705SXin Li bar(s); 295*67e74705SXin Li} 296*67e74705SXin Li 297*67e74705SXin Li//===----------------------------------------------------------------------===// 298*67e74705SXin Li// Test returning retained and not-retained values. 299*67e74705SXin Li//===----------------------------------------------------------------------===// 300*67e74705SXin Li 301*67e74705SXin Li// On return (intraprocedural), assume CF objects are leaked. 302*67e74705SXin LiCFStringRef test_return_ratained_CF(char *bytes) { 303*67e74705SXin Li CFStringRef str; 304*67e74705SXin Li return CFStringCreateWithCStringNoCopy(0, bytes, NSNEXTSTEPStringEncoding, 0); // expected-warning {{leak}} 305*67e74705SXin Li} 306*67e74705SXin Li 307*67e74705SXin Li// On return (intraprocedural), assume NSObjects are not leaked. 308*67e74705SXin Liid test_return_retained_NS() { 309*67e74705SXin Li return [[NSString alloc] init]; // no-warning 310*67e74705SXin Li} 311*67e74705SXin Li 312*67e74705SXin Livoid test_test_return_retained() { 313*67e74705SXin Li id x = test_return_retained_NS(); // expected-warning {{leak}} 314*67e74705SXin Li [x retain]; 315*67e74705SXin Li [x release]; 316*67e74705SXin Li} 317*67e74705SXin Li 318*67e74705SXin Li//===----------------------------------------------------------------------===// 319*67e74705SXin Li// Test not applying "double effects" from inlining and RetainCountChecker summaries. 320*67e74705SXin Li// If we inline a call, we should already see its retain/release semantics. 321*67e74705SXin Li//===----------------------------------------------------------------------===// 322*67e74705SXin Li 323*67e74705SXin Li__attribute__((cf_returns_retained)) CFStringRef test_return_inline(CFStringRef x) { 324*67e74705SXin Li CFRetain(x); 325*67e74705SXin Li return x; 326*67e74705SXin Li} 327*67e74705SXin Li 328*67e74705SXin Livoid test_test_return_inline(char *bytes) { 329*67e74705SXin Li CFStringRef str = CFStringCreateWithCStringNoCopy(0, bytes, NSNEXTSTEPStringEncoding, 0); 330*67e74705SXin Li // After this call, 'str' really has +2 reference count. 331*67e74705SXin Li CFStringRef str2 = test_return_inline(str); 332*67e74705SXin Li // After this call, 'str' really has a +1 reference count. 333*67e74705SXin Li CFRelease(str); 334*67e74705SXin Li // After this call, 'str2' and 'str' has a +0 reference count. 335*67e74705SXin Li CFRelease(str2); 336*67e74705SXin Li} 337*67e74705SXin Li 338*67e74705SXin Livoid test_test_return_inline_2(char *bytes) { 339*67e74705SXin Li CFStringRef str = CFStringCreateWithCStringNoCopy(0, bytes, NSNEXTSTEPStringEncoding, 0); // expected-warning {{leak}} 340*67e74705SXin Li // After this call, 'str' really has +2 reference count. 341*67e74705SXin Li CFStringRef str2 = test_return_inline(str); 342*67e74705SXin Li // After this call, 'str' really has a +1 reference count. 343*67e74705SXin Li CFRelease(str); 344*67e74705SXin Li} 345*67e74705SXin Li 346*67e74705SXin Liextern CFStringRef getString(void); 347*67e74705SXin LiCFStringRef testCovariantReturnType(void) __attribute__((cf_returns_retained)); 348*67e74705SXin Li 349*67e74705SXin Livoid usetestCovariantReturnType() { 350*67e74705SXin Li CFStringRef S = ((void*)0); 351*67e74705SXin Li S = testCovariantReturnType(); 352*67e74705SXin Li if (S) 353*67e74705SXin Li CFRelease(S); 354*67e74705SXin Li} 355*67e74705SXin Li 356*67e74705SXin LiCFStringRef testCovariantReturnType() { 357*67e74705SXin Li CFStringRef Str = ((void*)0); 358*67e74705SXin Li Str = getString(); 359*67e74705SXin Li if (Str) { 360*67e74705SXin Li CFRetain(Str); 361*67e74705SXin Li } 362*67e74705SXin Li return Str; 363*67e74705SXin Li} 364*67e74705SXin Li 365*67e74705SXin Li// Test that we reanalyze ObjC methods which have been inlined. When reanalyzing 366*67e74705SXin Li// them, make sure we inline very small functions. 367*67e74705SXin Liid returnInputParam(id x) { 368*67e74705SXin Li return x; 369*67e74705SXin Li} 370*67e74705SXin Li 371*67e74705SXin Li@interface MyClass : NSObject 372*67e74705SXin Li- (id)test_reanalyze_as_top_level; 373*67e74705SXin Li- (void)test_inline_tiny_when_reanalyzing; 374*67e74705SXin Li- (void)inline_test_reanalyze_as_top_level; 375*67e74705SXin Li@end 376*67e74705SXin Li 377*67e74705SXin Li@implementation MyClass 378*67e74705SXin Li- (void)test_inline_tiny_when_reanalyzing { 379*67e74705SXin Li id x = [[NSString alloc] init]; // no-warning 380*67e74705SXin Li x = returnInputParam(x); 381*67e74705SXin Li [x release]; 382*67e74705SXin Li} 383*67e74705SXin Li 384*67e74705SXin Li- (id)test_reanalyze_as_top_level { 385*67e74705SXin Li // This method does not follow naming conventions, so a warning will be 386*67e74705SXin Li // reported when it is reanalyzed at top level. 387*67e74705SXin Li return [[NSString alloc] init]; // expected-warning {{leak}} 388*67e74705SXin Li} 389*67e74705SXin Li 390*67e74705SXin Li- (void)inline_test_reanalyze_as_top_level { 391*67e74705SXin Li id x = [self test_reanalyze_as_top_level]; 392*67e74705SXin Li [x release]; 393*67e74705SXin Li [self test_inline_tiny_when_reanalyzing]; 394*67e74705SXin Li} 395*67e74705SXin Li@end 396