blob: 0bc12f7036d5adeb0568bbaaeef2bda5fd756faa [file] [log] [blame]
Ted Kremenek10369122009-05-20 22:39:57 +00001//>>SLICER
Ted Kremenekeea8c292009-07-06 21:58:46 +00002// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -verify %s &&
Ted Kremenekf6655792009-07-10 00:41:58 +00003// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic-old-cast -verify %s &&
Daniel Dunbara45cf5b2009-03-24 02:24:46 +00004// RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -verify %s
Ted Kremenekfe32cc02009-01-21 06:57:53 +00005
Ted Kremeneke5a068f2009-07-17 00:19:33 +00006#if __has_feature(attribute_ns_returns_retained)
7#define NS_RETURNS_RETAINED __attribute__((ns_returns_retained))
8#endif
9#if __has_feature(attribute_cf_returns_retained)
10#define CF_RETURNS_RETAINED __attribute__((cf_returns_retained))
11#endif
12
Ted Kremenek7e904222009-01-12 21:45:02 +000013//===----------------------------------------------------------------------===//
Ted Kremenek10369122009-05-20 22:39:57 +000014// The following code is reduced using delta-debugging from Mac OS X headers:
15//
16// #include <Cocoa/Cocoa.h>
17// #include <CoreFoundation/CoreFoundation.h>
18// #include <DiskArbitration/DiskArbitration.h>
19// #include <QuartzCore/QuartzCore.h>
20// #include <Quartz/Quartz.h>
Ted Kremenekdc935e92009-06-16 20:44:39 +000021// #include <IOKit/IOKitLib.h>
Ted Kremenek7e904222009-01-12 21:45:02 +000022//
23// It includes the basic definitions for the test cases below.
Ted Kremenek7e904222009-01-12 21:45:02 +000024//===----------------------------------------------------------------------===//
25
26typedef unsigned int __darwin_natural_t;
Ted Kremenekdc935e92009-06-16 20:44:39 +000027typedef unsigned long uintptr_t;
28typedef unsigned int uint32_t;
29typedef unsigned long long uint64_t;
Ted Kremenekebc6d912009-04-29 00:41:31 +000030typedef unsigned int UInt32;
Ted Kremenek7e904222009-01-12 21:45:02 +000031typedef signed long CFIndex;
32typedef const void * CFTypeRef;
33typedef const struct __CFString * CFStringRef;
34typedef const struct __CFAllocator * CFAllocatorRef;
Ted Kremenekdc935e92009-06-16 20:44:39 +000035extern const CFAllocatorRef kCFAllocatorDefault;
Ted Kremenek7e904222009-01-12 21:45:02 +000036extern CFTypeRef CFRetain(CFTypeRef cf);
37extern void CFRelease(CFTypeRef cf);
38typedef struct {
39}
40CFArrayCallBacks;
41extern const CFArrayCallBacks kCFTypeArrayCallBacks;
42typedef const struct __CFArray * CFArrayRef;
43typedef struct __CFArray * CFMutableArrayRef;
44extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks);
45extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx);
Ted Kremenek3987bbe2009-03-09 22:28:18 +000046extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value);
Ted Kremenek7e904222009-01-12 21:45:02 +000047typedef const struct __CFDictionary * CFDictionaryRef;
Ted Kremenekdc935e92009-06-16 20:44:39 +000048typedef struct __CFDictionary * CFMutableDictionaryRef;
Ted Kremenek7e904222009-01-12 21:45:02 +000049typedef UInt32 CFStringEncoding;
50enum {
51kCFStringEncodingMacRoman = 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 };
52extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding);
53typedef double CFTimeInterval;
54typedef CFTimeInterval CFAbsoluteTime;
Ted Kremenek3987bbe2009-03-09 22:28:18 +000055extern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void);
Ted Kremenek7e904222009-01-12 21:45:02 +000056typedef const struct __CFDate * CFDateRef;
57extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at);
Ted Kremenekdc935e92009-06-16 20:44:39 +000058extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate);
Ted Kremenek7e904222009-01-12 21:45:02 +000059typedef __darwin_natural_t natural_t;
60typedef natural_t mach_port_name_t;
61typedef mach_port_name_t mach_port_t;
Ted Kremenekebc6d912009-04-29 00:41:31 +000062typedef int kern_return_t;
63typedef kern_return_t mach_error_t;
Ted Kremenek7e904222009-01-12 21:45:02 +000064typedef signed char BOOL;
Ted Kremenekebc6d912009-04-29 00:41:31 +000065typedef unsigned long NSUInteger;
Ted Kremenekdc935e92009-06-16 20:44:39 +000066@class NSString, Protocol;
67extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
Ted Kremenek7e904222009-01-12 21:45:02 +000068typedef struct _NSZone NSZone;
69@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
70@protocol NSObject - (BOOL)isEqual:(id)object;
71- (id)retain;
72- (oneway void)release;
Ted Kremenekf6758642009-01-28 21:20:48 +000073- (id)autorelease;
Ted Kremenek7e904222009-01-12 21:45:02 +000074@end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone;
Ted Kremenek3987bbe2009-03-09 22:28:18 +000075@end @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone;
Ted Kremenek7e904222009-01-12 21:45:02 +000076@end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder;
Ted Kremenekebc6d912009-04-29 00:41:31 +000077@end @interface NSObject <NSObject> {
78}
Ted Kremenek340fd2d2009-03-13 20:27:06 +000079+ (id)allocWithZone:(NSZone *)zone;
Ted Kremenekebc6d912009-04-29 00:41:31 +000080+ (id)alloc;
81- (void)dealloc;
82@end extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
83typedef struct {
84}
85NSFastEnumerationState;
86@protocol NSFastEnumeration - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
Ted Kremenekdc935e92009-06-16 20:44:39 +000087@end @class NSString;
88@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count;
Ted Kremenek10369122009-05-20 22:39:57 +000089@end @interface NSArray (NSArrayCreation) + (id)array;
Ted Kremenekdc935e92009-06-16 20:44:39 +000090@end @interface NSAutoreleasePool : NSObject {
Ted Kremenek10369122009-05-20 22:39:57 +000091}
92- (void)drain;
Ted Kremenekdc935e92009-06-16 20:44:39 +000093@end extern NSString * const NSBundleDidLoadNotification;
94typedef double NSTimeInterval;
Ted Kremenekebc6d912009-04-29 00:41:31 +000095@interface NSDate : NSObject <NSCopying, NSCoding> - (NSTimeInterval)timeIntervalSinceReferenceDate;
Ted Kremenekdc935e92009-06-16 20:44:39 +000096@end typedef unsigned short unichar;
Ted Kremenek3987bbe2009-03-09 22:28:18 +000097@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length;
Ted Kremenekebc6d912009-04-29 00:41:31 +000098- ( const char *)UTF8String;
Ted Kremenek3987bbe2009-03-09 22:28:18 +000099- (id)initWithUTF8String:(const char *)nullTerminatedCString;
100+ (id)stringWithUTF8String:(const char *)nullTerminatedCString;
Ted Kremenekdc935e92009-06-16 20:44:39 +0000101@end @class NSString, NSData;
102@interface NSData : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length;
Ted Kremenek10369122009-05-20 22:39:57 +0000103+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length;
104+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b;
Ted Kremenekdc935e92009-06-16 20:44:39 +0000105@end @class NSString;
106@interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count;
Ted Kremenekebc6d912009-04-29 00:41:31 +0000107@end @interface NSMutableDictionary : NSDictionary - (void)removeObjectForKey:(id)aKey;
108- (void)setObject:(id)anObject forKey:(id)aKey;
109@end @interface NSMutableDictionary (NSMutableDictionaryCreation) + (id)dictionaryWithCapacity:(NSUInteger)numItems;
Ted Kremenekdc935e92009-06-16 20:44:39 +0000110@end typedef double CGFloat;
111struct CGSize {
112};
113typedef struct CGSize CGSize;
Ted Kremenek10369122009-05-20 22:39:57 +0000114struct CGRect {
115};
116typedef struct CGRect CGRect;
Ted Kremenekdc935e92009-06-16 20:44:39 +0000117@protocol NSLocking - (void)lock;
Ted Kremenek10369122009-05-20 22:39:57 +0000118- (id)init;
Ted Kremenekdc935e92009-06-16 20:44:39 +0000119@end @class NSURLAuthenticationChallenge;
Ted Kremenek7e904222009-01-12 21:45:02 +0000120typedef mach_port_t io_object_t;
Ted Kremenekdc935e92009-06-16 20:44:39 +0000121typedef char io_name_t[128];
122typedef io_object_t io_iterator_t;
Ted Kremenek7e904222009-01-12 21:45:02 +0000123typedef io_object_t io_service_t;
Ted Kremenekdc935e92009-06-16 20:44:39 +0000124typedef struct IONotificationPort * IONotificationPortRef;
125typedef void (*IOServiceMatchingCallback)( void * refcon, io_iterator_t iterator );
126io_service_t IOServiceGetMatchingService( mach_port_t masterPort, CFDictionaryRef matching );
127kern_return_t IOServiceGetMatchingServices( mach_port_t masterPort, CFDictionaryRef matching, io_iterator_t * existing );
128kern_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));
129kern_return_t IOServiceAddMatchingNotification( IONotificationPortRef notifyPort, const io_name_t notificationType, CFDictionaryRef matching, IOServiceMatchingCallback callback, void * refCon, io_iterator_t * notification );
130CFMutableDictionaryRef IOServiceMatching( const char * name );
131CFMutableDictionaryRef IOServiceNameMatching( const char * name );
132CFMutableDictionaryRef IOBSDNameMatching( mach_port_t masterPort, uint32_t options, const char * bsdName );
133CFMutableDictionaryRef IOOpenFirmwarePathMatching( mach_port_t masterPort, uint32_t options, const char * path );
134CFMutableDictionaryRef IORegistryEntryIDMatching( uint64_t entryID );
Ted Kremenek7e904222009-01-12 21:45:02 +0000135typedef struct __DASession * DASessionRef;
136extern DASessionRef DASessionCreate( CFAllocatorRef allocator );
137typedef struct __DADisk * DADiskRef;
138extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name );
139extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media );
140extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk );
141extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk );
Ted Kremenek10369122009-05-20 22:39:57 +0000142typedef struct CGColorSpace *CGColorSpaceRef;
143typedef struct CGImage *CGImageRef;
Ted Kremenekdc935e92009-06-16 20:44:39 +0000144 typedef struct CGLayer *CGLayerRef;
145 @class NSArray, NSError, NSEvent, NSMenu, NSUndoManager, NSWindow;
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000146@interface NSResponder : NSObject <NSCoding> {
Ted Kremenekebc6d912009-04-29 00:41:31 +0000147}
148@end @protocol NSAnimatablePropertyContainer - (id)animator;
149@end extern NSString *NSAnimationTriggerOrderIn ;
Ted Kremenekebc6d912009-04-29 00:41:31 +0000150@interface NSView : NSResponder <NSAnimatablePropertyContainer> {
Ted Kremenekdc935e92009-06-16 20:44:39 +0000151struct __VFlags2 {
Ted Kremenekebc6d912009-04-29 00:41:31 +0000152}
Ted Kremenekdc935e92009-06-16 20:44:39 +0000153_vFlags2;
154}
155@end extern NSString * const NSFullScreenModeAllScreens;
156@protocol NSChangeSpelling - (void)changeSpelling:(id)sender;
157@end @protocol NSIgnoreMisspelledWords - (void)ignoreSpelling:(id)sender;
158@end @class NSColor, NSFont, NSNotification;
159@interface NSText : NSView <NSChangeSpelling, NSIgnoreMisspelledWords> {
160}
161@end @protocol NSValidatedUserInterfaceItem - (SEL)action;
Ted Kremenekebc6d912009-04-29 00:41:31 +0000162@end @protocol NSUserInterfaceValidations - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem;
Ted Kremenekdc935e92009-06-16 20:44:39 +0000163@end @class NSArray, NSError, NSImage, NSView, NSNotificationCenter, NSURL, NSScreen, NSRunningApplication;
Ted Kremenekebc6d912009-04-29 00:41:31 +0000164@interface NSApplication : NSResponder <NSUserInterfaceValidations> {
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000165}
Ted Kremenekebc6d912009-04-29 00:41:31 +0000166@end enum {
167NSTerminateCancel = 0, NSTerminateNow = 1, NSTerminateLater = 2 };
168typedef NSUInteger NSApplicationTerminateReply;
169@protocol NSApplicationDelegate <NSObject> @optional - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
170@end enum {
Ted Kremenekdc935e92009-06-16 20:44:39 +0000171}
172_CFlags;
Ted Kremenek10369122009-05-20 22:39:57 +0000173@interface CIImage : NSObject <NSCoding, NSCopying> {
Ted Kremenek7e904222009-01-12 21:45:02 +0000174}
Ted Kremenek10369122009-05-20 22:39:57 +0000175typedef int CIFormat;
Ted Kremenekdc935e92009-06-16 20:44:39 +0000176@end enum {
Ted Kremenek7e904222009-01-12 21:45:02 +0000177kDAReturnSuccess = 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 };
178typedef mach_error_t DAReturn;
179typedef const struct __DADissenter * DADissenterRef;
180extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string );
Ted Kremenek10369122009-05-20 22:39:57 +0000181@interface CIContext: NSObject {
182}
183- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r;
184- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r format:(CIFormat)f colorSpace:(CGColorSpaceRef)cs;
Ted Kremenekdc935e92009-06-16 20:44:39 +0000185- (CGLayerRef)createCGLayerWithSize:(CGSize)size info:(CFDictionaryRef)d;
186@end @class NSURL;
187@protocol QCCompositionRenderer - (NSDictionary*) attributes;
188@end @interface QCRenderer : NSObject <QCCompositionRenderer> {
Ted Kremenek10369122009-05-20 22:39:57 +0000189}
190- (id) createSnapshotImageOfType:(NSString*)type;
Ted Kremenekdc935e92009-06-16 20:44:39 +0000191@end extern NSString* const QCViewDidStartRenderingNotification;
192@interface QCView : NSView <QCCompositionRenderer> {
Ted Kremenek10369122009-05-20 22:39:57 +0000193}
194- (id) createSnapshotImageOfType:(NSString*)type;
Ted Kremenekdc935e92009-06-16 20:44:39 +0000195@end enum {
196ICEXIFOrientation1 = 1, ICEXIFOrientation2 = 2, ICEXIFOrientation3 = 3, ICEXIFOrientation4 = 4, ICEXIFOrientation5 = 5, ICEXIFOrientation6 = 6, ICEXIFOrientation7 = 7, ICEXIFOrientation8 = 8, };
197@class ICDevice;
198@protocol ICDeviceDelegate <NSObject> @required - (void)didRemoveDevice:(ICDevice*)device;
199@end @class ICCameraDevice;
200@class ICScannerDevice;
201@protocol ICScannerDeviceDelegate <ICDeviceDelegate> @optional - (void)scannerDeviceDidBecomeAvailable:(ICScannerDevice*)scanner;
Ted Kremenek7e7ed522009-05-11 17:45:06 +0000202@end
Ted Kremenekdc935e92009-06-16 20:44:39 +0000203
Ted Kremenek7e904222009-01-12 21:45:02 +0000204//===----------------------------------------------------------------------===//
205// Test cases.
206//===----------------------------------------------------------------------===//
207
208CFAbsoluteTime f1() {
209 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
210 CFDateRef date = CFDateCreate(0, t);
211 CFRetain(date);
212 CFRelease(date);
213 CFDateGetAbsoluteTime(date); // no-warning
214 CFRelease(date);
215 t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}}
216 return t;
217}
218
219CFAbsoluteTime f2() {
220 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
221 CFDateRef date = CFDateCreate(0, t);
222 [((NSDate*) date) retain];
223 CFRelease(date);
224 CFDateGetAbsoluteTime(date); // no-warning
225 [((NSDate*) date) release];
226 t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}}
227 return t;
228}
229
230
231NSDate* global_x;
232
233// Test to see if we supresss an error when we store the pointer
234// to a global.
235
236CFAbsoluteTime f3() {
237 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
238 CFDateRef date = CFDateCreate(0, t);
239 [((NSDate*) date) retain];
240 CFRelease(date);
241 CFDateGetAbsoluteTime(date); // no-warning
242 global_x = (NSDate*) date;
243 [((NSDate*) date) release];
244 t = CFDateGetAbsoluteTime(date); // no-warning
245 return t;
246}
247
Ted Kremenekfe32cc02009-01-21 06:57:53 +0000248//---------------------------------------------------------------------------
249// Test case 'f4' differs for region store and basic store. See
250// retain-release-region-store.m and retain-release-basic-store.m.
251//---------------------------------------------------------------------------
Ted Kremenek7e904222009-01-12 21:45:02 +0000252
253// Test a leak.
254
255CFAbsoluteTime f5(int x) {
256 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000257 CFDateRef date = CFDateCreate(0, t); // expected-warning{{leak}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000258
259 if (x)
260 CFRelease(date);
261
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000262 return t;
Ted Kremenek7e904222009-01-12 21:45:02 +0000263}
264
265// Test a leak involving the return.
266
267CFDateRef f6(int x) {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000268 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // expected-warning{{leak}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000269 CFRetain(date);
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000270 return date;
Ted Kremenek7e904222009-01-12 21:45:02 +0000271}
272
273// Test a leak involving an overwrite.
274
275CFDateRef f7() {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000276 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); //expected-warning{{leak}}
Ted Kremenekf08ac272009-01-24 00:55:43 +0000277 CFRetain(date);
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000278 date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());
Ted Kremenek7e904222009-01-12 21:45:02 +0000279 return date;
280}
281
282// Generalization of Create rule. MyDateCreate returns a CFXXXTypeRef, and
283// has the word create.
284CFDateRef MyDateCreate();
285
286CFDateRef f8() {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000287 CFDateRef date = MyDateCreate(); // expected-warning{{leak}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000288 CFRetain(date);
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000289 return date;
Ted Kremenek7e904222009-01-12 21:45:02 +0000290}
291
292CFDateRef f9() {
293 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());
294 int *p = 0;
Ted Kremenek0b891a32009-03-09 22:46:49 +0000295 // When allocations fail, CFDateCreate can return null.
296 if (!date) *p = 1; // expected-warning{{null}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000297 return date;
298}
299
300// Handle DiskArbitration API:
301//
302// http://developer.apple.com/DOCUMENTATION/DARWIN/Reference/DiscArbitrationFramework/
303//
304void f10(io_service_t media, DADiskRef d, CFStringRef s) {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000305 DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, 0, "hello"); // expected-warning{{leak}}
306 if (disk) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000307
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000308 disk = DADiskCreateFromIOMedia(kCFAllocatorDefault, 0, media); // expected-warning{{leak}}
309 if (disk) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000310
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000311 CFDictionaryRef dict = DADiskCopyDescription(d); // expected-warning{{leak}}
312 if (dict) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000313
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000314 disk = DADiskCopyWholeDisk(d); // expected-warning{{leak}}
315 if (disk) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000316
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000317 DADissenterRef dissenter = DADissenterCreate(kCFAllocatorDefault, // expected-warning{{leak}}
Ted Kremenek7e904222009-01-12 21:45:02 +0000318 kDAReturnSuccess, s);
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000319 if (dissenter) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000320
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000321 DASessionRef session = DASessionCreate(kCFAllocatorDefault); // expected-warning{{leak}}
322 if (session) NSLog(@"ok");
Ted Kremenek7e904222009-01-12 21:45:02 +0000323}
324
325// Test retain/release checker with CFString and CFMutableArray.
326void f11() {
327 // Create the array.
328 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
329
330 // Create a string.
331 CFStringRef s1 = CFStringCreateWithCString(0, "hello world",
332 kCFStringEncodingUTF8);
333
334 // Add the string to the array.
335 CFArrayAppendValue(A, s1);
336
337 // Decrement the reference count.
338 CFRelease(s1); // no-warning
339
340 // Get the string. We don't own it.
341 s1 = (CFStringRef) CFArrayGetValueAtIndex(A, 0);
342
343 // Release the array.
344 CFRelease(A); // no-warning
345
346 // Release the string. This is a bug.
347 CFRelease(s1); // expected-warning{{Incorrect decrement of the reference count}}
348}
349
Ted Kremenek86afde32009-01-16 18:40:33 +0000350// PR 3337: Handle functions declared using typedefs.
351typedef CFTypeRef CREATEFUN();
352CREATEFUN MyCreateFun;
353
354void f12() {
355 CFTypeRef o = MyCreateFun(); // expected-warning {{leak}}
356}
Ted Kremenekf6758642009-01-28 21:20:48 +0000357
358void f13_autorelease() {
Ted Kremenek2d0ff622009-05-09 01:50:57 +0000359 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
Ted Kremenekf6758642009-01-28 21:20:48 +0000360 [(id) A autorelease]; // no-warning
361}
Ted Kremenekbea465ae2009-02-19 18:20:28 +0000362
Ted Kremenek2d0ff622009-05-09 01:50:57 +0000363void f13_autorelease_b() {
364 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
365 [(id) A autorelease];
Ted Kremenek3978f792009-05-10 05:11:21 +0000366 [(id) A autorelease]; // expected-warning{{Object sent -autorelease too many times}}
Ted Kremenek2d0ff622009-05-09 01:50:57 +0000367}
368
369CFMutableArrayRef f13_autorelease_c() {
370 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
371 [(id) A autorelease];
372 [(id) A autorelease];
Ted Kremenek3978f792009-05-10 05:11:21 +0000373 return A; // expected-warning{{Object sent -autorelease too many times}}
Ted Kremenek2d0ff622009-05-09 01:50:57 +0000374}
375
376CFMutableArrayRef f13_autorelease_d() {
377 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
378 [(id) A autorelease];
379 [(id) A autorelease];
Ted Kremenek3978f792009-05-10 05:11:21 +0000380 CFMutableArrayRef B = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{Object sent -autorelease too many times}}
Ted Kremenek2d0ff622009-05-09 01:50:57 +0000381 CFRelease(B); // no-warning
382}
383
384
Ted Kremenekbea465ae2009-02-19 18:20:28 +0000385// This case exercises the logic where the leak site is the same as the allocation site.
386void f14_leakimmediately() {
387 CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}}
388}
Ted Kremeneka7ec6052009-03-05 18:15:02 +0000389
Ted Kremenek25db1f32009-04-07 05:33:18 +0000390// Test that we track an allocated object beyond the point where the *name*
391// of the variable storing the reference is no longer live.
392void f15() {
393 // Create the array.
394 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
395 CFMutableArrayRef *B = &A;
396 // At this point, the name 'A' is no longer live.
397 CFRelease(*B); // no-warning
398}
399
Ted Kremenekc057f412009-07-14 00:43:42 +0000400// Test when we pass NULL to CFRetain/CFRelease.
401void f16(int x, CFTypeRef p) {
402 if (p)
403 return;
404
405 if (x) {
406 CFRelease(p); // expected-warning{{Null pointer argument in call to CFRelease}}
407 }
408 else {
409 CFRetain(p); // expected-warning{{Null pointer argument in call to CFRetain}}
410 }
411}
Ted Kremenek25db1f32009-04-07 05:33:18 +0000412
Ted Kremeneka7ec6052009-03-05 18:15:02 +0000413// Test basic tracking of ivars associated with 'self'. For the retain/release
414// checker we currently do not want to flag leaks associated with stores
415// of tracked objects to ivars.
416@interface SelfIvarTest : NSObject {
417 id myObj;
418}
419- (void)test_self_tracking;
420@end
421
422@implementation SelfIvarTest
423- (void)test_self_tracking {
424 myObj = (id) CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
425}
426@end
427
Ted Kremenekdee56e32009-05-10 06:25:57 +0000428// Test return of non-owned objects in contexts where an owned object
429// is expected.
430@interface TestReturnNotOwnedWhenExpectedOwned
431- (NSString*)newString;
432@end
433
434@implementation TestReturnNotOwnedWhenExpectedOwned
435- (NSString*)newString {
436 NSString *s = [NSString stringWithUTF8String:"hello"];
437 return s; // expected-warning{{Object with +0 retain counts returned to caller where a +1 (owning) retain count is expected}}
438}
439@end
440
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000441// <rdar://problem/6659160>
442int isFoo(char c);
443
444static void rdar_6659160(char *inkind, char *inname)
445{
446 // We currently expect that [NSObject alloc] cannot fail. This
447 // will be a toggled flag in the future. It can indeed return null, but
448 // Cocoa programmers generally aren't expected to reason about out-of-memory
449 // conditions.
450 NSString *kind = [[NSString alloc] initWithUTF8String:inkind]; // expected-warning{{leak}}
451
452 // We do allow stringWithUTF8String to fail. This isn't really correct, as
Ted Kremenekb294d192009-03-23 17:10:25 +0000453 // far as returning 0. In most error conditions it will throw an exception.
454 // If allocation fails it could return 0, but again this
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000455 // isn't expected.
456 NSString *name = [NSString stringWithUTF8String:inname];
457 if(!name)
458 return;
459
460 const char *kindC = 0;
461 const char *nameC = 0;
462
463 // In both cases, we cannot reach a point down below where we
464 // dereference kindC or nameC with either being null. This is because
465 // we assume that [NSObject alloc] doesn't fail and that we have the guard
466 // up above.
467
468 if(kind)
469 kindC = [kind UTF8String];
470 if(name)
471 nameC = [name UTF8String];
Ted Kremenek0b891a32009-03-09 22:46:49 +0000472 if(!isFoo(kindC[0])) // expected-warning{{null}}
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000473 return;
474 if(!isFoo(nameC[0])) // no-warning
475 return;
476
477 [kind release];
Ted Kremenek0a1f9c42009-04-23 21:25:57 +0000478 [name release]; // expected-warning{{Incorrect decrement of the reference count}}
Ted Kremenek3987bbe2009-03-09 22:28:18 +0000479}
Ted Kremeneka7ec6052009-03-05 18:15:02 +0000480
Ted Kremenek340fd2d2009-03-13 20:27:06 +0000481// PR 3677 - 'allocWithZone' should be treated as following the Cocoa naming
482// conventions with respect to 'return'ing ownership.
483@interface PR3677: NSObject @end
484@implementation PR3677
485+ (id)allocWithZone:(NSZone *)inZone {
486 return [super allocWithZone:inZone]; // no-warning
487}
488@end
489
Ted Kremenek67a3bb72009-03-19 19:50:58 +0000490// PR 3820 - Reason about calls to -dealloc
491void pr3820_DeallocInsteadOfRelease(void)
492{
493 id foo = [[NSString alloc] init]; // no-warning
494 [foo dealloc];
495 // foo is not leaked, since it has been deallocated.
496}
497
498void pr3820_ReleaseAfterDealloc(void)
499{
500 id foo = [[NSString alloc] init];
501 [foo dealloc];
502 [foo release]; // expected-warning{{used after it is release}}
503 // NSInternalInconsistencyException: message sent to deallocated object
504}
505
506void pr3820_DeallocAfterRelease(void)
507{
508 NSLog(@"\n\n[%s]", __FUNCTION__);
509 id foo = [[NSString alloc] init];
510 [foo release];
511 [foo dealloc]; // expected-warning{{used after it is released}}
512 // message sent to released object
513}
Ted Kremenekb294d192009-03-23 17:10:25 +0000514
515// From <rdar://problem/6704930>. The problem here is that 'length' binds to
516// '($0 - 1)' after '--length', but SimpleConstraintManager doesn't know how to
517// reason about '($0 - 1) > constant'. As a temporary hack, we drop the value
518// of '($0 - 1)' and conjure a new symbol.
519void rdar6704930(unsigned char *s, unsigned int length) {
520 NSString* name = 0;
521 if (s != 0) {
522 if (length > 0) {
523 while (length > 0) {
524 if (*s == ':') {
525 ++s;
526 --length;
527 name = [[NSString alloc] init]; // no-warning
528 break;
529 }
530 ++s;
531 --length;
532 }
533 if ((length == 0) && (name != 0)) {
534 [name release];
535 name = 0;
536 }
537 if (length == 0) { // no ':' found -> use it all as name
538 name = [[NSString alloc] init]; // no-warning
539 }
540 }
541 }
542
543 if (name != 0) {
544 [name release];
545 }
546}
547
Ted Kremenek44e662c2009-04-24 23:09:54 +0000548//===----------------------------------------------------------------------===//
Ted Kremenekebc6d912009-04-29 00:41:31 +0000549// <rdar://problem/6833332>
550// One build of the analyzer accidentally stopped tracking the allocated
551// object after the 'retain'.
Ted Kremenekd0e3ab22009-05-11 18:30:24 +0000552//===----------------------------------------------------------------------===//
Ted Kremenekebc6d912009-04-29 00:41:31 +0000553
554@interface rdar_6833332 : NSObject <NSApplicationDelegate> {
555 NSWindow *window;
556}
557@property (nonatomic, retain) NSWindow *window;
558@end
559
560@implementation rdar_6833332
561@synthesize window;
562- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
563 NSMutableDictionary *dict = [[NSMutableDictionary dictionaryWithCapacity:4] retain]; // expected-warning{{leak}}
564
565 [dict setObject:@"foo" forKey:@"bar"];
566
567 NSLog(@"%@", dict);
568}
569- (void)dealloc {
570 [window release];
571 [super dealloc];
572}
573@end
574
Ted Kremenekd73cfc72009-05-09 03:10:32 +0000575//===----------------------------------------------------------------------===//
Ted Kremenek7e7ed522009-05-11 17:45:06 +0000576// <rdar://problem/6257780> clang checker fails to catch use-after-release
577//===----------------------------------------------------------------------===//
578
Ted Kremenekd0e3ab22009-05-11 18:30:24 +0000579int rdar_6257780_Case1() {
Ted Kremenek7e7ed522009-05-11 17:45:06 +0000580 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
581 NSArray *array = [NSArray array];
582 [array release]; // expected-warning{{Incorrect decrement of the reference count of an object is not owned at this point by the caller}}
583 [pool drain];
584 return 0;
585}
586
587//===----------------------------------------------------------------------===//
Ted Kremenekd0e3ab22009-05-11 18:30:24 +0000588// <rdar://problem/6866843> Checker should understand new/setObject:/release constructs
589//===----------------------------------------------------------------------===//
590
591void rdar_6866843() {
592 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
593 NSMutableDictionary* dictionary = [[NSMutableDictionary alloc] init];
594 NSArray* array = [[NSArray alloc] init];
595 [dictionary setObject:array forKey:@"key"];
596 [array release];
597 // Using 'array' here should be fine
598 NSLog(@"array = %@\n", array); // no-warning
599 // Now the array is released
600 [dictionary release];
601 [pool drain];
602}
603
Ted Kremenek95d18192009-05-12 04:53:03 +0000604
605//===----------------------------------------------------------------------===//
606// <rdar://problem/6877235> Classes typedef-ed to CF objects should get the same treatment as CF objects
607//===----------------------------------------------------------------------===//
608
609typedef CFTypeRef OtherRef;
610
611@interface RDar6877235 : NSObject {}
612- (CFTypeRef)_copyCFTypeRef;
613- (OtherRef)_copyOtherRef;
614@end
615
616@implementation RDar6877235
617- (CFTypeRef)_copyCFTypeRef {
618 return [[NSString alloc] init]; // no-warning
619}
620- (OtherRef)_copyOtherRef {
621 return [[NSString alloc] init]; // no-warning
622}
623@end
624
Ted Kremenekd0e3ab22009-05-11 18:30:24 +0000625//===----------------------------------------------------------------------===//
Ted Kremenek1272f702009-05-12 20:06:54 +0000626//<rdar://problem/6320065> false positive - init method returns an object owned by caller
627//===----------------------------------------------------------------------===//
628
629@interface RDar6320065 : NSObject {
630 NSString *_foo;
631}
632- (id)initReturningNewClass;
633- (id)initReturningNewClassBad;
634- (id)initReturningNewClassBad2;
635@end
636
637@interface RDar6320065Subclass : RDar6320065
638@end
639
640@implementation RDar6320065
641- (id)initReturningNewClass {
642 [self release];
643 self = [[RDar6320065Subclass alloc] init]; // no-warning
644 return self;
645}
646- (id)initReturningNewClassBad {
647 [self release];
648 [[RDar6320065Subclass alloc] init]; // expected-warning {{leak}}
649 return self;
650}
651- (id)initReturningNewClassBad2 {
652 [self release];
653 self = [[RDar6320065Subclass alloc] init];
654 return [self autorelease]; // expected-warning{{Object with +0 retain counts returned to caller where a +1 (owning) retain count is expected}}
655}
656
657@end
658
659@implementation RDar6320065Subclass
660@end
661
662int RDar6320065_test() {
663 RDar6320065 *test = [[RDar6320065 alloc] init]; // no-warning
664 [test release];
665 return 0;
666}
667
668//===----------------------------------------------------------------------===//
Ted Kremenekf9fa3cb2009-05-14 21:29:16 +0000669// <rdar://problem/6859457> [NSData dataWithBytesNoCopy] does not return a retained object
670//===----------------------------------------------------------------------===//
671
672@interface RDar6859457 : NSObject {}
673- (NSString*) NoCopyString;
674- (NSString*) noCopyString;
675@end
676
677@implementation RDar6859457
678- (NSString*) NoCopyString { return [[NSString alloc] init]; } // no-warning
679- (NSString*) noCopyString { return [[NSString alloc] init]; } // no-warning
680@end
681
682void test_RDar6859457(RDar6859457 *x, void *bytes, NSUInteger dataLength) {
683 [x NoCopyString]; // expected-warning{{leak}}
684 [x noCopyString]; // expected-warning{{leak}}
685 [NSData dataWithBytesNoCopy:bytes length:dataLength]; // no-warning
686 [NSData dataWithBytesNoCopy:bytes length:dataLength freeWhenDone:1]; // no-warning
687}
688
689//===----------------------------------------------------------------------===//
Ted Kremenek501ba032009-05-18 23:14:34 +0000690// PR 4230 - an autorelease pool is not necessarily leaked during a premature
691// return
692//===----------------------------------------------------------------------===//
693
694static void PR4230(void)
695{
696 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // no-warning
697 NSString *object = [[[NSString alloc] init] autorelease]; // no-warning
698 return;
699}
700
701//===----------------------------------------------------------------------===//
Ted Kremenek32819772009-05-15 15:49:00 +0000702// Method name that has a null IdentifierInfo* for its first selector slot.
703// This test just makes sure that we handle it.
704//===----------------------------------------------------------------------===//
705
706@interface TestNullIdentifier
707@end
708
709@implementation TestNullIdentifier
710+ (id):(int)x, ... {
711 return [[NSString alloc] init]; // expected-warning{{leak}}
712}
Ted Kremenek10369122009-05-20 22:39:57 +0000713@end
Ted Kremenek32819772009-05-15 15:49:00 +0000714
715//===----------------------------------------------------------------------===//
Ted Kremeneke4302ee2009-05-16 01:38:01 +0000716// <rdar://problem/6893565> don't flag leaks for return types that cannot be
717// determined to be CF types
718//===----------------------------------------------------------------------===//
719
720// We don't know if 'struct s6893565' represents a Core Foundation type, so
721// we shouldn't emit an error here.
722typedef struct s6893565* TD6893565;
723
724@interface RDar6893565 {}
725-(TD6893565)newThing;
726@end
727
728@implementation RDar6893565
729-(TD6893565)newThing {
730 return (TD6893565) [[NSString alloc] init]; // no-warning
731}
732@end
733
734//===----------------------------------------------------------------------===//
Ted Kremenek10369122009-05-20 22:39:57 +0000735// <rdar://problem/6902710> clang: false positives w/QC and CoreImage methods
736//===----------------------------------------------------------------------===//
737
738void rdar6902710(QCView *view, QCRenderer *renderer, CIContext *context,
739 NSString *str, CIImage *img, CGRect rect,
740 CIFormat form, CGColorSpaceRef cs) {
741 [view createSnapshotImageOfType:str]; // expected-warning{{leak}}
742 [renderer createSnapshotImageOfType:str]; // expected-warning{{leak}}
743 [context createCGImage:img fromRect:rect]; // expected-warning{{leak}}
744 [context createCGImage:img fromRect:rect format:form colorSpace:cs]; // expected-warning{{leak}}
745}
746
747//===----------------------------------------------------------------------===//
Ted Kremenekdc935e92009-06-16 20:44:39 +0000748// <rdar://problem/6945561> -[CIContext createCGLayerWithSize:info:]
749// misinterpreted by clang scan-build
750//===----------------------------------------------------------------------===//
751
752void rdar6945561(CIContext *context, CGSize size, CFDictionaryRef d) {
753 [context createCGLayerWithSize:size info:d]; // expected-warning{{leak}}
754}
755
756//===----------------------------------------------------------------------===//
757// <rdar://problem/6961230> add knowledge of IOKit functions to retain/release
758// checker
759//===----------------------------------------------------------------------===//
760
761void IOBSDNameMatching_wrapper(mach_port_t masterPort, uint32_t options, const char * bsdName) {
762 IOBSDNameMatching(masterPort, options, bsdName); // expected-warning{{leak}}
763}
764
765void IOServiceMatching_wrapper(const char * name) {
766 IOServiceMatching(name); // expected-warning{{leak}}
767}
768
769void IOServiceNameMatching_wrapper(const char * name) {
770 IOServiceNameMatching(name); // expected-warning{{leak}}
771}
772
Ted Kremeneke5a068f2009-07-17 00:19:33 +0000773CF_RETURNS_RETAINED CFDictionaryRef CreateDict();
Ted Kremenekdc935e92009-06-16 20:44:39 +0000774
775void IOServiceAddNotification_wrapper(mach_port_t masterPort, const io_name_t notificationType,
776 mach_port_t wakePort, uintptr_t reference, io_iterator_t * notification ) {
777
778 CFDictionaryRef matching = CreateDict();
779 CFRelease(matching);
780 IOServiceAddNotification(masterPort, notificationType, matching, // expected-warning{{used after it is released}} expected-warning{{deprecated}}
781 wakePort, reference, notification);
782}
783
784void IORegistryEntryIDMatching_wrapper(uint64_t entryID ) {
785 IORegistryEntryIDMatching(entryID); // expected-warning{{leak}}
786}
787
788void IOOpenFirmwarePathMatching_wrapper(mach_port_t masterPort, uint32_t options,
789 const char * path) {
790 IOOpenFirmwarePathMatching(masterPort, options, path); // expected-warning{{leak}}
791}
792
793void IOServiceGetMatchingService_wrapper(mach_port_t masterPort) {
794 CFDictionaryRef matching = CreateDict();
795 IOServiceGetMatchingService(masterPort, matching);
796 CFRelease(matching); // expected-warning{{used after it is released}}
797}
798
799void IOServiceGetMatchingServices_wrapper(mach_port_t masterPort, io_iterator_t *existing) {
800 CFDictionaryRef matching = CreateDict();
801 IOServiceGetMatchingServices(masterPort, matching, existing);
802 CFRelease(matching); // expected-warning{{used after it is released}}
803}
804
805void IOServiceAddMatchingNotification_wrapper(IONotificationPortRef notifyPort, const io_name_t notificationType,
806 IOServiceMatchingCallback callback, void * refCon, io_iterator_t * notification) {
807
808 CFDictionaryRef matching = CreateDict();
809 IOServiceAddMatchingNotification(notifyPort, notificationType, matching, callback, refCon, notification);
810 CFRelease(matching); // expected-warning{{used after it is released}}
811}
812
813//===----------------------------------------------------------------------===//
Ted Kremenekd73cfc72009-05-09 03:10:32 +0000814// Tests of ownership attributes.
815//===----------------------------------------------------------------------===//
816
Ted Kremenek3b204e42009-05-13 21:07:32 +0000817typedef NSString* MyStringTy;
818
Ted Kremenekd73cfc72009-05-09 03:10:32 +0000819@interface TestOwnershipAttr : NSObject
Ted Kremeneke5a068f2009-07-17 00:19:33 +0000820- (NSString*) returnsAnOwnedString NS_RETURNS_RETAINED; // no-warning
821- (NSString*) returnsAnOwnedCFString CF_RETURNS_RETAINED; // no-warning
822- (MyStringTy) returnsAnOwnedTypedString NS_RETURNS_RETAINED; // no-warning
823- (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 Kremenekd73cfc72009-05-09 03:10:32 +0000824@end
825
Ted Kremeneke5a068f2009-07-17 00:19:33 +0000826static int ownership_attribute_doesnt_go_here NS_RETURNS_RETAINED; // expected-warning{{'ns_returns_retained' attribute only applies to function or method types}}
Ted Kremenek3b204e42009-05-13 21:07:32 +0000827
Ted Kremenekd73cfc72009-05-09 03:10:32 +0000828void test_attr_1(TestOwnershipAttr *X) {
829 NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
830}
831
832void test_attr_1b(TestOwnershipAttr *X) {
833 NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}}
834}
Ted Kremenekea1c2212009-06-05 23:00:33 +0000835
836@interface MyClassTestCFAttr : NSObject {}
Ted Kremeneke5a068f2009-07-17 00:19:33 +0000837- (NSDate*) returnsCFRetained CF_RETURNS_RETAINED;
838- (CFDateRef) returnsCFRetainedAsCF CF_RETURNS_RETAINED;
Ted Kremenekea1c2212009-06-05 23:00:33 +0000839- (NSDate*) alsoReturnsRetained;
Ted Kremenek0578e432009-07-06 18:30:43 +0000840- (CFDateRef) alsoReturnsRetainedAsCF;
Ted Kremeneke5a068f2009-07-17 00:19:33 +0000841- (NSDate*) returnsNSRetained NS_RETURNS_RETAINED;
Ted Kremenekea1c2212009-06-05 23:00:33 +0000842@end
843
Ted Kremeneke5a068f2009-07-17 00:19:33 +0000844CF_RETURNS_RETAINED
Ted Kremenekea1c2212009-06-05 23:00:33 +0000845CFDateRef returnsRetainedCFDate() {
846 return CFDateCreate(0, CFAbsoluteTimeGetCurrent());
847}
848
849@implementation MyClassTestCFAttr
850- (NSDate*) returnsCFRetained {
851 return (NSDate*) returnsRetainedCFDate(); // No leak.
852}
853
Ted Kremenek0578e432009-07-06 18:30:43 +0000854- (CFDateRef) returnsCFRetainedAsCF {
855 return returnsRetainedCFDate(); // No leak.
856}
857
858
Ted Kremenekea1c2212009-06-05 23:00:33 +0000859- (NSDate*) alsoReturnsRetained {
860 return (NSDate*) returnsRetainedCFDate(); // expected-warning{{leak}}
861}
862
Ted Kremenek0578e432009-07-06 18:30:43 +0000863- (CFDateRef) alsoReturnsRetainedAsCF {
864 return returnsRetainedCFDate(); // expected-warning{{leak}}
865}
866
867
Ted Kremenekea1c2212009-06-05 23:00:33 +0000868- (NSDate*) returnsNSRetained {
869 return (NSDate*) returnsRetainedCFDate(); // no-warning
870}
871@end
872
873
Ted Kremenekd73cfc72009-05-09 03:10:32 +0000874