]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/Analysis/retain-release-gc-only.m
Vendor import of clang trunk r135360:
[FreeBSD/FreeBSD.git] / test / Analysis / retain-release-gc-only.m
1 // RUN: %clang_cc1 -analyze -analyzer-checker=core,osx.cocoa.NSAutoreleasePool -analyzer-store=basic -verify -fobjc-gc-only -fblocks %s
2 // RUN: %clang_cc1 -analyze -analyzer-checker=core,osx.cocoa.NSAutoreleasePool -analyzer-store=region -fobjc-gc-only -fblocks -verify %s
3
4 //===----------------------------------------------------------------------===//
5 // Header stuff.
6 //===----------------------------------------------------------------------===//
7
8 typedef unsigned int __darwin_natural_t;
9 typedef unsigned long uintptr_t;
10 typedef unsigned int uint32_t;
11 typedef unsigned long long uint64_t;
12 typedef unsigned int UInt32;
13 typedef signed long CFIndex;
14 typedef struct {
15     CFIndex location;
16     CFIndex length;
17 } CFRange;
18 static __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 }
24 typedef const void * CFTypeRef;
25 typedef const struct __CFString * CFStringRef;
26 typedef const struct __CFAllocator * CFAllocatorRef;
27 extern const CFAllocatorRef kCFAllocatorDefault;
28 extern CFTypeRef CFRetain(CFTypeRef cf);
29 extern void CFRelease(CFTypeRef cf);
30 typedef struct {
31 }
32 CFArrayCallBacks;
33 extern const CFArrayCallBacks kCFTypeArrayCallBacks;
34 typedef const struct __CFArray * CFArrayRef;
35 typedef struct __CFArray * CFMutableArrayRef;
36 extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks);
37 extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx);
38 extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value);
39 typedef struct {
40 }
41 CFDictionaryKeyCallBacks;
42 extern const CFDictionaryKeyCallBacks kCFTypeDictionaryKeyCallBacks;
43 typedef struct {
44 }
45 CFDictionaryValueCallBacks;
46 extern const CFDictionaryValueCallBacks kCFTypeDictionaryValueCallBacks;
47 typedef const struct __CFDictionary * CFDictionaryRef;
48 typedef struct __CFDictionary * CFMutableDictionaryRef;
49 extern CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFDictionaryKeyCallBacks *keyCallBacks, const CFDictionaryValueCallBacks *valueCallBacks);
50 typedef UInt32 CFStringEncoding;
51 enum {
52 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  };
53 extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding);
54 typedef double CFTimeInterval;
55 typedef CFTimeInterval CFAbsoluteTime;
56 extern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void);
57 typedef const struct __CFDate * CFDateRef;
58 extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at);
59 extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate);
60 typedef __darwin_natural_t natural_t;
61 typedef natural_t mach_port_name_t;
62 typedef mach_port_name_t mach_port_t;
63 typedef int kern_return_t;
64 typedef kern_return_t mach_error_t;
65 enum {
66 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    };
67 typedef CFIndex CFNumberType;
68 typedef const struct __CFNumber * CFNumberRef;
69 extern CFNumberRef CFNumberCreate(CFAllocatorRef allocator, CFNumberType theType, const void *valuePtr);
70 typedef const struct __CFAttributedString *CFAttributedStringRef;
71 typedef struct __CFAttributedString *CFMutableAttributedStringRef;
72 extern CFAttributedStringRef CFAttributedStringCreate(CFAllocatorRef alloc, CFStringRef str, CFDictionaryRef attributes) ;
73 extern CFMutableAttributedStringRef CFAttributedStringCreateMutableCopy(CFAllocatorRef alloc, CFIndex maxLength, CFAttributedStringRef aStr) ;
74 extern void CFAttributedStringSetAttribute(CFMutableAttributedStringRef aStr, CFRange range, CFStringRef attrName, CFTypeRef value) ;
75 typedef signed char BOOL;
76 typedef unsigned long NSUInteger;
77 @class NSString, Protocol;
78 extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
79 typedef struct _NSZone NSZone;
80 @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
81 @protocol NSObject
82 - (BOOL)isEqual:(id)object;
83 - (id)retain;
84 - (oneway void)release;
85 - (id)autorelease;
86 - (Class)class;
87 @end  @protocol NSCopying  - (id)copyWithZone:(NSZone *)zone;
88 @end  @protocol NSMutableCopying  - (id)mutableCopyWithZone:(NSZone *)zone;
89 @end  @protocol NSCoding  - (void)encodeWithCoder:(NSCoder *)aCoder;
90 @end
91 @interface NSObject <NSObject> {}
92 + (id)allocWithZone:(NSZone *)zone;
93 + (id)alloc;
94 - (void)dealloc;
95 - (void)release;
96 - (id)copy;
97 @end
98 @interface NSObject (NSCoderMethods)
99 - (id)awakeAfterUsingCoder:(NSCoder *)aDecoder;
100 @end
101 extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
102 typedef struct {
103 }
104 NSFastEnumerationState;
105 @protocol NSFastEnumeration  - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
106 @end           @class NSString, NSDictionary;
107 @interface NSValue : NSObject <NSCopying, NSCoding>  - (void)getValue:(void *)value;
108 @end  @interface NSNumber : NSValue  - (char)charValue;
109 - (id)initWithInt:(int)value;
110 @end   @class NSString;
111 @interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>  - (NSUInteger)count;
112 @end  @interface NSArray (NSArrayCreation)  + (id)array;
113 @end       @interface NSAutoreleasePool : NSObject {
114 }
115 - (void)drain;
116 - (id)init;
117 @end extern NSString * const NSBundleDidLoadNotification;
118 typedef double NSTimeInterval;
119 @interface NSDate : NSObject <NSCopying, NSCoding>  - (NSTimeInterval)timeIntervalSinceReferenceDate;
120 @end            typedef unsigned short unichar;
121 @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>    - (NSUInteger)length;
122 - ( const char *)UTF8String;
123 - (id)initWithUTF8String:(const char *)nullTerminatedCString;
124 + (id)stringWithUTF8String:(const char *)nullTerminatedCString;
125 @end        @class NSString, NSURL, NSError;
126 @interface NSData : NSObject <NSCopying, NSMutableCopying, NSCoding>  - (NSUInteger)length;
127 + (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length;
128 + (id)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b;
129 @end   @class NSLocale, NSDate, NSCalendar, NSTimeZone, NSError, NSArray, NSMutableDictionary;
130 @interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>  - (NSUInteger)count;
131 @end    @interface NSMutableDictionary : NSDictionary  - (void)removeObjectForKey:(id)aKey;
132 - (void)setObject:(id)anObject forKey:(id)aKey;
133 @end  @interface NSMutableDictionary (NSMutableDictionaryCreation)  + (id)dictionaryWithCapacity:(NSUInteger)numItems;
134 @end  typedef double CGFloat;
135 struct CGSize {
136 };
137 typedef struct CGSize CGSize;
138 struct CGRect {
139 };
140 typedef struct CGRect CGRect;
141 typedef mach_port_t io_object_t;
142 typedef char io_name_t[128];
143 typedef io_object_t io_iterator_t;
144 typedef io_object_t io_service_t;
145 typedef struct IONotificationPort * IONotificationPortRef;
146 typedef void (*IOServiceMatchingCallback)(  void * refcon,  io_iterator_t iterator );
147 io_service_t IOServiceGetMatchingService(  mach_port_t masterPort,  CFDictionaryRef matching );
148 kern_return_t IOServiceGetMatchingServices(  mach_port_t masterPort,  CFDictionaryRef matching,  io_iterator_t * existing );
149 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));
150 kern_return_t IOServiceAddMatchingNotification(  IONotificationPortRef notifyPort,  const io_name_t notificationType,  CFDictionaryRef matching,         IOServiceMatchingCallback callback,         void * refCon,  io_iterator_t * notification );
151 CFMutableDictionaryRef IOServiceMatching(  const char * name );
152 CFMutableDictionaryRef IOServiceNameMatching(  const char * name );
153 CFMutableDictionaryRef IOBSDNameMatching(  mach_port_t masterPort,  uint32_t options,  const char * bsdName );
154 CFMutableDictionaryRef IOOpenFirmwarePathMatching(  mach_port_t masterPort,  uint32_t options,  const char * path );
155 CFMutableDictionaryRef IORegistryEntryIDMatching(  uint64_t entryID );
156 typedef struct __DASession * DASessionRef;
157 extern DASessionRef DASessionCreate( CFAllocatorRef allocator );
158 typedef struct __DADisk * DADiskRef;
159 extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name );
160 extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media );
161 extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk );
162 extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk );
163 @interface NSTask : NSObject - (id)init;
164 @end                    typedef struct CGColorSpace *CGColorSpaceRef;
165 typedef struct CGImage *CGImageRef;
166 typedef struct CGLayer *CGLayerRef;
167 @interface NSResponder : NSObject <NSCoding> {
168 }
169 @end    @protocol NSAnimatablePropertyContainer      - (id)animator;
170 @end  extern NSString *NSAnimationTriggerOrderIn ;
171 @interface NSView : NSResponder  <NSAnimatablePropertyContainer>  {
172 }
173 @end @protocol NSValidatedUserInterfaceItem - (SEL)action;
174 @end   @protocol NSUserInterfaceValidations - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem;
175 @end  @class NSDate, NSDictionary, NSError, NSException, NSNotification;
176 @interface NSApplication : NSResponder <NSUserInterfaceValidations> {
177 }
178 @end   enum {
179 NSTerminateCancel = 0,         NSTerminateNow = 1,         NSTerminateLater = 2 };
180 typedef NSUInteger NSApplicationTerminateReply;
181 @protocol NSApplicationDelegate <NSObject> @optional        - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
182 @end  @class NSAttributedString, NSEvent, NSFont, NSFormatter, NSImage, NSMenu, NSText, NSView, NSTextView;
183 @interface NSCell : NSObject <NSCopying, NSCoding> {
184 }
185 @end @class NSTextField, NSPanel, NSArray, NSWindow, NSImage, NSButton, NSError;
186 typedef struct {
187 }
188 CVTimeStamp;
189 @interface CIImage : NSObject <NSCoding, NSCopying> {
190 }
191 typedef int CIFormat;
192 @end  enum {
193 kDAReturnSuccess = 0,     kDAReturnError = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x01,     kDAReturnBusy = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x02,     kDAReturnBadArgument = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x03,     kDAReturnExclusiveAccess = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x04,     kDAReturnNoResources = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x05,     kDAReturnNotFound = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x06,     kDAReturnNotMounted = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x07,     kDAReturnNotPermitted = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x08,     kDAReturnNotPrivileged = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x09,     kDAReturnNotReady = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0A,     kDAReturnNotWritable = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0B,     kDAReturnUnsupported = (((0x3eU)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0C };
194 typedef mach_error_t DAReturn;
195 typedef const struct __DADissenter * DADissenterRef;
196 extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string );
197 @interface CIContext: NSObject {
198 }
199 - (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r;
200 - (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r     format:(CIFormat)f colorSpace:(CGColorSpaceRef)cs;
201 - (CGLayerRef)createCGLayerWithSize:(CGSize)size info:(CFDictionaryRef)d;
202 @end extern NSString* const QCRendererEventKey;
203 @protocol QCCompositionRenderer - (NSDictionary*) attributes;
204 @end   @interface QCRenderer : NSObject <QCCompositionRenderer> {
205 }
206 - (id) createSnapshotImageOfType:(NSString*)type;
207 @end  extern NSString* const QCViewDidStartRenderingNotification;
208 @interface QCView : NSView <QCCompositionRenderer> {
209 }
210 - (id) createSnapshotImageOfType:(NSString*)type;
211 @end    enum {
212 ICEXIFOrientation1 = 1,     ICEXIFOrientation2 = 2,     ICEXIFOrientation3 = 3,     ICEXIFOrientation4 = 4,     ICEXIFOrientation5 = 5,     ICEXIFOrientation6 = 6,     ICEXIFOrientation7 = 7,     ICEXIFOrientation8 = 8, };
213 @class ICDevice;
214 @protocol ICDeviceDelegate <NSObject>  @required      - (void)didRemoveDevice:(ICDevice*)device;
215 @end extern NSString *const ICScannerStatusWarmingUp;
216 @class ICScannerDevice;
217 @protocol ICScannerDeviceDelegate <ICDeviceDelegate>  @optional       - (void)scannerDeviceDidBecomeAvailable:(ICScannerDevice*)scanner;
218 @end
219 CFTypeRef CFMakeCollectable(CFTypeRef cf) ;
220
221 static __inline__ __attribute__((always_inline)) id NSMakeCollectable(CFTypeRef 
222 cf) {
223     return cf ? (id)CFMakeCollectable(cf) : ((void*)0);
224 }
225
226 //===----------------------------------------------------------------------===//
227 // Test cases.
228 //===----------------------------------------------------------------------===//
229
230 void f1() {
231   CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
232   id x = [(id) A autorelease];
233   CFRelease((CFMutableArrayRef) x);
234 }
235
236 void f2() {
237   CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}}
238   id x = [(id) A retain];
239   [x release];
240   [x release];
241 }
242
243 void f3() {
244   CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}}
245   CFMakeCollectable(A);
246   CFRetain(A);
247 }
248
249 void f3b() {
250   CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
251   CFMakeCollectable(A);
252 }
253
254
255 void f4() {
256   CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}}
257   NSMakeCollectable(A);
258   CFRetain(A);
259 }
260
261 void f4b() {
262   CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
263   NSMakeCollectable(A);
264 }
265
266 void f5() {
267   id x = [NSMakeCollectable(CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks)) autorelease]; // no-warning
268 }
269
270 void f5b() {
271   id x = [(id) CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks) autorelease]; // expected-warning{{leak}}
272 }
273
274 // Test return of non-owned objects in contexts where an owned object
275 // is expected.
276 @interface TestReturnNotOwnedWhenExpectedOwned
277 - (NSString*)newString;
278 - (CFMutableArrayRef)newArray;
279 @end
280
281 @implementation TestReturnNotOwnedWhenExpectedOwned
282 - (NSString*)newString {
283   NSString *s = [NSString stringWithUTF8String:"hello"]; // expected-warning{{Potential leak (when using garbage collection) of an object allocated}}
284   CFRetain(s);
285   return s;
286 }
287 - (CFMutableArrayRef)newArray{
288    return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
289 }
290 @end
291
292 //===----------------------------------------------------------------------===//
293 // <rdar://problem/6948053> False positive: object substitution during -init*
294 //   methods warns about returning +0 when using -fobjc-gc-only
295 //===----------------------------------------------------------------------===//
296
297 @interface MyClassRdar6948053 : NSObject
298 - (id) init;
299 + (id) shared;
300 @end
301
302 @implementation MyClassRdar6948053
303 +(id) shared {
304   return (id) 0;
305 }
306 - (id) init
307 {
308   Class myClass = [self class];  
309   [self release];
310   return [[myClass shared] retain]; // no-warning
311 }
312 @end
313
314 //===----------------------------------------------------------------------===//
315 // <rdar://problem/7174400> 'ciContext createCGImage:outputImage fromRect:' returns a retained CF object (not GC'ed)//===----------------------------------------------------------------------===//
316 //===----------------------------------------------------------------------===//
317
318 void rdar_7174400(QCView *view, QCRenderer *renderer, CIContext *context,
319                   NSString *str, CIImage *img, CGRect rect,
320                   CIFormat form, CGColorSpaceRef cs) {
321   [view createSnapshotImageOfType:str]; // no-warning
322   [renderer createSnapshotImageOfType:str]; // no-warning
323   [context createCGImage:img fromRect:rect]; // expected-warning{{leak}}
324   [context createCGImage:img fromRect:rect format:form colorSpace:cs]; // expected-warning{{leak}}
325 }
326
327 //===----------------------------------------------------------------------===//
328 // <rdar://problem/6250216> Warn against using -[NSAutoreleasePool release] in 
329 //  GC mode
330 //===----------------------------------------------------------------------===//
331
332 void rdar_6250216(void) {
333     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
334     [pool release]; // expected-warning{{Use -drain instead of -release when using NSAutoreleasePool and garbage collection}}
335 }
336
337
338 //===----------------------------------------------------------------------===//
339 // <rdar://problem/7407273> Don't crash when analyzing messages sent to blocks
340 //===----------------------------------------------------------------------===//
341
342 @class RDar7407273;
343 typedef void (^RDar7407273Block)(RDar7407273 *operation);
344 void rdar7407273(RDar7407273Block b) {
345   [b copy];
346 }
347
348 //===----------------------------------------------------------------------===//
349 // Tests of ownership attributes.
350 //===----------------------------------------------------------------------===//
351
352 @interface TestOwnershipAttr : NSObject
353 - (NSString*) returnsAnOwnedString __attribute__((ns_returns_retained));
354 - (NSString*) returnsAnOwnedCFString  __attribute__((cf_returns_retained));
355 @end
356
357 void test_attr_1(TestOwnershipAttr *X) {
358   NSString *str = [X returnsAnOwnedString]; // no-warning
359 }
360
361 void test_attr_1b(TestOwnershipAttr *X) {
362   NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}}
363 }
364
365 @interface MyClassTestCFAttr : NSObject {}
366 - (NSDate*) returnsCFRetained __attribute__((cf_returns_retained));
367 - (NSDate*) alsoReturnsRetained;
368 - (NSDate*) returnsNSRetained __attribute__((ns_returns_retained));
369 @end
370
371 __attribute__((cf_returns_retained))
372 CFDateRef returnsRetainedCFDate()  {
373   return CFDateCreate(0, CFAbsoluteTimeGetCurrent());
374 }
375
376 @implementation MyClassTestCFAttr
377 - (NSDate*) returnsCFRetained {
378   return (NSDate*) returnsRetainedCFDate(); // No leak.
379 }
380
381 - (NSDate*) alsoReturnsRetained {
382   return (NSDate*) returnsRetainedCFDate(); // expected-warning{{leak}}
383 }
384
385 - (NSDate*) returnsNSRetained {
386   return (NSDate*) returnsRetainedCFDate(); // expected-warning{{leak}}
387 }
388 @end