blob: 1c41d85d23e5fc4a398466d0141ee43f8b6c478c [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
10
Ted Kremenek12619382009-01-12 21:45:02 +000011//===----------------------------------------------------------------------===//
Ted Kremenek767d6492009-05-20 22:39:57 +000012// The following code is reduced using delta-debugging from Mac OS X headers:
13//
14// #include <Cocoa/Cocoa.h>
15// #include <CoreFoundation/CoreFoundation.h>
16// #include <DiskArbitration/DiskArbitration.h>
17// #include <QuartzCore/QuartzCore.h>
18// #include <Quartz/Quartz.h>
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +000019// #include <IOKit/IOKitLib.h>
Ted Kremenek12619382009-01-12 21:45:02 +000020//
21// It includes the basic definitions for the test cases below.
Ted Kremenek12619382009-01-12 21:45:02 +000022//===----------------------------------------------------------------------===//
23
24typedef unsigned int __darwin_natural_t;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +000025typedef unsigned long uintptr_t;
26typedef unsigned int uint32_t;
27typedef unsigned long long uint64_t;
Ted Kremenekfde52c92009-04-29 00:41:31 +000028typedef unsigned int UInt32;
Ted Kremenek12619382009-01-12 21:45:02 +000029typedef signed long CFIndex;
Ted Kremenekc4843812009-08-20 00:57:22 +000030typedef struct {
31 CFIndex location;
32 CFIndex length;
33} CFRange;
34static __inline__ __attribute__((always_inline)) CFRange CFRangeMake(CFIndex loc, CFIndex len) {
35 CFRange range;
36 range.location = loc;
37 range.length = len;
38 return range;
39}
Ted Kremenek12619382009-01-12 21:45:02 +000040typedef const void * CFTypeRef;
41typedef const struct __CFString * CFStringRef;
42typedef const struct __CFAllocator * CFAllocatorRef;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +000043extern const CFAllocatorRef kCFAllocatorDefault;
Ted Kremenek12619382009-01-12 21:45:02 +000044extern CFTypeRef CFRetain(CFTypeRef cf);
45extern void CFRelease(CFTypeRef cf);
46typedef struct {
47}
48CFArrayCallBacks;
49extern const CFArrayCallBacks kCFTypeArrayCallBacks;
50typedef const struct __CFArray * CFArrayRef;
51typedef struct __CFArray * CFMutableArrayRef;
52extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks);
53extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx);
Ted Kremenekcd57fce2009-03-09 22:28:18 +000054extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value);
Ted Kremenekc4843812009-08-20 00:57:22 +000055typedef struct {
56}
57CFDictionaryKeyCallBacks;
58extern const CFDictionaryKeyCallBacks kCFTypeDictionaryKeyCallBacks;
59typedef struct {
60}
61CFDictionaryValueCallBacks;
62extern const CFDictionaryValueCallBacks kCFTypeDictionaryValueCallBacks;
Ted Kremenek12619382009-01-12 21:45:02 +000063typedef const struct __CFDictionary * CFDictionaryRef;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +000064typedef struct __CFDictionary * CFMutableDictionaryRef;
Ted Kremenekc4843812009-08-20 00:57:22 +000065extern CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFDictionaryKeyCallBacks *keyCallBacks, const CFDictionaryValueCallBacks *valueCallBacks);
Ted Kremenek12619382009-01-12 21:45:02 +000066typedef UInt32 CFStringEncoding;
67enum {
68kCFStringEncodingMacRoman = 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 };
69extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding);
70typedef double CFTimeInterval;
71typedef CFTimeInterval CFAbsoluteTime;
Ted Kremenekcd57fce2009-03-09 22:28:18 +000072extern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void);
Ted Kremenek12619382009-01-12 21:45:02 +000073typedef const struct __CFDate * CFDateRef;
74extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at);
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +000075extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate);
Ted Kremenek12619382009-01-12 21:45:02 +000076typedef __darwin_natural_t natural_t;
77typedef natural_t mach_port_name_t;
78typedef mach_port_name_t mach_port_t;
Ted Kremenekfde52c92009-04-29 00:41:31 +000079typedef int kern_return_t;
80typedef kern_return_t mach_error_t;
Ted Kremenekc4843812009-08-20 00:57:22 +000081enum {
82kCFNumberSInt8Type = 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 };
83typedef CFIndex CFNumberType;
84typedef const struct __CFNumber * CFNumberRef;
85extern CFNumberRef CFNumberCreate(CFAllocatorRef allocator, CFNumberType theType, const void *valuePtr);
86typedef const struct __CFAttributedString *CFAttributedStringRef;
87typedef struct __CFAttributedString *CFMutableAttributedStringRef;
88extern CFAttributedStringRef CFAttributedStringCreate(CFAllocatorRef alloc, CFStringRef str, CFDictionaryRef attributes) ;
89extern CFMutableAttributedStringRef CFAttributedStringCreateMutableCopy(CFAllocatorRef alloc, CFIndex maxLength, CFAttributedStringRef aStr) ;
90extern void CFAttributedStringSetAttribute(CFMutableAttributedStringRef aStr, CFRange range, CFStringRef attrName, CFTypeRef value) ;
Ted Kremenek12619382009-01-12 21:45:02 +000091typedef signed char BOOL;
Ted Kremenekfde52c92009-04-29 00:41:31 +000092typedef unsigned long NSUInteger;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +000093@class NSString, Protocol;
94extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
Ted Kremenek12619382009-01-12 21:45:02 +000095typedef struct _NSZone NSZone;
96@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
Ted Kremenekac02f202009-08-20 05:13:36 +000097@protocol NSObject
98- (BOOL)isEqual:(id)object;
Ted Kremenek12619382009-01-12 21:45:02 +000099- (id)retain;
100- (oneway void)release;
Ted Kremeneka7ecc372009-01-28 21:20:48 +0000101- (id)autorelease;
Ted Kremenek12619382009-01-12 21:45:02 +0000102@end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone;
Ted Kremenekcd57fce2009-03-09 22:28:18 +0000103@end @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone;
Ted Kremenek12619382009-01-12 21:45:02 +0000104@end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder;
Ted Kremenekac02f202009-08-20 05:13:36 +0000105@end
106@interface NSObject <NSObject> {}
Ted Kremenek8be2a672009-03-13 20:27:06 +0000107+ (id)allocWithZone:(NSZone *)zone;
Ted Kremenekfde52c92009-04-29 00:41:31 +0000108+ (id)alloc;
109- (void)dealloc;
Ted Kremenekac02f202009-08-20 05:13:36 +0000110@end
111@interface NSObject (NSCoderMethods)
112- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder;
113@end
114extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
Ted Kremenekfde52c92009-04-29 00:41:31 +0000115typedef struct {
116}
117NSFastEnumerationState;
118@protocol NSFastEnumeration - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
Ted Kremenekc4843812009-08-20 00:57:22 +0000119@end @class NSString, NSDictionary;
120@interface NSValue : NSObject <NSCopying, NSCoding> - (void)getValue:(void *)value;
121@end @interface NSNumber : NSValue - (char)charValue;
122- (id)initWithInt:(int)value;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000123@end @class NSString;
124@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count;
Ted Kremenek767d6492009-05-20 22:39:57 +0000125@end @interface NSArray (NSArrayCreation) + (id)array;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000126@end @interface NSAutoreleasePool : NSObject {
Ted Kremenek767d6492009-05-20 22:39:57 +0000127}
128- (void)drain;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000129@end extern NSString * const NSBundleDidLoadNotification;
130typedef double NSTimeInterval;
Ted Kremenekfde52c92009-04-29 00:41:31 +0000131@interface NSDate : NSObject <NSCopying, NSCoding> - (NSTimeInterval)timeIntervalSinceReferenceDate;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000132@end typedef unsigned short unichar;
Ted Kremenekcd57fce2009-03-09 22:28:18 +0000133@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length;
Ted Kremenekfde52c92009-04-29 00:41:31 +0000134- ( const char *)UTF8String;
Ted Kremenekcd57fce2009-03-09 22:28:18 +0000135- (id)initWithUTF8String:(const char *)nullTerminatedCString;
136+ (id)stringWithUTF8String:(const char *)nullTerminatedCString;
Ted Kremenekc4843812009-08-20 00:57:22 +0000137@end @class NSString, NSURL, NSError;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000138@interface NSData : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length;
Ted Kremenek767d6492009-05-20 22:39:57 +0000139+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length;
140+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b;
Ted Kremenekc4843812009-08-20 00:57:22 +0000141@end @class NSLocale, NSDate, NSCalendar, NSTimeZone, NSError, NSArray, NSMutableDictionary;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000142@interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count;
Ted Kremenekfde52c92009-04-29 00:41:31 +0000143@end @interface NSMutableDictionary : NSDictionary - (void)removeObjectForKey:(id)aKey;
144- (void)setObject:(id)anObject forKey:(id)aKey;
145@end @interface NSMutableDictionary (NSMutableDictionaryCreation) + (id)dictionaryWithCapacity:(NSUInteger)numItems;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000146@end typedef double CGFloat;
147struct CGSize {
148};
149typedef struct CGSize CGSize;
Ted Kremenek767d6492009-05-20 22:39:57 +0000150struct CGRect {
151};
152typedef struct CGRect CGRect;
Ted Kremenek12619382009-01-12 21:45:02 +0000153typedef mach_port_t io_object_t;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000154typedef char io_name_t[128];
155typedef io_object_t io_iterator_t;
Ted Kremenek12619382009-01-12 21:45:02 +0000156typedef io_object_t io_service_t;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000157typedef struct IONotificationPort * IONotificationPortRef;
158typedef void (*IOServiceMatchingCallback)( void * refcon, io_iterator_t iterator );
159io_service_t IOServiceGetMatchingService( mach_port_t masterPort, CFDictionaryRef matching );
160kern_return_t IOServiceGetMatchingServices( mach_port_t masterPort, CFDictionaryRef matching, io_iterator_t * existing );
161kern_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));
162kern_return_t IOServiceAddMatchingNotification( IONotificationPortRef notifyPort, const io_name_t notificationType, CFDictionaryRef matching, IOServiceMatchingCallback callback, void * refCon, io_iterator_t * notification );
163CFMutableDictionaryRef IOServiceMatching( const char * name );
164CFMutableDictionaryRef IOServiceNameMatching( const char * name );
165CFMutableDictionaryRef IOBSDNameMatching( mach_port_t masterPort, uint32_t options, const char * bsdName );
166CFMutableDictionaryRef IOOpenFirmwarePathMatching( mach_port_t masterPort, uint32_t options, const char * path );
167CFMutableDictionaryRef IORegistryEntryIDMatching( uint64_t entryID );
Ted Kremenek12619382009-01-12 21:45:02 +0000168typedef struct __DASession * DASessionRef;
169extern DASessionRef DASessionCreate( CFAllocatorRef allocator );
170typedef struct __DADisk * DADiskRef;
171extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name );
172extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media );
173extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk );
174extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk );
Ted Kremenekc4843812009-08-20 00:57:22 +0000175@interface NSTask : NSObject - (id)init;
176@end typedef struct CGColorSpace *CGColorSpaceRef;
Ted Kremenek767d6492009-05-20 22:39:57 +0000177typedef struct CGImage *CGImageRef;
Ted Kremenekc4843812009-08-20 00:57:22 +0000178typedef struct CGLayer *CGLayerRef;
Ted Kremenekcd57fce2009-03-09 22:28:18 +0000179@interface NSResponder : NSObject <NSCoding> {
Ted Kremenekfde52c92009-04-29 00:41:31 +0000180}
181@end @protocol NSAnimatablePropertyContainer - (id)animator;
182@end extern NSString *NSAnimationTriggerOrderIn ;
Ted Kremenekfde52c92009-04-29 00:41:31 +0000183@interface NSView : NSResponder <NSAnimatablePropertyContainer> {
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000184}
185@end @protocol NSValidatedUserInterfaceItem - (SEL)action;
Ted Kremenekfde52c92009-04-29 00:41:31 +0000186@end @protocol NSUserInterfaceValidations - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem;
Ted Kremenekc4843812009-08-20 00:57:22 +0000187@end @class NSDate, NSDictionary, NSError, NSException, NSNotification;
Ted Kremenekfde52c92009-04-29 00:41:31 +0000188@interface NSApplication : NSResponder <NSUserInterfaceValidations> {
Ted Kremenekcd57fce2009-03-09 22:28:18 +0000189}
Ted Kremenekfde52c92009-04-29 00:41:31 +0000190@end enum {
191NSTerminateCancel = 0, NSTerminateNow = 1, NSTerminateLater = 2 };
192typedef NSUInteger NSApplicationTerminateReply;
193@protocol NSApplicationDelegate <NSObject> @optional - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
Ted Kremenekc4843812009-08-20 00:57:22 +0000194@end @class NSAttributedString, NSEvent, NSFont, NSFormatter, NSImage, NSMenu, NSText, NSView, NSTextView;
195@interface NSCell : NSObject <NSCopying, NSCoding> {
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000196}
Ted Kremenekc4843812009-08-20 00:57:22 +0000197@end @class NSTextField, NSPanel, NSArray, NSWindow, NSImage, NSButton, NSError;
198typedef struct {
199}
200CVTimeStamp;
Ted Kremenek767d6492009-05-20 22:39:57 +0000201@interface CIImage : NSObject <NSCoding, NSCopying> {
Ted Kremenek12619382009-01-12 21:45:02 +0000202}
Ted Kremenek767d6492009-05-20 22:39:57 +0000203typedef int CIFormat;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000204@end enum {
Ted Kremenek12619382009-01-12 21:45:02 +0000205kDAReturnSuccess = 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 };
206typedef mach_error_t DAReturn;
207typedef const struct __DADissenter * DADissenterRef;
208extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string );
Ted Kremenek767d6492009-05-20 22:39:57 +0000209@interface CIContext: NSObject {
210}
211- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r;
212- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r format:(CIFormat)f colorSpace:(CGColorSpaceRef)cs;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000213- (CGLayerRef)createCGLayerWithSize:(CGSize)size info:(CFDictionaryRef)d;
Ted Kremenekc4843812009-08-20 00:57:22 +0000214@end extern NSString* const QCRendererEventKey;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000215@protocol QCCompositionRenderer - (NSDictionary*) attributes;
216@end @interface QCRenderer : NSObject <QCCompositionRenderer> {
Ted Kremenek767d6492009-05-20 22:39:57 +0000217}
218- (id) createSnapshotImageOfType:(NSString*)type;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000219@end extern NSString* const QCViewDidStartRenderingNotification;
220@interface QCView : NSView <QCCompositionRenderer> {
Ted Kremenek767d6492009-05-20 22:39:57 +0000221}
222- (id) createSnapshotImageOfType:(NSString*)type;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000223@end enum {
224ICEXIFOrientation1 = 1, ICEXIFOrientation2 = 2, ICEXIFOrientation3 = 3, ICEXIFOrientation4 = 4, ICEXIFOrientation5 = 5, ICEXIFOrientation6 = 6, ICEXIFOrientation7 = 7, ICEXIFOrientation8 = 8, };
225@class ICDevice;
226@protocol ICDeviceDelegate <NSObject> @required - (void)didRemoveDevice:(ICDevice*)device;
Ted Kremenekc4843812009-08-20 00:57:22 +0000227@end extern NSString *const ICScannerStatusWarmingUp;
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000228@class ICScannerDevice;
229@protocol ICScannerDeviceDelegate <ICDeviceDelegate> @optional - (void)scannerDeviceDidBecomeAvailable:(ICScannerDevice*)scanner;
Ted Kremeneka0cc00d2009-05-11 17:45:06 +0000230@end
Ted Kremenekc4843812009-08-20 00:57:22 +0000231
Ted Kremenekcc58eae2009-09-01 18:33:16 +0000232typedef long unsigned int __darwin_size_t;
233typedef __darwin_size_t size_t;
234typedef unsigned long CFTypeID;
235struct CGPoint {
236 CGFloat x;
237 CGFloat y;
238};
239typedef struct CGPoint CGPoint;
240typedef struct CGGradient *CGGradientRef;
241typedef uint32_t CGGradientDrawingOptions;
242extern CFTypeID CGGradientGetTypeID(void);
243extern CGGradientRef CGGradientCreateWithColorComponents(CGColorSpaceRef
244 space, const CGFloat components[], const CGFloat locations[], size_t count);
245extern CGGradientRef CGGradientCreateWithColors(CGColorSpaceRef space,
246 CFArrayRef colors, const CGFloat locations[]);
247extern CGGradientRef CGGradientRetain(CGGradientRef gradient);
248extern void CGGradientRelease(CGGradientRef gradient);
249typedef struct CGContext *CGContextRef;
250extern void CGContextDrawLinearGradient(CGContextRef context,
251 CGGradientRef gradient, CGPoint startPoint, CGPoint endPoint,
252 CGGradientDrawingOptions options);
253extern CGColorSpaceRef CGColorSpaceCreateDeviceRGB(void);
254
Ted Kremenek12619382009-01-12 21:45:02 +0000255//===----------------------------------------------------------------------===//
256// Test cases.
257//===----------------------------------------------------------------------===//
258
259CFAbsoluteTime f1() {
260 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
261 CFDateRef date = CFDateCreate(0, t);
262 CFRetain(date);
263 CFRelease(date);
264 CFDateGetAbsoluteTime(date); // no-warning
265 CFRelease(date);
266 t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}}
267 return t;
268}
269
270CFAbsoluteTime f2() {
271 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
272 CFDateRef date = CFDateCreate(0, t);
273 [((NSDate*) date) retain];
274 CFRelease(date);
275 CFDateGetAbsoluteTime(date); // no-warning
276 [((NSDate*) date) release];
277 t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}}
278 return t;
279}
280
281
282NSDate* global_x;
283
284// Test to see if we supresss an error when we store the pointer
285// to a global.
286
287CFAbsoluteTime f3() {
288 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
289 CFDateRef date = CFDateCreate(0, t);
290 [((NSDate*) date) retain];
291 CFRelease(date);
292 CFDateGetAbsoluteTime(date); // no-warning
293 global_x = (NSDate*) date;
294 [((NSDate*) date) release];
295 t = CFDateGetAbsoluteTime(date); // no-warning
296 return t;
297}
298
Ted Kremenek0964a062009-01-21 06:57:53 +0000299//---------------------------------------------------------------------------
300// Test case 'f4' differs for region store and basic store. See
301// retain-release-region-store.m and retain-release-basic-store.m.
302//---------------------------------------------------------------------------
Ted Kremenek12619382009-01-12 21:45:02 +0000303
304// Test a leak.
305
306CFAbsoluteTime f5(int x) {
307 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
Ted Kremenekcf118d42009-02-04 23:49:09 +0000308 CFDateRef date = CFDateCreate(0, t); // expected-warning{{leak}}
Ted Kremenek12619382009-01-12 21:45:02 +0000309
310 if (x)
311 CFRelease(date);
312
Ted Kremenekcf118d42009-02-04 23:49:09 +0000313 return t;
Ted Kremenek12619382009-01-12 21:45:02 +0000314}
315
316// Test a leak involving the return.
317
318CFDateRef f6(int x) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000319 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // expected-warning{{leak}}
Ted Kremenek12619382009-01-12 21:45:02 +0000320 CFRetain(date);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000321 return date;
Ted Kremenek12619382009-01-12 21:45:02 +0000322}
323
324// Test a leak involving an overwrite.
325
326CFDateRef f7() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000327 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); //expected-warning{{leak}}
Ted Kremenek3148eb42009-01-24 00:55:43 +0000328 CFRetain(date);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000329 date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());
Ted Kremenek12619382009-01-12 21:45:02 +0000330 return date;
331}
332
333// Generalization of Create rule. MyDateCreate returns a CFXXXTypeRef, and
334// has the word create.
335CFDateRef MyDateCreate();
336
337CFDateRef f8() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000338 CFDateRef date = MyDateCreate(); // expected-warning{{leak}}
Ted Kremenek12619382009-01-12 21:45:02 +0000339 CFRetain(date);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000340 return date;
Ted Kremenek12619382009-01-12 21:45:02 +0000341}
342
343CFDateRef f9() {
344 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());
345 int *p = 0;
Ted Kremenek25d01ba2009-03-09 22:46:49 +0000346 // When allocations fail, CFDateCreate can return null.
347 if (!date) *p = 1; // expected-warning{{null}}
Ted Kremenek12619382009-01-12 21:45:02 +0000348 return date;
349}
350
351// Handle DiskArbitration API:
352//
353// http://developer.apple.com/DOCUMENTATION/DARWIN/Reference/DiscArbitrationFramework/
354//
355void f10(io_service_t media, DADiskRef d, CFStringRef s) {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000356 DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, 0, "hello"); // expected-warning{{leak}}
357 if (disk) NSLog(@"ok");
Ted Kremenek12619382009-01-12 21:45:02 +0000358
Ted Kremenekcf118d42009-02-04 23:49:09 +0000359 disk = DADiskCreateFromIOMedia(kCFAllocatorDefault, 0, media); // expected-warning{{leak}}
360 if (disk) NSLog(@"ok");
Ted Kremenek12619382009-01-12 21:45:02 +0000361
Ted Kremenekcf118d42009-02-04 23:49:09 +0000362 CFDictionaryRef dict = DADiskCopyDescription(d); // expected-warning{{leak}}
363 if (dict) NSLog(@"ok");
Ted Kremenek12619382009-01-12 21:45:02 +0000364
Ted Kremenekcf118d42009-02-04 23:49:09 +0000365 disk = DADiskCopyWholeDisk(d); // expected-warning{{leak}}
366 if (disk) NSLog(@"ok");
Ted Kremenek12619382009-01-12 21:45:02 +0000367
Ted Kremenekcf118d42009-02-04 23:49:09 +0000368 DADissenterRef dissenter = DADissenterCreate(kCFAllocatorDefault, // expected-warning{{leak}}
Ted Kremenek12619382009-01-12 21:45:02 +0000369 kDAReturnSuccess, s);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000370 if (dissenter) NSLog(@"ok");
Ted Kremenek12619382009-01-12 21:45:02 +0000371
Ted Kremenekcf118d42009-02-04 23:49:09 +0000372 DASessionRef session = DASessionCreate(kCFAllocatorDefault); // expected-warning{{leak}}
373 if (session) NSLog(@"ok");
Ted Kremenek12619382009-01-12 21:45:02 +0000374}
375
376// Test retain/release checker with CFString and CFMutableArray.
377void f11() {
378 // Create the array.
379 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
380
381 // Create a string.
382 CFStringRef s1 = CFStringCreateWithCString(0, "hello world",
383 kCFStringEncodingUTF8);
384
385 // Add the string to the array.
386 CFArrayAppendValue(A, s1);
387
388 // Decrement the reference count.
389 CFRelease(s1); // no-warning
390
391 // Get the string. We don't own it.
392 s1 = (CFStringRef) CFArrayGetValueAtIndex(A, 0);
393
394 // Release the array.
395 CFRelease(A); // no-warning
396
397 // Release the string. This is a bug.
398 CFRelease(s1); // expected-warning{{Incorrect decrement of the reference count}}
399}
400
Ted Kremenek99890652009-01-16 18:40:33 +0000401// PR 3337: Handle functions declared using typedefs.
402typedef CFTypeRef CREATEFUN();
403CREATEFUN MyCreateFun;
404
405void f12() {
406 CFTypeRef o = MyCreateFun(); // expected-warning {{leak}}
407}
Ted Kremeneka7ecc372009-01-28 21:20:48 +0000408
409void f13_autorelease() {
Ted Kremenek6b62ec92009-05-09 01:50:57 +0000410 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
Ted Kremeneka7ecc372009-01-28 21:20:48 +0000411 [(id) A autorelease]; // no-warning
412}
Ted Kremenek79f7f8a2009-02-19 18:20:28 +0000413
Ted Kremenek6b62ec92009-05-09 01:50:57 +0000414void f13_autorelease_b() {
415 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
416 [(id) A autorelease];
Ted Kremenekeaedfea2009-05-10 05:11:21 +0000417 [(id) A autorelease]; // expected-warning{{Object sent -autorelease too many times}}
Ted Kremenek6b62ec92009-05-09 01:50:57 +0000418}
419
420CFMutableArrayRef f13_autorelease_c() {
421 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
422 [(id) A autorelease];
423 [(id) A autorelease];
Ted Kremenekeaedfea2009-05-10 05:11:21 +0000424 return A; // expected-warning{{Object sent -autorelease too many times}}
Ted Kremenek6b62ec92009-05-09 01:50:57 +0000425}
426
427CFMutableArrayRef f13_autorelease_d() {
428 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
429 [(id) A autorelease];
430 [(id) A autorelease];
Ted Kremenekeaedfea2009-05-10 05:11:21 +0000431 CFMutableArrayRef B = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{Object sent -autorelease too many times}}
Ted Kremenek6b62ec92009-05-09 01:50:57 +0000432 CFRelease(B); // no-warning
Mike Stump08631d12009-07-21 18:58:15 +0000433 while (1) {}
Ted Kremenek6b62ec92009-05-09 01:50:57 +0000434}
435
436
Ted Kremenek79f7f8a2009-02-19 18:20:28 +0000437// This case exercises the logic where the leak site is the same as the allocation site.
438void f14_leakimmediately() {
439 CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}}
440}
Ted Kremenek9f45d282009-03-05 18:15:02 +0000441
Ted Kremeneke82e13a2009-04-07 05:33:18 +0000442// Test that we track an allocated object beyond the point where the *name*
443// of the variable storing the reference is no longer live.
444void f15() {
445 // Create the array.
446 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
447 CFMutableArrayRef *B = &A;
448 // At this point, the name 'A' is no longer live.
449 CFRelease(*B); // no-warning
450}
451
Ted Kremenek79b4f7d2009-07-14 00:43:42 +0000452// Test when we pass NULL to CFRetain/CFRelease.
453void f16(int x, CFTypeRef p) {
454 if (p)
455 return;
456
457 if (x) {
458 CFRelease(p); // expected-warning{{Null pointer argument in call to CFRelease}}
459 }
460 else {
461 CFRetain(p); // expected-warning{{Null pointer argument in call to CFRetain}}
462 }
463}
Ted Kremeneke82e13a2009-04-07 05:33:18 +0000464
Ted Kremenek9f45d282009-03-05 18:15:02 +0000465// Test basic tracking of ivars associated with 'self'. For the retain/release
466// checker we currently do not want to flag leaks associated with stores
467// of tracked objects to ivars.
468@interface SelfIvarTest : NSObject {
469 id myObj;
470}
471- (void)test_self_tracking;
472@end
473
474@implementation SelfIvarTest
475- (void)test_self_tracking {
476 myObj = (id) CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
477}
478@end
479
Ted Kremeneke8720ce2009-05-10 06:25:57 +0000480// Test return of non-owned objects in contexts where an owned object
481// is expected.
482@interface TestReturnNotOwnedWhenExpectedOwned
483- (NSString*)newString;
484@end
485
486@implementation TestReturnNotOwnedWhenExpectedOwned
487- (NSString*)newString {
488 NSString *s = [NSString stringWithUTF8String:"hello"];
489 return s; // expected-warning{{Object with +0 retain counts returned to caller where a +1 (owning) retain count is expected}}
490}
491@end
492
Ted Kremenekcd57fce2009-03-09 22:28:18 +0000493// <rdar://problem/6659160>
494int isFoo(char c);
495
496static void rdar_6659160(char *inkind, char *inname)
497{
498 // We currently expect that [NSObject alloc] cannot fail. This
499 // will be a toggled flag in the future. It can indeed return null, but
500 // Cocoa programmers generally aren't expected to reason about out-of-memory
501 // conditions.
502 NSString *kind = [[NSString alloc] initWithUTF8String:inkind]; // expected-warning{{leak}}
503
504 // We do allow stringWithUTF8String to fail. This isn't really correct, as
Ted Kremenek68ac94a2009-03-23 17:10:25 +0000505 // far as returning 0. In most error conditions it will throw an exception.
506 // If allocation fails it could return 0, but again this
Ted Kremenekcd57fce2009-03-09 22:28:18 +0000507 // isn't expected.
508 NSString *name = [NSString stringWithUTF8String:inname];
509 if(!name)
510 return;
511
512 const char *kindC = 0;
513 const char *nameC = 0;
514
515 // In both cases, we cannot reach a point down below where we
516 // dereference kindC or nameC with either being null. This is because
517 // we assume that [NSObject alloc] doesn't fail and that we have the guard
518 // up above.
519
520 if(kind)
521 kindC = [kind UTF8String];
522 if(name)
523 nameC = [name UTF8String];
Ted Kremenek25d01ba2009-03-09 22:46:49 +0000524 if(!isFoo(kindC[0])) // expected-warning{{null}}
Ted Kremenekcd57fce2009-03-09 22:28:18 +0000525 return;
526 if(!isFoo(nameC[0])) // no-warning
527 return;
528
529 [kind release];
Ted Kremenekf9df1362009-04-23 21:25:57 +0000530 [name release]; // expected-warning{{Incorrect decrement of the reference count}}
Ted Kremenekcd57fce2009-03-09 22:28:18 +0000531}
Ted Kremenek9f45d282009-03-05 18:15:02 +0000532
Ted Kremenek8be2a672009-03-13 20:27:06 +0000533// PR 3677 - 'allocWithZone' should be treated as following the Cocoa naming
534// conventions with respect to 'return'ing ownership.
535@interface PR3677: NSObject @end
536@implementation PR3677
537+ (id)allocWithZone:(NSZone *)inZone {
538 return [super allocWithZone:inZone]; // no-warning
539}
540@end
541
Ted Kremenekc505d4f2009-03-19 19:50:58 +0000542// PR 3820 - Reason about calls to -dealloc
543void pr3820_DeallocInsteadOfRelease(void)
544{
545 id foo = [[NSString alloc] init]; // no-warning
546 [foo dealloc];
547 // foo is not leaked, since it has been deallocated.
548}
549
550void pr3820_ReleaseAfterDealloc(void)
551{
552 id foo = [[NSString alloc] init];
553 [foo dealloc];
554 [foo release]; // expected-warning{{used after it is release}}
555 // NSInternalInconsistencyException: message sent to deallocated object
556}
557
558void pr3820_DeallocAfterRelease(void)
559{
560 NSLog(@"\n\n[%s]", __FUNCTION__);
561 id foo = [[NSString alloc] init];
562 [foo release];
563 [foo dealloc]; // expected-warning{{used after it is released}}
564 // message sent to released object
565}
Ted Kremenek68ac94a2009-03-23 17:10:25 +0000566
567// From <rdar://problem/6704930>. The problem here is that 'length' binds to
568// '($0 - 1)' after '--length', but SimpleConstraintManager doesn't know how to
569// reason about '($0 - 1) > constant'. As a temporary hack, we drop the value
570// of '($0 - 1)' and conjure a new symbol.
571void rdar6704930(unsigned char *s, unsigned int length) {
572 NSString* name = 0;
573 if (s != 0) {
574 if (length > 0) {
575 while (length > 0) {
576 if (*s == ':') {
577 ++s;
578 --length;
579 name = [[NSString alloc] init]; // no-warning
580 break;
581 }
582 ++s;
583 --length;
584 }
585 if ((length == 0) && (name != 0)) {
586 [name release];
587 name = 0;
588 }
589 if (length == 0) { // no ':' found -> use it all as name
590 name = [[NSString alloc] init]; // no-warning
591 }
592 }
593 }
594
595 if (name != 0) {
596 [name release];
597 }
598}
599
Ted Kremenek0fc169e2009-04-24 23:09:54 +0000600//===----------------------------------------------------------------------===//
Ted Kremenekfde52c92009-04-29 00:41:31 +0000601// <rdar://problem/6833332>
602// One build of the analyzer accidentally stopped tracking the allocated
603// object after the 'retain'.
Ted Kremenekebd5a2d2009-05-11 18:30:24 +0000604//===----------------------------------------------------------------------===//
Ted Kremenekfde52c92009-04-29 00:41:31 +0000605
606@interface rdar_6833332 : NSObject <NSApplicationDelegate> {
607 NSWindow *window;
608}
609@property (nonatomic, retain) NSWindow *window;
610@end
611
612@implementation rdar_6833332
613@synthesize window;
614- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
615 NSMutableDictionary *dict = [[NSMutableDictionary dictionaryWithCapacity:4] retain]; // expected-warning{{leak}}
616
617 [dict setObject:@"foo" forKey:@"bar"];
618
619 NSLog(@"%@", dict);
620}
621- (void)dealloc {
622 [window release];
623 [super dealloc];
624}
625@end
626
Ted Kremenek8c6096e2009-05-09 03:10:32 +0000627//===----------------------------------------------------------------------===//
Ted Kremeneka0cc00d2009-05-11 17:45:06 +0000628// <rdar://problem/6257780> clang checker fails to catch use-after-release
629//===----------------------------------------------------------------------===//
630
Ted Kremenekebd5a2d2009-05-11 18:30:24 +0000631int rdar_6257780_Case1() {
Ted Kremeneka0cc00d2009-05-11 17:45:06 +0000632 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
633 NSArray *array = [NSArray array];
Ted Kremenekbb206fd2009-10-01 17:31:50 +0000634 [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 +0000635 [pool drain];
636 return 0;
637}
638
639//===----------------------------------------------------------------------===//
Ted Kremenekebd5a2d2009-05-11 18:30:24 +0000640// <rdar://problem/6866843> Checker should understand new/setObject:/release constructs
641//===----------------------------------------------------------------------===//
642
643void rdar_6866843() {
644 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
645 NSMutableDictionary* dictionary = [[NSMutableDictionary alloc] init];
646 NSArray* array = [[NSArray alloc] init];
647 [dictionary setObject:array forKey:@"key"];
648 [array release];
649 // Using 'array' here should be fine
650 NSLog(@"array = %@\n", array); // no-warning
651 // Now the array is released
652 [dictionary release];
653 [pool drain];
654}
655
Ted Kremenek6738b732009-05-12 04:53:03 +0000656
657//===----------------------------------------------------------------------===//
658// <rdar://problem/6877235> Classes typedef-ed to CF objects should get the same treatment as CF objects
659//===----------------------------------------------------------------------===//
660
661typedef CFTypeRef OtherRef;
662
663@interface RDar6877235 : NSObject {}
664- (CFTypeRef)_copyCFTypeRef;
665- (OtherRef)_copyOtherRef;
666@end
667
668@implementation RDar6877235
669- (CFTypeRef)_copyCFTypeRef {
670 return [[NSString alloc] init]; // no-warning
671}
672- (OtherRef)_copyOtherRef {
673 return [[NSString alloc] init]; // no-warning
674}
675@end
676
Ted Kremenekebd5a2d2009-05-11 18:30:24 +0000677//===----------------------------------------------------------------------===//
Ted Kremenekac02f202009-08-20 05:13:36 +0000678//<rdar://problem/6320065> false positive - init method returns an object
679// owned by caller
Ted Kremenek78a35a32009-05-12 20:06:54 +0000680//===----------------------------------------------------------------------===//
681
682@interface RDar6320065 : NSObject {
683 NSString *_foo;
684}
685- (id)initReturningNewClass;
686- (id)initReturningNewClassBad;
687- (id)initReturningNewClassBad2;
688@end
689
690@interface RDar6320065Subclass : RDar6320065
691@end
692
693@implementation RDar6320065
694- (id)initReturningNewClass {
695 [self release];
696 self = [[RDar6320065Subclass alloc] init]; // no-warning
697 return self;
698}
699- (id)initReturningNewClassBad {
700 [self release];
701 [[RDar6320065Subclass alloc] init]; // expected-warning {{leak}}
702 return self;
703}
704- (id)initReturningNewClassBad2 {
705 [self release];
706 self = [[RDar6320065Subclass alloc] init];
707 return [self autorelease]; // expected-warning{{Object with +0 retain counts returned to caller where a +1 (owning) retain count is expected}}
708}
709
710@end
711
712@implementation RDar6320065Subclass
713@end
714
715int RDar6320065_test() {
716 RDar6320065 *test = [[RDar6320065 alloc] init]; // no-warning
717 [test release];
718 return 0;
719}
720
721//===----------------------------------------------------------------------===//
Ted Kremenekac02f202009-08-20 05:13:36 +0000722// <rdar://problem/7129086> -awakeAfterUsingCoder: returns an owned object
723// and claims the receiver
724//===----------------------------------------------------------------------===//
725
726@interface RDar7129086 : NSObject {} @end
727@implementation RDar7129086
728- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder {
729 [self release]; // no-warning
730 return [NSString alloc]; // no-warning
731}
732@end
733
734//===----------------------------------------------------------------------===//
735// <rdar://problem/6859457> [NSData dataWithBytesNoCopy] does not return a
736// retained object
Ted Kremenek30437662009-05-14 21:29:16 +0000737//===----------------------------------------------------------------------===//
738
739@interface RDar6859457 : NSObject {}
740- (NSString*) NoCopyString;
741- (NSString*) noCopyString;
742@end
743
744@implementation RDar6859457
745- (NSString*) NoCopyString { return [[NSString alloc] init]; } // no-warning
746- (NSString*) noCopyString { return [[NSString alloc] init]; } // no-warning
747@end
748
749void test_RDar6859457(RDar6859457 *x, void *bytes, NSUInteger dataLength) {
750 [x NoCopyString]; // expected-warning{{leak}}
751 [x noCopyString]; // expected-warning{{leak}}
752 [NSData dataWithBytesNoCopy:bytes length:dataLength]; // no-warning
753 [NSData dataWithBytesNoCopy:bytes length:dataLength freeWhenDone:1]; // no-warning
754}
755
756//===----------------------------------------------------------------------===//
Ted Kremenekba67f6a2009-05-18 23:14:34 +0000757// PR 4230 - an autorelease pool is not necessarily leaked during a premature
758// return
759//===----------------------------------------------------------------------===//
760
761static void PR4230(void)
762{
763 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // no-warning
764 NSString *object = [[[NSString alloc] init] autorelease]; // no-warning
765 return;
766}
767
768//===----------------------------------------------------------------------===//
Ted Kremenek7db16042009-05-15 15:49:00 +0000769// Method name that has a null IdentifierInfo* for its first selector slot.
770// This test just makes sure that we handle it.
771//===----------------------------------------------------------------------===//
772
773@interface TestNullIdentifier
774@end
775
776@implementation TestNullIdentifier
777+ (id):(int)x, ... {
778 return [[NSString alloc] init]; // expected-warning{{leak}}
779}
Ted Kremenek767d6492009-05-20 22:39:57 +0000780@end
Ted Kremenek7db16042009-05-15 15:49:00 +0000781
782//===----------------------------------------------------------------------===//
Ted Kremenekfae664a2009-05-16 01:38:01 +0000783// <rdar://problem/6893565> don't flag leaks for return types that cannot be
784// determined to be CF types
785//===----------------------------------------------------------------------===//
786
787// We don't know if 'struct s6893565' represents a Core Foundation type, so
788// we shouldn't emit an error here.
789typedef struct s6893565* TD6893565;
790
791@interface RDar6893565 {}
792-(TD6893565)newThing;
793@end
794
795@implementation RDar6893565
796-(TD6893565)newThing {
797 return (TD6893565) [[NSString alloc] init]; // no-warning
798}
799@end
800
801//===----------------------------------------------------------------------===//
Ted Kremenek767d6492009-05-20 22:39:57 +0000802// <rdar://problem/6902710> clang: false positives w/QC and CoreImage methods
803//===----------------------------------------------------------------------===//
804
805void rdar6902710(QCView *view, QCRenderer *renderer, CIContext *context,
806 NSString *str, CIImage *img, CGRect rect,
807 CIFormat form, CGColorSpaceRef cs) {
808 [view createSnapshotImageOfType:str]; // expected-warning{{leak}}
809 [renderer createSnapshotImageOfType:str]; // expected-warning{{leak}}
810 [context createCGImage:img fromRect:rect]; // expected-warning{{leak}}
811 [context createCGImage:img fromRect:rect format:form colorSpace:cs]; // expected-warning{{leak}}
812}
813
814//===----------------------------------------------------------------------===//
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000815// <rdar://problem/6945561> -[CIContext createCGLayerWithSize:info:]
816// misinterpreted by clang scan-build
817//===----------------------------------------------------------------------===//
818
819void rdar6945561(CIContext *context, CGSize size, CFDictionaryRef d) {
820 [context createCGLayerWithSize:size info:d]; // expected-warning{{leak}}
821}
822
823//===----------------------------------------------------------------------===//
824// <rdar://problem/6961230> add knowledge of IOKit functions to retain/release
825// checker
826//===----------------------------------------------------------------------===//
827
828void IOBSDNameMatching_wrapper(mach_port_t masterPort, uint32_t options, const char * bsdName) {
829 IOBSDNameMatching(masterPort, options, bsdName); // expected-warning{{leak}}
830}
831
832void IOServiceMatching_wrapper(const char * name) {
833 IOServiceMatching(name); // expected-warning{{leak}}
834}
835
836void IOServiceNameMatching_wrapper(const char * name) {
837 IOServiceNameMatching(name); // expected-warning{{leak}}
838}
839
Ted Kremenek71ef5d62009-07-17 00:19:33 +0000840CF_RETURNS_RETAINED CFDictionaryRef CreateDict();
Ted Kremenekfdd8b8e2009-06-16 20:44:39 +0000841
842void IOServiceAddNotification_wrapper(mach_port_t masterPort, const io_name_t notificationType,
843 mach_port_t wakePort, uintptr_t reference, io_iterator_t * notification ) {
844
845 CFDictionaryRef matching = CreateDict();
846 CFRelease(matching);
847 IOServiceAddNotification(masterPort, notificationType, matching, // expected-warning{{used after it is released}} expected-warning{{deprecated}}
848 wakePort, reference, notification);
849}
850
851void IORegistryEntryIDMatching_wrapper(uint64_t entryID ) {
852 IORegistryEntryIDMatching(entryID); // expected-warning{{leak}}
853}
854
855void IOOpenFirmwarePathMatching_wrapper(mach_port_t masterPort, uint32_t options,
856 const char * path) {
857 IOOpenFirmwarePathMatching(masterPort, options, path); // expected-warning{{leak}}
858}
859
860void IOServiceGetMatchingService_wrapper(mach_port_t masterPort) {
861 CFDictionaryRef matching = CreateDict();
862 IOServiceGetMatchingService(masterPort, matching);
863 CFRelease(matching); // expected-warning{{used after it is released}}
864}
865
866void IOServiceGetMatchingServices_wrapper(mach_port_t masterPort, io_iterator_t *existing) {
867 CFDictionaryRef matching = CreateDict();
868 IOServiceGetMatchingServices(masterPort, matching, existing);
869 CFRelease(matching); // expected-warning{{used after it is released}}
870}
871
872void IOServiceAddMatchingNotification_wrapper(IONotificationPortRef notifyPort, const io_name_t notificationType,
873 IOServiceMatchingCallback callback, void * refCon, io_iterator_t * notification) {
874
875 CFDictionaryRef matching = CreateDict();
876 IOServiceAddMatchingNotification(notifyPort, notificationType, matching, callback, refCon, notification);
877 CFRelease(matching); // expected-warning{{used after it is released}}
878}
879
880//===----------------------------------------------------------------------===//
Ted Kremenekc4843812009-08-20 00:57:22 +0000881// Test of handling objects whose references "escape" to containers.
882//===----------------------------------------------------------------------===//
883
Chris Lattnere0303582010-01-09 20:43:19 +0000884void CFDictionaryAddValue();
885
Ted Kremenekc4843812009-08-20 00:57:22 +0000886// <rdar://problem/6539791>
887void rdar_6539791(CFMutableDictionaryRef y, void* key, void* val_key) {
888 CFMutableDictionaryRef x = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
889 CFDictionaryAddValue(y, key, x);
890 CFRelease(x); // the dictionary keeps a reference, so the object isn't deallocated yet
891 signed z = 1;
892 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z);
893 if (value) {
894 CFDictionaryAddValue(x, val_key, value); // no-warning
895 CFRelease(value);
896 CFDictionaryAddValue(y, val_key, value); // no-warning
897 }
898}
899
900// <rdar://problem/6560661>
901// Same issue, except with "AppendValue" functions.
902void rdar_6560661(CFMutableArrayRef x) {
903 signed z = 1;
904 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z);
905 // CFArrayAppendValue keeps a reference to value.
906 CFArrayAppendValue(x, value);
907 CFRelease(value);
908 CFRetain(value);
909 CFRelease(value); // no-warning
910}
911
912// <rdar://problem/7152619>
913// Same issue, excwept with "CFAttributeStringSetAttribute".
914void rdar_7152619(CFStringRef str) {
915 CFAttributedStringRef string = CFAttributedStringCreate(kCFAllocatorDefault, str, 0);
916 CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutableCopy(kCFAllocatorDefault, 100, string);
917 CFRelease(string);
918 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}}
919 CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 1), str, number);
920 [number release];
921 [number retain];
922 CFRelease(attrString);
923}
924
925//===----------------------------------------------------------------------===//
Ted Kremenekcc58eae2009-09-01 18:33:16 +0000926// Test of handling CGGradientXXX functions.
927//===----------------------------------------------------------------------===//
928
929void rdar_7184450(CGContextRef myContext, CGFloat x, CGPoint myStartPoint,
930 CGPoint myEndPoint) {
931 size_t num_locations = 6;
932 CGFloat locations[6] = { 0.0, 0.265, 0.28, 0.31, 0.36, 1.0 };
933 CGFloat components[28] = { 239.0/256.0, 167.0/256.0, 170.0/256.0,
934 x, // Start color
935 207.0/255.0, 39.0/255.0, 39.0/255.0, x,
936 147.0/255.0, 21.0/255.0, 22.0/255.0, x,
937 175.0/255.0, 175.0/255.0, 175.0/255.0, x,
938 255.0/255.0,255.0/255.0, 255.0/255.0, x,
939 255.0/255.0,255.0/255.0, 255.0/255.0, x
940 }; // End color
941
942 CGGradientRef myGradient =
943 CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), // expected-warning{{leak}}
944 components, locations, num_locations);
945
946 CGContextDrawLinearGradient(myContext, myGradient, myStartPoint, myEndPoint,
947 0);
948 CGGradientRelease(myGradient);
949}
950
951void rdar_7184450_pos(CGContextRef myContext, CGFloat x, CGPoint myStartPoint,
952 CGPoint myEndPoint) {
953 size_t num_locations = 6;
954 CGFloat locations[6] = { 0.0, 0.265, 0.28, 0.31, 0.36, 1.0 };
955 CGFloat components[28] = { 239.0/256.0, 167.0/256.0, 170.0/256.0,
956 x, // Start color
957 207.0/255.0, 39.0/255.0, 39.0/255.0, x,
958 147.0/255.0, 21.0/255.0, 22.0/255.0, x,
959 175.0/255.0, 175.0/255.0, 175.0/255.0, x,
960 255.0/255.0,255.0/255.0, 255.0/255.0, x,
961 255.0/255.0,255.0/255.0, 255.0/255.0, x
962 }; // End color
963
964 CGGradientRef myGradient =
965 CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), components, locations, num_locations); // expected-warning 2 {{leak}}
966
967 CGContextDrawLinearGradient(myContext, myGradient, myStartPoint, myEndPoint,
968 0);
969}
970
971//===----------------------------------------------------------------------===//
Ted Kremenek6240cf12009-10-13 22:55:33 +0000972// <rdar://problem/7299394> clang false positive: retained instance passed to
973// thread in pthread_create marked as leak
974//
975// Until we have full IPA, the analyzer should stop tracking the reference
976// count of objects passed to pthread_create.
977//
978//===----------------------------------------------------------------------===//
979
980struct _opaque_pthread_t {};
981struct _opaque_pthread_attr_t {};
982typedef struct _opaque_pthread_t *__darwin_pthread_t;
983typedef struct _opaque_pthread_attr_t __darwin_pthread_attr_t;
984typedef __darwin_pthread_t pthread_t;
985typedef __darwin_pthread_attr_t pthread_attr_t;
986
987int pthread_create(pthread_t * restrict, const pthread_attr_t * restrict,
988 void *(*)(void *), void * restrict);
989
990void *rdar_7299394_start_routine(void *p) {
991 [((id) p) release];
992 return 0;
993}
994void rdar_7299394(pthread_attr_t *attr, pthread_t *thread, void *args) {
995 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
996 pthread_create(thread, attr, rdar_7299394_start_routine, number);
997}
998void rdar_7299394_positive(pthread_attr_t *attr, pthread_t *thread) {
999 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}}
1000}
1001
1002//===----------------------------------------------------------------------===//
Ted Kremenek008636a2009-10-14 00:27:24 +00001003// <rdar://problem/7283567> False leak associated with call to
1004// CVPixelBufferCreateWithBytes ()
1005//
1006// According to the Core Video Reference (ADC), CVPixelBufferCreateWithBytes and
1007// CVPixelBufferCreateWithPlanarBytes can release (via a callback) the
1008// pixel buffer object. These test cases show how the analyzer stops tracking
1009// the reference count for the objects passed for this argument. This
1010// could be made smarter.
1011//===----------------------------------------------------------------------===//
1012
1013typedef int int32_t;
1014typedef UInt32 FourCharCode;
1015typedef FourCharCode OSType;
1016typedef uint64_t CVOptionFlags;
1017typedef int32_t CVReturn;
1018typedef struct __CVBuffer *CVBufferRef;
1019typedef CVBufferRef CVImageBufferRef;
1020typedef CVImageBufferRef CVPixelBufferRef;
1021typedef void (*CVPixelBufferReleaseBytesCallback)( void *releaseRefCon, const void *baseAddress );
1022
1023extern CVReturn CVPixelBufferCreateWithBytes(CFAllocatorRef allocator,
1024 size_t width,
1025 size_t height,
1026 OSType pixelFormatType,
1027 void *baseAddress,
1028 size_t bytesPerRow,
1029 CVPixelBufferReleaseBytesCallback releaseCallback,
1030 void *releaseRefCon,
1031 CFDictionaryRef pixelBufferAttributes,
1032 CVPixelBufferRef *pixelBufferOut) ;
1033
1034typedef void (*CVPixelBufferReleasePlanarBytesCallback)( void *releaseRefCon, const void *dataPtr, size_t dataSize, size_t numberOfPlanes, const void *planeAddresses[] );
1035
1036extern CVReturn CVPixelBufferCreateWithPlanarBytes(CFAllocatorRef allocator,
1037 size_t width,
1038 size_t height,
1039 OSType pixelFormatType,
1040 void *dataPtr,
1041 size_t dataSize,
1042 size_t numberOfPlanes,
1043 void *planeBaseAddress[],
1044 size_t planeWidth[],
1045 size_t planeHeight[],
1046 size_t planeBytesPerRow[],
1047 CVPixelBufferReleasePlanarBytesCallback releaseCallback,
1048 void *releaseRefCon,
1049 CFDictionaryRef pixelBufferAttributes,
1050 CVPixelBufferRef *pixelBufferOut) ;
1051
1052extern CVReturn CVPixelBufferCreateWithBytes(CFAllocatorRef allocator,
1053 size_t width,
1054 size_t height,
1055 OSType pixelFormatType,
1056 void *baseAddress,
1057 size_t bytesPerRow,
1058 CVPixelBufferReleaseBytesCallback releaseCallback,
1059 void *releaseRefCon,
1060 CFDictionaryRef pixelBufferAttributes,
1061 CVPixelBufferRef *pixelBufferOut) ;
1062
1063CVReturn rdar_7283567(CFAllocatorRef allocator, size_t width, size_t height,
1064 OSType pixelFormatType, void *baseAddress,
1065 size_t bytesPerRow,
1066 CVPixelBufferReleaseBytesCallback releaseCallback,
1067 CFDictionaryRef pixelBufferAttributes,
1068 CVPixelBufferRef *pixelBufferOut) {
1069
1070 // For the allocated object, it doesn't really matter what type it is
1071 // for the purpose of this test. All we want to show is that
1072 // this is freed later by the callback.
1073 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1074
1075 return CVPixelBufferCreateWithBytes(allocator, width, height, pixelFormatType,
1076 baseAddress, bytesPerRow, releaseCallback,
1077 number, // potentially released by callback
1078 pixelBufferAttributes, pixelBufferOut) ;
1079}
1080
1081CVReturn rdar_7283567_2(CFAllocatorRef allocator, size_t width, size_t height,
1082 OSType pixelFormatType, void *dataPtr, size_t dataSize,
1083 size_t numberOfPlanes, void *planeBaseAddress[],
1084 size_t planeWidth[], size_t planeHeight[], size_t planeBytesPerRow[],
1085 CVPixelBufferReleasePlanarBytesCallback releaseCallback,
1086 CFDictionaryRef pixelBufferAttributes,
1087 CVPixelBufferRef *pixelBufferOut) {
1088
1089 // For the allocated object, it doesn't really matter what type it is
1090 // for the purpose of this test. All we want to show is that
1091 // this is freed later by the callback.
1092 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1093
1094 return CVPixelBufferCreateWithPlanarBytes(allocator,
1095 width, height, pixelFormatType, dataPtr, dataSize,
1096 numberOfPlanes, planeBaseAddress, planeWidth,
1097 planeHeight, planeBytesPerRow, releaseCallback,
1098 number, // potentially released by callback
1099 pixelBufferAttributes, pixelBufferOut) ;
1100}
1101
1102//===----------------------------------------------------------------------===//
Ted Kremenek882a51e2009-11-03 05:34:07 +00001103// <rdar://problem/7358899> False leak associated with
1104// CGBitmapContextCreateWithData
1105//===----------------------------------------------------------------------===//
1106typedef uint32_t CGBitmapInfo;
1107typedef void (*CGBitmapContextReleaseDataCallback)(void *releaseInfo, void *data);
1108
1109CGContextRef CGBitmapContextCreateWithData(void *data,
1110 size_t width, size_t height, size_t bitsPerComponent,
1111 size_t bytesPerRow, CGColorSpaceRef space, CGBitmapInfo bitmapInfo,
1112 CGBitmapContextReleaseDataCallback releaseCallback, void *releaseInfo);
1113
1114void rdar_7358899(void *data,
1115 size_t width, size_t height, size_t bitsPerComponent,
1116 size_t bytesPerRow, CGColorSpaceRef space, CGBitmapInfo bitmapInfo,
1117 CGBitmapContextReleaseDataCallback releaseCallback) {
1118
1119 // For the allocated object, it doesn't really matter what type it is
1120 // for the purpose of this test. All we want to show is that
1121 // this is freed later by the callback.
1122 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1123
Ted Kremenek002174f2009-11-03 05:39:12 +00001124 CGBitmapContextCreateWithData(data, width, height, bitsPerComponent, // expected-warning{{leak}}
Ted Kremenek882a51e2009-11-03 05:34:07 +00001125 bytesPerRow, space, bitmapInfo, releaseCallback, number);
1126}
1127
1128//===----------------------------------------------------------------------===//
Ted Kremeneke9731832009-10-20 00:13:00 +00001129// <rdar://problem/7265711> allow 'new', 'copy', 'alloc', 'init' prefix to
1130// start before '_' when determining Cocoa fundamental rule
1131//
1132// Previously the retain/release checker just skipped prefixes before the
1133// first '_' entirely. Now the checker honors the prefix if it results in a
1134// recognizable naming convention (e.g., 'new', 'init').
1135//===----------------------------------------------------------------------===//
1136
1137@interface RDar7265711 {}
1138- (id) new_stuff;
1139@end
1140
1141void rdar7265711_a(RDar7265711 *x) {
1142 id y = [x new_stuff]; // expected-warning{{leak}}
1143}
1144
1145void rdar7265711_b(RDar7265711 *x) {
1146 id y = [x new_stuff]; // no-warning
1147 [y release];
1148}
1149
1150//===----------------------------------------------------------------------===//
Ted Kremenek6fe2b7a2009-10-15 22:25:12 +00001151// <rdar://problem/7306898> clang thinks [NSCursor dragCopyCursor] returns a
1152// retained reference
1153//===----------------------------------------------------------------------===//
1154
1155@interface NSCursor : NSObject
1156+ (NSCursor *)dragCopyCursor;
1157@end
1158
1159void rdar7306898(void) {
1160 // 'dragCopyCursor' does not follow Cocoa's fundamental rule. It is a noun, not an sentence
1161 // implying a 'copy' of something.
1162 NSCursor *c = [NSCursor dragCopyCursor]; // no-warning
1163 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}}
1164}
1165
1166//===----------------------------------------------------------------------===//
Ted Kremenek50e837b2009-11-20 05:27:05 +00001167// <rdar://problem/7252064> sending 'release', 'retain', etc. to a Class
1168// directly is not likely what the user intended
1169//===----------------------------------------------------------------------===//
1170
1171@interface RDar7252064 : NSObject @end
1172void rdar7252064(void) {
1173 [RDar7252064 release]; // expected-warning{{The 'release' message should be sent to instances of class 'RDar7252064' and not the class directly}}
1174 [RDar7252064 retain]; // expected-warning{{The 'retain' message should be sent to instances of class 'RDar7252064' and not the class directly}}
1175 [RDar7252064 autorelease]; // expected-warning{{The 'autorelease' message should be sent to instances of class 'RDar7252064' and not the class directly}}
1176 [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}}
1177}
1178
1179//===----------------------------------------------------------------------===//
Ted Kremenek8c6096e2009-05-09 03:10:32 +00001180// Tests of ownership attributes.
1181//===----------------------------------------------------------------------===//
1182
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001183typedef NSString* MyStringTy;
1184
Ted Kremenek8b318262009-07-21 21:21:04 +00001185@protocol FooP;
1186
Ted Kremenek8c6096e2009-05-09 03:10:32 +00001187@interface TestOwnershipAttr : NSObject
Ted Kremenek71ef5d62009-07-17 00:19:33 +00001188- (NSString*) returnsAnOwnedString NS_RETURNS_RETAINED; // no-warning
1189- (NSString*) returnsAnOwnedCFString CF_RETURNS_RETAINED; // no-warning
1190- (MyStringTy) returnsAnOwnedTypedString NS_RETURNS_RETAINED; // no-warning
1191- (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 +00001192@end
1193
Ted Kremenek71ef5d62009-07-17 00:19:33 +00001194static 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 +00001195
Ted Kremenek8c6096e2009-05-09 03:10:32 +00001196void test_attr_1(TestOwnershipAttr *X) {
1197 NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
1198}
1199
1200void test_attr_1b(TestOwnershipAttr *X) {
1201 NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}}
1202}
Ted Kremenekb9d8db82009-06-05 23:00:33 +00001203
1204@interface MyClassTestCFAttr : NSObject {}
Ted Kremenek71ef5d62009-07-17 00:19:33 +00001205- (NSDate*) returnsCFRetained CF_RETURNS_RETAINED;
1206- (CFDateRef) returnsCFRetainedAsCF CF_RETURNS_RETAINED;
Ted Kremenekb9d8db82009-06-05 23:00:33 +00001207- (NSDate*) alsoReturnsRetained;
Ted Kremenek6d4b76d2009-07-06 18:30:43 +00001208- (CFDateRef) alsoReturnsRetainedAsCF;
Ted Kremenek71ef5d62009-07-17 00:19:33 +00001209- (NSDate*) returnsNSRetained NS_RETURNS_RETAINED;
Ted Kremenekb9d8db82009-06-05 23:00:33 +00001210@end
1211
Ted Kremenek71ef5d62009-07-17 00:19:33 +00001212CF_RETURNS_RETAINED
Ted Kremenekb9d8db82009-06-05 23:00:33 +00001213CFDateRef returnsRetainedCFDate() {
1214 return CFDateCreate(0, CFAbsoluteTimeGetCurrent());
1215}
1216
1217@implementation MyClassTestCFAttr
1218- (NSDate*) returnsCFRetained {
1219 return (NSDate*) returnsRetainedCFDate(); // No leak.
1220}
1221
Ted Kremenek6d4b76d2009-07-06 18:30:43 +00001222- (CFDateRef) returnsCFRetainedAsCF {
1223 return returnsRetainedCFDate(); // No leak.
1224}
1225
1226
Ted Kremenekb9d8db82009-06-05 23:00:33 +00001227- (NSDate*) alsoReturnsRetained {
1228 return (NSDate*) returnsRetainedCFDate(); // expected-warning{{leak}}
1229}
1230
Ted Kremenek6d4b76d2009-07-06 18:30:43 +00001231- (CFDateRef) alsoReturnsRetainedAsCF {
1232 return returnsRetainedCFDate(); // expected-warning{{leak}}
1233}
1234
1235
Ted Kremenekb9d8db82009-06-05 23:00:33 +00001236- (NSDate*) returnsNSRetained {
1237 return (NSDate*) returnsRetainedCFDate(); // no-warning
1238}
1239@end
1240
Ted Kremeneke2b57442009-09-15 00:40:32 +00001241//===----------------------------------------------------------------------===//
1242// Test that leaks post-dominated by "panic" functions are not reported.
1243//
1244// <rdar://problem/5905851> do not report a leak when post-dominated by a call
1245// to a noreturn or panic function
1246//===----------------------------------------------------------------------===//
1247
1248void panic() __attribute__((noreturn));
1249
1250void test_panic_negative() {
1251 signed z = 1;
1252 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // expected-warning{{leak}}
1253}
1254
1255void test_panic_positive() {
1256 signed z = 1;
1257 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // no-warning
1258 panic();
1259}
1260
1261void test_panic_neg_2(int x) {
1262 signed z = 1;
1263 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // expected-warning{{leak}}
1264 if (x)
1265 panic();
1266}
1267
1268void test_panic_pos_2(int x) {
1269 signed z = 1;
1270 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // no-warning
1271 if (x)
1272 panic();
1273 if (!x)
1274 panic();
1275}
1276
Ted Kremenek772250c2009-11-25 01:35:18 +00001277//===----------------------------------------------------------------------===//
1278// Test uses of blocks (closures)
1279//===----------------------------------------------------------------------===//
1280
1281void test_blocks_1_pos(void) {
1282 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}}
1283 ^{}();
1284}
1285
Ted Kremenek772250c2009-11-25 01:35:18 +00001286void test_blocks_1_indirect_release(void) {
1287 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1288 ^{ [number release]; }();
1289}
1290
1291void test_blocks_1_indirect_retain(void) {
1292 // Eventually this should be reported as a leak.
1293 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1294 ^{ [number retain]; }();
1295}
Ted Kremenek772250c2009-11-25 01:35:18 +00001296
1297void test_blocks_1_indirect_release_via_call(void) {
1298 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1299 ^(NSObject *o){ [o release]; }(number);
1300}
1301
1302void test_blocks_1_indirect_retain_via_call(void) {
1303 // Eventually this should be reported as a leak.
1304 NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1305 ^(NSObject *o){ [o retain]; }(number);
1306}
1307