blob: 9e5151d16704866cbba9c09970cecb5d8dfd1930 [file] [log] [blame]
Ted Kremenek565e4652010-02-05 02:06:54 +00001// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-check-objc-mem -analyzer-store=basic -fblocks -verify %s
2// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-check-objc-mem -analyzer-store=region -fblocks -verify %s
Ted Kremenek0964a062009-01-21 06:57:53 +00003
Ted Kremenek71ef5d62009-07-17 00:19:33 +00004#if __has_feature(attribute_ns_returns_retained)
5#define NS_RETURNS_RETAINED __attribute__((ns_returns_retained))
6#endif
7#if __has_feature(attribute_cf_returns_retained)
8#define CF_RETURNS_RETAINED __attribute__((cf_returns_retained))
9#endif
Ted Kremenek60411112010-02-18 00:06:12 +000010#if __has_feature(attribute_ns_returns_not_retained)
11#define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained))
12#endif
13#if __has_feature(attribute_cf_returns_not_retained)
14#define CF_RETURNS_NOT_RETAINED __attribute__((cf_returns_not_retained))
15#endif
Ted Kremenek71ef5d62009-07-17 00:19:33 +000016
Ted Kremenek12619382009-01-12 21:45:02 +000017//===----------------------------------------------------------------------===//
Ted Kremenek767d6492009-05-20 22:39:57 +000018// The following code is reduced using delta-debugging from Mac OS X headers:
19//
20// #include <Cocoa/Cocoa.h>
21// #include <CoreFoundation/CoreFoundation.h>
22// #include <DiskArbitration/DiskArbitration.h>
23// #include <QuartzCore/QuartzCore.h>
24// #include <Quartz/Quartz.h>
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +000025// #include <IOKit/IOKitLib.h>
Ted Kremenek12619382009-01-12 21:45:02 +000026//
27// It includes the basic definitions for the test cases below.
Ted Kremenek12619382009-01-12 21:45:02 +000028//===----------------------------------------------------------------------===//
29
30typedef unsigned int __darwin_natural_t;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +000031typedef unsigned long uintptr_t;
32typedef unsigned int uint32_t;
33typedef unsigned long long uint64_t;
Ted Kremenekfde52c92009-04-29 00:41:31 +000034typedef unsigned int UInt32;
Ted Kremenek12619382009-01-12 21:45:02 +000035typedef signed long CFIndex;
Ted Kremenekc4843812009-08-20 00:57:22 +000036typedef struct {
37 CFIndex location;
38 CFIndex length;
39} CFRange;
40static __inline__ __attribute__((always_inline)) CFRange CFRangeMake(CFIndex loc, CFIndex len) {
41 CFRange range;
42 range.location = loc;
43 range.length = len;
44 return range;
45}
Ted Kremenek12619382009-01-12 21:45:02 +000046typedef const void * CFTypeRef;
47typedef const struct __CFString * CFStringRef;
48typedef const struct __CFAllocator * CFAllocatorRef;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +000049extern const CFAllocatorRef kCFAllocatorDefault;
Ted Kremenek12619382009-01-12 21:45:02 +000050extern CFTypeRef CFRetain(CFTypeRef cf);
51extern void CFRelease(CFTypeRef cf);
52typedef struct {
53}
54CFArrayCallBacks;
55extern const CFArrayCallBacks kCFTypeArrayCallBacks;
56typedef const struct __CFArray * CFArrayRef;
57typedef struct __CFArray * CFMutableArrayRef;
58extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks);
59extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx);
Ted Kremenekcd57fce2009-03-09 22:28:18 +000060extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value);
Ted Kremenekc4843812009-08-20 00:57:22 +000061typedef struct {
62}
63CFDictionaryKeyCallBacks;
64extern const CFDictionaryKeyCallBacks kCFTypeDictionaryKeyCallBacks;
65typedef struct {
66}
67CFDictionaryValueCallBacks;
68extern const CFDictionaryValueCallBacks kCFTypeDictionaryValueCallBacks;
Ted Kremenek12619382009-01-12 21:45:02 +000069typedef const struct __CFDictionary * CFDictionaryRef;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +000070typedef struct __CFDictionary * CFMutableDictionaryRef;
Ted Kremenekc4843812009-08-20 00:57:22 +000071extern CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFDictionaryKeyCallBacks *keyCallBacks, const CFDictionaryValueCallBacks *valueCallBacks);
Ted Kremenek12619382009-01-12 21:45:02 +000072typedef UInt32 CFStringEncoding;
73enum {
74kCFStringEncodingMacRoman = 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 };
75extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding);
76typedef double CFTimeInterval;
77typedef CFTimeInterval CFAbsoluteTime;
Ted Kremenekcd57fce2009-03-09 22:28:18 +000078extern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void);
Ted Kremenek12619382009-01-12 21:45:02 +000079typedef const struct __CFDate * CFDateRef;
80extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at);
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +000081extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate);
Ted Kremenek12619382009-01-12 21:45:02 +000082typedef __darwin_natural_t natural_t;
83typedef natural_t mach_port_name_t;
84typedef mach_port_name_t mach_port_t;
Ted Kremenekfde52c92009-04-29 00:41:31 +000085typedef int kern_return_t;
86typedef kern_return_t mach_error_t;
Ted Kremenekc4843812009-08-20 00:57:22 +000087enum {
88kCFNumberSInt8Type = 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 };
89typedef CFIndex CFNumberType;
90typedef const struct __CFNumber * CFNumberRef;
91extern CFNumberRef CFNumberCreate(CFAllocatorRef allocator, CFNumberType theType, const void *valuePtr);
92typedef const struct __CFAttributedString *CFAttributedStringRef;
93typedef struct __CFAttributedString *CFMutableAttributedStringRef;
94extern CFAttributedStringRef CFAttributedStringCreate(CFAllocatorRef alloc, CFStringRef str, CFDictionaryRef attributes) ;
95extern CFMutableAttributedStringRef CFAttributedStringCreateMutableCopy(CFAllocatorRef alloc, CFIndex maxLength, CFAttributedStringRef aStr) ;
96extern void CFAttributedStringSetAttribute(CFMutableAttributedStringRef aStr, CFRange range, CFStringRef attrName, CFTypeRef value) ;
Ted Kremenek12619382009-01-12 21:45:02 +000097typedef signed char BOOL;
Ted Kremenekfde52c92009-04-29 00:41:31 +000098typedef unsigned long NSUInteger;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +000099@class NSString, Protocol;
100extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
Ted Kremenek12619382009-01-12 21:45:02 +0000101typedef struct _NSZone NSZone;
102@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
Ted Kremenekac02f202009-08-20 05:13:36 +0000103@protocol NSObject
104- (BOOL)isEqual:(id)object;
Ted Kremenek12619382009-01-12 21:45:02 +0000105- (id)retain;
106- (oneway void)release;
Ted Kremeneka7ecc372009-01-28 21:20:48 +0000107- (id)autorelease;
Ted Kremenek12619382009-01-12 21:45:02 +0000108@end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone;
Ted Kremenekcd57fce2009-03-09 22:28:18 +0000109@end @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone;
Ted Kremenek12619382009-01-12 21:45:02 +0000110@end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder;
Ted Kremenekac02f202009-08-20 05:13:36 +0000111@end
112@interface NSObject <NSObject> {}
Ted Kremenek8be2a672009-03-13 20:27:06 +0000113+ (id)allocWithZone:(NSZone *)zone;
Ted Kremenekfde52c92009-04-29 00:41:31 +0000114+ (id)alloc;
115- (void)dealloc;
Ted Kremenekac02f202009-08-20 05:13:36 +0000116@end
117@interface NSObject (NSCoderMethods)
118- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder;
119@end
120extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
Ted Kremenekfde52c92009-04-29 00:41:31 +0000121typedef struct {
122}
123NSFastEnumerationState;
124@protocol NSFastEnumeration - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
Ted Kremenekc4843812009-08-20 00:57:22 +0000125@end @class NSString, NSDictionary;
126@interface NSValue : NSObject <NSCopying, NSCoding> - (void)getValue:(void *)value;
127@end @interface NSNumber : NSValue - (char)charValue;
128- (id)initWithInt:(int)value;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000129@end @class NSString;
130@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count;
Ted Kremenek767d6492009-05-20 22:39:57 +0000131@end @interface NSArray (NSArrayCreation) + (id)array;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000132@end @interface NSAutoreleasePool : NSObject {
Ted Kremenek767d6492009-05-20 22:39:57 +0000133}
134- (void)drain;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000135@end extern NSString * const NSBundleDidLoadNotification;
136typedef double NSTimeInterval;
Ted Kremenekfde52c92009-04-29 00:41:31 +0000137@interface NSDate : NSObject <NSCopying, NSCoding> - (NSTimeInterval)timeIntervalSinceReferenceDate;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000138@end typedef unsigned short unichar;
Ted Kremenekcd57fce2009-03-09 22:28:18 +0000139@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length;
Ted Kremenekfde52c92009-04-29 00:41:31 +0000140- ( const char *)UTF8String;
Ted Kremenekcd57fce2009-03-09 22:28:18 +0000141- (id)initWithUTF8String:(const char *)nullTerminatedCString;
142+ (id)stringWithUTF8String:(const char *)nullTerminatedCString;
Ted Kremenekc4843812009-08-20 00:57:22 +0000143@end @class NSString, NSURL, NSError;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000144@interface NSData : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length;
Ted Kremenek767d6492009-05-20 22:39:57 +0000145+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length;
146+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b;
Ted Kremenekc4843812009-08-20 00:57:22 +0000147@end @class NSLocale, NSDate, NSCalendar, NSTimeZone, NSError, NSArray, NSMutableDictionary;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000148@interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count;
Ted Kremenekfde52c92009-04-29 00:41:31 +0000149@end @interface NSMutableDictionary : NSDictionary - (void)removeObjectForKey:(id)aKey;
150- (void)setObject:(id)anObject forKey:(id)aKey;
151@end @interface NSMutableDictionary (NSMutableDictionaryCreation) + (id)dictionaryWithCapacity:(NSUInteger)numItems;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000152@end typedef double CGFloat;
153struct CGSize {
154};
155typedef struct CGSize CGSize;
Ted Kremenek767d6492009-05-20 22:39:57 +0000156struct CGRect {
157};
158typedef struct CGRect CGRect;
Ted Kremenek12619382009-01-12 21:45:02 +0000159typedef mach_port_t io_object_t;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000160typedef char io_name_t[128];
161typedef io_object_t io_iterator_t;
Ted Kremenek12619382009-01-12 21:45:02 +0000162typedef io_object_t io_service_t;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000163typedef struct IONotificationPort * IONotificationPortRef;
164typedef void (*IOServiceMatchingCallback)( void * refcon, io_iterator_t iterator );
165io_service_t IOServiceGetMatchingService( mach_port_t masterPort, CFDictionaryRef matching );
166kern_return_t IOServiceGetMatchingServices( mach_port_t masterPort, CFDictionaryRef matching, io_iterator_t * existing );
167kern_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));
168kern_return_t IOServiceAddMatchingNotification( IONotificationPortRef notifyPort, const io_name_t notificationType, CFDictionaryRef matching, IOServiceMatchingCallback callback, void * refCon, io_iterator_t * notification );
169CFMutableDictionaryRef IOServiceMatching( const char * name );
170CFMutableDictionaryRef IOServiceNameMatching( const char * name );
171CFMutableDictionaryRef IOBSDNameMatching( mach_port_t masterPort, uint32_t options, const char * bsdName );
172CFMutableDictionaryRef IOOpenFirmwarePathMatching( mach_port_t masterPort, uint32_t options, const char * path );
173CFMutableDictionaryRef IORegistryEntryIDMatching( uint64_t entryID );
Ted Kremenek12619382009-01-12 21:45:02 +0000174typedef struct __DASession * DASessionRef;
175extern DASessionRef DASessionCreate( CFAllocatorRef allocator );
176typedef struct __DADisk * DADiskRef;
177extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name );
178extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media );
179extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk );
180extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk );
Ted Kremenekc4843812009-08-20 00:57:22 +0000181@interface NSTask : NSObject - (id)init;
182@end typedef struct CGColorSpace *CGColorSpaceRef;
Ted Kremenek767d6492009-05-20 22:39:57 +0000183typedef struct CGImage *CGImageRef;
Ted Kremenekc4843812009-08-20 00:57:22 +0000184typedef struct CGLayer *CGLayerRef;
Ted Kremenekcd57fce2009-03-09 22:28:18 +0000185@interface NSResponder : NSObject <NSCoding> {
Ted Kremenekfde52c92009-04-29 00:41:31 +0000186}
187@end @protocol NSAnimatablePropertyContainer - (id)animator;
188@end extern NSString *NSAnimationTriggerOrderIn ;
Ted Kremenekfde52c92009-04-29 00:41:31 +0000189@interface NSView : NSResponder <NSAnimatablePropertyContainer> {
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000190}
191@end @protocol NSValidatedUserInterfaceItem - (SEL)action;
Ted Kremenekfde52c92009-04-29 00:41:31 +0000192@end @protocol NSUserInterfaceValidations - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem;
Ted Kremenekc4843812009-08-20 00:57:22 +0000193@end @class NSDate, NSDictionary, NSError, NSException, NSNotification;
Ted Kremenekfde52c92009-04-29 00:41:31 +0000194@interface NSApplication : NSResponder <NSUserInterfaceValidations> {
Ted Kremenekcd57fce2009-03-09 22:28:18 +0000195}
Ted Kremenekfde52c92009-04-29 00:41:31 +0000196@end enum {
197NSTerminateCancel = 0, NSTerminateNow = 1, NSTerminateLater = 2 };
198typedef NSUInteger NSApplicationTerminateReply;
199@protocol NSApplicationDelegate <NSObject> @optional - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
Ted Kremenekc4843812009-08-20 00:57:22 +0000200@end @class NSAttributedString, NSEvent, NSFont, NSFormatter, NSImage, NSMenu, NSText, NSView, NSTextView;
201@interface NSCell : NSObject <NSCopying, NSCoding> {
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000202}
Ted Kremenekc4843812009-08-20 00:57:22 +0000203@end @class NSTextField, NSPanel, NSArray, NSWindow, NSImage, NSButton, NSError;
204typedef struct {
205}
206CVTimeStamp;
Ted Kremenek767d6492009-05-20 22:39:57 +0000207@interface CIImage : NSObject <NSCoding, NSCopying> {
Ted Kremenek12619382009-01-12 21:45:02 +0000208}
Ted Kremenek767d6492009-05-20 22:39:57 +0000209typedef int CIFormat;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000210@end enum {
Ted Kremenek12619382009-01-12 21:45:02 +0000211kDAReturnSuccess = 0, kDAReturnError = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x01, kDAReturnBusy = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x02, kDAReturnBadArgument = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x03, kDAReturnExclusiveAccess = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x04, kDAReturnNoResources = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x05, kDAReturnNotFound = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x06, kDAReturnNotMounted = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x07, kDAReturnNotPermitted = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x08, kDAReturnNotPrivileged = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x09, kDAReturnNotReady = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0A, kDAReturnNotWritable = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0B, kDAReturnUnsupported = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0C };
212typedef mach_error_t DAReturn;
213typedef const struct __DADissenter * DADissenterRef;
214extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string );
Ted Kremenek767d6492009-05-20 22:39:57 +0000215@interface CIContext: NSObject {
216}
217- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r;
218- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r format:(CIFormat)f colorSpace:(CGColorSpaceRef)cs;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000219- (CGLayerRef)createCGLayerWithSize:(CGSize)size info:(CFDictionaryRef)d;
Ted Kremenekc4843812009-08-20 00:57:22 +0000220@end extern NSString* const QCRendererEventKey;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000221@protocol QCCompositionRenderer - (NSDictionary*) attributes;
222@end @interface QCRenderer : NSObject <QCCompositionRenderer> {
Ted Kremenek767d6492009-05-20 22:39:57 +0000223}
224- (id) createSnapshotImageOfType:(NSString*)type;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000225@end extern NSString* const QCViewDidStartRenderingNotification;
226@interface QCView : NSView <QCCompositionRenderer> {
Ted Kremenek767d6492009-05-20 22:39:57 +0000227}
228- (id) createSnapshotImageOfType:(NSString*)type;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000229@end enum {
230ICEXIFOrientation1 = 1, ICEXIFOrientation2 = 2, ICEXIFOrientation3 = 3, ICEXIFOrientation4 = 4, ICEXIFOrientation5 = 5, ICEXIFOrientation6 = 6, ICEXIFOrientation7 = 7, ICEXIFOrientation8 = 8, };
231@class ICDevice;
232@protocol ICDeviceDelegate <NSObject> @required - (void)didRemoveDevice:(ICDevice*)device;
Ted Kremenekc4843812009-08-20 00:57:22 +0000233@end extern NSString *const ICScannerStatusWarmingUp;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000234@class ICScannerDevice;
235@protocol ICScannerDeviceDelegate <ICDeviceDelegate> @optional - (void)scannerDeviceDidBecomeAvailable:(ICScannerDevice*)scanner;
Ted Kremeneka0cc00d2009-05-11 17:45:06 +0000236@end
Ted Kremenekc4843812009-08-20 00:57:22 +0000237
Ted Kremenekcc58eae2009-09-01 18:33:16 +0000238typedef long unsigned int __darwin_size_t;
239typedef __darwin_size_t size_t;
240typedef unsigned long CFTypeID;
241struct CGPoint {
242 CGFloat x;
243 CGFloat y;
244};
245typedef struct CGPoint CGPoint;
246typedef struct CGGradient *CGGradientRef;
247typedef uint32_t CGGradientDrawingOptions;
248extern CFTypeID CGGradientGetTypeID(void);
249extern CGGradientRef CGGradientCreateWithColorComponents(CGColorSpaceRef
250 space, const CGFloat components[], const CGFloat locations[], size_t count);
251extern CGGradientRef CGGradientCreateWithColors(CGColorSpaceRef space,
252 CFArrayRef colors, const CGFloat locations[]);
253extern CGGradientRef CGGradientRetain(CGGradientRef gradient);
254extern void CGGradientRelease(CGGradientRef gradient);
255typedef struct CGContext *CGContextRef;
256extern void CGContextDrawLinearGradient(CGContextRef context,
257 CGGradientRef gradient, CGPoint startPoint, CGPoint endPoint,
258 CGGradientDrawingOptions options);
259extern CGColorSpaceRef CGColorSpaceCreateDeviceRGB(void);
260
Ted Kremenek12619382009-01-12 21:45:02 +0000261//===----------------------------------------------------------------------===//
262// Test cases.
263//===----------------------------------------------------------------------===//
264
265CFAbsoluteTime f1() {
266 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
267 CFDateRef date = CFDateCreate(0, t);
268 CFRetain(date);
269 CFRelease(date);
270 CFDateGetAbsoluteTime(date); // no-warning
271 CFRelease(date);
272 t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}}
273 return t;
274}
275
276CFAbsoluteTime f2() {
277 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
278 CFDateRef date = CFDateCreate(0, t);
279 [((NSDate*) date) retain];
280 CFRelease(date);
281 CFDateGetAbsoluteTime(date); // no-warning
282 [((NSDate*) date) release];
283 t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}}
284 return t;
285}
286
287
288NSDate* global_x;
289
290// Test to see if we supresss an error when we store the pointer
291// to a global.
292
293CFAbsoluteTime f3() {
294 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
295 CFDateRef date = CFDateCreate(0, t);
296 [((NSDate*) date) retain];
297 CFRelease(date);
298 CFDateGetAbsoluteTime(date); // no-warning
299 global_x = (NSDate*) date;
300 [((NSDate*) date) release];
301 t = CFDateGetAbsoluteTime(date); // no-warning
302 return t;
303}
304
Ted Kremenek0964a062009-01-21 06:57:53 +0000305//---------------------------------------------------------------------------
306// Test case 'f4' differs for region store and basic store. See
307// retain-release-region-store.m and retain-release-basic-store.m.
308//---------------------------------------------------------------------------
Ted Kremenek12619382009-01-12 21:45:02 +0000309
310// Test a leak.
311
312CFAbsoluteTime f5(int x) {
313 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000314 CFDateRef date = CFDateCreate(0, t); // expected-warning{{leak}}
Ted Kremenek12619382009-01-12 21:45:02 +0000315
316 if (x)
317 CFRelease(date);
318
Ted Kremenekcf118d42009-02-04 23:49:09 +0000319 return t;
Ted Kremenek12619382009-01-12 21:45:02 +0000320}
321
322// Test a leak involving the return.
323
324CFDateRef f6(int x) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000325 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // expected-warning{{leak}}
Ted Kremenek12619382009-01-12 21:45:02 +0000326 CFRetain(date);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000327 return date;
Ted Kremenek12619382009-01-12 21:45:02 +0000328}
329
330// Test a leak involving an overwrite.
331
332CFDateRef f7() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000333 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); //expected-warning{{leak}}
Ted Kremenek3148eb42009-01-24 00:55:43 +0000334 CFRetain(date);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000335 date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());
Ted Kremenek12619382009-01-12 21:45:02 +0000336 return date;
337}
338
339// Generalization of Create rule. MyDateCreate returns a CFXXXTypeRef, and
340// has the word create.
341CFDateRef MyDateCreate();
342
343CFDateRef f8() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000344 CFDateRef date = MyDateCreate(); // expected-warning{{leak}}
Ted Kremenek12619382009-01-12 21:45:02 +0000345 CFRetain(date);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000346 return date;
Ted Kremenek12619382009-01-12 21:45:02 +0000347}
348
349CFDateRef f9() {
350 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());
351 int *p = 0;
Ted Kremenek25d01ba2009-03-09 22:46:49 +0000352 // When allocations fail, CFDateCreate can return null.
353 if (!date) *p = 1; // expected-warning{{null}}
Ted Kremenek12619382009-01-12 21:45:02 +0000354 return date;
355}
356
357// Handle DiskArbitration API:
358//
359// http://developer.apple.com/DOCUMENTATION/DARWIN/Reference/DiscArbitrationFramework/
360//
361void f10(io_service_t media, DADiskRef d, CFStringRef s) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000362 DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, 0, "hello"); // expected-warning{{leak}}
363 if (disk) NSLog(@"ok");
Ted Kremenek12619382009-01-12 21:45:02 +0000364
Ted Kremenekcf118d42009-02-04 23:49:09 +0000365 disk = DADiskCreateFromIOMedia(kCFAllocatorDefault, 0, media); // expected-warning{{leak}}
366 if (disk) NSLog(@"ok");
Ted Kremenek12619382009-01-12 21:45:02 +0000367
Ted Kremenekcf118d42009-02-04 23:49:09 +0000368 CFDictionaryRef dict = DADiskCopyDescription(d); // expected-warning{{leak}}
369 if (dict) NSLog(@"ok");
Ted Kremenek12619382009-01-12 21:45:02 +0000370
Ted Kremenekcf118d42009-02-04 23:49:09 +0000371 disk = DADiskCopyWholeDisk(d); // expected-warning{{leak}}
372 if (disk) NSLog(@"ok");
Ted Kremenek12619382009-01-12 21:45:02 +0000373
Ted Kremenekcf118d42009-02-04 23:49:09 +0000374 DADissenterRef dissenter = DADissenterCreate(kCFAllocatorDefault, // expected-warning{{leak}}
Ted Kremenek12619382009-01-12 21:45:02 +0000375 kDAReturnSuccess, s);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000376 if (dissenter) NSLog(@"ok");
Ted Kremenek12619382009-01-12 21:45:02 +0000377
Ted Kremenekcf118d42009-02-04 23:49:09 +0000378 DASessionRef session = DASessionCreate(kCFAllocatorDefault); // expected-warning{{leak}}
379 if (session) NSLog(@"ok");
Ted Kremenek12619382009-01-12 21:45:02 +0000380}
381
382// Test retain/release checker with CFString and CFMutableArray.
383void f11() {
384 // Create the array.
385 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
386
387 // Create a string.
388 CFStringRef s1 = CFStringCreateWithCString(0, "hello world",
389 kCFStringEncodingUTF8);
390
391 // Add the string to the array.
392 CFArrayAppendValue(A, s1);
393
394 // Decrement the reference count.
395 CFRelease(s1); // no-warning
396
397 // Get the string. We don't own it.
398 s1 = (CFStringRef) CFArrayGetValueAtIndex(A, 0);
399
400 // Release the array.
401 CFRelease(A); // no-warning
402
403 // Release the string. This is a bug.
404 CFRelease(s1); // expected-warning{{Incorrect decrement of the reference count}}
405}
406
Ted Kremenek99890652009-01-16 18:40:33 +0000407// PR 3337: Handle functions declared using typedefs.
408typedef CFTypeRef CREATEFUN();
409CREATEFUN MyCreateFun;
410
411void f12() {
412 CFTypeRef o = MyCreateFun(); // expected-warning {{leak}}
413}
Ted Kremeneka7ecc372009-01-28 21:20:48 +0000414
415void f13_autorelease() {
Ted Kremenek6b62ec92009-05-09 01:50:57 +0000416 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
Ted Kremeneka7ecc372009-01-28 21:20:48 +0000417 [(id) A autorelease]; // no-warning
418}
Ted Kremenek79f7f8a2009-02-19 18:20:28 +0000419
Ted Kremenek6b62ec92009-05-09 01:50:57 +0000420void f13_autorelease_b() {
421 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
422 [(id) A autorelease];
Ted Kremenekeaedfea2009-05-10 05:11:21 +0000423 [(id) A autorelease]; // expected-warning{{Object sent -autorelease too many times}}
Ted Kremenek6b62ec92009-05-09 01:50:57 +0000424}
425
426CFMutableArrayRef f13_autorelease_c() {
427 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
428 [(id) A autorelease];
429 [(id) A autorelease];
Ted Kremenekeaedfea2009-05-10 05:11:21 +0000430 return A; // expected-warning{{Object sent -autorelease too many times}}
Ted Kremenek6b62ec92009-05-09 01:50:57 +0000431}
432
433CFMutableArrayRef f13_autorelease_d() {
434 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
435 [(id) A autorelease];
436 [(id) A autorelease];
Ted Kremenekeaedfea2009-05-10 05:11:21 +0000437 CFMutableArrayRef B = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{Object sent -autorelease too many times}}
Ted Kremenek6b62ec92009-05-09 01:50:57 +0000438 CFRelease(B); // no-warning
Mike Stump08631d12009-07-21 18:58:15 +0000439 while (1) {}
Ted Kremenek6b62ec92009-05-09 01:50:57 +0000440}
441
442
Ted Kremenek79f7f8a2009-02-19 18:20:28 +0000443// This case exercises the logic where the leak site is the same as the allocation site.
444void f14_leakimmediately() {
445 CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}}
446}
Ted Kremenek9f45d282009-03-05 18:15:02 +0000447
Ted Kremeneke82e13a2009-04-07 05:33:18 +0000448// Test that we track an allocated object beyond the point where the *name*
449// of the variable storing the reference is no longer live.
450void f15() {
451 // Create the array.
452 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
453 CFMutableArrayRef *B = &A;
454 // At this point, the name 'A' is no longer live.
455 CFRelease(*B); // no-warning
456}
457
Ted Kremenek79b4f7d2009-07-14 00:43:42 +0000458// Test when we pass NULL to CFRetain/CFRelease.
459void f16(int x, CFTypeRef p) {
460 if (p)
461 return;
462
463 if (x) {
464 CFRelease(p); // expected-warning{{Null pointer argument in call to CFRelease}}
465 }
466 else {
467 CFRetain(p); // expected-warning{{Null pointer argument in call to CFRetain}}
468 }
469}
Ted Kremeneke82e13a2009-04-07 05:33:18 +0000470
Ted Kremenek9f45d282009-03-05 18:15:02 +0000471// Test basic tracking of ivars associated with 'self'. For the retain/release
472// checker we currently do not want to flag leaks associated with stores
473// of tracked objects to ivars.
474@interface SelfIvarTest : NSObject {
475 id myObj;
476}
477- (void)test_self_tracking;
478@end
479
480@implementation SelfIvarTest
481- (void)test_self_tracking {
482 myObj = (id) CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
483}
484@end
485
Ted Kremeneke8720ce2009-05-10 06:25:57 +0000486// Test return of non-owned objects in contexts where an owned object
487// is expected.
488@interface TestReturnNotOwnedWhenExpectedOwned
489- (NSString*)newString;
490@end
491
492@implementation TestReturnNotOwnedWhenExpectedOwned
493- (NSString*)newString {
494 NSString *s = [NSString stringWithUTF8String:"hello"];
495 return s; // expected-warning{{Object with +0 retain counts returned to caller where a +1 (owning) retain count is expected}}
496}
497@end
498
Ted Kremenekcd57fce2009-03-09 22:28:18 +0000499// <rdar://problem/6659160>
500int isFoo(char c);
501
502static void rdar_6659160(char *inkind, char *inname)
503{
504 // We currently expect that [NSObject alloc] cannot fail. This
505 // will be a toggled flag in the future. It can indeed return null, but
506 // Cocoa programmers generally aren't expected to reason about out-of-memory
507 // conditions.
508 NSString *kind = [[NSString alloc] initWithUTF8String:inkind]; // expected-warning{{leak}}
509
510 // We do allow stringWithUTF8String to fail. This isn't really correct, as
Ted Kremenek68ac94a2009-03-23 17:10:25 +0000511 // far as returning 0. In most error conditions it will throw an exception.
512 // If allocation fails it could return 0, but again this
Ted Kremenekcd57fce2009-03-09 22:28:18 +0000513 // isn't expected.
514 NSString *name = [NSString stringWithUTF8String:inname];
515 if(!name)
516 return;
517
518 const char *kindC = 0;
519 const char *nameC = 0;
520
521 // In both cases, we cannot reach a point down below where we
522 // dereference kindC or nameC with either being null. This is because
523 // we assume that [NSObject alloc] doesn't fail and that we have the guard
524 // up above.
525
526 if(kind)
527 kindC = [kind UTF8String];
528 if(name)
529 nameC = [name UTF8String];
Ted Kremenek25d01ba2009-03-09 22:46:49 +0000530 if(!isFoo(kindC[0])) // expected-warning{{null}}
Ted Kremenekcd57fce2009-03-09 22:28:18 +0000531 return;
532 if(!isFoo(nameC[0])) // no-warning
533 return;
534
535 [kind release];
Ted Kremenekf9df1362009-04-23 21:25:57 +0000536 [name release]; // expected-warning{{Incorrect decrement of the reference count}}
Ted Kremenekcd57fce2009-03-09 22:28:18 +0000537}
Ted Kremenek9f45d282009-03-05 18:15:02 +0000538
Ted Kremenek8be2a672009-03-13 20:27:06 +0000539// PR 3677 - 'allocWithZone' should be treated as following the Cocoa naming
540// conventions with respect to 'return'ing ownership.
541@interface PR3677: NSObject @end
542@implementation PR3677
543+ (id)allocWithZone:(NSZone *)inZone {
544 return [super allocWithZone:inZone]; // no-warning
545}
546@end
547
Ted Kremenekc505d4f2009-03-19 19:50:58 +0000548// PR 3820 - Reason about calls to -dealloc
549void pr3820_DeallocInsteadOfRelease(void)
550{
551 id foo = [[NSString alloc] init]; // no-warning
552 [foo dealloc];
553 // foo is not leaked, since it has been deallocated.
554}
555
556void pr3820_ReleaseAfterDealloc(void)
557{
558 id foo = [[NSString alloc] init];
559 [foo dealloc];
560 [foo release]; // expected-warning{{used after it is release}}
561 // NSInternalInconsistencyException: message sent to deallocated object
562}
563
564void pr3820_DeallocAfterRelease(void)
565{
566 NSLog(@"\n\n[%s]", __FUNCTION__);
567 id foo = [[NSString alloc] init];
568 [foo release];
569 [foo dealloc]; // expected-warning{{used after it is released}}
570 // message sent to released object
571}
Ted Kremenek68ac94a2009-03-23 17:10:25 +0000572
573// From <rdar://problem/6704930>. The problem here is that 'length' binds to
574// '($0 - 1)' after '--length', but SimpleConstraintManager doesn't know how to
575// reason about '($0 - 1) > constant'. As a temporary hack, we drop the value
576// of '($0 - 1)' and conjure a new symbol.
577void rdar6704930(unsigned char *s, unsigned int length) {
578 NSString* name = 0;
579 if (s != 0) {
580 if (length > 0) {
581 while (length > 0) {
582 if (*s == ':') {
583 ++s;
584 --length;
585 name = [[NSString alloc] init]; // no-warning
586 break;
587 }
588 ++s;
589 --length;
590 }
591 if ((length == 0) && (name != 0)) {
592 [name release];
593 name = 0;
594 }
595 if (length == 0) { // no ':' found -> use it all as name
596 name = [[NSString alloc] init]; // no-warning
597 }
598 }
599 }
600
601 if (name != 0) {
602 [name release];
603 }
604}
605
Ted Kremenek0fc169e2009-04-24 23:09:54 +0000606//===----------------------------------------------------------------------===//
Ted Kremenekfde52c92009-04-29 00:41:31 +0000607// <rdar://problem/6833332>
608// One build of the analyzer accidentally stopped tracking the allocated
609// object after the 'retain'.
Ted Kremenekebd5a2d2009-05-11 18:30:24 +0000610//===----------------------------------------------------------------------===//
Ted Kremenekfde52c92009-04-29 00:41:31 +0000611
612@interface rdar_6833332 : NSObject <NSApplicationDelegate> {
613 NSWindow *window;
614}
615@property (nonatomic, retain) NSWindow *window;
616@end
617
618@implementation rdar_6833332
619@synthesize window;
620- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
621 NSMutableDictionary *dict = [[NSMutableDictionary dictionaryWithCapacity:4] retain]; // expected-warning{{leak}}
622
623 [dict setObject:@"foo" forKey:@"bar"];
624
625 NSLog(@"%@", dict);
626}
627- (void)dealloc {
628 [window release];
629 [super dealloc];
630}
631@end
632
Ted Kremenek8c6096e2009-05-09 03:10:32 +0000633//===----------------------------------------------------------------------===//
Ted Kremeneka0cc00d2009-05-11 17:45:06 +0000634// <rdar://problem/6257780> clang checker fails to catch use-after-release
635//===----------------------------------------------------------------------===//
636
Ted Kremenekebd5a2d2009-05-11 18:30:24 +0000637int rdar_6257780_Case1() {
Ted Kremeneka0cc00d2009-05-11 17:45:06 +0000638 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
639 NSArray *array = [NSArray array];
Ted Kremenekbb206fd2009-10-01 17:31:50 +0000640 [array release]; // expected-warning{{Incorrect decrement of the reference count of an object that is not owned at this point by the caller}}
Ted Kremeneka0cc00d2009-05-11 17:45:06 +0000641 [pool drain];
642 return 0;
643}
644
645//===----------------------------------------------------------------------===//
Ted Kremenekebd5a2d2009-05-11 18:30:24 +0000646// <rdar://problem/6866843> Checker should understand new/setObject:/release constructs
647//===----------------------------------------------------------------------===//
648
649void rdar_6866843() {
650 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
651 NSMutableDictionary* dictionary = [[NSMutableDictionary alloc] init];
652 NSArray* array = [[NSArray alloc] init];
653 [dictionary setObject:array forKey:@"key"];
654 [array release];
655 // Using 'array' here should be fine
656 NSLog(@"array = %@\n", array); // no-warning
657 // Now the array is released
658 [dictionary release];
659 [pool drain];
660}
661
Ted Kremenek6738b732009-05-12 04:53:03 +0000662
663//===----------------------------------------------------------------------===//
664// <rdar://problem/6877235> Classes typedef-ed to CF objects should get the same treatment as CF objects
665//===----------------------------------------------------------------------===//
666
667typedef CFTypeRef OtherRef;
668
669@interface RDar6877235 : NSObject {}
670- (CFTypeRef)_copyCFTypeRef;
671- (OtherRef)_copyOtherRef;
672@end
673
674@implementation RDar6877235
675- (CFTypeRef)_copyCFTypeRef {
676 return [[NSString alloc] init]; // no-warning
677}
678- (OtherRef)_copyOtherRef {
679 return [[NSString alloc] init]; // no-warning
680}
681@end
682
Ted Kremenekebd5a2d2009-05-11 18:30:24 +0000683//===----------------------------------------------------------------------===//
Ted Kremenekac02f202009-08-20 05:13:36 +0000684//<rdar://problem/6320065> false positive - init method returns an object
685// owned by caller
Ted Kremenek78a35a32009-05-12 20:06:54 +0000686//===----------------------------------------------------------------------===//
687
688@interface RDar6320065 : NSObject {
689 NSString *_foo;
690}
691- (id)initReturningNewClass;
692- (id)initReturningNewClassBad;
693- (id)initReturningNewClassBad2;
694@end
695
696@interface RDar6320065Subclass : RDar6320065
697@end
698
699@implementation RDar6320065
700- (id)initReturningNewClass {
701 [self release];
702 self = [[RDar6320065Subclass alloc] init]; // no-warning
703 return self;
704}
705- (id)initReturningNewClassBad {
706 [self release];
707 [[RDar6320065Subclass alloc] init]; // expected-warning {{leak}}
708 return self;
709}
710- (id)initReturningNewClassBad2 {
711 [self release];
712 self = [[RDar6320065Subclass alloc] init];
713 return [self autorelease]; // expected-warning{{Object with +0 retain counts returned to caller where a +1 (owning) retain count is expected}}
714}
715
716@end
717
718@implementation RDar6320065Subclass
719@end
720
721int RDar6320065_test() {
722 RDar6320065 *test = [[RDar6320065 alloc] init]; // no-warning
723 [test release];
724 return 0;
725}
726
727//===----------------------------------------------------------------------===//
Ted Kremenekac02f202009-08-20 05:13:36 +0000728// <rdar://problem/7129086> -awakeAfterUsingCoder: returns an owned object
729// and claims the receiver
730//===----------------------------------------------------------------------===//
731
732@interface RDar7129086 : NSObject {} @end
733@implementation RDar7129086
734- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder {
735 [self release]; // no-warning
736 return [NSString alloc]; // no-warning
737}
738@end
739
740//===----------------------------------------------------------------------===//
741// <rdar://problem/6859457> [NSData dataWithBytesNoCopy] does not return a
742// retained object
Ted Kremenek30437662009-05-14 21:29:16 +0000743//===----------------------------------------------------------------------===//
744
745@interface RDar6859457 : NSObject {}
746- (NSString*) NoCopyString;
747- (NSString*) noCopyString;
748@end
749
750@implementation RDar6859457
751- (NSString*) NoCopyString { return [[NSString alloc] init]; } // no-warning
752- (NSString*) noCopyString { return [[NSString alloc] init]; } // no-warning
753@end
754
755void test_RDar6859457(RDar6859457 *x, void *bytes, NSUInteger dataLength) {
756 [x NoCopyString]; // expected-warning{{leak}}
757 [x noCopyString]; // expected-warning{{leak}}
758 [NSData dataWithBytesNoCopy:bytes length:dataLength]; // no-warning
759 [NSData dataWithBytesNoCopy:bytes length:dataLength freeWhenDone:1]; // no-warning
760}
761
762//===----------------------------------------------------------------------===//
Ted Kremenekba67f6a2009-05-18 23:14:34 +0000763// PR 4230 - an autorelease pool is not necessarily leaked during a premature
764// return
765//===----------------------------------------------------------------------===//
766
767static void PR4230(void)
768{
769 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // no-warning
770 NSString *object = [[[NSString alloc] init] autorelease]; // no-warning
771 return;
772}
773
774//===----------------------------------------------------------------------===//
Ted Kremenek7db16042009-05-15 15:49:00 +0000775// Method name that has a null IdentifierInfo* for its first selector slot.
776// This test just makes sure that we handle it.
777//===----------------------------------------------------------------------===//
778
779@interface TestNullIdentifier
780@end
781
782@implementation TestNullIdentifier
783+ (id):(int)x, ... {
784 return [[NSString alloc] init]; // expected-warning{{leak}}
785}
Ted Kremenek767d6492009-05-20 22:39:57 +0000786@end
Ted Kremenek7db16042009-05-15 15:49:00 +0000787
788//===----------------------------------------------------------------------===//
Ted Kremenekfae664a2009-05-16 01:38:01 +0000789// <rdar://problem/6893565> don't flag leaks for return types that cannot be
790// determined to be CF types
791//===----------------------------------------------------------------------===//
792
793// We don't know if 'struct s6893565' represents a Core Foundation type, so
794// we shouldn't emit an error here.
795typedef struct s6893565* TD6893565;
796
797@interface RDar6893565 {}
798-(TD6893565)newThing;
799@end
800
801@implementation RDar6893565
802-(TD6893565)newThing {
803 return (TD6893565) [[NSString alloc] init]; // no-warning
804}
805@end
806
807//===----------------------------------------------------------------------===//
Ted Kremenek767d6492009-05-20 22:39:57 +0000808// <rdar://problem/6902710> clang: false positives w/QC and CoreImage methods
809//===----------------------------------------------------------------------===//
810
811void rdar6902710(QCView *view, QCRenderer *renderer, CIContext *context,
812 NSString *str, CIImage *img, CGRect rect,
813 CIFormat form, CGColorSpaceRef cs) {
814 [view createSnapshotImageOfType:str]; // expected-warning{{leak}}
815 [renderer createSnapshotImageOfType:str]; // expected-warning{{leak}}
816 [context createCGImage:img fromRect:rect]; // expected-warning{{leak}}
817 [context createCGImage:img fromRect:rect format:form colorSpace:cs]; // expected-warning{{leak}}
818}
819
820//===----------------------------------------------------------------------===//
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000821// <rdar://problem/6945561> -[CIContext createCGLayerWithSize:info:]
822// misinterpreted by clang scan-build
823//===----------------------------------------------------------------------===//
824
825void rdar6945561(CIContext *context, CGSize size, CFDictionaryRef d) {
826 [context createCGLayerWithSize:size info:d]; // expected-warning{{leak}}
827}
828
829//===----------------------------------------------------------------------===//
830// <rdar://problem/6961230> add knowledge of IOKit functions to retain/release
831// checker
832//===----------------------------------------------------------------------===//
833
834void IOBSDNameMatching_wrapper(mach_port_t masterPort, uint32_t options, const char * bsdName) {
835 IOBSDNameMatching(masterPort, options, bsdName); // expected-warning{{leak}}
836}
837
838void IOServiceMatching_wrapper(const char * name) {
839 IOServiceMatching(name); // expected-warning{{leak}}
840}
841
842void IOServiceNameMatching_wrapper(const char * name) {
843 IOServiceNameMatching(name); // expected-warning{{leak}}
844}
845
Ted Kremenek71ef5d62009-07-17 00:19:33 +0000846CF_RETURNS_RETAINED CFDictionaryRef CreateDict();
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000847
848void IOServiceAddNotification_wrapper(mach_port_t masterPort, const io_name_t notificationType,
849 mach_port_t wakePort, uintptr_t reference, io_iterator_t * notification ) {
850
851 CFDictionaryRef matching = CreateDict();
852 CFRelease(matching);
853 IOServiceAddNotification(masterPort, notificationType, matching, // expected-warning{{used after it is released}} expected-warning{{deprecated}}
854 wakePort, reference, notification);
855}
856
857void IORegistryEntryIDMatching_wrapper(uint64_t entryID ) {
858 IORegistryEntryIDMatching(entryID); // expected-warning{{leak}}
859}
860
861void IOOpenFirmwarePathMatching_wrapper(mach_port_t masterPort, uint32_t options,
862 const char * path) {
863 IOOpenFirmwarePathMatching(masterPort, options, path); // expected-warning{{leak}}
864}
865
866void IOServiceGetMatchingService_wrapper(mach_port_t masterPort) {
867 CFDictionaryRef matching = CreateDict();
868 IOServiceGetMatchingService(masterPort, matching);
869 CFRelease(matching); // expected-warning{{used after it is released}}
870}
871
872void IOServiceGetMatchingServices_wrapper(mach_port_t masterPort, io_iterator_t *existing) {
873 CFDictionaryRef matching = CreateDict();
874 IOServiceGetMatchingServices(masterPort, matching, existing);
875 CFRelease(matching); // expected-warning{{used after it is released}}
876}
877
878void IOServiceAddMatchingNotification_wrapper(IONotificationPortRef notifyPort, const io_name_t notificationType,
879 IOServiceMatchingCallback callback, void * refCon, io_iterator_t * notification) {
880
881 CFDictionaryRef matching = CreateDict();
882 IOServiceAddMatchingNotification(notifyPort, notificationType, matching, callback, refCon, notification);
883 CFRelease(matching); // expected-warning{{used after it is released}}
884}
885
886//===----------------------------------------------------------------------===//
Ted Kremenekc4843812009-08-20 00:57:22 +0000887// Test of handling objects whose references "escape" to containers.
888//===----------------------------------------------------------------------===//
889
Chris Lattnere0303582010-01-09 20:43:19 +0000890void CFDictionaryAddValue();
891
Ted Kremenekc4843812009-08-20 00:57:22 +0000892// <rdar://problem/6539791>
893void rdar_6539791(CFMutableDictionaryRef y, void* key, void* val_key) {
894 CFMutableDictionaryRef x = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
895 CFDictionaryAddValue(y, key, x);
896 CFRelease(x); // the dictionary keeps a reference, so the object isn't deallocated yet
897 signed z = 1;
898 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z);
899 if (value) {
900 CFDictionaryAddValue(x, val_key, value); // no-warning
901 CFRelease(value);
902 CFDictionaryAddValue(y, val_key, value); // no-warning
903 }
904}
905
906// <rdar://problem/6560661>
907// Same issue, except with "AppendValue" functions.
908void rdar_6560661(CFMutableArrayRef x) {
909 signed z = 1;
910 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z);
911 // CFArrayAppendValue keeps a reference to value.
912 CFArrayAppendValue(x, value);
913 CFRelease(value);
914 CFRetain(value);
915 CFRelease(value); // no-warning
916}
917
918// <rdar://problem/7152619>
919// Same issue, excwept with "CFAttributeStringSetAttribute".
920void rdar_7152619(CFStringRef str) {
921 CFAttributedStringRef string = CFAttributedStringCreate(kCFAllocatorDefault, str, 0);
922 CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutableCopy(kCFAllocatorDefault, 100, string);
923 CFRelease(string);
924 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}}
925 CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 1), str, number);
926 [number release];
927 [number retain];
928 CFRelease(attrString);
929}
930
931//===----------------------------------------------------------------------===//
Ted Kremenekcc58eae2009-09-01 18:33:16 +0000932// Test of handling CGGradientXXX functions.
933//===----------------------------------------------------------------------===//
934
935void rdar_7184450(CGContextRef myContext, CGFloat x, CGPoint myStartPoint,
936 CGPoint myEndPoint) {
937 size_t num_locations = 6;
938 CGFloat locations[6] = { 0.0, 0.265, 0.28, 0.31, 0.36, 1.0 };
939 CGFloat components[28] = { 239.0/256.0, 167.0/256.0, 170.0/256.0,
940 x, // Start color
941 207.0/255.0, 39.0/255.0, 39.0/255.0, x,
942 147.0/255.0, 21.0/255.0, 22.0/255.0, x,
943 175.0/255.0, 175.0/255.0, 175.0/255.0, x,
944 255.0/255.0,255.0/255.0, 255.0/255.0, x,
945 255.0/255.0,255.0/255.0, 255.0/255.0, x
946 }; // End color
947
948 CGGradientRef myGradient =
949 CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), // expected-warning{{leak}}
950 components, locations, num_locations);
951
952 CGContextDrawLinearGradient(myContext, myGradient, myStartPoint, myEndPoint,
953 0);
954 CGGradientRelease(myGradient);
955}
956
957void rdar_7184450_pos(CGContextRef myContext, CGFloat x, CGPoint myStartPoint,
958 CGPoint myEndPoint) {
959 size_t num_locations = 6;
960 CGFloat locations[6] = { 0.0, 0.265, 0.28, 0.31, 0.36, 1.0 };
961 CGFloat components[28] = { 239.0/256.0, 167.0/256.0, 170.0/256.0,
962 x, // Start color
963 207.0/255.0, 39.0/255.0, 39.0/255.0, x,
964 147.0/255.0, 21.0/255.0, 22.0/255.0, x,
965 175.0/255.0, 175.0/255.0, 175.0/255.0, x,
966 255.0/255.0,255.0/255.0, 255.0/255.0, x,
967 255.0/255.0,255.0/255.0, 255.0/255.0, x
968 }; // End color
969
970 CGGradientRef myGradient =
971 CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), components, locations, num_locations); // expected-warning 2 {{leak}}
972
973 CGContextDrawLinearGradient(myContext, myGradient, myStartPoint, myEndPoint,
974 0);
975}
976
977//===----------------------------------------------------------------------===//
Ted Kremenek6240cf12009-10-13 22:55:33 +0000978// <rdar://problem/7299394> clang false positive: retained instance passed to
979// thread in pthread_create marked as leak
980//
981// Until we have full IPA, the analyzer should stop tracking the reference
982// count of objects passed to pthread_create.
983//
984//===----------------------------------------------------------------------===//
985
986struct _opaque_pthread_t {};
987struct _opaque_pthread_attr_t {};
988typedef struct _opaque_pthread_t *__darwin_pthread_t;
989typedef struct _opaque_pthread_attr_t __darwin_pthread_attr_t;
990typedef __darwin_pthread_t pthread_t;
991typedef __darwin_pthread_attr_t pthread_attr_t;
992
993int pthread_create(pthread_t * restrict, const pthread_attr_t * restrict,
994 void *(*)(void *), void * restrict);
995
996void *rdar_7299394_start_routine(void *p) {
997 [((id) p) release];
998 return 0;
999}
1000void rdar_7299394(pthread_attr_t *attr, pthread_t *thread, void *args) {
1001 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1002 pthread_create(thread, attr, rdar_7299394_start_routine, number);
1003}
1004void rdar_7299394_positive(pthread_attr_t *attr, pthread_t *thread) {
1005 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}}
1006}
1007
1008//===----------------------------------------------------------------------===//
Ted Kremenek008636a2009-10-14 00:27:24 +00001009// <rdar://problem/7283567> False leak associated with call to
1010// CVPixelBufferCreateWithBytes ()
1011//
1012// According to the Core Video Reference (ADC), CVPixelBufferCreateWithBytes and
1013// CVPixelBufferCreateWithPlanarBytes can release (via a callback) the
1014// pixel buffer object. These test cases show how the analyzer stops tracking
1015// the reference count for the objects passed for this argument. This
1016// could be made smarter.
1017//===----------------------------------------------------------------------===//
1018
1019typedef int int32_t;
1020typedef UInt32 FourCharCode;
1021typedef FourCharCode OSType;
1022typedef uint64_t CVOptionFlags;
1023typedef int32_t CVReturn;
1024typedef struct __CVBuffer *CVBufferRef;
1025typedef CVBufferRef CVImageBufferRef;
1026typedef CVImageBufferRef CVPixelBufferRef;
1027typedef void (*CVPixelBufferReleaseBytesCallback)( void *releaseRefCon, const void *baseAddress );
1028
1029extern CVReturn CVPixelBufferCreateWithBytes(CFAllocatorRef allocator,
1030 size_t width,
1031 size_t height,
1032 OSType pixelFormatType,
1033 void *baseAddress,
1034 size_t bytesPerRow,
1035 CVPixelBufferReleaseBytesCallback releaseCallback,
1036 void *releaseRefCon,
1037 CFDictionaryRef pixelBufferAttributes,
1038 CVPixelBufferRef *pixelBufferOut) ;
1039
1040typedef void (*CVPixelBufferReleasePlanarBytesCallback)( void *releaseRefCon, const void *dataPtr, size_t dataSize, size_t numberOfPlanes, const void *planeAddresses[] );
1041
1042extern CVReturn CVPixelBufferCreateWithPlanarBytes(CFAllocatorRef allocator,
1043 size_t width,
1044 size_t height,
1045 OSType pixelFormatType,
1046 void *dataPtr,
1047 size_t dataSize,
1048 size_t numberOfPlanes,
1049 void *planeBaseAddress[],
1050 size_t planeWidth[],
1051 size_t planeHeight[],
1052 size_t planeBytesPerRow[],
1053 CVPixelBufferReleasePlanarBytesCallback releaseCallback,
1054 void *releaseRefCon,
1055 CFDictionaryRef pixelBufferAttributes,
1056 CVPixelBufferRef *pixelBufferOut) ;
1057
1058extern CVReturn CVPixelBufferCreateWithBytes(CFAllocatorRef allocator,
1059 size_t width,
1060 size_t height,
1061 OSType pixelFormatType,
1062 void *baseAddress,
1063 size_t bytesPerRow,
1064 CVPixelBufferReleaseBytesCallback releaseCallback,
1065 void *releaseRefCon,
1066 CFDictionaryRef pixelBufferAttributes,
1067 CVPixelBufferRef *pixelBufferOut) ;
1068
1069CVReturn rdar_7283567(CFAllocatorRef allocator, size_t width, size_t height,
1070 OSType pixelFormatType, void *baseAddress,
1071 size_t bytesPerRow,
1072 CVPixelBufferReleaseBytesCallback releaseCallback,
1073 CFDictionaryRef pixelBufferAttributes,
1074 CVPixelBufferRef *pixelBufferOut) {
1075
1076 // For the allocated object, it doesn't really matter what type it is
1077 // for the purpose of this test. All we want to show is that
1078 // this is freed later by the callback.
1079 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1080
1081 return CVPixelBufferCreateWithBytes(allocator, width, height, pixelFormatType,
1082 baseAddress, bytesPerRow, releaseCallback,
1083 number, // potentially released by callback
1084 pixelBufferAttributes, pixelBufferOut) ;
1085}
1086
1087CVReturn rdar_7283567_2(CFAllocatorRef allocator, size_t width, size_t height,
1088 OSType pixelFormatType, void *dataPtr, size_t dataSize,
1089 size_t numberOfPlanes, void *planeBaseAddress[],
1090 size_t planeWidth[], size_t planeHeight[], size_t planeBytesPerRow[],
1091 CVPixelBufferReleasePlanarBytesCallback releaseCallback,
1092 CFDictionaryRef pixelBufferAttributes,
1093 CVPixelBufferRef *pixelBufferOut) {
1094
1095 // For the allocated object, it doesn't really matter what type it is
1096 // for the purpose of this test. All we want to show is that
1097 // this is freed later by the callback.
1098 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1099
1100 return CVPixelBufferCreateWithPlanarBytes(allocator,
1101 width, height, pixelFormatType, dataPtr, dataSize,
1102 numberOfPlanes, planeBaseAddress, planeWidth,
1103 planeHeight, planeBytesPerRow, releaseCallback,
1104 number, // potentially released by callback
1105 pixelBufferAttributes, pixelBufferOut) ;
1106}
1107
1108//===----------------------------------------------------------------------===//
Ted Kremenek882a51e2009-11-03 05:34:07 +00001109// <rdar://problem/7358899> False leak associated with
1110// CGBitmapContextCreateWithData
1111//===----------------------------------------------------------------------===//
1112typedef uint32_t CGBitmapInfo;
1113typedef void (*CGBitmapContextReleaseDataCallback)(void *releaseInfo, void *data);
1114
1115CGContextRef CGBitmapContextCreateWithData(void *data,
1116 size_t width, size_t height, size_t bitsPerComponent,
1117 size_t bytesPerRow, CGColorSpaceRef space, CGBitmapInfo bitmapInfo,
1118 CGBitmapContextReleaseDataCallback releaseCallback, void *releaseInfo);
1119
1120void rdar_7358899(void *data,
1121 size_t width, size_t height, size_t bitsPerComponent,
1122 size_t bytesPerRow, CGColorSpaceRef space, CGBitmapInfo bitmapInfo,
1123 CGBitmapContextReleaseDataCallback releaseCallback) {
1124
1125 // For the allocated object, it doesn't really matter what type it is
1126 // for the purpose of this test. All we want to show is that
1127 // this is freed later by the callback.
1128 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1129
Ted Kremenek002174f2009-11-03 05:39:12 +00001130 CGBitmapContextCreateWithData(data, width, height, bitsPerComponent, // expected-warning{{leak}}
Ted Kremenek882a51e2009-11-03 05:34:07 +00001131 bytesPerRow, space, bitmapInfo, releaseCallback, number);
1132}
1133
1134//===----------------------------------------------------------------------===//
Ted Kremeneke9731832009-10-20 00:13:00 +00001135// <rdar://problem/7265711> allow 'new', 'copy', 'alloc', 'init' prefix to
1136// start before '_' when determining Cocoa fundamental rule
1137//
1138// Previously the retain/release checker just skipped prefixes before the
1139// first '_' entirely. Now the checker honors the prefix if it results in a
1140// recognizable naming convention (e.g., 'new', 'init').
1141//===----------------------------------------------------------------------===//
1142
1143@interface RDar7265711 {}
1144- (id) new_stuff;
1145@end
1146
1147void rdar7265711_a(RDar7265711 *x) {
1148 id y = [x new_stuff]; // expected-warning{{leak}}
1149}
1150
1151void rdar7265711_b(RDar7265711 *x) {
1152 id y = [x new_stuff]; // no-warning
1153 [y release];
1154}
1155
1156//===----------------------------------------------------------------------===//
Ted Kremenek6fe2b7a2009-10-15 22:25:12 +00001157// <rdar://problem/7306898> clang thinks [NSCursor dragCopyCursor] returns a
1158// retained reference
1159//===----------------------------------------------------------------------===//
1160
1161@interface NSCursor : NSObject
1162+ (NSCursor *)dragCopyCursor;
1163@end
1164
1165void rdar7306898(void) {
1166 // 'dragCopyCursor' does not follow Cocoa's fundamental rule. It is a noun, not an sentence
1167 // implying a 'copy' of something.
1168 NSCursor *c = [NSCursor dragCopyCursor]; // no-warning
1169 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}}
1170}
1171
1172//===----------------------------------------------------------------------===//
Ted Kremenek50e837b2009-11-20 05:27:05 +00001173// <rdar://problem/7252064> sending 'release', 'retain', etc. to a Class
1174// directly is not likely what the user intended
1175//===----------------------------------------------------------------------===//
1176
1177@interface RDar7252064 : NSObject @end
1178void rdar7252064(void) {
1179 [RDar7252064 release]; // expected-warning{{The 'release' message should be sent to instances of class 'RDar7252064' and not the class directly}}
1180 [RDar7252064 retain]; // expected-warning{{The 'retain' message should be sent to instances of class 'RDar7252064' and not the class directly}}
1181 [RDar7252064 autorelease]; // expected-warning{{The 'autorelease' message should be sent to instances of class 'RDar7252064' and not the class directly}}
1182 [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}}
1183}
1184
1185//===----------------------------------------------------------------------===//
Ted Kremenek8c6096e2009-05-09 03:10:32 +00001186// Tests of ownership attributes.
1187//===----------------------------------------------------------------------===//
1188
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001189typedef NSString* MyStringTy;
1190
Ted Kremenek8b318262009-07-21 21:21:04 +00001191@protocol FooP;
1192
Ted Kremenek8c6096e2009-05-09 03:10:32 +00001193@interface TestOwnershipAttr : NSObject
Ted Kremenek71ef5d62009-07-17 00:19:33 +00001194- (NSString*) returnsAnOwnedString NS_RETURNS_RETAINED; // no-warning
1195- (NSString*) returnsAnOwnedCFString CF_RETURNS_RETAINED; // no-warning
1196- (MyStringTy) returnsAnOwnedTypedString NS_RETURNS_RETAINED; // no-warning
Ted Kremenek60411112010-02-18 00:06:12 +00001197- (NSString*) newString NS_RETURNS_NOT_RETAINED; // no-warning
1198- (NSString*) newStringNoAttr;
Ted Kremenek71ef5d62009-07-17 00:19:33 +00001199- (int) returnsAnOwnedInt NS_RETURNS_RETAINED; // expected-warning{{'ns_returns_retained' attribute only applies to functions or methods that return a pointer or Objective-C object}}
Ted Kremenek8c6096e2009-05-09 03:10:32 +00001200@end
1201
Ted Kremenek71ef5d62009-07-17 00:19:33 +00001202static int ownership_attribute_doesnt_go_here NS_RETURNS_RETAINED; // expected-warning{{'ns_returns_retained' attribute only applies to function or method types}}
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001203
Ted Kremenek8c6096e2009-05-09 03:10:32 +00001204void test_attr_1(TestOwnershipAttr *X) {
1205 NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
1206}
1207
1208void test_attr_1b(TestOwnershipAttr *X) {
1209 NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}}
1210}
Ted Kremenekb9d8db82009-06-05 23:00:33 +00001211
Ted Kremenek60411112010-02-18 00:06:12 +00001212void test_attr1c(TestOwnershipAttr *X) {
1213 NSString *str = [X newString]; // no-warning
1214 NSString *str2 = [X newStringNoAttr]; // expected-warning{{leak}}
1215}
1216
Ted Kremenekb9d8db82009-06-05 23:00:33 +00001217@interface MyClassTestCFAttr : NSObject {}
Ted Kremenek71ef5d62009-07-17 00:19:33 +00001218- (NSDate*) returnsCFRetained CF_RETURNS_RETAINED;
1219- (CFDateRef) returnsCFRetainedAsCF CF_RETURNS_RETAINED;
Ted Kremenek60411112010-02-18 00:06:12 +00001220- (CFDateRef) newCFRetainedAsCF CF_RETURNS_NOT_RETAINED;
1221- (CFDateRef) newCFRetainedAsCFNoAttr;
Ted Kremenekb9d8db82009-06-05 23:00:33 +00001222- (NSDate*) alsoReturnsRetained;
Ted Kremenek6d4b76d2009-07-06 18:30:43 +00001223- (CFDateRef) alsoReturnsRetainedAsCF;
Ted Kremenek71ef5d62009-07-17 00:19:33 +00001224- (NSDate*) returnsNSRetained NS_RETURNS_RETAINED;
Ted Kremenekb9d8db82009-06-05 23:00:33 +00001225@end
1226
Ted Kremenek71ef5d62009-07-17 00:19:33 +00001227CF_RETURNS_RETAINED
Ted Kremenekb9d8db82009-06-05 23:00:33 +00001228CFDateRef returnsRetainedCFDate() {
1229 return CFDateCreate(0, CFAbsoluteTimeGetCurrent());
1230}
1231
1232@implementation MyClassTestCFAttr
1233- (NSDate*) returnsCFRetained {
1234 return (NSDate*) returnsRetainedCFDate(); // No leak.
1235}
1236
Ted Kremenek6d4b76d2009-07-06 18:30:43 +00001237- (CFDateRef) returnsCFRetainedAsCF {
1238 return returnsRetainedCFDate(); // No leak.
1239}
1240
Ted Kremenek60411112010-02-18 00:06:12 +00001241- (CFDateRef) newCFRetainedAsCF {
1242 return (CFDateRef)[(id)[self returnsCFRetainedAsCF] autorelease];
1243}
1244
1245- (CFDateRef) newCFRetainedAsCFNoAttr {
1246 return (CFDateRef)[(id)[self returnsCFRetainedAsCF] autorelease]; // expected-warning{{Object with +0 retain counts returned to caller where a +1 (owning) retain count is expected}}
1247}
Ted Kremenek6d4b76d2009-07-06 18:30:43 +00001248
Ted Kremenekb9d8db82009-06-05 23:00:33 +00001249- (NSDate*) alsoReturnsRetained {
1250 return (NSDate*) returnsRetainedCFDate(); // expected-warning{{leak}}
1251}
1252
Ted Kremenek6d4b76d2009-07-06 18:30:43 +00001253- (CFDateRef) alsoReturnsRetainedAsCF {
1254 return returnsRetainedCFDate(); // expected-warning{{leak}}
1255}
1256
1257
Ted Kremenekb9d8db82009-06-05 23:00:33 +00001258- (NSDate*) returnsNSRetained {
1259 return (NSDate*) returnsRetainedCFDate(); // no-warning
1260}
1261@end
1262
Ted Kremeneke2b57442009-09-15 00:40:32 +00001263//===----------------------------------------------------------------------===//
1264// Test that leaks post-dominated by "panic" functions are not reported.
1265//
1266// <rdar://problem/5905851> do not report a leak when post-dominated by a call
1267// to a noreturn or panic function
1268//===----------------------------------------------------------------------===//
1269
1270void panic() __attribute__((noreturn));
Ted Kremenekcfd8ea92010-03-26 22:57:13 +00001271void panic_not_in_hardcoded_list() __attribute__((noreturn));
Ted Kremeneke2b57442009-09-15 00:40:32 +00001272
1273void test_panic_negative() {
1274 signed z = 1;
1275 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // expected-warning{{leak}}
1276}
1277
1278void test_panic_positive() {
1279 signed z = 1;
1280 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // no-warning
1281 panic();
1282}
1283
1284void test_panic_neg_2(int x) {
1285 signed z = 1;
1286 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // expected-warning{{leak}}
1287 if (x)
1288 panic();
1289}
1290
1291void test_panic_pos_2(int x) {
1292 signed z = 1;
1293 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // no-warning
1294 if (x)
Ted Kremeneke2b57442009-09-15 00:40:32 +00001295 panic();
Ted Kremenekcfd8ea92010-03-26 22:57:13 +00001296 if (!x) {
1297 // This showed up in <rdar://problem/7796563>, where we silently missed checking
1298 // the function type for noreturn. "panic()" is a hard-coded known panic function
1299 // that isn't always noreturn.
1300 panic_not_in_hardcoded_list();
1301 }
Ted Kremeneke2b57442009-09-15 00:40:32 +00001302}
1303
Ted Kremenek772250c2009-11-25 01:35:18 +00001304//===----------------------------------------------------------------------===//
1305// Test uses of blocks (closures)
1306//===----------------------------------------------------------------------===//
1307
1308void test_blocks_1_pos(void) {
1309 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}}
1310 ^{}();
1311}
1312
Ted Kremenek772250c2009-11-25 01:35:18 +00001313void test_blocks_1_indirect_release(void) {
1314 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1315 ^{ [number release]; }();
1316}
1317
1318void test_blocks_1_indirect_retain(void) {
1319 // Eventually this should be reported as a leak.
1320 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1321 ^{ [number retain]; }();
1322}
Ted Kremenek772250c2009-11-25 01:35:18 +00001323
1324void test_blocks_1_indirect_release_via_call(void) {
1325 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1326 ^(NSObject *o){ [o release]; }(number);
1327}
1328
1329void test_blocks_1_indirect_retain_via_call(void) {
1330 // Eventually this should be reported as a leak.
1331 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1332 ^(NSObject *o){ [o retain]; }(number);
1333}
1334
Ted Kremenekd775c662010-05-21 21:57:00 +00001335//===--------------------------------------------------------------------===//
1336// Test sending message to super that returns an object alias. Previously
1337// this caused a crash in the analyzer.
1338//===--------------------------------------------------------------------===//
1339
1340@interface Rdar8015556 : NSObject {} @end
1341@implementation Rdar8015556
1342- (id)retain {
1343 return [super retain];
1344}
1345@end
1346