]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/Analysis/retain-release.m
Update clang to r96341.
[FreeBSD/FreeBSD.git] / test / Analysis / retain-release.m
1 // 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
3
4 #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
11 //===----------------------------------------------------------------------===//
12 // 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>
19 // #include <IOKit/IOKitLib.h>
20 //
21 // It includes the basic definitions for the test cases below.
22 //===----------------------------------------------------------------------===//
23
24 typedef unsigned int __darwin_natural_t;
25 typedef unsigned long uintptr_t;
26 typedef unsigned int uint32_t;
27 typedef unsigned long long uint64_t;
28 typedef unsigned int UInt32;
29 typedef signed long CFIndex;
30 typedef struct {
31     CFIndex location;
32     CFIndex length;
33 } CFRange;
34 static __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 }
40 typedef const void * CFTypeRef;
41 typedef const struct __CFString * CFStringRef;
42 typedef const struct __CFAllocator * CFAllocatorRef;
43 extern const CFAllocatorRef kCFAllocatorDefault;
44 extern CFTypeRef CFRetain(CFTypeRef cf);
45 extern void CFRelease(CFTypeRef cf);
46 typedef struct {
47 }
48 CFArrayCallBacks;
49 extern const CFArrayCallBacks kCFTypeArrayCallBacks;
50 typedef const struct __CFArray * CFArrayRef;
51 typedef struct __CFArray * CFMutableArrayRef;
52 extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks);
53 extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx);
54 extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value);
55 typedef struct {
56 }
57 CFDictionaryKeyCallBacks;
58 extern const CFDictionaryKeyCallBacks kCFTypeDictionaryKeyCallBacks;
59 typedef struct {
60 }
61 CFDictionaryValueCallBacks;
62 extern const CFDictionaryValueCallBacks kCFTypeDictionaryValueCallBacks;
63 typedef const struct __CFDictionary * CFDictionaryRef;
64 typedef struct __CFDictionary * CFMutableDictionaryRef;
65 extern CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFDictionaryKeyCallBacks *keyCallBacks, const CFDictionaryValueCallBacks *valueCallBacks);
66 typedef UInt32 CFStringEncoding;
67 enum {
68 kCFStringEncodingMacRoman = 0,     kCFStringEncodingWindowsLatin1 = 0x0500,     kCFStringEncodingISOLatin1 = 0x0201,     kCFStringEncodingNextStepLatin = 0x0B01,     kCFStringEncodingASCII = 0x0600,     kCFStringEncodingUnicode = 0x0100,     kCFStringEncodingUTF8 = 0x08000100,     kCFStringEncodingNonLossyASCII = 0x0BFF      ,     kCFStringEncodingUTF16 = 0x0100,     kCFStringEncodingUTF16BE = 0x10000100,     kCFStringEncodingUTF16LE = 0x14000100,      kCFStringEncodingUTF32 = 0x0c000100,     kCFStringEncodingUTF32BE = 0x18000100,     kCFStringEncodingUTF32LE = 0x1c000100  };
69 extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding);
70 typedef double CFTimeInterval;
71 typedef CFTimeInterval CFAbsoluteTime;
72 extern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void);
73 typedef const struct __CFDate * CFDateRef;
74 extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at);
75 extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate);
76 typedef __darwin_natural_t natural_t;
77 typedef natural_t mach_port_name_t;
78 typedef mach_port_name_t mach_port_t;
79 typedef int kern_return_t;
80 typedef kern_return_t mach_error_t;
81 enum {
82 kCFNumberSInt8Type = 1,     kCFNumberSInt16Type = 2,     kCFNumberSInt32Type = 3,     kCFNumberSInt64Type = 4,     kCFNumberFloat32Type = 5,     kCFNumberFloat64Type = 6,      kCFNumberCharType = 7,     kCFNumberShortType = 8,     kCFNumberIntType = 9,     kCFNumberLongType = 10,     kCFNumberLongLongType = 11,     kCFNumberFloatType = 12,     kCFNumberDoubleType = 13,      kCFNumberCFIndexType = 14,      kCFNumberNSIntegerType = 15,     kCFNumberCGFloatType = 16,     kCFNumberMaxType = 16    };
83 typedef CFIndex CFNumberType;
84 typedef const struct __CFNumber * CFNumberRef;
85 extern CFNumberRef CFNumberCreate(CFAllocatorRef allocator, CFNumberType theType, const void *valuePtr);
86 typedef const struct __CFAttributedString *CFAttributedStringRef;
87 typedef struct __CFAttributedString *CFMutableAttributedStringRef;
88 extern CFAttributedStringRef CFAttributedStringCreate(CFAllocatorRef alloc, CFStringRef str, CFDictionaryRef attributes) ;
89 extern CFMutableAttributedStringRef CFAttributedStringCreateMutableCopy(CFAllocatorRef alloc, CFIndex maxLength, CFAttributedStringRef aStr) ;
90 extern void CFAttributedStringSetAttribute(CFMutableAttributedStringRef aStr, CFRange range, CFStringRef attrName, CFTypeRef value) ;
91 typedef signed char BOOL;
92 typedef unsigned long NSUInteger;
93 @class NSString, Protocol;
94 extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
95 typedef struct _NSZone NSZone;
96 @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
97 @protocol NSObject
98 - (BOOL)isEqual:(id)object;
99 - (id)retain;
100 - (oneway void)release;
101 - (id)autorelease;
102 @end  @protocol NSCopying  - (id)copyWithZone:(NSZone *)zone;
103 @end  @protocol NSMutableCopying  - (id)mutableCopyWithZone:(NSZone *)zone;
104 @end  @protocol NSCoding  - (void)encodeWithCoder:(NSCoder *)aCoder;
105 @end
106 @interface NSObject <NSObject> {}
107 + (id)allocWithZone:(NSZone *)zone;
108 + (id)alloc;
109 - (void)dealloc;
110 @end
111 @interface NSObject (NSCoderMethods)
112 - (id)awakeAfterUsingCoder:(NSCoder *)aDecoder;
113 @end
114 extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
115 typedef struct {
116 }
117 NSFastEnumerationState;
118 @protocol NSFastEnumeration  - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
119 @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;
123 @end   @class NSString;
124 @interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>  - (NSUInteger)count;
125 @end  @interface NSArray (NSArrayCreation)  + (id)array;
126 @end       @interface NSAutoreleasePool : NSObject {
127 }
128 - (void)drain;
129 @end extern NSString * const NSBundleDidLoadNotification;
130 typedef double NSTimeInterval;
131 @interface NSDate : NSObject <NSCopying, NSCoding>  - (NSTimeInterval)timeIntervalSinceReferenceDate;
132 @end            typedef unsigned short unichar;
133 @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>    - (NSUInteger)length;
134 - ( const char *)UTF8String;
135 - (id)initWithUTF8String:(const char *)nullTerminatedCString;
136 + (id)stringWithUTF8String:(const char *)nullTerminatedCString;
137 @end        @class NSString, NSURL, NSError;
138 @interface NSData : NSObject <NSCopying, NSMutableCopying, NSCoding>  - (NSUInteger)length;
139 + (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length;
140 + (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b;
141 @end   @class NSLocale, NSDate, NSCalendar, NSTimeZone, NSError, NSArray, NSMutableDictionary;
142 @interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>  - (NSUInteger)count;
143 @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;
146 @end  typedef double CGFloat;
147 struct CGSize {
148 };
149 typedef struct CGSize CGSize;
150 struct CGRect {
151 };
152 typedef struct CGRect CGRect;
153 typedef mach_port_t io_object_t;
154 typedef char io_name_t[128];
155 typedef io_object_t io_iterator_t;
156 typedef io_object_t io_service_t;
157 typedef struct IONotificationPort * IONotificationPortRef;
158 typedef void (*IOServiceMatchingCallback)(  void * refcon,  io_iterator_t iterator );
159 io_service_t IOServiceGetMatchingService(  mach_port_t masterPort,  CFDictionaryRef matching );
160 kern_return_t IOServiceGetMatchingServices(  mach_port_t masterPort,  CFDictionaryRef matching,  io_iterator_t * existing );
161 kern_return_t IOServiceAddNotification(  mach_port_t masterPort,  const io_name_t notificationType,  CFDictionaryRef matching,  mach_port_t wakePort,  uintptr_t reference,  io_iterator_t * notification ) __attribute__((deprecated));
162 kern_return_t IOServiceAddMatchingNotification(  IONotificationPortRef notifyPort,  const io_name_t notificationType,  CFDictionaryRef matching,         IOServiceMatchingCallback callback,         void * refCon,  io_iterator_t * notification );
163 CFMutableDictionaryRef IOServiceMatching(  const char * name );
164 CFMutableDictionaryRef IOServiceNameMatching(  const char * name );
165 CFMutableDictionaryRef IOBSDNameMatching(  mach_port_t masterPort,  uint32_t options,  const char * bsdName );
166 CFMutableDictionaryRef IOOpenFirmwarePathMatching(  mach_port_t masterPort,  uint32_t options,  const char * path );
167 CFMutableDictionaryRef IORegistryEntryIDMatching(  uint64_t entryID );
168 typedef struct __DASession * DASessionRef;
169 extern DASessionRef DASessionCreate( CFAllocatorRef allocator );
170 typedef struct __DADisk * DADiskRef;
171 extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name );
172 extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media );
173 extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk );
174 extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk );
175 @interface NSTask : NSObject - (id)init;
176 @end                    typedef struct CGColorSpace *CGColorSpaceRef;
177 typedef struct CGImage *CGImageRef;
178 typedef struct CGLayer *CGLayerRef;
179 @interface NSResponder : NSObject <NSCoding> {
180 }
181 @end    @protocol NSAnimatablePropertyContainer      - (id)animator;
182 @end  extern NSString *NSAnimationTriggerOrderIn ;
183 @interface NSView : NSResponder  <NSAnimatablePropertyContainer>  {
184 }
185 @end @protocol NSValidatedUserInterfaceItem - (SEL)action;
186 @end   @protocol NSUserInterfaceValidations - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem;
187 @end  @class NSDate, NSDictionary, NSError, NSException, NSNotification;
188 @interface NSApplication : NSResponder <NSUserInterfaceValidations> {
189 }
190 @end   enum {
191 NSTerminateCancel = 0,         NSTerminateNow = 1,         NSTerminateLater = 2 };
192 typedef NSUInteger NSApplicationTerminateReply;
193 @protocol NSApplicationDelegate <NSObject> @optional        - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
194 @end  @class NSAttributedString, NSEvent, NSFont, NSFormatter, NSImage, NSMenu, NSText, NSView, NSTextView;
195 @interface NSCell : NSObject <NSCopying, NSCoding> {
196 }
197 @end @class NSTextField, NSPanel, NSArray, NSWindow, NSImage, NSButton, NSError;
198 typedef struct {
199 }
200 CVTimeStamp;
201 @interface CIImage : NSObject <NSCoding, NSCopying> {
202 }
203 typedef int CIFormat;
204 @end  enum {
205 kDAReturnSuccess = 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 };
206 typedef mach_error_t DAReturn;
207 typedef const struct __DADissenter * DADissenterRef;
208 extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string );
209 @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;
213 - (CGLayerRef)createCGLayerWithSize:(CGSize)size info:(CFDictionaryRef)d;
214 @end extern NSString* const QCRendererEventKey;
215 @protocol QCCompositionRenderer - (NSDictionary*) attributes;
216 @end   @interface QCRenderer : NSObject <QCCompositionRenderer> {
217 }
218 - (id) createSnapshotImageOfType:(NSString*)type;
219 @end  extern NSString* const QCViewDidStartRenderingNotification;
220 @interface QCView : NSView <QCCompositionRenderer> {
221 }
222 - (id) createSnapshotImageOfType:(NSString*)type;
223 @end    enum {
224 ICEXIFOrientation1 = 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;
227 @end extern NSString *const ICScannerStatusWarmingUp;
228 @class ICScannerDevice;
229 @protocol ICScannerDeviceDelegate <ICDeviceDelegate>  @optional       - (void)scannerDeviceDidBecomeAvailable:(ICScannerDevice*)scanner;
230 @end
231
232 typedef long unsigned int __darwin_size_t;
233 typedef __darwin_size_t size_t;
234 typedef unsigned long CFTypeID;
235 struct CGPoint {
236   CGFloat x;
237   CGFloat y;
238 };
239 typedef struct CGPoint CGPoint;
240 typedef struct CGGradient *CGGradientRef;
241 typedef uint32_t CGGradientDrawingOptions;
242 extern CFTypeID CGGradientGetTypeID(void);
243 extern CGGradientRef CGGradientCreateWithColorComponents(CGColorSpaceRef
244   space, const CGFloat components[], const CGFloat locations[], size_t count);
245 extern CGGradientRef CGGradientCreateWithColors(CGColorSpaceRef space,
246   CFArrayRef colors, const CGFloat locations[]);
247 extern CGGradientRef CGGradientRetain(CGGradientRef gradient);
248 extern void CGGradientRelease(CGGradientRef gradient);
249 typedef struct CGContext *CGContextRef;
250 extern void CGContextDrawLinearGradient(CGContextRef context,
251     CGGradientRef gradient, CGPoint startPoint, CGPoint endPoint,
252     CGGradientDrawingOptions options);
253 extern CGColorSpaceRef CGColorSpaceCreateDeviceRGB(void);
254
255 //===----------------------------------------------------------------------===//
256 // Test cases.
257 //===----------------------------------------------------------------------===//
258
259 CFAbsoluteTime 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
270 CFAbsoluteTime 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
282 NSDate* global_x;
283
284 // Test to see if we supresss an error when we store the pointer
285 // to a global.
286
287 CFAbsoluteTime 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
299 //---------------------------------------------------------------------------
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 //---------------------------------------------------------------------------
303
304 // Test a leak.
305
306 CFAbsoluteTime f5(int x) {  
307   CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
308   CFDateRef date = CFDateCreate(0, t); // expected-warning{{leak}}
309   
310   if (x)
311     CFRelease(date);
312   
313   return t;
314 }
315
316 // Test a leak involving the return.
317
318 CFDateRef f6(int x) {  
319   CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());  // expected-warning{{leak}}
320   CFRetain(date);
321   return date;
322 }
323
324 // Test a leak involving an overwrite.
325
326 CFDateRef f7() {
327   CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());  //expected-warning{{leak}}
328   CFRetain(date);
329   date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());
330   return date;
331 }
332
333 // Generalization of Create rule.  MyDateCreate returns a CFXXXTypeRef, and
334 // has the word create.
335 CFDateRef MyDateCreate();
336
337 CFDateRef f8() {
338   CFDateRef date = MyDateCreate(); // expected-warning{{leak}}
339   CFRetain(date);  
340   return date;
341 }
342
343 CFDateRef f9() {
344   CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());
345   int *p = 0;
346   // When allocations fail, CFDateCreate can return null.
347   if (!date) *p = 1; // expected-warning{{null}}
348   return date;
349 }
350
351 // Handle DiskArbitration API:
352 //
353 // http://developer.apple.com/DOCUMENTATION/DARWIN/Reference/DiscArbitrationFramework/
354 //
355 void f10(io_service_t media, DADiskRef d, CFStringRef s) {
356   DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, 0, "hello"); // expected-warning{{leak}}
357   if (disk) NSLog(@"ok");
358   
359   disk = DADiskCreateFromIOMedia(kCFAllocatorDefault, 0, media); // expected-warning{{leak}}
360   if (disk) NSLog(@"ok");
361
362   CFDictionaryRef dict = DADiskCopyDescription(d);  // expected-warning{{leak}}
363   if (dict) NSLog(@"ok"); 
364   
365   disk = DADiskCopyWholeDisk(d); // expected-warning{{leak}}
366   if (disk) NSLog(@"ok");
367     
368   DADissenterRef dissenter = DADissenterCreate(kCFAllocatorDefault,   // expected-warning{{leak}}
369                                                 kDAReturnSuccess, s);
370   if (dissenter) NSLog(@"ok");
371   
372   DASessionRef session = DASessionCreate(kCFAllocatorDefault);  // expected-warning{{leak}}
373   if (session) NSLog(@"ok");
374 }
375
376 // Test retain/release checker with CFString and CFMutableArray.
377 void 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
401 // PR 3337: Handle functions declared using typedefs.
402 typedef CFTypeRef CREATEFUN();
403 CREATEFUN MyCreateFun;
404
405 void f12() {
406   CFTypeRef o = MyCreateFun(); // expected-warning {{leak}}
407 }
408
409 void f13_autorelease() {
410   CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
411   [(id) A autorelease]; // no-warning
412 }
413
414 void f13_autorelease_b() {
415   CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
416   [(id) A autorelease];
417   [(id) A autorelease]; // expected-warning{{Object sent -autorelease too many times}}
418 }
419
420 CFMutableArrayRef f13_autorelease_c() {
421   CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
422   [(id) A autorelease];
423   [(id) A autorelease]; 
424   return A; // expected-warning{{Object sent -autorelease too many times}}
425 }
426
427 CFMutableArrayRef f13_autorelease_d() {
428   CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
429   [(id) A autorelease];
430   [(id) A autorelease]; 
431   CFMutableArrayRef B = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{Object sent -autorelease too many times}}
432   CFRelease(B); // no-warning
433   while (1) {}
434 }
435
436
437 // This case exercises the logic where the leak site is the same as the allocation site.
438 void f14_leakimmediately() {
439   CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}}
440 }
441
442 // Test that we track an allocated object beyond the point where the *name*
443 // of the variable storing the reference is no longer live.
444 void 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
452 // Test when we pass NULL to CFRetain/CFRelease.
453 void 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 }
464
465 // 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
480 // 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
493 // <rdar://problem/6659160>
494 int isFoo(char c);
495
496 static 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
505   // far as returning 0.  In most error conditions it will throw an exception.
506   // If allocation fails it could return 0, but again this
507   // 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];
524   if(!isFoo(kindC[0])) // expected-warning{{null}}
525     return;
526   if(!isFoo(nameC[0])) // no-warning
527     return;
528
529   [kind release];
530   [name release]; // expected-warning{{Incorrect decrement of the reference count}}
531 }
532
533 // 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
542 // PR 3820 - Reason about calls to -dealloc
543 void 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
550 void 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
558 void 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 }
566
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.
571 void 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
600 //===----------------------------------------------------------------------===//
601 // <rdar://problem/6833332>
602 // One build of the analyzer accidentally stopped tracking the allocated
603 // object after the 'retain'.
604 //===----------------------------------------------------------------------===//
605
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
627 //===----------------------------------------------------------------------===//
628 // <rdar://problem/6257780> clang checker fails to catch use-after-release
629 //===----------------------------------------------------------------------===//
630                                  
631 int rdar_6257780_Case1() {
632   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
633   NSArray *array = [NSArray array];
634   [array release]; // expected-warning{{Incorrect decrement of the reference count of an object that is not owned at this point by the caller}}
635   [pool drain];
636   return 0;
637 }
638
639 //===----------------------------------------------------------------------===//
640 // <rdar://problem/6866843> Checker should understand new/setObject:/release constructs
641 //===----------------------------------------------------------------------===//
642
643 void 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
656
657 //===----------------------------------------------------------------------===//
658 // <rdar://problem/6877235> Classes typedef-ed to CF objects should get the same treatment as CF objects
659 //===----------------------------------------------------------------------===//
660
661 typedef 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
677 //===----------------------------------------------------------------------===//
678 //<rdar://problem/6320065> false positive - init method returns an object
679 // owned by caller
680 //===----------------------------------------------------------------------===//
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
715 int RDar6320065_test() {
716   RDar6320065 *test = [[RDar6320065 alloc] init]; // no-warning
717   [test release];
718   return 0;
719 }
720
721 //===----------------------------------------------------------------------===//
722 // <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
737 //===----------------------------------------------------------------------===//
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
749 void 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 //===----------------------------------------------------------------------===//
757 // PR 4230 - an autorelease pool is not necessarily leaked during a premature
758 //  return
759 //===----------------------------------------------------------------------===//
760
761 static 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 //===----------------------------------------------------------------------===//
769 // 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 }
780 @end
781
782 //===----------------------------------------------------------------------===//
783 // <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.
789 typedef 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 //===----------------------------------------------------------------------===//
802 // <rdar://problem/6902710> clang: false positives w/QC and CoreImage methods
803 //===----------------------------------------------------------------------===//
804
805 void 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 //===----------------------------------------------------------------------===//
815 // <rdar://problem/6945561> -[CIContext createCGLayerWithSize:info:]
816 //                           misinterpreted by clang scan-build
817 //===----------------------------------------------------------------------===//
818
819 void 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
828 void IOBSDNameMatching_wrapper(mach_port_t masterPort, uint32_t options,  const char * bsdName) {  
829   IOBSDNameMatching(masterPort, options, bsdName); // expected-warning{{leak}}
830 }
831
832 void IOServiceMatching_wrapper(const char * name) {
833   IOServiceMatching(name); // expected-warning{{leak}}
834 }
835
836 void IOServiceNameMatching_wrapper(const char * name) {
837   IOServiceNameMatching(name); // expected-warning{{leak}}
838 }
839
840 CF_RETURNS_RETAINED CFDictionaryRef CreateDict();
841
842 void 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
851 void IORegistryEntryIDMatching_wrapper(uint64_t entryID ) {
852   IORegistryEntryIDMatching(entryID); // expected-warning{{leak}}
853 }
854
855 void IOOpenFirmwarePathMatching_wrapper(mach_port_t masterPort, uint32_t options,
856                                         const char * path) {
857   IOOpenFirmwarePathMatching(masterPort, options, path); // expected-warning{{leak}}
858 }
859
860 void 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
866 void 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
872 void 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 //===----------------------------------------------------------------------===//
881 // Test of handling objects whose references "escape" to containers.
882 //===----------------------------------------------------------------------===//
883
884 void CFDictionaryAddValue();
885
886 // <rdar://problem/6539791>
887 void 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.
902 void 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".
914 void 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 //===----------------------------------------------------------------------===//
926 // Test of handling CGGradientXXX functions.
927 //===----------------------------------------------------------------------===//
928
929 void 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
951 void 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 //===----------------------------------------------------------------------===//
972 // <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
980 struct _opaque_pthread_t {};
981 struct _opaque_pthread_attr_t {};
982 typedef struct _opaque_pthread_t *__darwin_pthread_t;
983 typedef struct _opaque_pthread_attr_t __darwin_pthread_attr_t;
984 typedef __darwin_pthread_t pthread_t;
985 typedef __darwin_pthread_attr_t pthread_attr_t;
986
987 int pthread_create(pthread_t * restrict, const pthread_attr_t * restrict,
988                    void *(*)(void *), void * restrict);
989
990 void *rdar_7299394_start_routine(void *p) {
991   [((id) p) release];
992   return 0;
993 }
994 void 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 }
998 void rdar_7299394_positive(pthread_attr_t *attr, pthread_t *thread) {
999   NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}}
1000 }
1001
1002 //===----------------------------------------------------------------------===//
1003 // <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
1013 typedef int int32_t;
1014 typedef UInt32 FourCharCode;
1015 typedef FourCharCode OSType;
1016 typedef uint64_t CVOptionFlags;
1017 typedef int32_t CVReturn;
1018 typedef struct __CVBuffer *CVBufferRef;
1019 typedef CVBufferRef CVImageBufferRef;
1020 typedef CVImageBufferRef CVPixelBufferRef;
1021 typedef void (*CVPixelBufferReleaseBytesCallback)( void *releaseRefCon, const void *baseAddress );
1022
1023 extern 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
1034 typedef void (*CVPixelBufferReleasePlanarBytesCallback)( void *releaseRefCon, const void *dataPtr, size_t dataSize, size_t numberOfPlanes, const void *planeAddresses[] );
1035
1036 extern 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
1052 extern 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
1063 CVReturn 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
1081 CVReturn 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 //===----------------------------------------------------------------------===//
1103 // <rdar://problem/7358899> False leak associated with 
1104 //  CGBitmapContextCreateWithData
1105 //===----------------------------------------------------------------------===//
1106 typedef uint32_t CGBitmapInfo;
1107 typedef void (*CGBitmapContextReleaseDataCallback)(void *releaseInfo, void *data);
1108     
1109 CGContextRef 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
1114 void 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
1124   CGBitmapContextCreateWithData(data, width, height, bitsPerComponent, // expected-warning{{leak}}
1125     bytesPerRow, space, bitmapInfo, releaseCallback, number);
1126 }
1127
1128 //===----------------------------------------------------------------------===//
1129 // <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
1141 void rdar7265711_a(RDar7265711 *x) {
1142   id y = [x new_stuff]; // expected-warning{{leak}}
1143 }
1144
1145 void rdar7265711_b(RDar7265711 *x) {
1146   id y = [x new_stuff]; // no-warning
1147   [y release];
1148 }
1149
1150 //===----------------------------------------------------------------------===//
1151 // <rdar://problem/7306898> clang thinks [NSCursor dragCopyCursor] returns a
1152 //                          retained reference
1153 //===----------------------------------------------------------------------===//
1154
1155 @interface NSCursor : NSObject
1156 + (NSCursor *)dragCopyCursor;
1157 @end
1158
1159 void 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 //===----------------------------------------------------------------------===//
1167 // <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
1172 void 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 //===----------------------------------------------------------------------===//
1180 // Tests of ownership attributes.
1181 //===----------------------------------------------------------------------===//
1182
1183 typedef NSString* MyStringTy;
1184
1185 @protocol FooP;
1186
1187 @interface TestOwnershipAttr : NSObject
1188 - (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}}
1192 @end
1193
1194 static int ownership_attribute_doesnt_go_here NS_RETURNS_RETAINED; // expected-warning{{'ns_returns_retained' attribute only applies to function or method types}}
1195
1196 void test_attr_1(TestOwnershipAttr *X) {
1197   NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
1198 }
1199
1200 void test_attr_1b(TestOwnershipAttr *X) {
1201   NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}}
1202 }
1203
1204 @interface MyClassTestCFAttr : NSObject {}
1205 - (NSDate*) returnsCFRetained CF_RETURNS_RETAINED;
1206 - (CFDateRef) returnsCFRetainedAsCF CF_RETURNS_RETAINED;
1207 - (NSDate*) alsoReturnsRetained;
1208 - (CFDateRef) alsoReturnsRetainedAsCF;
1209 - (NSDate*) returnsNSRetained NS_RETURNS_RETAINED;
1210 @end
1211
1212 CF_RETURNS_RETAINED
1213 CFDateRef returnsRetainedCFDate()  {
1214   return CFDateCreate(0, CFAbsoluteTimeGetCurrent());
1215 }
1216
1217 @implementation MyClassTestCFAttr
1218 - (NSDate*) returnsCFRetained {
1219   return (NSDate*) returnsRetainedCFDate(); // No leak.
1220 }
1221
1222 - (CFDateRef) returnsCFRetainedAsCF {
1223   return returnsRetainedCFDate(); // No leak.
1224 }
1225
1226
1227 - (NSDate*) alsoReturnsRetained {
1228   return (NSDate*) returnsRetainedCFDate(); // expected-warning{{leak}}
1229 }
1230
1231 - (CFDateRef) alsoReturnsRetainedAsCF {
1232   return returnsRetainedCFDate(); // expected-warning{{leak}}
1233 }
1234
1235
1236 - (NSDate*) returnsNSRetained {
1237   return (NSDate*) returnsRetainedCFDate(); // no-warning
1238 }
1239 @end
1240
1241 //===----------------------------------------------------------------------===//
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
1248 void panic() __attribute__((noreturn));
1249
1250 void test_panic_negative() {
1251   signed z = 1;
1252   CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z);  // expected-warning{{leak}}
1253 }
1254
1255 void test_panic_positive() {
1256   signed z = 1;
1257   CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // no-warning
1258   panic();
1259 }
1260
1261 void 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
1268 void 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
1277 //===----------------------------------------------------------------------===//
1278 // Test uses of blocks (closures)
1279 //===----------------------------------------------------------------------===//
1280
1281 void test_blocks_1_pos(void) {
1282   NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}}
1283   ^{}();
1284 }
1285
1286 void test_blocks_1_indirect_release(void) {
1287   NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1288   ^{ [number release]; }();
1289 }
1290
1291 void 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 }
1296
1297 void 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
1302 void 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