Ted Kremenek | 35bdbf4 | 2011-05-02 19:42:42 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,osx.coreFoundation.CFRetainRelease,osx.cocoa.ClassRelease -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 | |
| 38 | typedef unsigned int __darwin_natural_t; |
| 39 | typedef unsigned long uintptr_t; |
| 40 | typedef unsigned int uint32_t; |
| 41 | typedef unsigned long long uint64_t; |
| 42 | typedef unsigned int UInt32; |
| 43 | typedef signed long CFIndex; |
| 44 | typedef struct { |
| 45 | CFIndex location; |
| 46 | CFIndex length; |
| 47 | } CFRange; |
| 48 | static __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 | } |
| 54 | typedef const void * CFTypeRef; |
| 55 | typedef const struct __CFString * CFStringRef; |
| 56 | typedef const struct __CFAllocator * CFAllocatorRef; |
| 57 | extern const CFAllocatorRef kCFAllocatorDefault; |
| 58 | extern CFTypeRef CFRetain(CFTypeRef cf); |
| 59 | extern void CFRelease(CFTypeRef cf); |
| 60 | typedef struct { |
| 61 | } |
| 62 | CFArrayCallBacks; |
| 63 | extern const CFArrayCallBacks kCFTypeArrayCallBacks; |
| 64 | typedef const struct __CFArray * CFArrayRef; |
| 65 | typedef struct __CFArray * CFMutableArrayRef; |
| 66 | extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks); |
| 67 | extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx); |
| 68 | extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value); |
| 69 | typedef struct { |
| 70 | } |
| 71 | CFDictionaryKeyCallBacks; |
| 72 | extern const CFDictionaryKeyCallBacks kCFTypeDictionaryKeyCallBacks; |
| 73 | typedef struct { |
| 74 | } |
| 75 | CFDictionaryValueCallBacks; |
| 76 | extern const CFDictionaryValueCallBacks kCFTypeDictionaryValueCallBacks; |
| 77 | typedef const struct __CFDictionary * CFDictionaryRef; |
| 78 | typedef struct __CFDictionary * CFMutableDictionaryRef; |
| 79 | extern CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFDictionaryKeyCallBacks *keyCallBacks, const CFDictionaryValueCallBacks *valueCallBacks); |
| 80 | typedef UInt32 CFStringEncoding; |
| 81 | enum { |
| 82 | kCFStringEncodingMacRoman = 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 }; |
| 83 | extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding); |
| 84 | typedef double CFTimeInterval; |
| 85 | typedef CFTimeInterval CFAbsoluteTime; |
| 86 | extern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void); |
| 87 | typedef const struct __CFDate * CFDateRef; |
| 88 | extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at); |
| 89 | extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate); |
| 90 | typedef __darwin_natural_t natural_t; |
| 91 | typedef natural_t mach_port_name_t; |
| 92 | typedef mach_port_name_t mach_port_t; |
| 93 | typedef int kern_return_t; |
| 94 | typedef kern_return_t mach_error_t; |
| 95 | enum { |
| 96 | kCFNumberSInt8Type = 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 }; |
| 97 | typedef CFIndex CFNumberType; |
| 98 | typedef const struct __CFNumber * CFNumberRef; |
| 99 | extern CFNumberRef CFNumberCreate(CFAllocatorRef allocator, CFNumberType theType, const void *valuePtr); |
| 100 | typedef const struct __CFAttributedString *CFAttributedStringRef; |
| 101 | typedef struct __CFAttributedString *CFMutableAttributedStringRef; |
| 102 | extern CFAttributedStringRef CFAttributedStringCreate(CFAllocatorRef alloc, CFStringRef str, CFDictionaryRef attributes) ; |
| 103 | extern CFMutableAttributedStringRef CFAttributedStringCreateMutableCopy(CFAllocatorRef alloc, CFIndex maxLength, CFAttributedStringRef aStr) ; |
| 104 | extern void CFAttributedStringSetAttribute(CFMutableAttributedStringRef aStr, CFRange range, CFStringRef attrName, CFTypeRef value) ; |
| 105 | typedef signed char BOOL; |
| 106 | typedef unsigned long NSUInteger; |
| 107 | @class NSString, Protocol; |
| 108 | extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); |
| 109 | typedef struct _NSZone NSZone; |
| 110 | @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 111 | @protocol NSObject |
| 112 | - (BOOL)isEqual:(id)object; |
| 113 | - (id)retain; |
| 114 | - (oneway void)release; |
| 115 | - (id)autorelease; |
| 116 | @end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; |
| 117 | @end @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; |
| 118 | @end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; |
| 119 | @end |
| 120 | @interface NSObject <NSObject> {} |
| 121 | + (id)allocWithZone:(NSZone *)zone; |
| 122 | + (id)alloc; |
| 123 | - (void)dealloc; |
Douglas Gregor | 4c4efee | 2011-06-13 16:42:53 +0000 | [diff] [blame] | 124 | - (id)init; |
Ted Kremenek | 35bdbf4 | 2011-05-02 19:42:42 +0000 | [diff] [blame] | 125 | @end |
| 126 | @interface NSObject (NSCoderMethods) |
| 127 | - (id)awakeAfterUsingCoder:(NSCoder *)aDecoder; |
| 128 | @end |
| 129 | extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); |
| 130 | typedef struct { |
| 131 | } |
| 132 | NSFastEnumerationState; |
| 133 | @protocol NSFastEnumeration - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len; |
| 134 | @end @class NSString, NSDictionary; |
| 135 | @interface NSValue : NSObject <NSCopying, NSCoding> - (void)getValue:(void *)value; |
| 136 | @end @interface NSNumber : NSValue - (char)charValue; |
| 137 | - (id)initWithInt:(int)value; |
| 138 | @end @class NSString; |
| 139 | @interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count; |
| 140 | @end @interface NSArray (NSArrayCreation) + (id)array; |
| 141 | @end @interface NSAutoreleasePool : NSObject { |
| 142 | } |
| 143 | - (void)drain; |
| 144 | @end extern NSString * const NSBundleDidLoadNotification; |
| 145 | typedef double NSTimeInterval; |
| 146 | @interface NSDate : NSObject <NSCopying, NSCoding> - (NSTimeInterval)timeIntervalSinceReferenceDate; |
| 147 | @end typedef unsigned short unichar; |
| 148 | @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; |
| 149 | - ( const char *)UTF8String; |
| 150 | - (id)initWithUTF8String:(const char *)nullTerminatedCString; |
| 151 | + (id)stringWithUTF8String:(const char *)nullTerminatedCString; |
| 152 | @end @class NSString, NSURL, NSError; |
| 153 | @interface NSData : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; |
| 154 | + (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length; |
| 155 | + (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b; |
| 156 | @end @class NSLocale, NSDate, NSCalendar, NSTimeZone, NSError, NSArray, NSMutableDictionary; |
| 157 | @interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count; |
| 158 | @end @interface NSMutableDictionary : NSDictionary - (void)removeObjectForKey:(id)aKey; |
| 159 | - (void)setObject:(id)anObject forKey:(id)aKey; |
| 160 | @end @interface NSMutableDictionary (NSMutableDictionaryCreation) + (id)dictionaryWithCapacity:(NSUInteger)numItems; |
| 161 | @end typedef double CGFloat; |
| 162 | struct CGSize { |
| 163 | }; |
| 164 | typedef struct CGSize CGSize; |
| 165 | struct CGRect { |
| 166 | }; |
| 167 | typedef struct CGRect CGRect; |
| 168 | typedef mach_port_t io_object_t; |
| 169 | typedef char io_name_t[128]; |
| 170 | typedef io_object_t io_iterator_t; |
| 171 | typedef io_object_t io_service_t; |
| 172 | typedef struct IONotificationPort * IONotificationPortRef; |
| 173 | typedef void (*IOServiceMatchingCallback)( void * refcon, io_iterator_t iterator ); |
| 174 | io_service_t IOServiceGetMatchingService( mach_port_t masterPort, CFDictionaryRef matching ); |
| 175 | kern_return_t IOServiceGetMatchingServices( mach_port_t masterPort, CFDictionaryRef matching, io_iterator_t * existing ); |
| 176 | kern_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)); |
| 177 | kern_return_t IOServiceAddMatchingNotification( IONotificationPortRef notifyPort, const io_name_t notificationType, CFDictionaryRef matching, IOServiceMatchingCallback callback, void * refCon, io_iterator_t * notification ); |
| 178 | CFMutableDictionaryRef IOServiceMatching( const char * name ); |
| 179 | CFMutableDictionaryRef IOServiceNameMatching( const char * name ); |
| 180 | CFMutableDictionaryRef IOBSDNameMatching( mach_port_t masterPort, uint32_t options, const char * bsdName ); |
| 181 | CFMutableDictionaryRef IOOpenFirmwarePathMatching( mach_port_t masterPort, uint32_t options, const char * path ); |
| 182 | CFMutableDictionaryRef IORegistryEntryIDMatching( uint64_t entryID ); |
| 183 | typedef struct __DASession * DASessionRef; |
| 184 | extern DASessionRef DASessionCreate( CFAllocatorRef allocator ); |
| 185 | typedef struct __DADisk * DADiskRef; |
| 186 | extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name ); |
| 187 | extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media ); |
| 188 | extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk ); |
| 189 | extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk ); |
| 190 | @interface NSTask : NSObject - (id)init; |
| 191 | @end typedef struct CGColorSpace *CGColorSpaceRef; |
| 192 | typedef struct CGImage *CGImageRef; |
| 193 | typedef struct CGLayer *CGLayerRef; |
| 194 | @interface NSResponder : NSObject <NSCoding> { |
| 195 | } |
| 196 | @end @protocol NSAnimatablePropertyContainer - (id)animator; |
| 197 | @end extern NSString *NSAnimationTriggerOrderIn ; |
| 198 | @interface NSView : NSResponder <NSAnimatablePropertyContainer> { |
| 199 | } |
| 200 | @end @protocol NSValidatedUserInterfaceItem - (SEL)action; |
| 201 | @end @protocol NSUserInterfaceValidations - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem; |
| 202 | @end @class NSDate, NSDictionary, NSError, NSException, NSNotification; |
| 203 | @interface NSApplication : NSResponder <NSUserInterfaceValidations> { |
| 204 | } |
| 205 | @end enum { |
| 206 | NSTerminateCancel = 0, NSTerminateNow = 1, NSTerminateLater = 2 }; |
| 207 | typedef NSUInteger NSApplicationTerminateReply; |
| 208 | @protocol NSApplicationDelegate <NSObject> @optional - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender; |
| 209 | @end @class NSAttributedString, NSEvent, NSFont, NSFormatter, NSImage, NSMenu, NSText, NSView, NSTextView; |
| 210 | @interface NSCell : NSObject <NSCopying, NSCoding> { |
| 211 | } |
| 212 | @end @class NSTextField, NSPanel, NSArray, NSWindow, NSImage, NSButton, NSError; |
| 213 | typedef struct { |
| 214 | } |
| 215 | CVTimeStamp; |
| 216 | @interface CIImage : NSObject <NSCoding, NSCopying> { |
| 217 | } |
| 218 | typedef int CIFormat; |
| 219 | @end enum { |
| 220 | kDAReturnSuccess = 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 }; |
| 221 | typedef mach_error_t DAReturn; |
| 222 | typedef const struct __DADissenter * DADissenterRef; |
| 223 | extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string ); |
| 224 | @interface CIContext: NSObject { |
| 225 | } |
| 226 | - (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r; |
| 227 | - (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r format:(CIFormat)f colorSpace:(CGColorSpaceRef)cs; |
| 228 | - (CGLayerRef)createCGLayerWithSize:(CGSize)size info:(CFDictionaryRef)d; |
| 229 | @end extern NSString* const QCRendererEventKey; |
| 230 | @protocol QCCompositionRenderer - (NSDictionary*) attributes; |
| 231 | @end @interface QCRenderer : NSObject <QCCompositionRenderer> { |
| 232 | } |
| 233 | - (id) createSnapshotImageOfType:(NSString*)type; |
| 234 | @end extern NSString* const QCViewDidStartRenderingNotification; |
| 235 | @interface QCView : NSView <QCCompositionRenderer> { |
| 236 | } |
| 237 | - (id) createSnapshotImageOfType:(NSString*)type; |
| 238 | @end enum { |
| 239 | ICEXIFOrientation1 = 1, ICEXIFOrientation2 = 2, ICEXIFOrientation3 = 3, ICEXIFOrientation4 = 4, ICEXIFOrientation5 = 5, ICEXIFOrientation6 = 6, ICEXIFOrientation7 = 7, ICEXIFOrientation8 = 8, }; |
| 240 | @class ICDevice; |
| 241 | @protocol ICDeviceDelegate <NSObject> @required - (void)didRemoveDevice:(ICDevice*)device; |
| 242 | @end extern NSString *const ICScannerStatusWarmingUp; |
| 243 | @class ICScannerDevice; |
| 244 | @protocol ICScannerDeviceDelegate <ICDeviceDelegate> @optional - (void)scannerDeviceDidBecomeAvailable:(ICScannerDevice*)scanner; |
| 245 | @end |
| 246 | |
| 247 | typedef long unsigned int __darwin_size_t; |
| 248 | typedef __darwin_size_t size_t; |
| 249 | typedef unsigned long CFTypeID; |
| 250 | struct CGPoint { |
| 251 | CGFloat x; |
| 252 | CGFloat y; |
| 253 | }; |
| 254 | typedef struct CGPoint CGPoint; |
| 255 | typedef struct CGGradient *CGGradientRef; |
| 256 | typedef uint32_t CGGradientDrawingOptions; |
| 257 | extern CFTypeID CGGradientGetTypeID(void); |
| 258 | extern CGGradientRef CGGradientCreateWithColorComponents(CGColorSpaceRef |
| 259 | space, const CGFloat components[], const CGFloat locations[], size_t count); |
| 260 | extern CGGradientRef CGGradientCreateWithColors(CGColorSpaceRef space, |
| 261 | CFArrayRef colors, const CGFloat locations[]); |
| 262 | extern CGGradientRef CGGradientRetain(CGGradientRef gradient); |
| 263 | extern void CGGradientRelease(CGGradientRef gradient); |
| 264 | typedef struct CGContext *CGContextRef; |
| 265 | extern void CGContextDrawLinearGradient(CGContextRef context, |
| 266 | CGGradientRef gradient, CGPoint startPoint, CGPoint endPoint, |
| 267 | CGGradientDrawingOptions options); |
| 268 | extern CGColorSpaceRef CGColorSpaceCreateDeviceRGB(void); |
| 269 | |
| 270 | //===----------------------------------------------------------------------===// |
| 271 | // Test cases. |
| 272 | //===----------------------------------------------------------------------===// |
| 273 | |
| 274 | class SmartPointer { |
| 275 | id x; |
| 276 | public: |
| 277 | SmartPointer(id x) : x(x) {} |
| 278 | ~SmartPointer() { [x release]; } |
Ted Kremenek | 9ca2851 | 2011-05-02 21:21:42 +0000 | [diff] [blame] | 279 | |
| 280 | void adopt(id x); |
| 281 | void noAdopt(id x); |
Ted Kremenek | 35bdbf4 | 2011-05-02 19:42:42 +0000 | [diff] [blame] | 282 | }; |
| 283 | |
Ted Kremenek | 9ca2851 | 2011-05-02 21:21:42 +0000 | [diff] [blame] | 284 | void test_positive() { |
| 285 | id x = [[NSObject alloc] init]; // expected-warning {{leak}} |
| 286 | } |
| 287 | |
Ted Kremenek | 35bdbf4 | 2011-05-02 19:42:42 +0000 | [diff] [blame] | 288 | void test_smartpointer_1() { |
| 289 | id x = [[NSObject alloc] init]; // no-warning |
| 290 | SmartPointer foo(x); |
| 291 | } |
| 292 | |
Ted Kremenek | 9ca2851 | 2011-05-02 21:21:42 +0000 | [diff] [blame] | 293 | void test_smartpointer_2() { |
| 294 | id x = [[NSObject alloc] init]; // no-warning |
| 295 | SmartPointer foo(0); |
| 296 | foo.adopt(x); |
| 297 | } |
| 298 | |
| 299 | // FIXME: Eventually we want annotations to say whether or not |
| 300 | // a C++ method claims ownership of an Objective-C object. |
| 301 | void test_smartpointer_3() { |
| 302 | id x = [[NSObject alloc] init]; // no-warning |
| 303 | SmartPointer foo(0); |
| 304 | foo.noAdopt(x); |
| 305 | } |
| 306 | |
| 307 | |