1// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,osx.coreFoundation.CFRetainRelease,osx.cocoa.ClassRelease,osx.cocoa.RetainCount -analyzer-store=region -fblocks -verify %s 2 3#if __has_feature(attribute_ns_returns_retained) 4#define NS_RETURNS_RETAINED __attribute__((ns_returns_retained)) 5#endif 6#if __has_feature(attribute_cf_returns_retained) 7#define CF_RETURNS_RETAINED __attribute__((cf_returns_retained)) 8#endif 9#if __has_feature(attribute_ns_returns_not_retained) 10#define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained)) 11#endif 12#if __has_feature(attribute_cf_returns_not_retained) 13#define CF_RETURNS_NOT_RETAINED __attribute__((cf_returns_not_retained)) 14#endif 15#if __has_feature(attribute_ns_consumes_self) 16#define NS_CONSUMES_SELF __attribute__((ns_consumes_self)) 17#endif 18#if __has_feature(attribute_ns_consumed) 19#define NS_CONSUMED __attribute__((ns_consumed)) 20#endif 21#if __has_feature(attribute_cf_consumed) 22#define CF_CONSUMED __attribute__((cf_consumed)) 23#endif 24 25//===----------------------------------------------------------------------===// 26// The following code is reduced using delta-debugging from Mac OS X headers: 27// 28// #include <Cocoa/Cocoa.h> 29// #include <CoreFoundation/CoreFoundation.h> 30// #include <DiskArbitration/DiskArbitration.h> 31// #include <QuartzCore/QuartzCore.h> 32// #include <Quartz/Quartz.h> 33// #include <IOKit/IOKitLib.h> 34// 35// It includes the basic definitions for the test cases below. 36//===----------------------------------------------------------------------===// 37 38typedef unsigned int __darwin_natural_t; 39typedef unsigned long uintptr_t; 40typedef unsigned int uint32_t; 41typedef unsigned long long uint64_t; 42typedef unsigned int UInt32; 43typedef signed long CFIndex; 44typedef struct { 45 CFIndex location; 46 CFIndex length; 47} CFRange; 48static __inline__ __attribute__((always_inline)) CFRange CFRangeMake(CFIndex loc, CFIndex len) { 49 CFRange range; 50 range.location = loc; 51 range.length = len; 52 return range; 53} 54typedef const void * CFTypeRef; 55typedef const struct __CFString * CFStringRef; 56typedef const struct __CFAllocator * CFAllocatorRef; 57extern const CFAllocatorRef kCFAllocatorDefault; 58extern CFTypeRef CFRetain(CFTypeRef cf); 59extern void CFRelease(CFTypeRef cf); 60typedef struct { 61} 62CFArrayCallBacks; 63extern const CFArrayCallBacks kCFTypeArrayCallBacks; 64typedef const struct __CFArray * CFArrayRef; 65typedef struct __CFArray * CFMutableArrayRef; 66extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks); 67extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx); 68extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value); 69typedef struct { 70} 71CFDictionaryKeyCallBacks; 72extern const CFDictionaryKeyCallBacks kCFTypeDictionaryKeyCallBacks; 73typedef struct { 74} 75CFDictionaryValueCallBacks; 76extern const CFDictionaryValueCallBacks kCFTypeDictionaryValueCallBacks; 77typedef const struct __CFDictionary * CFDictionaryRef; 78typedef struct __CFDictionary * CFMutableDictionaryRef; 79extern CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFDictionaryKeyCallBacks *keyCallBacks, const CFDictionaryValueCallBacks *valueCallBacks); 80typedef UInt32 CFStringEncoding; 81enum { 82kCFStringEncodingMacRoman = 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 }; 83extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding); 84typedef double CFTimeInterval; 85typedef CFTimeInterval CFAbsoluteTime; 86extern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void); 87typedef const struct __CFDate * CFDateRef; 88extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at); 89extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate); 90typedef __darwin_natural_t natural_t; 91typedef natural_t mach_port_name_t; 92typedef mach_port_name_t mach_port_t; 93typedef int kern_return_t; 94typedef kern_return_t mach_error_t; 95enum { 96kCFNumberSInt8Type = 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 }; 97typedef CFIndex CFNumberType; 98typedef const struct __CFNumber * CFNumberRef; 99extern CFNumberRef CFNumberCreate(CFAllocatorRef allocator, CFNumberType theType, const void *valuePtr); 100typedef const struct __CFAttributedString *CFAttributedStringRef; 101typedef struct __CFAttributedString *CFMutableAttributedStringRef; 102extern CFAttributedStringRef CFAttributedStringCreate(CFAllocatorRef alloc, CFStringRef str, CFDictionaryRef attributes) ; 103extern CFMutableAttributedStringRef CFAttributedStringCreateMutableCopy(CFAllocatorRef alloc, CFIndex maxLength, CFAttributedStringRef aStr) ; 104extern void CFAttributedStringSetAttribute(CFMutableAttributedStringRef aStr, CFRange range, CFStringRef attrName, CFTypeRef value) ; 105typedef signed char BOOL; 106typedef unsigned long NSUInteger; 107@class NSString, Protocol; 108extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); 109typedef struct _NSZone NSZone; 110@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; 111@protocol NSObject 112- (BOOL)isEqual:(id)object; 113- (id)retain; 114- (id)copy; 115- (oneway void)release; 116- (id)autorelease; 117@end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; 118@end @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; 119@end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; 120@end 121@interface NSObject <NSObject> {} 122+ (id)allocWithZone:(NSZone *)zone; 123+ (id)alloc; 124- (void)dealloc; 125- (id)init; 126@end 127@interface NSObject (NSCoderMethods) 128- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder; 129@end 130extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); 131typedef struct { 132} 133NSFastEnumerationState; 134@protocol NSFastEnumeration - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len; 135@end @class NSString, NSDictionary; 136@interface NSValue : NSObject <NSCopying, NSCoding> - (void)getValue:(void *)value; 137@end @interface NSNumber : NSValue - (char)charValue; 138- (id)initWithInt:(int)value; 139@end @class NSString; 140@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count; 141@end @interface NSArray (NSArrayCreation) + (id)array; 142@end @interface NSAutoreleasePool : NSObject { 143} 144- (void)drain; 145@end extern NSString * const NSBundleDidLoadNotification; 146typedef double NSTimeInterval; 147@interface NSDate : NSObject <NSCopying, NSCoding> - (NSTimeInterval)timeIntervalSinceReferenceDate; 148@end typedef unsigned short unichar; 149@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; 150- ( const char *)UTF8String; 151- (id)initWithUTF8String:(const char *)nullTerminatedCString; 152+ (id)stringWithUTF8String:(const char *)nullTerminatedCString; 153@end @class NSString, NSURL, NSError; 154@interface NSData : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; 155+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length; 156+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b; 157@end @class NSLocale, NSDate, NSCalendar, NSTimeZone, NSError, NSArray, NSMutableDictionary; 158@interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count; 159@end @interface NSMutableDictionary : NSDictionary - (void)removeObjectForKey:(id)aKey; 160- (void)setObject:(id)anObject forKey:(id)aKey; 161@end @interface NSMutableDictionary (NSMutableDictionaryCreation) + (id)dictionaryWithCapacity:(NSUInteger)numItems; 162@end typedef double CGFloat; 163struct CGSize { 164}; 165typedef struct CGSize CGSize; 166struct CGRect { 167}; 168typedef struct CGRect CGRect; 169typedef mach_port_t io_object_t; 170typedef char io_name_t[128]; 171typedef io_object_t io_iterator_t; 172typedef io_object_t io_service_t; 173typedef struct IONotificationPort * IONotificationPortRef; 174typedef void (*IOServiceMatchingCallback)( void * refcon, io_iterator_t iterator ); 175io_service_t IOServiceGetMatchingService( mach_port_t masterPort, CFDictionaryRef matching ); 176kern_return_t IOServiceGetMatchingServices( mach_port_t masterPort, CFDictionaryRef matching, io_iterator_t * existing ); 177kern_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)); 178kern_return_t IOServiceAddMatchingNotification( IONotificationPortRef notifyPort, const io_name_t notificationType, CFDictionaryRef matching, IOServiceMatchingCallback callback, void * refCon, io_iterator_t * notification ); 179CFMutableDictionaryRef IOServiceMatching( const char * name ); 180CFMutableDictionaryRef IOServiceNameMatching( const char * name ); 181CFMutableDictionaryRef IOBSDNameMatching( mach_port_t masterPort, uint32_t options, const char * bsdName ); 182CFMutableDictionaryRef IOOpenFirmwarePathMatching( mach_port_t masterPort, uint32_t options, const char * path ); 183CFMutableDictionaryRef IORegistryEntryIDMatching( uint64_t entryID ); 184typedef struct __DASession * DASessionRef; 185extern DASessionRef DASessionCreate( CFAllocatorRef allocator ); 186typedef struct __DADisk * DADiskRef; 187extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name ); 188extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media ); 189extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk ); 190extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk ); 191@interface NSTask : NSObject - (id)init; 192@end typedef struct CGColorSpace *CGColorSpaceRef; 193typedef struct CGImage *CGImageRef; 194typedef struct CGLayer *CGLayerRef; 195@interface NSResponder : NSObject <NSCoding> { 196} 197@end @protocol NSAnimatablePropertyContainer - (id)animator; 198@end extern NSString *NSAnimationTriggerOrderIn ; 199@interface NSView : NSResponder <NSAnimatablePropertyContainer> { 200} 201@end @protocol NSValidatedUserInterfaceItem - (SEL)action; 202@end @protocol NSUserInterfaceValidations - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem; 203@end @class NSDate, NSDictionary, NSError, NSException, NSNotification; 204@interface NSApplication : NSResponder <NSUserInterfaceValidations> { 205} 206@end enum { 207NSTerminateCancel = 0, NSTerminateNow = 1, NSTerminateLater = 2 }; 208typedef NSUInteger NSApplicationTerminateReply; 209@protocol NSApplicationDelegate <NSObject> @optional - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender; 210@end @class NSAttributedString, NSEvent, NSFont, NSFormatter, NSImage, NSMenu, NSText, NSView, NSTextView; 211@interface NSCell : NSObject <NSCopying, NSCoding> { 212} 213@end @class NSTextField, NSPanel, NSArray, NSWindow, NSImage, NSButton, NSError; 214typedef struct { 215} 216CVTimeStamp; 217@interface CIImage : NSObject <NSCoding, NSCopying> { 218} 219typedef int CIFormat; 220@end enum { 221kDAReturnSuccess = 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 }; 222typedef mach_error_t DAReturn; 223typedef const struct __DADissenter * DADissenterRef; 224extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string ); 225@interface CIContext: NSObject { 226} 227- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r; 228- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r format:(CIFormat)f colorSpace:(CGColorSpaceRef)cs; 229- (CGLayerRef)createCGLayerWithSize:(CGSize)size info:(CFDictionaryRef)d; 230@end extern NSString* const QCRendererEventKey; 231@protocol QCCompositionRenderer - (NSDictionary*) attributes; 232@end @interface QCRenderer : NSObject <QCCompositionRenderer> { 233} 234- (id) createSnapshotImageOfType:(NSString*)type; 235@end extern NSString* const QCViewDidStartRenderingNotification; 236@interface QCView : NSView <QCCompositionRenderer> { 237} 238- (id) createSnapshotImageOfType:(NSString*)type; 239@end enum { 240ICEXIFOrientation1 = 1, ICEXIFOrientation2 = 2, ICEXIFOrientation3 = 3, ICEXIFOrientation4 = 4, ICEXIFOrientation5 = 5, ICEXIFOrientation6 = 6, ICEXIFOrientation7 = 7, ICEXIFOrientation8 = 8, }; 241@class ICDevice; 242@protocol ICDeviceDelegate <NSObject> @required - (void)didRemoveDevice:(ICDevice*)device; 243@end extern NSString *const ICScannerStatusWarmingUp; 244@class ICScannerDevice; 245@protocol ICScannerDeviceDelegate <ICDeviceDelegate> @optional - (void)scannerDeviceDidBecomeAvailable:(ICScannerDevice*)scanner; 246@end 247 248typedef long unsigned int __darwin_size_t; 249typedef __darwin_size_t size_t; 250typedef unsigned long CFTypeID; 251struct CGPoint { 252 CGFloat x; 253 CGFloat y; 254}; 255typedef struct CGPoint CGPoint; 256typedef struct CGGradient *CGGradientRef; 257typedef uint32_t CGGradientDrawingOptions; 258extern CFTypeID CGGradientGetTypeID(void); 259extern CGGradientRef CGGradientCreateWithColorComponents(CGColorSpaceRef 260 space, const CGFloat components[], const CGFloat locations[], size_t count); 261extern CGGradientRef CGGradientCreateWithColors(CGColorSpaceRef space, 262 CFArrayRef colors, const CGFloat locations[]); 263extern CGGradientRef CGGradientRetain(CGGradientRef gradient); 264extern void CGGradientRelease(CGGradientRef gradient); 265typedef struct CGContext *CGContextRef; 266extern void CGContextDrawLinearGradient(CGContextRef context, 267 CGGradientRef gradient, CGPoint startPoint, CGPoint endPoint, 268 CGGradientDrawingOptions options); 269extern CGColorSpaceRef CGColorSpaceCreateDeviceRGB(void); 270 271//===----------------------------------------------------------------------===// 272// Test cases. 273//===----------------------------------------------------------------------===// 274 275class SmartPointer { 276 id x; 277public: 278 SmartPointer(id x) : x(x) {} 279 ~SmartPointer() { [x release]; } 280 281 void adopt(id x); 282 void noAdopt(id x); 283}; 284 285void test_positive() { 286 id x = [[NSObject alloc] init]; // expected-warning {{leak}} 287} 288 289void test_smartpointer_1() { 290 id x = [[NSObject alloc] init]; // no-warning 291 SmartPointer foo(x); 292} 293 294void test_smartpointer_2() { 295 id x = [[NSObject alloc] init]; // no-warning 296 SmartPointer foo(0); 297 foo.adopt(x); 298} 299 300// FIXME: Eventually we want annotations to say whether or not 301// a C++ method claims ownership of an Objective-C object. 302void test_smartpointer_3() { 303 id x = [[NSObject alloc] init]; // no-warning 304 SmartPointer foo(0); 305 foo.noAdopt(x); 306} 307 308void test_smartpointer_4() { 309 id x = [[NSObject alloc] init]; // no-warning 310 SmartPointer *foo = new SmartPointer(x); 311 delete foo; 312} 313 314extern CFStringRef ElectronMicroscopyEngage(void); 315void test_microscopy() { 316 NSString *token = (NSString*) ElectronMicroscopyEngage(); 317 [token release]; // expected-warning {{object that is not owned}} 318} 319 320extern CFStringRef Scopy(void); 321void test_Scopy() { 322 NSString *token = (NSString*) Scopy(); 323 [token release]; // expected-warning {{object that is not owned}} 324} 325 326//===----------------------------------------------------------------------===// 327// Test handling of template functions used to do magic with 328// tracked retained pointers. 329//===----------------------------------------------------------------------===// 330 331template <typename T, typename U> T static_objc_cast(U* value) 332{ 333 // ...debugging code omitted... 334 return static_cast<T>(value); 335} 336 337int rdar10553686(void) 338{ 339 NSObject* bar = static_objc_cast<NSObject*>([[NSObject alloc] init]); 340 [bar release]; 341 return 0; 342} 343int rdar10553686_positive(void) 344{ 345 NSObject* bar = static_objc_cast<NSObject*>([[NSObject alloc] init]); 346 [bar release]; 347 [bar retain]; // expected-warning {{used after it is released}} 348 return 0; 349} 350 351@interface NSMapTable : NSObject <NSCopying, NSCoding, NSFastEnumeration> 352@end 353extern void *NSMapGet(NSMapTable *table, const void *key); 354extern void NSMapInsert(NSMapTable *table, const void *key, const void *value); 355extern void NSMapInsertKnownAbsent(NSMapTable *table, const void *key, const void *value); 356char *strdup(const char *s); 357 358NSString * radar11152419(NSString *string1, NSString *key1, NSMapTable *map) { 359 NSString *string = ( NSString *)NSMapGet(map, key1); 360 if (!string) { 361 string = [string1 copy]; 362 NSString *key = [key1 copy]; 363 NSMapInsert(map, (void*) key, (void*)string); // no warning 364 NSMapInsertKnownAbsent(map, (void*)key, (void*)string); // no warning 365 } 366 return string; 367} 368 369//===----------------------------------------------------------------------===// 370// Don't crash on non-member functions with "callbacks" but without names. 371//===----------------------------------------------------------------------===// 372 373struct IntWrapper { 374 int arg; 375}; 376 377int operator>> (const IntWrapper &W, int (*f)(int)) { 378 return f(W.arg); 379} 380 381void testCallback() { 382 IntWrapper val = { 42 }; 383 384 extern int process(int); 385 val >> process; 386} 387 388