blob: 3eeebc4a8f0a652bc273dab9873112987453c065 [file] [log] [blame]
Daniel Dunbar4fcfde42009-11-08 01:45:36 +00001// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -verify -fobjc-gc-only %s
Daniel Dunbard7d5f022009-03-24 02:24:46 +00002// RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -fobjc-gc-only -verify %s
Ted Kremenekabf43972009-01-28 21:44:40 +00003
4//===----------------------------------------------------------------------===//
5// Header stuff.
6//===----------------------------------------------------------------------===//
Ted Kremeneke798e7c2009-04-27 19:14:45 +00007
Ted Kremenekabf43972009-01-28 21:44:40 +00008typedef unsigned int __darwin_natural_t;
Ted Kremeneka834fb42009-08-28 19:52:12 +00009typedef unsigned long uintptr_t;
10typedef unsigned int uint32_t;
11typedef unsigned long long uint64_t;
12typedef unsigned int UInt32;
Ted Kremenekabf43972009-01-28 21:44:40 +000013typedef signed long CFIndex;
Ted Kremeneka834fb42009-08-28 19:52:12 +000014typedef struct {
15 CFIndex location;
16 CFIndex length;
17} CFRange;
18static __inline__ __attribute__((always_inline)) CFRange CFRangeMake(CFIndex loc, CFIndex len) {
19 CFRange range;
20 range.location = loc;
21 range.length = len;
22 return range;
23}
Ted Kremenekabf43972009-01-28 21:44:40 +000024typedef const void * CFTypeRef;
25typedef const struct __CFString * CFStringRef;
26typedef const struct __CFAllocator * CFAllocatorRef;
27extern const CFAllocatorRef kCFAllocatorDefault;
28extern CFTypeRef CFRetain(CFTypeRef cf);
29extern void CFRelease(CFTypeRef cf);
30typedef struct {
31}
Ted Kremeneke798e7c2009-04-27 19:14:45 +000032CFArrayCallBacks;
Ted Kremenekabf43972009-01-28 21:44:40 +000033extern const CFArrayCallBacks kCFTypeArrayCallBacks;
34typedef const struct __CFArray * CFArrayRef;
35typedef struct __CFArray * CFMutableArrayRef;
36extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks);
37extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx);
Ted Kremeneke798e7c2009-04-27 19:14:45 +000038extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value);
Ted Kremeneka834fb42009-08-28 19:52:12 +000039typedef struct {
40}
41CFDictionaryKeyCallBacks;
42extern const CFDictionaryKeyCallBacks kCFTypeDictionaryKeyCallBacks;
43typedef struct {
44}
45CFDictionaryValueCallBacks;
46extern const CFDictionaryValueCallBacks kCFTypeDictionaryValueCallBacks;
Ted Kremenekabf43972009-01-28 21:44:40 +000047typedef const struct __CFDictionary * CFDictionaryRef;
Ted Kremeneka834fb42009-08-28 19:52:12 +000048typedef struct __CFDictionary * CFMutableDictionaryRef;
49extern CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFDictionaryKeyCallBacks *keyCallBacks, const CFDictionaryValueCallBacks *valueCallBacks);
Ted Kremenekabf43972009-01-28 21:44:40 +000050typedef UInt32 CFStringEncoding;
51enum {
52kCFStringEncodingMacRoman = 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 };
53extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding);
54typedef double CFTimeInterval;
55typedef CFTimeInterval CFAbsoluteTime;
Ted Kremeneke798e7c2009-04-27 19:14:45 +000056extern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void);
Ted Kremenekabf43972009-01-28 21:44:40 +000057typedef const struct __CFDate * CFDateRef;
58extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at);
59extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate);
60typedef __darwin_natural_t natural_t;
61typedef natural_t mach_port_name_t;
62typedef mach_port_name_t mach_port_t;
Ted Kremeneka834fb42009-08-28 19:52:12 +000063typedef int kern_return_t;
64typedef kern_return_t mach_error_t;
65enum {
66kCFNumberSInt8Type = 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 };
67typedef CFIndex CFNumberType;
68typedef const struct __CFNumber * CFNumberRef;
69extern CFNumberRef CFNumberCreate(CFAllocatorRef allocator, CFNumberType theType, const void *valuePtr);
70typedef const struct __CFAttributedString *CFAttributedStringRef;
71typedef struct __CFAttributedString *CFMutableAttributedStringRef;
72extern CFAttributedStringRef CFAttributedStringCreate(CFAllocatorRef alloc, CFStringRef str, CFDictionaryRef attributes) ;
73extern CFMutableAttributedStringRef CFAttributedStringCreateMutableCopy(CFAllocatorRef alloc, CFIndex maxLength, CFAttributedStringRef aStr) ;
74extern void CFAttributedStringSetAttribute(CFMutableAttributedStringRef aStr, CFRange range, CFStringRef attrName, CFTypeRef value) ;
Ted Kremenekabf43972009-01-28 21:44:40 +000075typedef signed char BOOL;
Ted Kremeneka834fb42009-08-28 19:52:12 +000076typedef unsigned long NSUInteger;
Ted Kremeneke798e7c2009-04-27 19:14:45 +000077@class NSString, Protocol;
78extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
Ted Kremenekabf43972009-01-28 21:44:40 +000079typedef struct _NSZone NSZone;
80@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
Ted Kremeneka834fb42009-08-28 19:52:12 +000081@protocol NSObject
82- (BOOL)isEqual:(id)object;
Ted Kremenekabf43972009-01-28 21:44:40 +000083- (id)retain;
84- (oneway void)release;
85- (id)autorelease;
Ted Kremeneka834fb42009-08-28 19:52:12 +000086- (Class)class;
Ted Kremenekabf43972009-01-28 21:44:40 +000087@end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone;
Ted Kremeneke798e7c2009-04-27 19:14:45 +000088@end @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone;
Ted Kremenekabf43972009-01-28 21:44:40 +000089@end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder;
Ted Kremeneke798e7c2009-04-27 19:14:45 +000090@end
91@interface NSObject <NSObject> {}
Ted Kremeneke798e7c2009-04-27 19:14:45 +000092+ (id)allocWithZone:(NSZone *)zone;
Ted Kremeneka834fb42009-08-28 19:52:12 +000093+ (id)alloc;
94- (void)dealloc;
Ted Kremenek54cb7cc2009-11-03 08:03:59 +000095- (void)release;
Ted Kremeneka834fb42009-08-28 19:52:12 +000096@end
97@interface NSObject (NSCoderMethods)
98- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder;
99@end
100extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
101typedef struct {
102}
103NSFastEnumerationState;
104@protocol NSFastEnumeration - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
105@end @class NSString, NSDictionary;
106@interface NSValue : NSObject <NSCopying, NSCoding> - (void)getValue:(void *)value;
107@end @interface NSNumber : NSValue - (char)charValue;
108- (id)initWithInt:(int)value;
109@end @class NSString;
110@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count;
111@end @interface NSArray (NSArrayCreation) + (id)array;
112@end @interface NSAutoreleasePool : NSObject {
113}
114- (void)drain;
115@end extern NSString * const NSBundleDidLoadNotification;
116typedef double NSTimeInterval;
117@interface NSDate : NSObject <NSCopying, NSCoding> - (NSTimeInterval)timeIntervalSinceReferenceDate;
118@end typedef unsigned short unichar;
Ted Kremeneke798e7c2009-04-27 19:14:45 +0000119@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length;
Ted Kremeneka834fb42009-08-28 19:52:12 +0000120- ( const char *)UTF8String;
Ted Kremeneke798e7c2009-04-27 19:14:45 +0000121- (id)initWithUTF8String:(const char *)nullTerminatedCString;
122+ (id)stringWithUTF8String:(const char *)nullTerminatedCString;
Ted Kremeneka834fb42009-08-28 19:52:12 +0000123@end @class NSString, NSURL, NSError;
124@interface NSData : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length;
125+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length;
126+ (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b;
127@end @class NSLocale, NSDate, NSCalendar, NSTimeZone, NSError, NSArray, NSMutableDictionary;
128@interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> - (NSUInteger)count;
129@end @interface NSMutableDictionary : NSDictionary - (void)removeObjectForKey:(id)aKey;
130- (void)setObject:(id)anObject forKey:(id)aKey;
131@end @interface NSMutableDictionary (NSMutableDictionaryCreation) + (id)dictionaryWithCapacity:(NSUInteger)numItems;
132@end typedef double CGFloat;
133struct CGSize {
134};
135typedef struct CGSize CGSize;
136struct CGRect {
137};
138typedef struct CGRect CGRect;
Ted Kremenekabf43972009-01-28 21:44:40 +0000139typedef mach_port_t io_object_t;
Ted Kremeneka834fb42009-08-28 19:52:12 +0000140typedef char io_name_t[128];
141typedef io_object_t io_iterator_t;
Ted Kremenekabf43972009-01-28 21:44:40 +0000142typedef io_object_t io_service_t;
Ted Kremeneka834fb42009-08-28 19:52:12 +0000143typedef struct IONotificationPort * IONotificationPortRef;
144typedef void (*IOServiceMatchingCallback)( void * refcon, io_iterator_t iterator );
145io_service_t IOServiceGetMatchingService( mach_port_t masterPort, CFDictionaryRef matching );
146kern_return_t IOServiceGetMatchingServices( mach_port_t masterPort, CFDictionaryRef matching, io_iterator_t * existing );
147kern_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));
148kern_return_t IOServiceAddMatchingNotification( IONotificationPortRef notifyPort, const io_name_t notificationType, CFDictionaryRef matching, IOServiceMatchingCallback callback, void * refCon, io_iterator_t * notification );
149CFMutableDictionaryRef IOServiceMatching( const char * name );
150CFMutableDictionaryRef IOServiceNameMatching( const char * name );
151CFMutableDictionaryRef IOBSDNameMatching( mach_port_t masterPort, uint32_t options, const char * bsdName );
152CFMutableDictionaryRef IOOpenFirmwarePathMatching( mach_port_t masterPort, uint32_t options, const char * path );
153CFMutableDictionaryRef IORegistryEntryIDMatching( uint64_t entryID );
Ted Kremenekabf43972009-01-28 21:44:40 +0000154typedef struct __DASession * DASessionRef;
155extern DASessionRef DASessionCreate( CFAllocatorRef allocator );
156typedef struct __DADisk * DADiskRef;
157extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name );
158extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media );
159extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk );
160extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk );
Ted Kremeneka834fb42009-08-28 19:52:12 +0000161@interface NSTask : NSObject - (id)init;
162@end typedef struct CGColorSpace *CGColorSpaceRef;
163typedef struct CGImage *CGImageRef;
164typedef struct CGLayer *CGLayerRef;
Ted Kremeneke798e7c2009-04-27 19:14:45 +0000165@interface NSResponder : NSObject <NSCoding> {
166}
Ted Kremeneka834fb42009-08-28 19:52:12 +0000167@end @protocol NSAnimatablePropertyContainer - (id)animator;
168@end extern NSString *NSAnimationTriggerOrderIn ;
169@interface NSView : NSResponder <NSAnimatablePropertyContainer> {
Ted Kremeneke798e7c2009-04-27 19:14:45 +0000170}
Ted Kremeneka834fb42009-08-28 19:52:12 +0000171@end @protocol NSValidatedUserInterfaceItem - (SEL)action;
172@end @protocol NSUserInterfaceValidations - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem;
173@end @class NSDate, NSDictionary, NSError, NSException, NSNotification;
174@interface NSApplication : NSResponder <NSUserInterfaceValidations> {
175}
176@end enum {
177NSTerminateCancel = 0, NSTerminateNow = 1, NSTerminateLater = 2 };
178typedef NSUInteger NSApplicationTerminateReply;
179@protocol NSApplicationDelegate <NSObject> @optional - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
180@end @class NSAttributedString, NSEvent, NSFont, NSFormatter, NSImage, NSMenu, NSText, NSView, NSTextView;
Ted Kremeneke798e7c2009-04-27 19:14:45 +0000181@interface NSCell : NSObject <NSCopying, NSCoding> {
182}
Ted Kremeneka834fb42009-08-28 19:52:12 +0000183@end @class NSTextField, NSPanel, NSArray, NSWindow, NSImage, NSButton, NSError;
184typedef struct {
Ted Kremenekabf43972009-01-28 21:44:40 +0000185}
Ted Kremeneka834fb42009-08-28 19:52:12 +0000186CVTimeStamp;
187@interface CIImage : NSObject <NSCoding, NSCopying> {
188}
189typedef int CIFormat;
190@end enum {
Ted Kremenekabf43972009-01-28 21:44:40 +0000191kDAReturnSuccess = 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 };
192typedef mach_error_t DAReturn;
193typedef const struct __DADissenter * DADissenterRef;
194extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string );
Ted Kremeneka834fb42009-08-28 19:52:12 +0000195@interface CIContext: NSObject {
196}
197- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r;
198- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r format:(CIFormat)f colorSpace:(CGColorSpaceRef)cs;
199- (CGLayerRef)createCGLayerWithSize:(CGSize)size info:(CFDictionaryRef)d;
200@end extern NSString* const QCRendererEventKey;
201@protocol QCCompositionRenderer - (NSDictionary*) attributes;
202@end @interface QCRenderer : NSObject <QCCompositionRenderer> {
203}
204- (id) createSnapshotImageOfType:(NSString*)type;
205@end extern NSString* const QCViewDidStartRenderingNotification;
206@interface QCView : NSView <QCCompositionRenderer> {
207}
208- (id) createSnapshotImageOfType:(NSString*)type;
209@end enum {
210ICEXIFOrientation1 = 1, ICEXIFOrientation2 = 2, ICEXIFOrientation3 = 3, ICEXIFOrientation4 = 4, ICEXIFOrientation5 = 5, ICEXIFOrientation6 = 6, ICEXIFOrientation7 = 7, ICEXIFOrientation8 = 8, };
211@class ICDevice;
212@protocol ICDeviceDelegate <NSObject> @required - (void)didRemoveDevice:(ICDevice*)device;
213@end extern NSString *const ICScannerStatusWarmingUp;
214@class ICScannerDevice;
215@protocol ICScannerDeviceDelegate <ICDeviceDelegate> @optional - (void)scannerDeviceDidBecomeAvailable:(ICScannerDevice*)scanner;
216@end
Ted Kremeneke798e7c2009-04-27 19:14:45 +0000217CFTypeRef CFMakeCollectable(CFTypeRef cf) ;
218
Ted Kremenek56db7e82009-06-03 19:19:06 +0000219static __inline__ __attribute__((always_inline)) id NSMakeCollectable(CFTypeRef
220cf) {
221 return cf ? (id)CFMakeCollectable(cf) : ((void*)0);
222}
223
Ted Kremenekabf43972009-01-28 21:44:40 +0000224//===----------------------------------------------------------------------===//
225// Test cases.
226//===----------------------------------------------------------------------===//
227
228void f1() {
Ted Kremenekb2b14d72009-02-18 22:11:23 +0000229 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
Ted Kremenekabf43972009-01-28 21:44:40 +0000230 id x = [(id) A autorelease];
Ted Kremenekb2b14d72009-02-18 22:11:23 +0000231 CFRelease((CFMutableArrayRef) x);
232}
233
234void f2() {
235 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}}
236 id x = [(id) A retain];
237 [x release];
238 [x release];
239}
240
241void f3() {
242 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}}
243 CFMakeCollectable(A);
244 CFRetain(A);
Ted Kremenekabf43972009-01-28 21:44:40 +0000245}
Ted Kremenek4064de92009-04-27 18:27:22 +0000246
Ted Kremenek56db7e82009-06-03 19:19:06 +0000247void f3b() {
248 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
249 CFMakeCollectable(A);
250}
251
252
253void f4() {
254 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}}
255 NSMakeCollectable(A);
256 CFRetain(A);
257}
258
259void f4b() {
260 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
261 NSMakeCollectable(A);
262}
263
264void f5() {
265 id x = [NSMakeCollectable(CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks)) autorelease]; // no-warning
266}
267
268void f5b() {
269 id x = [(id) CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks) autorelease]; // expected-warning{{leak}}
270}
271
Ted Kremeneke8720ce2009-05-10 06:25:57 +0000272// Test return of non-owned objects in contexts where an owned object
273// is expected.
274@interface TestReturnNotOwnedWhenExpectedOwned
275- (NSString*)newString;
Ted Kremenek82f2be52009-05-10 16:52:15 +0000276- (CFMutableArrayRef)newArray;
Ted Kremeneke8720ce2009-05-10 06:25:57 +0000277@end
278
279@implementation TestReturnNotOwnedWhenExpectedOwned
280- (NSString*)newString {
Ted Kremenek56db7e82009-06-03 19:19:06 +0000281 NSString *s = [NSString stringWithUTF8String:"hello"]; // expected-warning{{Potential leak (when using garbage collection) of an object allocated}}
Ted Kremenek82f2be52009-05-10 16:52:15 +0000282 CFRetain(s);
283 return s;
284}
285- (CFMutableArrayRef)newArray{
286 return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
Ted Kremeneke8720ce2009-05-10 06:25:57 +0000287}
288@end
289
Ted Kremenek8c6096e2009-05-09 03:10:32 +0000290//===----------------------------------------------------------------------===//
Ted Kremenek547d4952009-06-05 23:18:01 +0000291// <rdar://problem/6948053> False positive: object substitution during -init*
292// methods warns about returning +0 when using -fobjc-gc-only
293//===----------------------------------------------------------------------===//
294
295@interface MyClassRdar6948053 : NSObject
296- (id) init;
297+ (id) shared;
298@end
299
300@implementation MyClassRdar6948053
301+(id) shared {
302 return (id) 0;
303}
304- (id) init
305{
306 Class myClass = [self class];
307 [self release];
308 return [[myClass shared] retain]; // no-warning
309}
310@end
311
312//===----------------------------------------------------------------------===//
Ted Kremeneka834fb42009-08-28 19:52:12 +0000313// <rdar://problem/7174400> 'ciContext createCGImage:outputImage fromRect:' returns a retained CF object (not GC'ed)//===----------------------------------------------------------------------===//
314//===----------------------------------------------------------------------===//
315
316void rdar_7174400(QCView *view, QCRenderer *renderer, CIContext *context,
317 NSString *str, CIImage *img, CGRect rect,
318 CIFormat form, CGColorSpaceRef cs) {
319 [view createSnapshotImageOfType:str]; // no-warning
320 [renderer createSnapshotImageOfType:str]; // no-warning
321 [context createCGImage:img fromRect:rect]; // expected-warning{{leak}}
322 [context createCGImage:img fromRect:rect format:form colorSpace:cs]; // expected-warning{{leak}}
323}
324
325//===----------------------------------------------------------------------===//
Ted Kremenek54cb7cc2009-11-03 08:03:59 +0000326// <rdar://problem/6250216> Warn against using -[NSAutoreleasePool release] in
327// GC mode
328//===----------------------------------------------------------------------===//
329
330void rdar_6250216(void) {
331 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
332 [pool release]; // expected-warning{{Use -drain instead of -release when using NSAutoreleasePool and garbage collection}}
333}
334
335//===----------------------------------------------------------------------===//
Ted Kremenek8c6096e2009-05-09 03:10:32 +0000336// Tests of ownership attributes.
337//===----------------------------------------------------------------------===//
338
339@interface TestOwnershipAttr : NSObject
340- (NSString*) returnsAnOwnedString __attribute__((ns_returns_retained));
341- (NSString*) returnsAnOwnedCFString __attribute__((cf_returns_retained));
342@end
343
344void test_attr_1(TestOwnershipAttr *X) {
345 NSString *str = [X returnsAnOwnedString]; // no-warning
346}
347
348void test_attr_1b(TestOwnershipAttr *X) {
349 NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}}
350}
351
Ted Kremenekb9d8db82009-06-05 23:00:33 +0000352@interface MyClassTestCFAttr : NSObject {}
353- (NSDate*) returnsCFRetained __attribute__((cf_returns_retained));
354- (NSDate*) alsoReturnsRetained;
355- (NSDate*) returnsNSRetained __attribute__((ns_returns_retained));
356@end
357
358__attribute__((cf_returns_retained))
359CFDateRef returnsRetainedCFDate() {
360 return CFDateCreate(0, CFAbsoluteTimeGetCurrent());
361}
362
363@implementation MyClassTestCFAttr
364- (NSDate*) returnsCFRetained {
365 return (NSDate*) returnsRetainedCFDate(); // No leak.
366}
367
368- (NSDate*) alsoReturnsRetained {
369 return (NSDate*) returnsRetainedCFDate(); // expected-warning{{leak}}
370}
371
372- (NSDate*) returnsNSRetained {
373 return (NSDate*) returnsRetainedCFDate(); // expected-warning{{leak}}
374}
Daniel Dunbar4fcfde42009-11-08 01:45:36 +0000375@end