xref: /aosp_15_r20/external/clang/test/ARCMT/objcmt-arc-cf-annotations.m.result (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li// RUN: rm -rf %t
2*67e74705SXin Li// RUN: %clang_cc1 -fblocks -objcmt-migrate-annotation -objcmt-migrate-instancetype -objcmt-migrate-readwrite-property -mt-migrate-directory %t %s -x objective-c -triple x86_64-apple-darwin11
3*67e74705SXin Li// RUN: c-arcmt-test -mt-migrate-directory %t | arcmt-test -verify-transformed-files %s.result
4*67e74705SXin Li// RUN: %clang_cc1 -fblocks -triple x86_64-apple-darwin10 -fsyntax-only -x objective-c %s.result
5*67e74705SXin Li
6*67e74705SXin Li#ifndef CF_IMPLICIT_BRIDGING_ENABLED
7*67e74705SXin Li#if __has_feature(arc_cf_code_audited)
8*67e74705SXin Li#define CF_IMPLICIT_BRIDGING_ENABLED _Pragma("clang arc_cf_code_audited begin")
9*67e74705SXin Li#else
10*67e74705SXin Li#define CF_IMPLICIT_BRIDGING_ENABLED
11*67e74705SXin Li#endif
12*67e74705SXin Li#endif
13*67e74705SXin Li
14*67e74705SXin Li#ifndef CF_IMPLICIT_BRIDGING_DISABLED
15*67e74705SXin Li#if __has_feature(arc_cf_code_audited)
16*67e74705SXin Li#define CF_IMPLICIT_BRIDGING_DISABLED _Pragma("clang arc_cf_code_audited end")
17*67e74705SXin Li#else
18*67e74705SXin Li#define CF_IMPLICIT_BRIDGING_DISABLED
19*67e74705SXin Li#endif
20*67e74705SXin Li#endif
21*67e74705SXin Li
22*67e74705SXin Li#if __has_feature(attribute_ns_returns_retained)
23*67e74705SXin Li#define NS_RETURNS_RETAINED __attribute__((ns_returns_retained))
24*67e74705SXin Li#endif
25*67e74705SXin Li#if __has_feature(attribute_cf_returns_retained)
26*67e74705SXin Li#define CF_RETURNS_RETAINED __attribute__((cf_returns_retained))
27*67e74705SXin Li#endif
28*67e74705SXin Li#if __has_feature(attribute_ns_returns_not_retained)
29*67e74705SXin Li#define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained))
30*67e74705SXin Li#endif
31*67e74705SXin Li#if __has_feature(attribute_cf_returns_not_retained)
32*67e74705SXin Li#define CF_RETURNS_NOT_RETAINED __attribute__((cf_returns_not_retained))
33*67e74705SXin Li#endif
34*67e74705SXin Li#if __has_feature(attribute_ns_consumes_self)
35*67e74705SXin Li#define NS_CONSUMES_SELF __attribute__((ns_consumes_self))
36*67e74705SXin Li#endif
37*67e74705SXin Li#if __has_feature(attribute_ns_consumed)
38*67e74705SXin Li#define NS_CONSUMED __attribute__((ns_consumed))
39*67e74705SXin Li#endif
40*67e74705SXin Li#if __has_feature(attribute_cf_consumed)
41*67e74705SXin Li#define CF_CONSUMED __attribute__((cf_consumed))
42*67e74705SXin Li#endif
43*67e74705SXin Li#if __has_attribute(ns_returns_autoreleased)
44*67e74705SXin Li#define NS_RETURNS_AUTORELEASED __attribute__((ns_returns_autoreleased))
45*67e74705SXin Li#endif
46*67e74705SXin Li
47*67e74705SXin Li//===----------------------------------------------------------------------===//
48*67e74705SXin Li// The following code is reduced using delta-debugging from Mac OS X headers:
49*67e74705SXin Li//
50*67e74705SXin Li// #include <Cocoa/Cocoa.h>
51*67e74705SXin Li// #include <CoreFoundation/CoreFoundation.h>
52*67e74705SXin Li// #include <DiskArbitration/DiskArbitration.h>
53*67e74705SXin Li// #include <QuartzCore/QuartzCore.h>
54*67e74705SXin Li// #include <Quartz/Quartz.h>
55*67e74705SXin Li// #include <IOKit/IOKitLib.h>
56*67e74705SXin Li//
57*67e74705SXin Li// It includes the basic definitions for the test cases below.
58*67e74705SXin Li//===----------------------------------------------------------------------===//
59*67e74705SXin Li
60*67e74705SXin Litypedef unsigned int __darwin_natural_t;
61*67e74705SXin Litypedef unsigned long uintptr_t;
62*67e74705SXin Litypedef unsigned int uint32_t;
63*67e74705SXin Litypedef unsigned long long uint64_t;
64*67e74705SXin Litypedef unsigned int UInt32;
65*67e74705SXin Litypedef signed long CFIndex;
66*67e74705SXin Litypedef CFIndex CFByteOrder;
67*67e74705SXin Litypedef struct {
68*67e74705SXin Li    CFIndex location;
69*67e74705SXin Li    CFIndex length;
70*67e74705SXin Li} CFRange;
71*67e74705SXin Listatic __inline__ __attribute__((always_inline)) CFRange CFRangeMake(CFIndex loc, CFIndex len) {
72*67e74705SXin Li    CFRange range;
73*67e74705SXin Li    range.location = loc;
74*67e74705SXin Li    range.length = len;
75*67e74705SXin Li    return range;
76*67e74705SXin Li}
77*67e74705SXin Litypedef const void * CFTypeRef;
78*67e74705SXin Litypedef const struct __CFString * CFStringRef;
79*67e74705SXin Litypedef const struct __CFAllocator * CFAllocatorRef;
80*67e74705SXin Liextern const CFAllocatorRef kCFAllocatorDefault;
81*67e74705SXin Liextern CFTypeRef CFRetain(CFTypeRef cf);
82*67e74705SXin Li
83*67e74705SXin LiCF_IMPLICIT_BRIDGING_ENABLED
84*67e74705SXin Li
85*67e74705SXin Liextern void CFRelease(CFTypeRef cf);
86*67e74705SXin Li
87*67e74705SXin LiCF_IMPLICIT_BRIDGING_DISABLED
88*67e74705SXin Li
89*67e74705SXin Liextern CFTypeRef CFMakeCollectable(CFTypeRef cf);
90*67e74705SXin Litypedef struct {
91*67e74705SXin Li}
92*67e74705SXin LiCFArrayCallBacks;
93*67e74705SXin Liextern const CFArrayCallBacks kCFTypeArrayCallBacks;
94*67e74705SXin Litypedef const struct __CFArray * CFArrayRef;
95*67e74705SXin Litypedef struct __CFArray * CFMutableArrayRef;
96*67e74705SXin Liextern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks) CF_RETURNS_RETAINED;
97*67e74705SXin Liextern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx) CF_RETURNS_NOT_RETAINED;
98*67e74705SXin Liextern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value);
99*67e74705SXin Litypedef struct {
100*67e74705SXin Li}
101*67e74705SXin LiCFDictionaryKeyCallBacks;
102*67e74705SXin Liextern const CFDictionaryKeyCallBacks kCFTypeDictionaryKeyCallBacks;
103*67e74705SXin Litypedef struct {
104*67e74705SXin Li}
105*67e74705SXin LiCFDictionaryValueCallBacks;
106*67e74705SXin Liextern const CFDictionaryValueCallBacks kCFTypeDictionaryValueCallBacks;
107*67e74705SXin Litypedef const struct __CFDictionary * CFDictionaryRef;
108*67e74705SXin Litypedef struct __CFDictionary * CFMutableDictionaryRef;
109*67e74705SXin Liextern CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFDictionaryKeyCallBacks *keyCallBacks, const CFDictionaryValueCallBacks *valueCallBacks) CF_RETURNS_RETAINED;
110*67e74705SXin Litypedef UInt32 CFStringEncoding;
111*67e74705SXin Lienum {
112*67e74705SXin LikCFStringEncodingMacRoman = 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  };
113*67e74705SXin Liextern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding) CF_RETURNS_RETAINED;
114*67e74705SXin Litypedef double CFTimeInterval;
115*67e74705SXin Litypedef CFTimeInterval CFAbsoluteTime;
116*67e74705SXin Liextern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void);
117*67e74705SXin Litypedef const struct __CFDate * CFDateRef;
118*67e74705SXin Liextern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at) CF_RETURNS_RETAINED;
119*67e74705SXin Liextern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate);
120*67e74705SXin Litypedef __darwin_natural_t natural_t;
121*67e74705SXin Litypedef natural_t mach_port_name_t;
122*67e74705SXin Litypedef mach_port_name_t mach_port_t;
123*67e74705SXin Litypedef int kern_return_t;
124*67e74705SXin Litypedef kern_return_t mach_error_t;
125*67e74705SXin Lienum {
126*67e74705SXin LikCFNumberSInt8Type = 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    };
127*67e74705SXin Litypedef CFIndex CFNumberType;
128*67e74705SXin Litypedef const struct __CFNumber * CFNumberRef;
129*67e74705SXin Liextern CFNumberRef CFNumberCreate(CFAllocatorRef allocator, CFNumberType theType, const void *valuePtr) CF_RETURNS_RETAINED;
130*67e74705SXin Litypedef const struct __CFAttributedString *CFAttributedStringRef;
131*67e74705SXin Litypedef struct __CFAttributedString *CFMutableAttributedStringRef;
132*67e74705SXin Liextern CFAttributedStringRef CFAttributedStringCreate(CFAllocatorRef alloc, CFStringRef str, CFDictionaryRef attributes) CF_RETURNS_RETAINED ;
133*67e74705SXin Liextern CFMutableAttributedStringRef CFAttributedStringCreateMutableCopy(CFAllocatorRef alloc, CFIndex maxLength, CFAttributedStringRef aStr) CF_RETURNS_RETAINED ;
134*67e74705SXin Liextern void CFAttributedStringSetAttribute(CFMutableAttributedStringRef aStr, CFRange range, CFStringRef attrName, CFTypeRef value) ;
135*67e74705SXin Litypedef signed char BOOL;
136*67e74705SXin Litypedef unsigned long NSUInteger;
137*67e74705SXin Li@class NSString, Protocol;
138*67e74705SXin Liextern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
139*67e74705SXin Litypedef struct _NSZone NSZone;
140*67e74705SXin Li@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
141*67e74705SXin Li@protocol NSObject
142*67e74705SXin Li- (BOOL)isEqual:(id)object;
143*67e74705SXin Li- (id)retain;
144*67e74705SXin Li- (oneway void)release;
145*67e74705SXin Li- (id)autorelease;
146*67e74705SXin Li- (NSString *)description;
147*67e74705SXin Li- (instancetype)init;
148*67e74705SXin Li@end
149*67e74705SXin Li@protocol NSCopying
150*67e74705SXin Li- (id)copyWithZone:(NSZone *)zone;
151*67e74705SXin Li@end
152*67e74705SXin Li@protocol NSMutableCopying  - (id)mutableCopyWithZone:(NSZone *)zone;
153*67e74705SXin Li@end
154*67e74705SXin Li@protocol NSCoding  - (void)encodeWithCoder:(NSCoder *)aCoder;
155*67e74705SXin Li@end
156*67e74705SXin Li@interface NSObject <NSObject> {}
157*67e74705SXin Li+ (id)allocWithZone:(NSZone *)zone;
158*67e74705SXin Li+ (id)alloc;
159*67e74705SXin Li+ (id)new;
160*67e74705SXin Li- (void)dealloc;
161*67e74705SXin Li@end
162*67e74705SXin Li@interface NSObject (NSCoderMethods)
163*67e74705SXin Li- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder;
164*67e74705SXin Li@end
165*67e74705SXin Liextern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
166*67e74705SXin Litypedef struct {
167*67e74705SXin Li}
168*67e74705SXin LiNSFastEnumerationState;
169*67e74705SXin Li@protocol NSFastEnumeration
170*67e74705SXin Li- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
171*67e74705SXin Li@end
172*67e74705SXin Li@class NSString, NSDictionary;
173*67e74705SXin Li@interface NSValue : NSObject <NSCopying, NSCoding>  - (void)getValue:(void *)value;
174*67e74705SXin Li@end
175*67e74705SXin Li@interface NSNumber : NSValue
176*67e74705SXin Li- (char)charValue;
177*67e74705SXin Li- (instancetype)initWithInt:(int)value;
178*67e74705SXin Li+ (NSNumber *)numberWithInt:(int)value;
179*67e74705SXin Li@end
180*67e74705SXin Li@class NSString;
181*67e74705SXin Li@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>
182*67e74705SXin Li- (NSUInteger)count;
183*67e74705SXin Li- (instancetype)initWithObjects:(const id [])objects count:(NSUInteger)cnt;
184*67e74705SXin Li+ (instancetype)arrayWithObject:(id)anObject;
185*67e74705SXin Li+ (instancetype)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt;
186*67e74705SXin Li+ (instancetype)arrayWithObjects:(id)firstObj, ... __attribute__((sentinel(0,1)));
187*67e74705SXin Li- (instancetype)initWithObjects:(id)firstObj, ... __attribute__((sentinel(0,1)));
188*67e74705SXin Li- (instancetype)initWithArray:(NSArray *)array;
189*67e74705SXin Li@end  @interface NSArray (NSArrayCreation)  + (instancetype)array;
190*67e74705SXin Li@end       @interface NSAutoreleasePool : NSObject {
191*67e74705SXin Li}
192*67e74705SXin Li- (void)drain;
193*67e74705SXin Li@end extern NSString * const NSBundleDidLoadNotification;
194*67e74705SXin Litypedef double NSTimeInterval;
195*67e74705SXin Li@interface NSDate : NSObject <NSCopying, NSCoding>  - (NSTimeInterval)timeIntervalSinceReferenceDate;
196*67e74705SXin Li@end            typedef unsigned short unichar;
197*67e74705SXin Li@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
198*67e74705SXin Li- (NSUInteger)length;
199*67e74705SXin Li- (NSString *)stringByAppendingString:(NSString *)aString;
200*67e74705SXin Li- ( const char *)UTF8String;
201*67e74705SXin Li- (instancetype)initWithUTF8String:(const char *)nullTerminatedCString;
202*67e74705SXin Li+ (instancetype)stringWithUTF8String:(const char *)nullTerminatedCString;
203*67e74705SXin Li@end        @class NSString, NSURL, NSError;
204*67e74705SXin Li@interface NSData : NSObject <NSCopying, NSMutableCopying, NSCoding>  - (NSUInteger)length;
205*67e74705SXin Li+ (instancetype)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length;
206*67e74705SXin Li+ (instancetype)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b;
207*67e74705SXin Li@end   @class NSLocale, NSDate, NSCalendar, NSTimeZone, NSError, NSArray, NSMutableDictionary;
208*67e74705SXin Li@interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>
209*67e74705SXin Li- (NSUInteger)count;
210*67e74705SXin Li+ (instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
211*67e74705SXin Li+ (instancetype)dictionaryWithObjects:(const id [])objects forKeys:(const id <NSCopying> [])keys count:(NSUInteger)cnt;
212*67e74705SXin Li@end
213*67e74705SXin Li@interface NSMutableDictionary : NSDictionary  - (void)removeObjectForKey:(id)aKey;
214*67e74705SXin Li- (void)setObject:(id)anObject forKey:(id)aKey;
215*67e74705SXin Li@end  @interface NSMutableDictionary (NSMutableDictionaryCreation)  + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems;
216*67e74705SXin Li@end  typedef double CGFloat;
217*67e74705SXin Listruct CGSize {
218*67e74705SXin Li};
219*67e74705SXin Litypedef struct CGSize CGSize;
220*67e74705SXin Listruct CGRect {
221*67e74705SXin Li};
222*67e74705SXin Litypedef struct CGRect CGRect;
223*67e74705SXin Litypedef mach_port_t io_object_t;
224*67e74705SXin Litypedef char io_name_t[128];
225*67e74705SXin Litypedef io_object_t io_iterator_t;
226*67e74705SXin Litypedef io_object_t io_service_t;
227*67e74705SXin Litypedef struct IONotificationPort * IONotificationPortRef;
228*67e74705SXin Litypedef void (*IOServiceMatchingCallback)(  void * refcon,  io_iterator_t iterator );
229*67e74705SXin Li
230*67e74705SXin LiCF_IMPLICIT_BRIDGING_ENABLED
231*67e74705SXin Li
232*67e74705SXin Liio_service_t IOServiceGetMatchingService(  mach_port_t masterPort,  CFDictionaryRef matching );
233*67e74705SXin Likern_return_t IOServiceGetMatchingServices(  mach_port_t masterPort,  CFDictionaryRef matching,  io_iterator_t * existing );
234*67e74705SXin Li
235*67e74705SXin LiCF_IMPLICIT_BRIDGING_DISABLED
236*67e74705SXin Li
237*67e74705SXin Likern_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)); // expected-note {{'IOServiceAddNotification' declared here}}
238*67e74705SXin Likern_return_t IOServiceAddMatchingNotification(  IONotificationPortRef notifyPort,  const io_name_t notificationType,  CFDictionaryRef CF_CONSUMED matching,         IOServiceMatchingCallback callback,         void * refCon,  io_iterator_t * notification );
239*67e74705SXin Li
240*67e74705SXin LiCF_IMPLICIT_BRIDGING_ENABLED
241*67e74705SXin Li
242*67e74705SXin LiCFMutableDictionaryRef IOServiceMatching(  const char * name );
243*67e74705SXin LiCFMutableDictionaryRef IOServiceNameMatching(  const char * name );
244*67e74705SXin LiCFMutableDictionaryRef IOBSDNameMatching(  mach_port_t masterPort,  uint32_t options,  const char * bsdName );
245*67e74705SXin LiCFMutableDictionaryRef IOOpenFirmwarePathMatching(  mach_port_t masterPort,  uint32_t options,  const char * path );
246*67e74705SXin LiCFMutableDictionaryRef IORegistryEntryIDMatching(  uint64_t entryID );
247*67e74705SXin Li
248*67e74705SXin LiCF_IMPLICIT_BRIDGING_DISABLED
249*67e74705SXin Li
250*67e74705SXin Litypedef struct __DASession * DASessionRef;
251*67e74705SXin Liextern DASessionRef DASessionCreate( CFAllocatorRef allocator ) CF_RETURNS_RETAINED;
252*67e74705SXin Litypedef struct __DADisk * DADiskRef;
253*67e74705SXin Liextern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name ) CF_RETURNS_RETAINED;
254*67e74705SXin Liextern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media ) CF_RETURNS_RETAINED;
255*67e74705SXin Liextern CFDictionaryRef DADiskCopyDescription( DADiskRef disk ) CF_RETURNS_RETAINED;
256*67e74705SXin Liextern DADiskRef DADiskCopyWholeDisk( DADiskRef disk ) CF_RETURNS_RETAINED;
257*67e74705SXin Li@interface NSTask : NSObject - (instancetype)init;
258*67e74705SXin Li@end                    typedef struct CGColorSpace *CGColorSpaceRef;
259*67e74705SXin Litypedef struct CGImage *CGImageRef;
260*67e74705SXin Litypedef struct CGLayer *CGLayerRef;
261*67e74705SXin Li@interface NSResponder : NSObject <NSCoding> {
262*67e74705SXin Li}
263*67e74705SXin Li@end    @protocol NSAnimatablePropertyContainer      - (id)animator;
264*67e74705SXin Li@end  extern NSString *NSAnimationTriggerOrderIn ;
265*67e74705SXin Li@interface NSView : NSResponder  <NSAnimatablePropertyContainer>  {
266*67e74705SXin Li}
267*67e74705SXin Li@end @protocol NSValidatedUserInterfaceItem - (SEL)action;
268*67e74705SXin Li@end   @protocol NSUserInterfaceValidations - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem;
269*67e74705SXin Li@end  @class NSDate, NSDictionary, NSError, NSException, NSNotification;
270*67e74705SXin Li@class NSTextField, NSPanel, NSArray, NSWindow, NSImage, NSButton, NSError;
271*67e74705SXin Li@interface NSApplication : NSResponder <NSUserInterfaceValidations> {
272*67e74705SXin Li}
273*67e74705SXin Li- (void)beginSheet:(NSWindow *)sheet modalForWindow:(NSWindow *)docWindow modalDelegate:(id)modalDelegate didEndSelector:(SEL)didEndSelector contextInfo:(void *)contextInfo;
274*67e74705SXin Li@end   enum {
275*67e74705SXin LiNSTerminateCancel = 0,         NSTerminateNow = 1,         NSTerminateLater = 2 };
276*67e74705SXin Litypedef NSUInteger NSApplicationTerminateReply;
277*67e74705SXin Li@protocol NSApplicationDelegate <NSObject> @optional        - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
278*67e74705SXin Li@end  @class NSAttributedString, NSEvent, NSFont, NSFormatter, NSImage, NSMenu, NSText, NSView, NSTextView;
279*67e74705SXin Li@interface NSCell : NSObject <NSCopying, NSCoding> {
280*67e74705SXin Li}
281*67e74705SXin Li@end
282*67e74705SXin Litypedef struct {
283*67e74705SXin Li}
284*67e74705SXin LiCVTimeStamp;
285*67e74705SXin Li@interface CIImage : NSObject <NSCoding, NSCopying> {
286*67e74705SXin Li}
287*67e74705SXin Litypedef int CIFormat;
288*67e74705SXin Li@end  enum {
289*67e74705SXin LikDAReturnSuccess = 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 };
290*67e74705SXin Litypedef mach_error_t DAReturn;
291*67e74705SXin Litypedef const struct __DADissenter * DADissenterRef;
292*67e74705SXin Liextern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string ) CF_RETURNS_RETAINED;
293*67e74705SXin Li@interface CIContext: NSObject {
294*67e74705SXin Li}
295*67e74705SXin Li- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r CF_RETURNS_RETAINED;
296*67e74705SXin Li- (CGImageRef)createCGImage:(CIImage *)im fromRect:(CGRect)r     format:(CIFormat)f colorSpace:(CGColorSpaceRef)cs CF_RETURNS_RETAINED;
297*67e74705SXin Li- (CGLayerRef)createCGLayerWithSize:(CGSize)size info:(CFDictionaryRef)d CF_RETURNS_RETAINED;
298*67e74705SXin Li@end extern NSString* const QCRendererEventKey;
299*67e74705SXin Li@protocol QCCompositionRenderer - (NSDictionary*) attributes;
300*67e74705SXin Li@end   @interface QCRenderer : NSObject <QCCompositionRenderer> {
301*67e74705SXin Li}
302*67e74705SXin Li- (id) createSnapshotImageOfType:(NSString*)type;
303*67e74705SXin Li@end  extern NSString* const QCViewDidStartRenderingNotification;
304*67e74705SXin Li@interface QCView : NSView <QCCompositionRenderer> {
305*67e74705SXin Li}
306*67e74705SXin Li- (id) createSnapshotImageOfType:(NSString*)type;
307*67e74705SXin Li@end    enum {
308*67e74705SXin LiICEXIFOrientation1 = 1,     ICEXIFOrientation2 = 2,     ICEXIFOrientation3 = 3,     ICEXIFOrientation4 = 4,     ICEXIFOrientation5 = 5,     ICEXIFOrientation6 = 6,     ICEXIFOrientation7 = 7,     ICEXIFOrientation8 = 8, };
309*67e74705SXin Li@class ICDevice;
310*67e74705SXin Li@protocol ICDeviceDelegate <NSObject>  @required      - (void)didRemoveDevice:(ICDevice*)device;
311*67e74705SXin Li@end extern NSString *const ICScannerStatusWarmingUp;
312*67e74705SXin Li@class ICScannerDevice;
313*67e74705SXin Li@protocol ICScannerDeviceDelegate <ICDeviceDelegate>  @optional       - (void)scannerDeviceDidBecomeAvailable:(ICScannerDevice*)scanner;
314*67e74705SXin Li@end
315*67e74705SXin Li
316*67e74705SXin Litypedef long unsigned int __darwin_size_t;
317*67e74705SXin Litypedef __darwin_size_t size_t;
318*67e74705SXin Litypedef unsigned long CFTypeID;
319*67e74705SXin Listruct CGPoint {
320*67e74705SXin Li  CGFloat x;
321*67e74705SXin Li  CGFloat y;
322*67e74705SXin Li};
323*67e74705SXin Litypedef struct CGPoint CGPoint;
324*67e74705SXin Litypedef struct CGGradient *CGGradientRef;
325*67e74705SXin Litypedef uint32_t CGGradientDrawingOptions;
326*67e74705SXin Liextern CFTypeID CGGradientGetTypeID(void);
327*67e74705SXin Liextern CGGradientRef CGGradientCreateWithColorComponents(CGColorSpaceRef
328*67e74705SXin Li  space, const CGFloat components[], const CGFloat locations[], size_t count) CF_RETURNS_RETAINED;
329*67e74705SXin Liextern CGGradientRef CGGradientCreateWithColors(CGColorSpaceRef space,
330*67e74705SXin Li  CFArrayRef colors, const CGFloat locations[]) CF_RETURNS_RETAINED;
331*67e74705SXin Liextern CGGradientRef CGGradientRetain(CGGradientRef gradient);
332*67e74705SXin Li
333*67e74705SXin LiCF_IMPLICIT_BRIDGING_ENABLED
334*67e74705SXin Li
335*67e74705SXin Liextern void CGGradientRelease(CGGradientRef gradient);
336*67e74705SXin Li
337*67e74705SXin LiCF_IMPLICIT_BRIDGING_DISABLED
338*67e74705SXin Li
339*67e74705SXin Litypedef struct CGContext *CGContextRef;
340*67e74705SXin Liextern void CGContextDrawLinearGradient(CGContextRef context,
341*67e74705SXin Li    CGGradientRef gradient, CGPoint startPoint, CGPoint endPoint,
342*67e74705SXin Li    CGGradientDrawingOptions options);
343*67e74705SXin Li
344*67e74705SXin LiCF_IMPLICIT_BRIDGING_ENABLED
345*67e74705SXin Li
346*67e74705SXin Liextern CGColorSpaceRef CGColorSpaceCreateDeviceRGB(void);
347*67e74705SXin Li
348*67e74705SXin LiCF_IMPLICIT_BRIDGING_DISABLED
349*67e74705SXin Li
350*67e74705SXin Li
351*67e74705SXin Li@interface NSMutableArray : NSObject
352*67e74705SXin Li- (void)addObject:(id)object;
353*67e74705SXin Li+ (instancetype)array;
354*67e74705SXin Li@end
355*67e74705SXin Li
356*67e74705SXin Li// This is how NSMakeCollectable is declared in the OS X 10.8 headers.
357*67e74705SXin Liid NSMakeCollectable(CFTypeRef __attribute__((cf_consumed))) __attribute__((ns_returns_retained));
358*67e74705SXin Li
359*67e74705SXin Litypedef const struct __CFUUID * CFUUIDRef;
360*67e74705SXin Li
361*67e74705SXin Liextern
362*67e74705SXin Livoid *CFPlugInInstanceCreate(CFAllocatorRef allocator, CFUUIDRef factoryUUID, CFUUIDRef typeUUID);
363*67e74705SXin Li
364*67e74705SXin Li//===----------------------------------------------------------------------===//
365*67e74705SXin Li// Test cases.
366*67e74705SXin Li//===----------------------------------------------------------------------===//
367*67e74705SXin Li
368*67e74705SXin LiCFAbsoluteTime f1() {
369*67e74705SXin Li  CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
370*67e74705SXin Li  CFDateRef date = CFDateCreate(0, t);
371*67e74705SXin Li  CFRetain(date);
372*67e74705SXin Li  CFRelease(date);
373*67e74705SXin Li  CFDateGetAbsoluteTime(date); // no-warning
374*67e74705SXin Li  CFRelease(date);
375*67e74705SXin Li  t = CFDateGetAbsoluteTime(date);   // expected-warning{{Reference-counted object is used after it is released}}
376*67e74705SXin Li  return t;
377*67e74705SXin Li}
378*67e74705SXin Li
379*67e74705SXin LiCFAbsoluteTime f2() {
380*67e74705SXin Li  CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
381*67e74705SXin Li  CFDateRef date = CFDateCreate(0, t);
382*67e74705SXin Li  [((NSDate*) date) retain];
383*67e74705SXin Li  CFRelease(date);
384*67e74705SXin Li  CFDateGetAbsoluteTime(date); // no-warning
385*67e74705SXin Li  [((NSDate*) date) release];
386*67e74705SXin Li  t = CFDateGetAbsoluteTime(date);   // expected-warning{{Reference-counted object is used after it is released}}
387*67e74705SXin Li  return t;
388*67e74705SXin Li}
389*67e74705SXin Li
390*67e74705SXin Li
391*67e74705SXin LiNSDate* global_x;
392*67e74705SXin Li
393*67e74705SXin Li// Test to see if we suppress an error when we store the pointer
394*67e74705SXin Li// to a global.
395*67e74705SXin Li
396*67e74705SXin LiCFAbsoluteTime f3() {
397*67e74705SXin Li  CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
398*67e74705SXin Li  CFDateRef date = CFDateCreate(0, t);
399*67e74705SXin Li  [((NSDate*) date) retain];
400*67e74705SXin Li  CFRelease(date);
401*67e74705SXin Li  CFDateGetAbsoluteTime(date); // no-warning
402*67e74705SXin Li  global_x = (NSDate*) date;
403*67e74705SXin Li  [((NSDate*) date) release];
404*67e74705SXin Li  t = CFDateGetAbsoluteTime(date);   // no-warning
405*67e74705SXin Li  return t;
406*67e74705SXin Li}
407*67e74705SXin Li
408*67e74705SXin Li//---------------------------------------------------------------------------
409*67e74705SXin Li// Test case 'f4' differs for region store and basic store.  See
410*67e74705SXin Li// retain-release-region-store.m and retain-release-basic-store.m.
411*67e74705SXin Li//---------------------------------------------------------------------------
412*67e74705SXin Li
413*67e74705SXin Li// Test a leak.
414*67e74705SXin Li
415*67e74705SXin LiCFAbsoluteTime f5(int x) {
416*67e74705SXin Li  CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
417*67e74705SXin Li  CFDateRef date = CFDateCreate(0, t); // expected-warning{{leak}}
418*67e74705SXin Li
419*67e74705SXin Li  if (x)
420*67e74705SXin Li    CFRelease(date);
421*67e74705SXin Li
422*67e74705SXin Li  return t;
423*67e74705SXin Li}
424*67e74705SXin Li
425*67e74705SXin Li// Test a leak involving the return.
426*67e74705SXin Li
427*67e74705SXin LiCFDateRef f6(int x) {
428*67e74705SXin Li  CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());  // expected-warning{{leak}}
429*67e74705SXin Li  CFRetain(date);
430*67e74705SXin Li  return date;
431*67e74705SXin Li}
432*67e74705SXin Li
433*67e74705SXin Li// Test a leak involving an overwrite.
434*67e74705SXin Li
435*67e74705SXin LiCFDateRef f7() {
436*67e74705SXin Li  CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());  //expected-warning{{leak}}
437*67e74705SXin Li  CFRetain(date);
438*67e74705SXin Li  date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // expected-warning {{leak}}
439*67e74705SXin Li  return date;
440*67e74705SXin Li}
441*67e74705SXin Li
442*67e74705SXin Li// Generalization of Create rule.  MyDateCreate returns a CFXXXTypeRef, and
443*67e74705SXin Li// has the word create.
444*67e74705SXin Li
445*67e74705SXin LiCF_IMPLICIT_BRIDGING_ENABLED
446*67e74705SXin Li
447*67e74705SXin LiCFDateRef MyDateCreate();
448*67e74705SXin Li
449*67e74705SXin LiCF_IMPLICIT_BRIDGING_DISABLED
450*67e74705SXin Li
451*67e74705SXin Li
452*67e74705SXin LiCFDateRef f8() {
453*67e74705SXin Li  CFDateRef date = MyDateCreate(); // expected-warning{{leak}}
454*67e74705SXin Li  CFRetain(date);
455*67e74705SXin Li  return date;
456*67e74705SXin Li}
457*67e74705SXin Li
458*67e74705SXin Li__attribute__((cf_returns_retained)) CFDateRef f9() {
459*67e74705SXin Li  CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // no-warning
460*67e74705SXin Li  int *p = 0;
461*67e74705SXin Li  // When allocations fail, CFDateCreate can return null.
462*67e74705SXin Li  if (!date) *p = 1; // expected-warning{{null}}
463*67e74705SXin Li  return date;
464*67e74705SXin Li}
465*67e74705SXin Li
466*67e74705SXin Li// Handle DiskArbitration API:
467*67e74705SXin Li//
468*67e74705SXin Li// http://developer.apple.com/DOCUMENTATION/DARWIN/Reference/DiscArbitrationFramework/
469*67e74705SXin Li//
470*67e74705SXin Livoid f10(io_service_t media, DADiskRef d, CFStringRef s) {
471*67e74705SXin Li  DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, 0, "hello"); // expected-warning{{leak}}
472*67e74705SXin Li  if (disk) NSLog(@"ok");
473*67e74705SXin Li
474*67e74705SXin Li  disk = DADiskCreateFromIOMedia(kCFAllocatorDefault, 0, media); // expected-warning{{leak}}
475*67e74705SXin Li  if (disk) NSLog(@"ok");
476*67e74705SXin Li
477*67e74705SXin Li  CFDictionaryRef dict = DADiskCopyDescription(d);  // expected-warning{{leak}}
478*67e74705SXin Li  if (dict) NSLog(@"ok");
479*67e74705SXin Li
480*67e74705SXin Li  disk = DADiskCopyWholeDisk(d); // expected-warning{{leak}}
481*67e74705SXin Li  if (disk) NSLog(@"ok");
482*67e74705SXin Li
483*67e74705SXin Li  DADissenterRef dissenter = DADissenterCreate(kCFAllocatorDefault,   // expected-warning{{leak}}
484*67e74705SXin Li                                                kDAReturnSuccess, s);
485*67e74705SXin Li  if (dissenter) NSLog(@"ok");
486*67e74705SXin Li
487*67e74705SXin Li  DASessionRef session = DASessionCreate(kCFAllocatorDefault);  // expected-warning{{leak}}
488*67e74705SXin Li  if (session) NSLog(@"ok");
489*67e74705SXin Li}
490*67e74705SXin Li
491*67e74705SXin Li// Test retain/release checker with CFString and CFMutableArray.
492*67e74705SXin Livoid f11() {
493*67e74705SXin Li  // Create the array.
494*67e74705SXin Li  CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
495*67e74705SXin Li
496*67e74705SXin Li  // Create a string.
497*67e74705SXin Li  CFStringRef s1 = CFStringCreateWithCString(0, "hello world",
498*67e74705SXin Li                                             kCFStringEncodingUTF8);
499*67e74705SXin Li
500*67e74705SXin Li  // Add the string to the array.
501*67e74705SXin Li  CFArrayAppendValue(A, s1);
502*67e74705SXin Li
503*67e74705SXin Li  // Decrement the reference count.
504*67e74705SXin Li  CFRelease(s1); // no-warning
505*67e74705SXin Li
506*67e74705SXin Li  // Get the string.  We don't own it.
507*67e74705SXin Li  s1 = (CFStringRef) CFArrayGetValueAtIndex(A, 0);
508*67e74705SXin Li
509*67e74705SXin Li  // Release the array.
510*67e74705SXin Li  CFRelease(A); // no-warning
511*67e74705SXin Li
512*67e74705SXin Li  // Release the string.  This is a bug.
513*67e74705SXin Li  CFRelease(s1); // expected-warning{{Incorrect decrement of the reference count}}
514*67e74705SXin Li}
515*67e74705SXin Li
516*67e74705SXin Li// PR 3337: Handle functions declared using typedefs.
517*67e74705SXin Litypedef CFTypeRef CREATEFUN();
518*67e74705SXin Li
519*67e74705SXin LiCF_IMPLICIT_BRIDGING_ENABLED
520*67e74705SXin Li
521*67e74705SXin LiCFTypeRef MyCreateFun();
522*67e74705SXin Li
523*67e74705SXin LiCF_IMPLICIT_BRIDGING_DISABLED
524*67e74705SXin Li
525*67e74705SXin Li
526*67e74705SXin Livoid f12() {
527*67e74705SXin Li  CFTypeRef o = MyCreateFun(); // expected-warning {{leak}}
528*67e74705SXin Li}
529*67e74705SXin Li
530*67e74705SXin Livoid f13_autorelease() {
531*67e74705SXin Li  CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
532*67e74705SXin Li  [(id) A autorelease]; // no-warning
533*67e74705SXin Li}
534*67e74705SXin Li
535*67e74705SXin Livoid f13_autorelease_b() {
536*67e74705SXin Li  CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
537*67e74705SXin Li  [(id) A autorelease];
538*67e74705SXin Li  [(id) A autorelease];
539*67e74705SXin Li} // expected-warning{{Object autoreleased too many times}}
540*67e74705SXin Li
541*67e74705SXin LiCFMutableArrayRef f13_autorelease_c() {
542*67e74705SXin Li  CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
543*67e74705SXin Li  [(id) A autorelease];
544*67e74705SXin Li  [(id) A autorelease];
545*67e74705SXin Li  return A; // expected-warning{{Object autoreleased too many times}}
546*67e74705SXin Li}
547*67e74705SXin Li
548*67e74705SXin LiCFMutableArrayRef f13_autorelease_d() {
549*67e74705SXin Li  CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
550*67e74705SXin Li  [(id) A autorelease];
551*67e74705SXin Li  [(id) A autorelease];
552*67e74705SXin Li  CFMutableArrayRef B = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{Object autoreleased too many times}}
553*67e74705SXin Li  CFRelease(B); // no-warning
554*67e74705SXin Li  while (1) {}
555*67e74705SXin Li}
556*67e74705SXin Li
557*67e74705SXin Li
558*67e74705SXin Li// This case exercises the logic where the leak site is the same as the allocation site.
559*67e74705SXin Livoid f14_leakimmediately() {
560*67e74705SXin Li  CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}}
561*67e74705SXin Li}
562*67e74705SXin Li
563*67e74705SXin Li// Test that we track an allocated object beyond the point where the *name*
564*67e74705SXin Li// of the variable storing the reference is no longer live.
565*67e74705SXin Livoid f15() {
566*67e74705SXin Li  // Create the array.
567*67e74705SXin Li  CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks);
568*67e74705SXin Li  CFMutableArrayRef *B = &A;
569*67e74705SXin Li  // At this point, the name 'A' is no longer live.
570*67e74705SXin Li  CFRelease(*B);  // no-warning
571*67e74705SXin Li}
572*67e74705SXin Li
573*67e74705SXin Li// Test when we pass NULL to CFRetain/CFRelease/CFMakeCollectable/CFAutorelease.
574*67e74705SXin Livoid f16(int x, CFTypeRef p) {
575*67e74705SXin Li  if (p)
576*67e74705SXin Li    return;
577*67e74705SXin Li
578*67e74705SXin Li  switch (x) {
579*67e74705SXin Li  case 0:
580*67e74705SXin Li    CFRelease(p);
581*67e74705SXin Li    break;
582*67e74705SXin Li  case 1:
583*67e74705SXin Li    CFRetain(p);
584*67e74705SXin Li    break;
585*67e74705SXin Li  case 2:
586*67e74705SXin Li    CFMakeCollectable(p);
587*67e74705SXin Li    break;
588*67e74705SXin Li  case 3:
589*67e74705SXin Li    CFAutorelease(p);
590*67e74705SXin Li    break;
591*67e74705SXin Li  default:
592*67e74705SXin Li    break;
593*67e74705SXin Li  }
594*67e74705SXin Li}
595*67e74705SXin Li
596*67e74705SXin Li// Test that an object is non-null after CFRetain/CFRelease/CFMakeCollectable/CFAutorelease.
597*67e74705SXin Livoid f17(int x, CFTypeRef p) {
598*67e74705SXin Li  switch (x) {
599*67e74705SXin Li  case 0:
600*67e74705SXin Li    CFRelease(p);
601*67e74705SXin Li    if (!p)
602*67e74705SXin Li      CFRelease(0); // no-warning
603*67e74705SXin Li    break;
604*67e74705SXin Li  case 1:
605*67e74705SXin Li    CFRetain(p);
606*67e74705SXin Li    if (!p)
607*67e74705SXin Li      CFRetain(0); // no-warning
608*67e74705SXin Li    break;
609*67e74705SXin Li  case 2:
610*67e74705SXin Li    CFMakeCollectable(p);
611*67e74705SXin Li    if (!p)
612*67e74705SXin Li      CFMakeCollectable(0); // no-warning
613*67e74705SXin Li    break;
614*67e74705SXin Li  case 3:
615*67e74705SXin Li    CFAutorelease(p);
616*67e74705SXin Li    if (!p)
617*67e74705SXin Li      CFAutorelease(0); // no-warning
618*67e74705SXin Li    break;
619*67e74705SXin Li  default:
620*67e74705SXin Li    break;
621*67e74705SXin Li  }
622*67e74705SXin Li}
623*67e74705SXin Li
624*67e74705SXin Li// Test basic tracking of ivars associated with 'self'.  For the retain/release
625*67e74705SXin Li// checker we currently do not want to flag leaks associated with stores
626*67e74705SXin Li// of tracked objects to ivars.
627*67e74705SXin Li@interface SelfIvarTest : NSObject {
628*67e74705SXin Li  id myObj;
629*67e74705SXin Li}
630*67e74705SXin Li- (void)test_self_tracking;
631*67e74705SXin Li@end
632*67e74705SXin Li
633*67e74705SXin Li@implementation SelfIvarTest
634*67e74705SXin Li- (void)test_self_tracking {
635*67e74705SXin Li  myObj = (id) CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
636*67e74705SXin Li}
637*67e74705SXin Li@end
638*67e74705SXin Li
639*67e74705SXin Li// Test return of non-owned objects in contexts where an owned object
640*67e74705SXin Li// is expected.
641*67e74705SXin Li@interface TestReturnNotOwnedWhenExpectedOwned
642*67e74705SXin Li- (NSString*)newString;
643*67e74705SXin Li@end
644*67e74705SXin Li
645*67e74705SXin Li@implementation TestReturnNotOwnedWhenExpectedOwned
646*67e74705SXin Li- (NSString*)newString {
647*67e74705SXin Li  NSString *s = [NSString stringWithUTF8String:"hello"];
648*67e74705SXin Li  return s; // expected-warning{{Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected}}
649*67e74705SXin Li}
650*67e74705SXin Li@end
651*67e74705SXin Li
652*67e74705SXin Li// <rdar://problem/6659160>
653*67e74705SXin Liint isFoo(char c);
654*67e74705SXin Li
655*67e74705SXin Listatic void rdar_6659160(char *inkind, char *inname)
656*67e74705SXin Li{
657*67e74705SXin Li  // We currently expect that [NSObject alloc] cannot fail.  This
658*67e74705SXin Li  // will be a toggled flag in the future.  It can indeed return null, but
659*67e74705SXin Li  // Cocoa programmers generally aren't expected to reason about out-of-memory
660*67e74705SXin Li  // conditions.
661*67e74705SXin Li  NSString *kind = [[NSString alloc] initWithUTF8String:inkind];  // expected-warning{{leak}}
662*67e74705SXin Li
663*67e74705SXin Li  // We do allow stringWithUTF8String to fail.  This isn't really correct, as
664*67e74705SXin Li  // far as returning 0.  In most error conditions it will throw an exception.
665*67e74705SXin Li  // If allocation fails it could return 0, but again this
666*67e74705SXin Li  // isn't expected.
667*67e74705SXin Li  NSString *name = [NSString stringWithUTF8String:inname];
668*67e74705SXin Li  if(!name)
669*67e74705SXin Li    return;
670*67e74705SXin Li
671*67e74705SXin Li  const char *kindC = 0;
672*67e74705SXin Li  const char *nameC = 0;
673*67e74705SXin Li
674*67e74705SXin Li  // In both cases, we cannot reach a point down below where we
675*67e74705SXin Li  // dereference kindC or nameC with either being null.  This is because
676*67e74705SXin Li  // we assume that [NSObject alloc] doesn't fail and that we have the guard
677*67e74705SXin Li  // up above.
678*67e74705SXin Li
679*67e74705SXin Li  if(kind)
680*67e74705SXin Li    kindC = [kind UTF8String];
681*67e74705SXin Li  if(name)
682*67e74705SXin Li    nameC = [name UTF8String];
683*67e74705SXin Li  if(!isFoo(kindC[0])) // expected-warning{{null}}
684*67e74705SXin Li    return;
685*67e74705SXin Li  if(!isFoo(nameC[0])) // no-warning
686*67e74705SXin Li    return;
687*67e74705SXin Li
688*67e74705SXin Li  [kind release];
689*67e74705SXin Li  [name release]; // expected-warning{{Incorrect decrement of the reference count}}
690*67e74705SXin Li}
691*67e74705SXin Li
692*67e74705SXin Li// PR 3677 - 'allocWithZone' should be treated as following the Cocoa naming
693*67e74705SXin Li//  conventions with respect to 'return'ing ownership.
694*67e74705SXin Li@interface PR3677: NSObject @end
695*67e74705SXin Li@implementation PR3677
696*67e74705SXin Li+ (id)allocWithZone:(NSZone *)inZone {
697*67e74705SXin Li  return [super allocWithZone:inZone];  // no-warning
698*67e74705SXin Li}
699*67e74705SXin Li@end
700*67e74705SXin Li
701*67e74705SXin Li// PR 3820 - Reason about calls to -dealloc
702*67e74705SXin Livoid pr3820_DeallocInsteadOfRelease(void)
703*67e74705SXin Li{
704*67e74705SXin Li  id foo = [[NSString alloc] init]; // no-warning
705*67e74705SXin Li  [foo dealloc];
706*67e74705SXin Li  // foo is not leaked, since it has been deallocated.
707*67e74705SXin Li}
708*67e74705SXin Li
709*67e74705SXin Livoid pr3820_ReleaseAfterDealloc(void)
710*67e74705SXin Li{
711*67e74705SXin Li  id foo = [[NSString alloc] init];
712*67e74705SXin Li  [foo dealloc];
713*67e74705SXin Li  [foo release];  // expected-warning{{used after it is release}}
714*67e74705SXin Li  // NSInternalInconsistencyException: message sent to deallocated object
715*67e74705SXin Li}
716*67e74705SXin Li
717*67e74705SXin Livoid pr3820_DeallocAfterRelease(void)
718*67e74705SXin Li{
719*67e74705SXin Li  NSLog(@"\n\n[%s]", __FUNCTION__);
720*67e74705SXin Li  id foo = [[NSString alloc] init];
721*67e74705SXin Li  [foo release];
722*67e74705SXin Li  [foo dealloc]; // expected-warning{{used after it is released}}
723*67e74705SXin Li  // message sent to released object
724*67e74705SXin Li}
725*67e74705SXin Li
726*67e74705SXin Li// From <rdar://problem/6704930>.  The problem here is that 'length' binds to
727*67e74705SXin Li// '($0 - 1)' after '--length', but SimpleConstraintManager doesn't know how to
728*67e74705SXin Li// reason about '($0 - 1) > constant'.  As a temporary hack, we drop the value
729*67e74705SXin Li// of '($0 - 1)' and conjure a new symbol.
730*67e74705SXin Livoid rdar6704930(unsigned char *s, unsigned int length) {
731*67e74705SXin Li  NSString* name = 0;
732*67e74705SXin Li  if (s != 0) {
733*67e74705SXin Li    if (length > 0) {
734*67e74705SXin Li      while (length > 0) {
735*67e74705SXin Li        if (*s == ':') {
736*67e74705SXin Li          ++s;
737*67e74705SXin Li          --length;
738*67e74705SXin Li          name = [[NSString alloc] init]; // no-warning
739*67e74705SXin Li          break;
740*67e74705SXin Li        }
741*67e74705SXin Li        ++s;
742*67e74705SXin Li        --length;
743*67e74705SXin Li      }
744*67e74705SXin Li      if ((length == 0) && (name != 0)) {
745*67e74705SXin Li        [name release];
746*67e74705SXin Li        name = 0;
747*67e74705SXin Li      }
748*67e74705SXin Li      if (length == 0) { // no ':' found -> use it all as name
749*67e74705SXin Li        name = [[NSString alloc] init]; // no-warning
750*67e74705SXin Li      }
751*67e74705SXin Li    }
752*67e74705SXin Li  }
753*67e74705SXin Li
754*67e74705SXin Li  if (name != 0) {
755*67e74705SXin Li    [name release];
756*67e74705SXin Li  }
757*67e74705SXin Li}
758*67e74705SXin Li
759*67e74705SXin Li//===----------------------------------------------------------------------===//
760*67e74705SXin Li// <rdar://problem/6833332>
761*67e74705SXin Li// One build of the analyzer accidentally stopped tracking the allocated
762*67e74705SXin Li// object after the 'retain'.
763*67e74705SXin Li//===----------------------------------------------------------------------===//
764*67e74705SXin Li
765*67e74705SXin Li@interface rdar_6833332 : NSObject <NSApplicationDelegate> {
766*67e74705SXin Li    NSWindow *window;
767*67e74705SXin Li}
768*67e74705SXin Li@property (nonatomic, retain) NSWindow *window;
769*67e74705SXin Li@end
770*67e74705SXin Li
771*67e74705SXin Li@implementation rdar_6833332
772*67e74705SXin Li@synthesize window;
773*67e74705SXin Li- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
774*67e74705SXin Li NSMutableDictionary *dict = [[NSMutableDictionary dictionaryWithCapacity:4] retain]; // expected-warning{{leak}}
775*67e74705SXin Li
776*67e74705SXin Li [dict setObject:@"foo" forKey:@"bar"];
777*67e74705SXin Li
778*67e74705SXin Li NSLog(@"%@", dict);
779*67e74705SXin Li}
780*67e74705SXin Li- (void)dealloc {
781*67e74705SXin Li    [window release];
782*67e74705SXin Li    [super dealloc];
783*67e74705SXin Li}
784*67e74705SXin Li
785*67e74705SXin Li- (void)radar10102244 {
786*67e74705SXin Li NSMutableDictionary *dict = [[NSMutableDictionary dictionaryWithCapacity:4] retain]; // expected-warning{{leak}}
787*67e74705SXin Li if (window)
788*67e74705SXin Li   NSLog(@"%@", window);
789*67e74705SXin Li}
790*67e74705SXin Li@end
791*67e74705SXin Li
792*67e74705SXin Li//===----------------------------------------------------------------------===//
793*67e74705SXin Li// <rdar://problem/6257780> clang checker fails to catch use-after-release
794*67e74705SXin Li//===----------------------------------------------------------------------===//
795*67e74705SXin Li
796*67e74705SXin Liint rdar_6257780_Case1() {
797*67e74705SXin Li  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
798*67e74705SXin Li  NSArray *array = [NSArray array];
799*67e74705SXin Li  [array release]; // expected-warning{{Incorrect decrement of the reference count of an object that is not owned at this point by the caller}}
800*67e74705SXin Li  [pool drain];
801*67e74705SXin Li  return 0;
802*67e74705SXin Li}
803*67e74705SXin Li
804*67e74705SXin Li//===----------------------------------------------------------------------===//
805*67e74705SXin Li// <rdar://problem/10640253> Analyzer is confused about NSAutoreleasePool -allocWithZone:.
806*67e74705SXin Li//===----------------------------------------------------------------------===//
807*67e74705SXin Li
808*67e74705SXin Livoid rdar_10640253_autorelease_allocWithZone() {
809*67e74705SXin Li    NSAutoreleasePool *pool = [[NSAutoreleasePool allocWithZone:(NSZone*)0] init];
810*67e74705SXin Li    (void) pool;
811*67e74705SXin Li}
812*67e74705SXin Li
813*67e74705SXin Li//===----------------------------------------------------------------------===//
814*67e74705SXin Li// <rdar://problem/6866843> Checker should understand new/setObject:/release constructs
815*67e74705SXin Li//===----------------------------------------------------------------------===//
816*67e74705SXin Li
817*67e74705SXin Livoid rdar_6866843() {
818*67e74705SXin Li NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
819*67e74705SXin Li NSMutableDictionary* dictionary = [[NSMutableDictionary alloc] init];
820*67e74705SXin Li NSArray* array = [[NSArray alloc] init];
821*67e74705SXin Li [dictionary setObject:array forKey:@"key"];
822*67e74705SXin Li [array release];
823*67e74705SXin Li // Using 'array' here should be fine
824*67e74705SXin Li NSLog(@"array = %@\n", array); // no-warning
825*67e74705SXin Li // Now the array is released
826*67e74705SXin Li [dictionary release];
827*67e74705SXin Li [pool drain];
828*67e74705SXin Li}
829*67e74705SXin Li
830*67e74705SXin Li
831*67e74705SXin Li//===----------------------------------------------------------------------===//
832*67e74705SXin Li// <rdar://problem/6877235> Classes typedef-ed to CF objects should get the same treatment as CF objects
833*67e74705SXin Li//===----------------------------------------------------------------------===//
834*67e74705SXin Li
835*67e74705SXin Litypedef CFTypeRef OtherRef;
836*67e74705SXin Li
837*67e74705SXin Li@interface RDar6877235 : NSObject {}
838*67e74705SXin Li- (CFTypeRef)_copyCFTypeRef CF_RETURNS_RETAINED;
839*67e74705SXin Li- (OtherRef)_copyOtherRef CF_RETURNS_RETAINED;
840*67e74705SXin Li@end
841*67e74705SXin Li
842*67e74705SXin Li@implementation RDar6877235
843*67e74705SXin Li- (CFTypeRef)_copyCFTypeRef {
844*67e74705SXin Li  return [[NSString alloc] init]; // no-warning
845*67e74705SXin Li}
846*67e74705SXin Li- (OtherRef)_copyOtherRef {
847*67e74705SXin Li  return [[NSString alloc] init]; // no-warning
848*67e74705SXin Li}
849*67e74705SXin Li@end
850*67e74705SXin Li
851*67e74705SXin Li//===----------------------------------------------------------------------===//
852*67e74705SXin Li// <rdar://problem/6320065> false positive - init method returns an object
853*67e74705SXin Li// owned by caller
854*67e74705SXin Li//===----------------------------------------------------------------------===//
855*67e74705SXin Li
856*67e74705SXin Li@interface RDar6320065 : NSObject {
857*67e74705SXin Li  NSString *_foo;
858*67e74705SXin Li}
859*67e74705SXin Li- (instancetype)initReturningNewClass;
860*67e74705SXin Li- (id)_initReturningNewClassBad;
861*67e74705SXin Li- (instancetype)initReturningNewClassBad2;
862*67e74705SXin Li@end
863*67e74705SXin Li
864*67e74705SXin Li@interface RDar6320065Subclass : RDar6320065
865*67e74705SXin Li@end
866*67e74705SXin Li
867*67e74705SXin Li@implementation RDar6320065
868*67e74705SXin Li- (instancetype)initReturningNewClass {
869*67e74705SXin Li  [self release];
870*67e74705SXin Li  self = [[RDar6320065Subclass alloc] init]; // no-warning
871*67e74705SXin Li  return self;
872*67e74705SXin Li}
873*67e74705SXin Li- (id)_initReturningNewClassBad {
874*67e74705SXin Li  [self release];
875*67e74705SXin Li  [[RDar6320065Subclass alloc] init]; // expected-warning {{leak}}
876*67e74705SXin Li  return self;
877*67e74705SXin Li}
878*67e74705SXin Li- (instancetype)initReturningNewClassBad2 {
879*67e74705SXin Li  [self release];
880*67e74705SXin Li  self = [[RDar6320065Subclass alloc] init];
881*67e74705SXin Li  return [self autorelease]; // expected-warning{{Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected}}
882*67e74705SXin Li}
883*67e74705SXin Li
884*67e74705SXin Li@end
885*67e74705SXin Li
886*67e74705SXin Li@implementation RDar6320065Subclass
887*67e74705SXin Li@end
888*67e74705SXin Li
889*67e74705SXin Liint RDar6320065_test() {
890*67e74705SXin Li  RDar6320065 *test = [[RDar6320065 alloc] init]; // no-warning
891*67e74705SXin Li  [test release];
892*67e74705SXin Li  return 0;
893*67e74705SXin Li}
894*67e74705SXin Li
895*67e74705SXin Li//===----------------------------------------------------------------------===//
896*67e74705SXin Li// <rdar://problem/7129086> -awakeAfterUsingCoder: returns an owned object
897*67e74705SXin Li//  and claims the receiver
898*67e74705SXin Li//===----------------------------------------------------------------------===//
899*67e74705SXin Li
900*67e74705SXin Li@interface RDar7129086 : NSObject {} @end
901*67e74705SXin Li@implementation RDar7129086
902*67e74705SXin Li- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder {
903*67e74705SXin Li  [self release]; // no-warning
904*67e74705SXin Li  return [NSString alloc];  // no-warning
905*67e74705SXin Li}
906*67e74705SXin Li@end
907*67e74705SXin Li
908*67e74705SXin Li//===----------------------------------------------------------------------===//
909*67e74705SXin Li// <rdar://problem/6859457> [NSData dataWithBytesNoCopy] does not return a
910*67e74705SXin Li//  retained object
911*67e74705SXin Li//===----------------------------------------------------------------------===//
912*67e74705SXin Li
913*67e74705SXin Li@interface RDar6859457 : NSObject {}
914*67e74705SXin Li- (NSString*) NoCopyString;
915*67e74705SXin Li- (NSString*) noCopyString;
916*67e74705SXin Li@end
917*67e74705SXin Li
918*67e74705SXin Li@implementation RDar6859457
919*67e74705SXin Li- (NSString*) NoCopyString { return [[NSString alloc] init]; } // expected-warning{{leak}}
920*67e74705SXin Li- (NSString*) noCopyString { return [[NSString alloc] init]; } // expected-warning{{leak}}
921*67e74705SXin Li@end
922*67e74705SXin Li
923*67e74705SXin Livoid test_RDar6859457(RDar6859457 *x, void *bytes, NSUInteger dataLength) {
924*67e74705SXin Li  [x NoCopyString]; // expected-warning{{leak}}
925*67e74705SXin Li  [x noCopyString]; // expected-warning{{leak}}
926*67e74705SXin Li  [NSData dataWithBytesNoCopy:bytes length:dataLength];  // no-warning
927*67e74705SXin Li  [NSData dataWithBytesNoCopy:bytes length:dataLength freeWhenDone:1]; // no-warning
928*67e74705SXin Li}
929*67e74705SXin Li
930*67e74705SXin Li//===----------------------------------------------------------------------===//
931*67e74705SXin Li// PR 4230 - an autorelease pool is not necessarily leaked during a premature
932*67e74705SXin Li//  return
933*67e74705SXin Li//===----------------------------------------------------------------------===//
934*67e74705SXin Li
935*67e74705SXin Listatic void PR4230(void)
936*67e74705SXin Li{
937*67e74705SXin Li  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // no-warning
938*67e74705SXin Li  NSString *object = [[[NSString alloc] init] autorelease]; // no-warning
939*67e74705SXin Li  return;
940*67e74705SXin Li}
941*67e74705SXin Li
942*67e74705SXin Listatic void PR4230_new(void)
943*67e74705SXin Li{
944*67e74705SXin Li  NSAutoreleasePool *pool = [NSAutoreleasePool new]; // no-warning
945*67e74705SXin Li  NSString *object = [[[NSString alloc] init] autorelease]; // no-warning
946*67e74705SXin Li  return;
947*67e74705SXin Li}
948*67e74705SXin Li
949*67e74705SXin Li//===----------------------------------------------------------------------===//
950*67e74705SXin Li// Method name that has a null IdentifierInfo* for its first selector slot.
951*67e74705SXin Li// This test just makes sure that we handle it.
952*67e74705SXin Li//===----------------------------------------------------------------------===//
953*67e74705SXin Li
954*67e74705SXin Li@interface TestNullIdentifier
955*67e74705SXin Li@end
956*67e74705SXin Li
957*67e74705SXin Li@implementation TestNullIdentifier
958*67e74705SXin Li+ (id):(int)x, ... {
959*67e74705SXin Li  return [[NSString alloc] init]; // expected-warning{{leak}}
960*67e74705SXin Li}
961*67e74705SXin Li@end
962*67e74705SXin Li
963*67e74705SXin Li//===----------------------------------------------------------------------===//
964*67e74705SXin Li// <rdar://problem/6893565> don't flag leaks for return types that cannot be
965*67e74705SXin Li//                          determined to be CF types
966*67e74705SXin Li//===----------------------------------------------------------------------===//
967*67e74705SXin Li
968*67e74705SXin Li// We don't know if 'struct s6893565' represents a Core Foundation type, so
969*67e74705SXin Li// we shouldn't emit an error here.
970*67e74705SXin Litypedef struct s6893565* TD6893565;
971*67e74705SXin Li
972*67e74705SXin Li@interface RDar6893565 {}
973*67e74705SXin Li-(TD6893565)newThing;
974*67e74705SXin Li@end
975*67e74705SXin Li
976*67e74705SXin Li@implementation RDar6893565
977*67e74705SXin Li-(TD6893565)newThing {
978*67e74705SXin Li  return (TD6893565) [[NSString alloc] init]; // no-warning
979*67e74705SXin Li}
980*67e74705SXin Li@end
981*67e74705SXin Li
982*67e74705SXin Li//===----------------------------------------------------------------------===//
983*67e74705SXin Li// <rdar://problem/6902710> clang: false positives w/QC and CoreImage methods
984*67e74705SXin Li//===----------------------------------------------------------------------===//
985*67e74705SXin Li
986*67e74705SXin Livoid rdar6902710(QCView *view, QCRenderer *renderer, CIContext *context,
987*67e74705SXin Li                 NSString *str, CIImage *img, CGRect rect,
988*67e74705SXin Li                 CIFormat form, CGColorSpaceRef cs) {
989*67e74705SXin Li  [view createSnapshotImageOfType:str]; // expected-warning{{leak}}
990*67e74705SXin Li  [renderer createSnapshotImageOfType:str]; // expected-warning{{leak}}
991*67e74705SXin Li  [context createCGImage:img fromRect:rect]; // expected-warning{{leak}}
992*67e74705SXin Li  [context createCGImage:img fromRect:rect format:form colorSpace:cs]; // expected-warning{{leak}}
993*67e74705SXin Li}
994*67e74705SXin Li
995*67e74705SXin Li//===----------------------------------------------------------------------===//
996*67e74705SXin Li// <rdar://problem/6945561> -[CIContext createCGLayerWithSize:info:]
997*67e74705SXin Li//                           misinterpreted by clang scan-build
998*67e74705SXin Li//===----------------------------------------------------------------------===//
999*67e74705SXin Li
1000*67e74705SXin Livoid rdar6945561(CIContext *context, CGSize size, CFDictionaryRef d) {
1001*67e74705SXin Li  [context createCGLayerWithSize:size info:d]; // expected-warning{{leak}}
1002*67e74705SXin Li}
1003*67e74705SXin Li
1004*67e74705SXin Li//===----------------------------------------------------------------------===//
1005*67e74705SXin Li// <rdar://problem/6961230> add knowledge of IOKit functions to retain/release
1006*67e74705SXin Li//                          checker
1007*67e74705SXin Li//===----------------------------------------------------------------------===//
1008*67e74705SXin Li
1009*67e74705SXin Livoid IOBSDNameMatching_wrapper(mach_port_t masterPort, uint32_t options,  const char * bsdName) {
1010*67e74705SXin Li  IOBSDNameMatching(masterPort, options, bsdName); // expected-warning{{leak}}
1011*67e74705SXin Li}
1012*67e74705SXin Li
1013*67e74705SXin Livoid IOServiceMatching_wrapper(const char * name) {
1014*67e74705SXin Li  IOServiceMatching(name); // expected-warning{{leak}}
1015*67e74705SXin Li}
1016*67e74705SXin Li
1017*67e74705SXin Livoid IOServiceNameMatching_wrapper(const char * name) {
1018*67e74705SXin Li  IOServiceNameMatching(name); // expected-warning{{leak}}
1019*67e74705SXin Li}
1020*67e74705SXin Li
1021*67e74705SXin LiCF_RETURNS_RETAINED CFDictionaryRef CreateDict();
1022*67e74705SXin Li
1023*67e74705SXin Livoid IOServiceAddNotification_wrapper(mach_port_t masterPort, const io_name_t notificationType,
1024*67e74705SXin Li  mach_port_t wakePort, uintptr_t reference, io_iterator_t * notification ) {
1025*67e74705SXin Li
1026*67e74705SXin Li  CFDictionaryRef matching = CreateDict();
1027*67e74705SXin Li  CFRelease(matching);
1028*67e74705SXin Li  IOServiceAddNotification(masterPort, notificationType, matching, // expected-warning{{used after it is released}} expected-warning{{deprecated}}
1029*67e74705SXin Li                           wakePort, reference, notification);
1030*67e74705SXin Li}
1031*67e74705SXin Li
1032*67e74705SXin Livoid IORegistryEntryIDMatching_wrapper(uint64_t entryID ) {
1033*67e74705SXin Li  IORegistryEntryIDMatching(entryID); // expected-warning{{leak}}
1034*67e74705SXin Li}
1035*67e74705SXin Li
1036*67e74705SXin Livoid IOOpenFirmwarePathMatching_wrapper(mach_port_t masterPort, uint32_t options,
1037*67e74705SXin Li                                        const char * path) {
1038*67e74705SXin Li  IOOpenFirmwarePathMatching(masterPort, options, path); // expected-warning{{leak}}
1039*67e74705SXin Li}
1040*67e74705SXin Li
1041*67e74705SXin Livoid IOServiceGetMatchingService_wrapper(mach_port_t masterPort) {
1042*67e74705SXin Li  CFDictionaryRef matching = CreateDict();
1043*67e74705SXin Li  IOServiceGetMatchingService(masterPort, matching);
1044*67e74705SXin Li  CFRelease(matching); // expected-warning{{used after it is released}}
1045*67e74705SXin Li}
1046*67e74705SXin Li
1047*67e74705SXin Livoid IOServiceGetMatchingServices_wrapper(mach_port_t masterPort, io_iterator_t *existing) {
1048*67e74705SXin Li  CFDictionaryRef matching = CreateDict();
1049*67e74705SXin Li  IOServiceGetMatchingServices(masterPort, matching, existing);
1050*67e74705SXin Li  CFRelease(matching); // expected-warning{{used after it is released}}
1051*67e74705SXin Li}
1052*67e74705SXin Li
1053*67e74705SXin Livoid IOServiceAddMatchingNotification_wrapper(IONotificationPortRef notifyPort, const io_name_t notificationType,
1054*67e74705SXin Li  IOServiceMatchingCallback callback, void * refCon, io_iterator_t * notification) {
1055*67e74705SXin Li
1056*67e74705SXin Li  CFDictionaryRef matching = CreateDict();
1057*67e74705SXin Li  IOServiceAddMatchingNotification(notifyPort, notificationType, matching, callback, refCon, notification);
1058*67e74705SXin Li  CFRelease(matching); // expected-warning{{used after it is released}}
1059*67e74705SXin Li}
1060*67e74705SXin Li
1061*67e74705SXin Li//===----------------------------------------------------------------------===//
1062*67e74705SXin Li// Test of handling objects whose references "escape" to containers.
1063*67e74705SXin Li//===----------------------------------------------------------------------===//
1064*67e74705SXin Li
1065*67e74705SXin Livoid CFDictionaryAddValue(CFMutableDictionaryRef, void *, void *);
1066*67e74705SXin Li
1067*67e74705SXin Li// <rdar://problem/6539791>
1068*67e74705SXin Livoid rdar_6539791(CFMutableDictionaryRef y, void* key, void* val_key) {
1069*67e74705SXin Li  CFMutableDictionaryRef x = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
1070*67e74705SXin Li  CFDictionaryAddValue(y, key, x);
1071*67e74705SXin Li  CFRelease(x); // the dictionary keeps a reference, so the object isn't deallocated yet
1072*67e74705SXin Li  signed z = 1;
1073*67e74705SXin Li  CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z);
1074*67e74705SXin Li  if (value) {
1075*67e74705SXin Li    CFDictionaryAddValue(x, val_key, (void*)value); // no-warning
1076*67e74705SXin Li    CFRelease(value);
1077*67e74705SXin Li    CFDictionaryAddValue(y, val_key, (void*)value); // no-warning
1078*67e74705SXin Li  }
1079*67e74705SXin Li}
1080*67e74705SXin Li
1081*67e74705SXin Li// <rdar://problem/6560661>
1082*67e74705SXin Li// Same issue, except with "AppendValue" functions.
1083*67e74705SXin Livoid rdar_6560661(CFMutableArrayRef x) {
1084*67e74705SXin Li  signed z = 1;
1085*67e74705SXin Li  CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z);
1086*67e74705SXin Li  // CFArrayAppendValue keeps a reference to value.
1087*67e74705SXin Li  CFArrayAppendValue(x, value);
1088*67e74705SXin Li  CFRelease(value);
1089*67e74705SXin Li  CFRetain(value);
1090*67e74705SXin Li  CFRelease(value); // no-warning
1091*67e74705SXin Li}
1092*67e74705SXin Li
1093*67e74705SXin Li// <rdar://problem/7152619>
1094*67e74705SXin Li// Same issue, excwept with "CFAttributeStringSetAttribute".
1095*67e74705SXin Livoid rdar_7152619(CFStringRef str) {
1096*67e74705SXin Li  CFAttributedStringRef string = CFAttributedStringCreate(kCFAllocatorDefault, str, 0);
1097*67e74705SXin Li  CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutableCopy(kCFAllocatorDefault, 100, string);
1098*67e74705SXin Li  CFRelease(string);
1099*67e74705SXin Li  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}}
1100*67e74705SXin Li  CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 1), str, number);
1101*67e74705SXin Li  [number release];
1102*67e74705SXin Li  [number retain];
1103*67e74705SXin Li  CFRelease(attrString);
1104*67e74705SXin Li}
1105*67e74705SXin Li
1106*67e74705SXin Li//===----------------------------------------------------------------------===//
1107*67e74705SXin Li// Test of handling CGGradientXXX functions.
1108*67e74705SXin Li//===----------------------------------------------------------------------===//
1109*67e74705SXin Li
1110*67e74705SXin Livoid rdar_7184450(CGContextRef myContext, CGFloat x, CGPoint myStartPoint,
1111*67e74705SXin Li                  CGPoint myEndPoint) {
1112*67e74705SXin Li  size_t num_locations = 6;
1113*67e74705SXin Li  CGFloat locations[6] = { 0.0, 0.265, 0.28, 0.31, 0.36, 1.0 };
1114*67e74705SXin Li  CGFloat components[28] = { 239.0/256.0, 167.0/256.0, 170.0/256.0,
1115*67e74705SXin Li     x,  // Start color
1116*67e74705SXin Li    207.0/255.0, 39.0/255.0, 39.0/255.0, x,
1117*67e74705SXin Li    147.0/255.0, 21.0/255.0, 22.0/255.0, x,
1118*67e74705SXin Li    175.0/255.0, 175.0/255.0, 175.0/255.0, x,
1119*67e74705SXin Li    255.0/255.0,255.0/255.0, 255.0/255.0, x,
1120*67e74705SXin Li    255.0/255.0,255.0/255.0, 255.0/255.0, x
1121*67e74705SXin Li  }; // End color
1122*67e74705SXin Li
1123*67e74705SXin Li  CGGradientRef myGradient =
1124*67e74705SXin Li    CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), // expected-warning{{leak}}
1125*67e74705SXin Li      components, locations, num_locations);
1126*67e74705SXin Li
1127*67e74705SXin Li  CGContextDrawLinearGradient(myContext, myGradient, myStartPoint, myEndPoint,
1128*67e74705SXin Li                              0);
1129*67e74705SXin Li  CGGradientRelease(myGradient);
1130*67e74705SXin Li}
1131*67e74705SXin Li
1132*67e74705SXin Livoid rdar_7184450_pos(CGContextRef myContext, CGFloat x, CGPoint myStartPoint,
1133*67e74705SXin Li                  CGPoint myEndPoint) {
1134*67e74705SXin Li  size_t num_locations = 6;
1135*67e74705SXin Li  CGFloat locations[6] = { 0.0, 0.265, 0.28, 0.31, 0.36, 1.0 };
1136*67e74705SXin Li  CGFloat components[28] = { 239.0/256.0, 167.0/256.0, 170.0/256.0,
1137*67e74705SXin Li     x,  // Start color
1138*67e74705SXin Li    207.0/255.0, 39.0/255.0, 39.0/255.0, x,
1139*67e74705SXin Li    147.0/255.0, 21.0/255.0, 22.0/255.0, x,
1140*67e74705SXin Li    175.0/255.0, 175.0/255.0, 175.0/255.0, x,
1141*67e74705SXin Li    255.0/255.0,255.0/255.0, 255.0/255.0, x,
1142*67e74705SXin Li    255.0/255.0,255.0/255.0, 255.0/255.0, x
1143*67e74705SXin Li  }; // End color
1144*67e74705SXin Li
1145*67e74705SXin Li  CGGradientRef myGradient =
1146*67e74705SXin Li   CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), components, locations, num_locations); // expected-warning 2 {{leak}}
1147*67e74705SXin Li
1148*67e74705SXin Li  CGContextDrawLinearGradient(myContext, myGradient, myStartPoint, myEndPoint,
1149*67e74705SXin Li                              0);
1150*67e74705SXin Li}
1151*67e74705SXin Li
1152*67e74705SXin Li//===----------------------------------------------------------------------===//
1153*67e74705SXin Li// <rdar://problem/7299394> clang false positive: retained instance passed to
1154*67e74705SXin Li//                          thread in pthread_create marked as leak
1155*67e74705SXin Li//
1156*67e74705SXin Li// Until we have full IPA, the analyzer should stop tracking the reference
1157*67e74705SXin Li// count of objects passed to pthread_create.
1158*67e74705SXin Li//
1159*67e74705SXin Li//===----------------------------------------------------------------------===//
1160*67e74705SXin Li
1161*67e74705SXin Listruct _opaque_pthread_t {};
1162*67e74705SXin Listruct _opaque_pthread_attr_t {};
1163*67e74705SXin Litypedef struct _opaque_pthread_t *__darwin_pthread_t;
1164*67e74705SXin Litypedef struct _opaque_pthread_attr_t __darwin_pthread_attr_t;
1165*67e74705SXin Litypedef __darwin_pthread_t pthread_t;
1166*67e74705SXin Litypedef __darwin_pthread_attr_t pthread_attr_t;
1167*67e74705SXin Litypedef unsigned long __darwin_pthread_key_t;
1168*67e74705SXin Litypedef __darwin_pthread_key_t pthread_key_t;
1169*67e74705SXin Li
1170*67e74705SXin Liint pthread_create(pthread_t *, const pthread_attr_t *,
1171*67e74705SXin Li                   void *(*)(void *), void *);
1172*67e74705SXin Li
1173*67e74705SXin Liint pthread_setspecific(pthread_key_t key, const void *value);
1174*67e74705SXin Li
1175*67e74705SXin Livoid *rdar_7299394_start_routine(void *p) {
1176*67e74705SXin Li  [((id) p) release];
1177*67e74705SXin Li  return 0;
1178*67e74705SXin Li}
1179*67e74705SXin Livoid rdar_7299394(pthread_attr_t *attr, pthread_t *thread, void *args) {
1180*67e74705SXin Li  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1181*67e74705SXin Li  pthread_create(thread, attr, rdar_7299394_start_routine, number);
1182*67e74705SXin Li}
1183*67e74705SXin Livoid rdar_7299394_positive(pthread_attr_t *attr, pthread_t *thread) {
1184*67e74705SXin Li  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}}
1185*67e74705SXin Li}
1186*67e74705SXin Li
1187*67e74705SXin Li//===----------------------------------------------------------------------===//
1188*67e74705SXin Li// <rdar://problem/11282706> false positive with not understanding thread
1189*67e74705SXin Li// local storage
1190*67e74705SXin Li//===----------------------------------------------------------------------===//
1191*67e74705SXin Li
1192*67e74705SXin Livoid rdar11282706(pthread_key_t key) {
1193*67e74705SXin Li  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1194*67e74705SXin Li  pthread_setspecific(key, (void*) number);
1195*67e74705SXin Li}
1196*67e74705SXin Li
1197*67e74705SXin Li//===----------------------------------------------------------------------===//
1198*67e74705SXin Li// <rdar://problem/7283567> False leak associated with call to
1199*67e74705SXin Li//                          CVPixelBufferCreateWithBytes ()
1200*67e74705SXin Li//
1201*67e74705SXin Li// According to the Core Video Reference (ADC), CVPixelBufferCreateWithBytes and
1202*67e74705SXin Li// CVPixelBufferCreateWithPlanarBytes can release (via a callback) the
1203*67e74705SXin Li// pixel buffer object.  These test cases show how the analyzer stops tracking
1204*67e74705SXin Li// the reference count for the objects passed for this argument.  This
1205*67e74705SXin Li// could be made smarter.
1206*67e74705SXin Li//===----------------------------------------------------------------------===//
1207*67e74705SXin Li
1208*67e74705SXin Litypedef int int32_t;
1209*67e74705SXin Litypedef UInt32 FourCharCode;
1210*67e74705SXin Litypedef FourCharCode OSType;
1211*67e74705SXin Litypedef uint64_t CVOptionFlags;
1212*67e74705SXin Litypedef int32_t CVReturn;
1213*67e74705SXin Litypedef struct __CVBuffer *CVBufferRef;
1214*67e74705SXin Litypedef CVBufferRef CVImageBufferRef;
1215*67e74705SXin Litypedef CVImageBufferRef CVPixelBufferRef;
1216*67e74705SXin Litypedef void (*CVPixelBufferReleaseBytesCallback)( void *releaseRefCon, const void *baseAddress );
1217*67e74705SXin Li
1218*67e74705SXin Liextern CVReturn CVPixelBufferCreateWithBytes(CFAllocatorRef allocator,
1219*67e74705SXin Li            size_t width,
1220*67e74705SXin Li            size_t height,
1221*67e74705SXin Li            OSType pixelFormatType,
1222*67e74705SXin Li            void *baseAddress,
1223*67e74705SXin Li            size_t bytesPerRow,
1224*67e74705SXin Li            CVPixelBufferReleaseBytesCallback releaseCallback,
1225*67e74705SXin Li            void *releaseRefCon,
1226*67e74705SXin Li            CFDictionaryRef pixelBufferAttributes,
1227*67e74705SXin Li                   CVPixelBufferRef *pixelBufferOut) ;
1228*67e74705SXin Li
1229*67e74705SXin Litypedef void (*CVPixelBufferReleasePlanarBytesCallback)( void *releaseRefCon, const void *dataPtr, size_t dataSize, size_t numberOfPlanes, const void *planeAddresses[] );
1230*67e74705SXin Li
1231*67e74705SXin Liextern CVReturn CVPixelBufferCreateWithPlanarBytes(CFAllocatorRef allocator,
1232*67e74705SXin Li        size_t width,
1233*67e74705SXin Li        size_t height,
1234*67e74705SXin Li        OSType pixelFormatType,
1235*67e74705SXin Li        void *dataPtr,
1236*67e74705SXin Li        size_t dataSize,
1237*67e74705SXin Li        size_t numberOfPlanes,
1238*67e74705SXin Li        void *planeBaseAddress[],
1239*67e74705SXin Li        size_t planeWidth[],
1240*67e74705SXin Li        size_t planeHeight[],
1241*67e74705SXin Li        size_t planeBytesPerRow[],
1242*67e74705SXin Li        CVPixelBufferReleasePlanarBytesCallback releaseCallback,
1243*67e74705SXin Li        void *releaseRefCon,
1244*67e74705SXin Li        CFDictionaryRef pixelBufferAttributes,
1245*67e74705SXin Li        CVPixelBufferRef *pixelBufferOut) ;
1246*67e74705SXin Li
1247*67e74705SXin Liextern CVReturn CVPixelBufferCreateWithBytes(CFAllocatorRef allocator,
1248*67e74705SXin Li            size_t width,
1249*67e74705SXin Li            size_t height,
1250*67e74705SXin Li            OSType pixelFormatType,
1251*67e74705SXin Li            void *baseAddress,
1252*67e74705SXin Li            size_t bytesPerRow,
1253*67e74705SXin Li            CVPixelBufferReleaseBytesCallback releaseCallback,
1254*67e74705SXin Li            void *releaseRefCon,
1255*67e74705SXin Li            CFDictionaryRef pixelBufferAttributes,
1256*67e74705SXin Li                   CVPixelBufferRef *pixelBufferOut) ;
1257*67e74705SXin Li
1258*67e74705SXin LiCVReturn rdar_7283567(CFAllocatorRef allocator, size_t width, size_t height,
1259*67e74705SXin Li                      OSType pixelFormatType, void *baseAddress,
1260*67e74705SXin Li                      size_t bytesPerRow,
1261*67e74705SXin Li                      CVPixelBufferReleaseBytesCallback releaseCallback,
1262*67e74705SXin Li                      CFDictionaryRef pixelBufferAttributes,
1263*67e74705SXin Li                      CVPixelBufferRef *pixelBufferOut) {
1264*67e74705SXin Li
1265*67e74705SXin Li  // For the allocated object, it doesn't really matter what type it is
1266*67e74705SXin Li  // for the purpose of this test.  All we want to show is that
1267*67e74705SXin Li  // this is freed later by the callback.
1268*67e74705SXin Li  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1269*67e74705SXin Li
1270*67e74705SXin Li  return CVPixelBufferCreateWithBytes(allocator, width, height, pixelFormatType,
1271*67e74705SXin Li                                baseAddress, bytesPerRow, releaseCallback,
1272*67e74705SXin Li                                number, // potentially released by callback
1273*67e74705SXin Li                                pixelBufferAttributes, pixelBufferOut) ;
1274*67e74705SXin Li}
1275*67e74705SXin Li
1276*67e74705SXin LiCVReturn rdar_7283567_2(CFAllocatorRef allocator, size_t width, size_t height,
1277*67e74705SXin Li        OSType pixelFormatType, void *dataPtr, size_t dataSize,
1278*67e74705SXin Li        size_t numberOfPlanes, void *planeBaseAddress[],
1279*67e74705SXin Li        size_t planeWidth[], size_t planeHeight[], size_t planeBytesPerRow[],
1280*67e74705SXin Li        CVPixelBufferReleasePlanarBytesCallback releaseCallback,
1281*67e74705SXin Li        CFDictionaryRef pixelBufferAttributes,
1282*67e74705SXin Li        CVPixelBufferRef *pixelBufferOut) {
1283*67e74705SXin Li
1284*67e74705SXin Li    // For the allocated object, it doesn't really matter what type it is
1285*67e74705SXin Li    // for the purpose of this test.  All we want to show is that
1286*67e74705SXin Li    // this is freed later by the callback.
1287*67e74705SXin Li    NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1288*67e74705SXin Li
1289*67e74705SXin Li    return CVPixelBufferCreateWithPlanarBytes(allocator,
1290*67e74705SXin Li              width, height, pixelFormatType, dataPtr, dataSize,
1291*67e74705SXin Li              numberOfPlanes, planeBaseAddress, planeWidth,
1292*67e74705SXin Li              planeHeight, planeBytesPerRow, releaseCallback,
1293*67e74705SXin Li              number, // potentially released by callback
1294*67e74705SXin Li              pixelBufferAttributes, pixelBufferOut) ;
1295*67e74705SXin Li}
1296*67e74705SXin Li
1297*67e74705SXin Li//===----------------------------------------------------------------------===//
1298*67e74705SXin Li// <rdar://problem/7358899> False leak associated with
1299*67e74705SXin Li//  CGBitmapContextCreateWithData
1300*67e74705SXin Li//===----------------------------------------------------------------------===//
1301*67e74705SXin Litypedef uint32_t CGBitmapInfo;
1302*67e74705SXin Litypedef void (*CGBitmapContextReleaseDataCallback)(void *releaseInfo, void *data);
1303*67e74705SXin Li
1304*67e74705SXin LiCGContextRef CGBitmapContextCreateWithData(void *data,
1305*67e74705SXin Li    size_t width, size_t height, size_t bitsPerComponent,
1306*67e74705SXin Li    size_t bytesPerRow, CGColorSpaceRef space, CGBitmapInfo bitmapInfo,
1307*67e74705SXin Li    CGBitmapContextReleaseDataCallback releaseCallback, void *releaseInfo) CF_RETURNS_RETAINED;
1308*67e74705SXin Li
1309*67e74705SXin Livoid rdar_7358899(void *data,
1310*67e74705SXin Li      size_t width, size_t height, size_t bitsPerComponent,
1311*67e74705SXin Li      size_t bytesPerRow, CGColorSpaceRef space, CGBitmapInfo bitmapInfo,
1312*67e74705SXin Li      CGBitmapContextReleaseDataCallback releaseCallback) {
1313*67e74705SXin Li
1314*67e74705SXin Li    // For the allocated object, it doesn't really matter what type it is
1315*67e74705SXin Li    // for the purpose of this test.  All we want to show is that
1316*67e74705SXin Li    // this is freed later by the callback.
1317*67e74705SXin Li    NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1318*67e74705SXin Li
1319*67e74705SXin Li  CGBitmapContextCreateWithData(data, width, height, bitsPerComponent, // expected-warning{{leak}}
1320*67e74705SXin Li    bytesPerRow, space, bitmapInfo, releaseCallback, number);
1321*67e74705SXin Li}
1322*67e74705SXin Li
1323*67e74705SXin Li//===----------------------------------------------------------------------===//
1324*67e74705SXin Li// <rdar://problem/7265711> allow 'new', 'copy', 'alloc', 'init' prefix to
1325*67e74705SXin Li//  start before '_' when determining Cocoa fundamental rule
1326*67e74705SXin Li//
1327*67e74705SXin Li// Previously the retain/release checker just skipped prefixes before the
1328*67e74705SXin Li// first '_' entirely.  Now the checker honors the prefix if it results in a
1329*67e74705SXin Li// recognizable naming convention (e.g., 'new', 'init').
1330*67e74705SXin Li//===----------------------------------------------------------------------===//
1331*67e74705SXin Li
1332*67e74705SXin Li@interface RDar7265711 {}
1333*67e74705SXin Li- (id) new_stuff;
1334*67e74705SXin Li@end
1335*67e74705SXin Li
1336*67e74705SXin Livoid rdar7265711_a(RDar7265711 *x) {
1337*67e74705SXin Li  id y = [x new_stuff]; // expected-warning{{leak}}
1338*67e74705SXin Li}
1339*67e74705SXin Li
1340*67e74705SXin Livoid rdar7265711_b(RDar7265711 *x) {
1341*67e74705SXin Li  id y = [x new_stuff]; // no-warning
1342*67e74705SXin Li  [y release];
1343*67e74705SXin Li}
1344*67e74705SXin Li
1345*67e74705SXin Li//===----------------------------------------------------------------------===//
1346*67e74705SXin Li// <rdar://problem/7306898> clang thinks [NSCursor dragCopyCursor] returns a
1347*67e74705SXin Li//                          retained reference
1348*67e74705SXin Li//===----------------------------------------------------------------------===//
1349*67e74705SXin Li
1350*67e74705SXin Li@interface NSCursor : NSObject
1351*67e74705SXin Li+ (NSCursor *)dragCopyCursor;
1352*67e74705SXin Li@end
1353*67e74705SXin Li
1354*67e74705SXin Livoid rdar7306898(void) {
1355*67e74705SXin Li  // 'dragCopyCursor' does not follow Cocoa's fundamental rule.  It is a noun, not an sentence
1356*67e74705SXin Li  // implying a 'copy' of something.
1357*67e74705SXin Li  NSCursor *c =  [NSCursor dragCopyCursor]; // no-warning
1358*67e74705SXin Li  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}}
1359*67e74705SXin Li}
1360*67e74705SXin Li
1361*67e74705SXin Li//===----------------------------------------------------------------------===//
1362*67e74705SXin Li// <rdar://problem/7252064> sending 'release', 'retain', etc. to a Class
1363*67e74705SXin Li// directly is not likely what the user intended
1364*67e74705SXin Li//===----------------------------------------------------------------------===//
1365*67e74705SXin Li
1366*67e74705SXin Li@interface RDar7252064 : NSObject @end
1367*67e74705SXin Livoid rdar7252064(void) {
1368*67e74705SXin Li  [RDar7252064 release]; // expected-warning{{The 'release' message should be sent to instances of class 'RDar7252064' and not the class directly}}
1369*67e74705SXin Li  [RDar7252064 retain]; // expected-warning{{The 'retain' message should be sent to instances of class 'RDar7252064' and not the class directly}}
1370*67e74705SXin Li  [RDar7252064 autorelease]; // expected-warning{{The 'autorelease' message should be sent to instances of class 'RDar7252064' and not the class directly}}
1371*67e74705SXin Li  [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}}
1372*67e74705SXin Li}
1373*67e74705SXin Li
1374*67e74705SXin Li//===----------------------------------------------------------------------===//
1375*67e74705SXin Li// Tests of ownership attributes.
1376*67e74705SXin Li//===----------------------------------------------------------------------===//
1377*67e74705SXin Li
1378*67e74705SXin Litypedef NSString* MyStringTy;
1379*67e74705SXin Li
1380*67e74705SXin Li@protocol FooP;
1381*67e74705SXin Li
1382*67e74705SXin Li@interface TestOwnershipAttr : NSObject
1383*67e74705SXin Li- (NSString*) returnsAnOwnedString  NS_RETURNS_RETAINED; // no-warning
1384*67e74705SXin Li- (NSString*) returnsAnOwnedCFString  CF_RETURNS_RETAINED; // no-warning
1385*67e74705SXin Li- (MyStringTy) returnsAnOwnedTypedString NS_RETURNS_RETAINED; // no-warning
1386*67e74705SXin Li- (NSString*) newString NS_RETURNS_NOT_RETAINED; // no-warning
1387*67e74705SXin Li- (NSString*) newString_auto NS_RETURNS_AUTORELEASED; // no-warning
1388*67e74705SXin Li- (NSString*) newStringNoAttr;
1389*67e74705SXin Li- (int) returnsAnOwnedInt NS_RETURNS_RETAINED; // expected-warning{{'ns_returns_retained' attribute only applies to methods that return an Objective-C object}}
1390*67e74705SXin Li- (id) pseudoInit NS_CONSUMES_SELF NS_RETURNS_RETAINED;
1391*67e74705SXin Li+ (void) consume:(id) NS_CONSUMED x;
1392*67e74705SXin Li+ (void) consume2:(id) CF_CONSUMED x;
1393*67e74705SXin Li@end
1394*67e74705SXin Li
1395*67e74705SXin Listatic int ownership_attribute_doesnt_go_here NS_RETURNS_RETAINED; // expected-warning{{'ns_returns_retained' attribute only applies to functions and methods}}
1396*67e74705SXin Li
1397*67e74705SXin Livoid test_attr_1(TestOwnershipAttr *X) {
1398*67e74705SXin Li  NSString *str = [X returnsAnOwnedString]; // expected-warning{{leak}}
1399*67e74705SXin Li}
1400*67e74705SXin Li
1401*67e74705SXin Livoid test_attr_1b(TestOwnershipAttr *X) {
1402*67e74705SXin Li  NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}}
1403*67e74705SXin Li}
1404*67e74705SXin Li
1405*67e74705SXin Livoid test_attr1c(TestOwnershipAttr *X) {
1406*67e74705SXin Li  NSString *str = [X newString]; // no-warning
1407*67e74705SXin Li  NSString *str2 = [X newStringNoAttr]; // expected-warning{{leak}}
1408*67e74705SXin Li  NSString *str3 = [X newString_auto]; // no-warning
1409*67e74705SXin Li  NSString *str4 = [[X newString_auto] retain]; // expected-warning {{leak}}
1410*67e74705SXin Li}
1411*67e74705SXin Li
1412*67e74705SXin Livoid testattr2_a() {
1413*67e74705SXin Li  TestOwnershipAttr *x = [TestOwnershipAttr alloc]; // expected-warning{{leak}}
1414*67e74705SXin Li}
1415*67e74705SXin Li
1416*67e74705SXin Livoid testattr2_b() {
1417*67e74705SXin Li  TestOwnershipAttr *x = [[TestOwnershipAttr alloc] pseudoInit];  // expected-warning{{leak}}
1418*67e74705SXin Li}
1419*67e74705SXin Li
1420*67e74705SXin Livoid testattr2_b_11358224_self_assign_looses_the_leak() {
1421*67e74705SXin Li  TestOwnershipAttr *x = [[TestOwnershipAttr alloc] pseudoInit];// expected-warning{{leak}}
1422*67e74705SXin Li  x = x;
1423*67e74705SXin Li}
1424*67e74705SXin Li
1425*67e74705SXin Livoid testattr2_c() {
1426*67e74705SXin Li  TestOwnershipAttr *x = [[TestOwnershipAttr alloc] pseudoInit]; // no-warning
1427*67e74705SXin Li  [x release];
1428*67e74705SXin Li}
1429*67e74705SXin Li
1430*67e74705SXin Livoid testattr3() {
1431*67e74705SXin Li  TestOwnershipAttr *x = [TestOwnershipAttr alloc]; // no-warning
1432*67e74705SXin Li  [TestOwnershipAttr consume:x];
1433*67e74705SXin Li  TestOwnershipAttr *y = [TestOwnershipAttr alloc]; // no-warning
1434*67e74705SXin Li  [TestOwnershipAttr consume2:y];
1435*67e74705SXin Li}
1436*67e74705SXin Li
1437*67e74705SXin Livoid consume_ns(id NS_CONSUMED x);
1438*67e74705SXin Livoid consume_cf(id CF_CONSUMED x);
1439*67e74705SXin Li
1440*67e74705SXin Livoid testattr4() {
1441*67e74705SXin Li  TestOwnershipAttr *x = [TestOwnershipAttr alloc]; // no-warning
1442*67e74705SXin Li  consume_ns(x);
1443*67e74705SXin Li  TestOwnershipAttr *y = [TestOwnershipAttr alloc]; // no-warning
1444*67e74705SXin Li  consume_cf(y);
1445*67e74705SXin Li}
1446*67e74705SXin Li
1447*67e74705SXin Li@interface TestOwnershipAttr2 : NSObject
1448*67e74705SXin Li- (NSString*) newString NS_RETURNS_NOT_RETAINED; // no-warning
1449*67e74705SXin Li@end
1450*67e74705SXin Li
1451*67e74705SXin Li@implementation TestOwnershipAttr2
1452*67e74705SXin Li- (NSString*) newString {
1453*67e74705SXin Li  return [NSString alloc]; // expected-warning {{Potential leak of an object}}
1454*67e74705SXin Li}
1455*67e74705SXin Li@end
1456*67e74705SXin Li
1457*67e74705SXin Li@interface MyClassTestCFAttr : NSObject {}
1458*67e74705SXin Li- (NSDate*) returnsCFRetained CF_RETURNS_RETAINED;
1459*67e74705SXin Li- (CFDateRef) returnsCFRetainedAsCF CF_RETURNS_RETAINED;
1460*67e74705SXin Li- (CFDateRef) newCFRetainedAsCF CF_RETURNS_NOT_RETAINED;
1461*67e74705SXin Li- (CFDateRef) newCFRetainedAsCFNoAttr CF_RETURNS_RETAINED;
1462*67e74705SXin Li- (NSDate*) alsoReturnsRetained;
1463*67e74705SXin Li- (CFDateRef) alsoReturnsRetainedAsCF CF_RETURNS_NOT_RETAINED;
1464*67e74705SXin Li- (NSDate*) returnsNSRetained NS_RETURNS_RETAINED;
1465*67e74705SXin Li@end
1466*67e74705SXin Li
1467*67e74705SXin LiCF_RETURNS_RETAINED
1468*67e74705SXin LiCFDateRef returnsRetainedCFDate()  {
1469*67e74705SXin Li  return CFDateCreate(0, CFAbsoluteTimeGetCurrent());
1470*67e74705SXin Li}
1471*67e74705SXin Li
1472*67e74705SXin Li@implementation MyClassTestCFAttr
1473*67e74705SXin Li- (NSDate*) returnsCFRetained {
1474*67e74705SXin Li  return (NSDate*) returnsRetainedCFDate(); // No leak.
1475*67e74705SXin Li}
1476*67e74705SXin Li
1477*67e74705SXin Li- (CFDateRef) returnsCFRetainedAsCF {
1478*67e74705SXin Li  return returnsRetainedCFDate(); // No leak.
1479*67e74705SXin Li}
1480*67e74705SXin Li
1481*67e74705SXin Li- (CFDateRef) newCFRetainedAsCF {
1482*67e74705SXin Li  return (CFDateRef)[(id)[self returnsCFRetainedAsCF] autorelease];
1483*67e74705SXin Li}
1484*67e74705SXin Li
1485*67e74705SXin Li- (CFDateRef) newCFRetainedAsCFNoAttr {
1486*67e74705SXin Li  return (CFDateRef)[(id)[self returnsCFRetainedAsCF] autorelease]; // expected-warning{{Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected}}
1487*67e74705SXin Li}
1488*67e74705SXin Li
1489*67e74705SXin Li- (NSDate*) alsoReturnsRetained {
1490*67e74705SXin Li  return (NSDate*) returnsRetainedCFDate(); // expected-warning{{leak}}
1491*67e74705SXin Li}
1492*67e74705SXin Li
1493*67e74705SXin Li- (CFDateRef) alsoReturnsRetainedAsCF {
1494*67e74705SXin Li  return returnsRetainedCFDate(); // expected-warning{{leak}}
1495*67e74705SXin Li}
1496*67e74705SXin Li
1497*67e74705SXin Li
1498*67e74705SXin Li- (NSDate*) returnsNSRetained {
1499*67e74705SXin Li  return (NSDate*) returnsRetainedCFDate(); // no-warning
1500*67e74705SXin Li}
1501*67e74705SXin Li@end
1502*67e74705SXin Li
1503*67e74705SXin Li//===----------------------------------------------------------------------===//
1504*67e74705SXin Li// Test that leaks post-dominated by "panic" functions are not reported.
1505*67e74705SXin Li//
1506*67e74705SXin Li// <rdar://problem/5905851> do not report a leak when post-dominated by a call
1507*67e74705SXin Li// to a noreturn or panic function
1508*67e74705SXin Li//===----------------------------------------------------------------------===//
1509*67e74705SXin Li
1510*67e74705SXin Livoid panic() __attribute__((noreturn));
1511*67e74705SXin Livoid panic_not_in_hardcoded_list() __attribute__((noreturn));
1512*67e74705SXin Li
1513*67e74705SXin Livoid test_panic_negative() {
1514*67e74705SXin Li  signed z = 1;
1515*67e74705SXin Li  CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z);  // expected-warning{{leak}}
1516*67e74705SXin Li}
1517*67e74705SXin Li
1518*67e74705SXin Livoid test_panic_positive() {
1519*67e74705SXin Li  signed z = 1;
1520*67e74705SXin Li  CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // no-warning
1521*67e74705SXin Li  panic();
1522*67e74705SXin Li}
1523*67e74705SXin Li
1524*67e74705SXin Livoid test_panic_neg_2(int x) {
1525*67e74705SXin Li  signed z = 1;
1526*67e74705SXin Li  CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // expected-warning{{leak}}
1527*67e74705SXin Li  if (x)
1528*67e74705SXin Li    panic();
1529*67e74705SXin Li}
1530*67e74705SXin Li
1531*67e74705SXin Livoid test_panic_pos_2(int x) {
1532*67e74705SXin Li  signed z = 1;
1533*67e74705SXin Li  CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &z); // no-warning
1534*67e74705SXin Li  if (x)
1535*67e74705SXin Li    panic();
1536*67e74705SXin Li  if (!x) {
1537*67e74705SXin Li    // This showed up in <rdar://problem/7796563>, where we silently missed checking
1538*67e74705SXin Li    // the function type for noreturn.  "panic()" is a hard-coded known panic function
1539*67e74705SXin Li    // that isn't always noreturn.
1540*67e74705SXin Li    panic_not_in_hardcoded_list();
1541*67e74705SXin Li  }
1542*67e74705SXin Li}
1543*67e74705SXin Li
1544*67e74705SXin Li//===----------------------------------------------------------------------===//
1545*67e74705SXin Li// Test uses of blocks (closures)
1546*67e74705SXin Li//===----------------------------------------------------------------------===//
1547*67e74705SXin Li
1548*67e74705SXin Livoid test_blocks_1_pos(void) {
1549*67e74705SXin Li  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning{{leak}}
1550*67e74705SXin Li  ^{}();
1551*67e74705SXin Li}
1552*67e74705SXin Li
1553*67e74705SXin Livoid test_blocks_1_indirect_release(void) {
1554*67e74705SXin Li  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1555*67e74705SXin Li  ^{ [number release]; }();
1556*67e74705SXin Li}
1557*67e74705SXin Li
1558*67e74705SXin Livoid test_blocks_1_indirect_retain(void) {
1559*67e74705SXin Li  // Eventually this should be reported as a leak.
1560*67e74705SXin Li  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1561*67e74705SXin Li  ^{ [number retain]; }();
1562*67e74705SXin Li}
1563*67e74705SXin Li
1564*67e74705SXin Livoid test_blocks_1_indirect_release_via_call(void) {
1565*67e74705SXin Li  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // no-warning
1566*67e74705SXin Li  ^(NSObject *o){ [o release]; }(number);
1567*67e74705SXin Li}
1568*67e74705SXin Li
1569*67e74705SXin Livoid test_blocks_1_indirect_retain_via_call(void) {
1570*67e74705SXin Li  NSNumber *number = [[NSNumber alloc] initWithInt:5]; // expected-warning {{leak}}
1571*67e74705SXin Li  ^(NSObject *o){ [o retain]; }(number);
1572*67e74705SXin Li}
1573*67e74705SXin Li
1574*67e74705SXin Li//===--------------------------------------------------------------------===//
1575*67e74705SXin Li// Test sending message to super that returns an object alias.  Previously
1576*67e74705SXin Li// this caused a crash in the analyzer.
1577*67e74705SXin Li//===--------------------------------------------------------------------===//
1578*67e74705SXin Li
1579*67e74705SXin Li@interface Rdar8015556 : NSObject {} @end
1580*67e74705SXin Li@implementation Rdar8015556
1581*67e74705SXin Li- (id)retain {
1582*67e74705SXin Li  return [super retain];
1583*67e74705SXin Li}
1584*67e74705SXin Li@end
1585*67e74705SXin Li
1586*67e74705SXin Li// <rdar://problem/8272168> - Correcly handle Class<...> in Cocoa Conventions
1587*67e74705SXin Li// detector.
1588*67e74705SXin Li
1589*67e74705SXin Li@protocol Prot_R8272168 @end
1590*67e74705SXin LiClass <Prot_R8272168> GetAClassThatImplementsProt_R8272168();
1591*67e74705SXin Livoid r8272168() {
1592*67e74705SXin Li  GetAClassThatImplementsProt_R8272168();
1593*67e74705SXin Li}
1594*67e74705SXin Li
1595*67e74705SXin Li// Test case for <rdar://problem/8356342>, which in the past triggered
1596*67e74705SXin Li// a false positive.
1597*67e74705SXin Li@interface RDar8356342
1598*67e74705SXin Li- (NSDate*) rdar8356342:(NSDate *)inValue;
1599*67e74705SXin Li@end
1600*67e74705SXin Li
1601*67e74705SXin Li@implementation RDar8356342
1602*67e74705SXin Li- (NSDate*) rdar8356342:(NSDate*)inValue {
1603*67e74705SXin Li  NSDate *outValue = inValue;
1604*67e74705SXin Li  if (outValue == 0)
1605*67e74705SXin Li    outValue = [[NSDate alloc] init]; // no-warning
1606*67e74705SXin Li
1607*67e74705SXin Li  if (outValue != inValue)
1608*67e74705SXin Li    [outValue autorelease];
1609*67e74705SXin Li
1610*67e74705SXin Li  return outValue;
1611*67e74705SXin Li}
1612*67e74705SXin Li@end
1613*67e74705SXin Li
1614*67e74705SXin Li// <rdar://problem/8724287> - This test case previously crashed because
1615*67e74705SXin Li// of a bug in BugReporter.
1616*67e74705SXin Liextern const void *CFDictionaryGetValue(CFDictionaryRef theDict, const void *key) CF_RETURNS_NOT_RETAINED;
1617*67e74705SXin Litypedef struct __CFError * CFErrorRef;
1618*67e74705SXin Liextern const CFStringRef kCFErrorUnderlyingErrorKey;
1619*67e74705SXin Liextern CFDictionaryRef CFErrorCopyUserInfo(CFErrorRef err) CF_RETURNS_RETAINED;
1620*67e74705SXin Listatic void rdar_8724287(CFErrorRef error)
1621*67e74705SXin Li{
1622*67e74705SXin Li    CFErrorRef error_to_dump;
1623*67e74705SXin Li
1624*67e74705SXin Li    error_to_dump = error;
1625*67e74705SXin Li    while (error_to_dump != ((void*)0)) {
1626*67e74705SXin Li        CFDictionaryRef info;
1627*67e74705SXin Li
1628*67e74705SXin Li        info = CFErrorCopyUserInfo(error_to_dump); // expected-warning{{Potential leak of an object}}
1629*67e74705SXin Li
1630*67e74705SXin Li        if (info != ((void*)0)) {
1631*67e74705SXin Li        }
1632*67e74705SXin Li
1633*67e74705SXin Li        error_to_dump = (CFErrorRef) CFDictionaryGetValue(info, kCFErrorUnderlyingErrorKey);
1634*67e74705SXin Li    }
1635*67e74705SXin Li}
1636*67e74705SXin Li
1637*67e74705SXin Li// <rdar://problem/9234108> - Make sure the model applies cf_consumed
1638*67e74705SXin Li// correctly in argument positions besides the first.
1639*67e74705SXin Li
1640*67e74705SXin LiCF_IMPLICIT_BRIDGING_ENABLED
1641*67e74705SXin Li
1642*67e74705SXin Liextern void *CFStringCreate(void);
1643*67e74705SXin Li
1644*67e74705SXin LiCF_IMPLICIT_BRIDGING_DISABLED
1645*67e74705SXin Li
1646*67e74705SXin Liextern void rdar_9234108_helper(void *key, void * CF_CONSUMED value);
1647*67e74705SXin Livoid rdar_9234108() {
1648*67e74705SXin Li  rdar_9234108_helper(0, CFStringCreate());
1649*67e74705SXin Li}
1650*67e74705SXin Li
1651*67e74705SXin Li// <rdar://problem/9726279> - Make sure that objc_method_family works
1652*67e74705SXin Li// to override naming conventions.
1653*67e74705SXin Listruct TwoDoubles {
1654*67e74705SXin Li  double one;
1655*67e74705SXin Li  double two;
1656*67e74705SXin Li};
1657*67e74705SXin Litypedef struct TwoDoubles TwoDoubles;
1658*67e74705SXin Li
1659*67e74705SXin Li@interface NSValue (Mine)
1660*67e74705SXin Li- (id)_prefix_initWithTwoDoubles:(TwoDoubles)twoDoubles __attribute__((objc_method_family(init)));
1661*67e74705SXin Li@end
1662*67e74705SXin Li
1663*67e74705SXin Li@implementation NSValue (Mine)
1664*67e74705SXin Li- (id)_prefix_initWithTwoDoubles:(TwoDoubles)twoDoubles
1665*67e74705SXin Li{
1666*67e74705SXin Li  return [self init];
1667*67e74705SXin Li}
1668*67e74705SXin Li@end
1669*67e74705SXin Li
1670*67e74705SXin Livoid rdar9726279() {
1671*67e74705SXin Li  TwoDoubles twoDoubles = { 0.0, 0.0 };
1672*67e74705SXin Li  NSValue *value = [[NSValue alloc] _prefix_initWithTwoDoubles:twoDoubles];
1673*67e74705SXin Li  [value release];
1674*67e74705SXin Li}
1675*67e74705SXin Li
1676*67e74705SXin Li// <rdar://problem/9732321>
1677*67e74705SXin Li// Test camelcase support for CF conventions.  While Core Foundation APIs
1678*67e74705SXin Li// don't use camel casing, other code is allowed to use it.
1679*67e74705SXin LiCFArrayRef camelcase_create_1() {
1680*67e74705SXin Li  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
1681*67e74705SXin Li}
1682*67e74705SXin Li
1683*67e74705SXin LiCFArrayRef camelcase_createno() {
1684*67e74705SXin Li  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning {{leak}}
1685*67e74705SXin Li}
1686*67e74705SXin Li
1687*67e74705SXin LiCFArrayRef camelcase_copy() {
1688*67e74705SXin Li  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
1689*67e74705SXin Li}
1690*67e74705SXin Li
1691*67e74705SXin LiCFArrayRef camelcase_copying() {
1692*67e74705SXin Li  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning {{leak}}
1693*67e74705SXin Li}
1694*67e74705SXin Li
1695*67e74705SXin LiCFArrayRef copyCamelCase() {
1696*67e74705SXin Li  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
1697*67e74705SXin Li}
1698*67e74705SXin Li
1699*67e74705SXin LiCFArrayRef __copyCamelCase() {
1700*67e74705SXin Li  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
1701*67e74705SXin Li}
1702*67e74705SXin Li
1703*67e74705SXin LiCFArrayRef __createCamelCase() {
1704*67e74705SXin Li  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
1705*67e74705SXin Li}
1706*67e74705SXin Li
1707*67e74705SXin LiCFArrayRef camel_create() {
1708*67e74705SXin Li  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
1709*67e74705SXin Li}
1710*67e74705SXin Li
1711*67e74705SXin Li
1712*67e74705SXin LiCFArrayRef camel_creat() {
1713*67e74705SXin Li  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning {{leak}}
1714*67e74705SXin Li}
1715*67e74705SXin Li
1716*67e74705SXin LiCFArrayRef camel_copy() {
1717*67e74705SXin Li  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
1718*67e74705SXin Li}
1719*67e74705SXin Li
1720*67e74705SXin LiCFArrayRef camel_copyMachine() {
1721*67e74705SXin Li  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
1722*67e74705SXin Li}
1723*67e74705SXin Li
1724*67e74705SXin LiCFArrayRef camel_copymachine() {
1725*67e74705SXin Li  return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning {{leak}}
1726*67e74705SXin Li}
1727*67e74705SXin Li
1728*67e74705SXin Li// rdar://problem/8024350
1729*67e74705SXin Li@protocol F18P
1730*67e74705SXin Li- (id) clone;
1731*67e74705SXin Li@end
1732*67e74705SXin Li@interface F18 : NSObject<F18P> @end
1733*67e74705SXin Li@interface F18(Cat)
1734*67e74705SXin Li- (id) clone NS_RETURNS_RETAINED;
1735*67e74705SXin Li@end
1736*67e74705SXin Li
1737*67e74705SXin Li@implementation F18
1738*67e74705SXin Li- (id) clone {
1739*67e74705SXin Li  return [F18 alloc];
1740*67e74705SXin Li}
1741*67e74705SXin Li@end
1742*67e74705SXin Li
1743*67e74705SXin Li// Radar 6582778.
1744*67e74705SXin Livoid rdar6582778(void) {
1745*67e74705SXin Li  CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
1746*67e74705SXin Li  CFTypeRef vals[] = { CFDateCreate(0, t) }; // expected-warning {{leak}}
1747*67e74705SXin Li}
1748*67e74705SXin Li
1749*67e74705SXin LiCFTypeRef global;
1750*67e74705SXin Li
1751*67e74705SXin Livoid rdar6582778_2(void) {
1752*67e74705SXin Li  CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
1753*67e74705SXin Li  global = CFDateCreate(0, t); // no-warning
1754*67e74705SXin Li}
1755*67e74705SXin Li
1756*67e74705SXin Li// <rdar://problem/10232019> - Test that objects passed to containers
1757*67e74705SXin Li// are marked "escaped".
1758*67e74705SXin Li
1759*67e74705SXin Livoid rdar10232019() {
1760*67e74705SXin Li  NSMutableArray *array = [NSMutableArray array];
1761*67e74705SXin Li
1762*67e74705SXin Li  NSString *string = [[NSString alloc] initWithUTF8String:"foo"];
1763*67e74705SXin Li  [array addObject:string];
1764*67e74705SXin Li  [string release];
1765*67e74705SXin Li
1766*67e74705SXin Li  NSString *otherString = [string stringByAppendingString:@"bar"]; // no-warning
1767*67e74705SXin Li  NSLog(@"%@", otherString);
1768*67e74705SXin Li}
1769*67e74705SXin Li
1770*67e74705SXin Livoid rdar10232019_positive() {
1771*67e74705SXin Li  NSMutableArray *array = [NSMutableArray array];
1772*67e74705SXin Li
1773*67e74705SXin Li  NSString *string = [[NSString alloc] initWithUTF8String:"foo"];
1774*67e74705SXin Li  [string release];
1775*67e74705SXin Li
1776*67e74705SXin Li  NSString *otherString = [string stringByAppendingString:@"bar"]; // expected-warning {{Reference-counted object is used after it is release}}
1777*67e74705SXin Li  NSLog(@"%@", otherString);
1778*67e74705SXin Li}
1779*67e74705SXin Li
1780*67e74705SXin Li// RetainCountChecker support for XPC.
1781*67e74705SXin Li// <rdar://problem/9658496>
1782*67e74705SXin Litypedef void * xpc_object_t;
1783*67e74705SXin Lixpc_object_t _CFXPCCreateXPCObjectFromCFObject(CFTypeRef cf);
1784*67e74705SXin Livoid xpc_release(xpc_object_t object);
1785*67e74705SXin Li
1786*67e74705SXin Livoid rdar9658496() {
1787*67e74705SXin Li  CFStringRef cf;
1788*67e74705SXin Li  xpc_object_t xpc;
1789*67e74705SXin Li  cf = CFStringCreateWithCString( ((CFAllocatorRef)0), "test", kCFStringEncodingUTF8 ); // no-warning
1790*67e74705SXin Li  xpc = _CFXPCCreateXPCObjectFromCFObject( cf );
1791*67e74705SXin Li  CFRelease(cf);
1792*67e74705SXin Li  xpc_release(xpc);
1793*67e74705SXin Li}
1794*67e74705SXin Li
1795*67e74705SXin Li// Support annotations with method families.
1796*67e74705SXin Li@interface RDar10824732 : NSObject
1797*67e74705SXin Li- (instancetype)initWithObj:(id CF_CONSUMED)obj;
1798*67e74705SXin Li@end
1799*67e74705SXin Li
1800*67e74705SXin Li@implementation RDar10824732
1801*67e74705SXin Li- (instancetype)initWithObj:(id)obj {
1802*67e74705SXin Li  [obj release];
1803*67e74705SXin Li  return [super init];
1804*67e74705SXin Li}
1805*67e74705SXin Li@end
1806*67e74705SXin Li
1807*67e74705SXin Livoid rdar_10824732() {
1808*67e74705SXin Li  @autoreleasepool {
1809*67e74705SXin Li    NSString *obj = @"test";
1810*67e74705SXin Li    RDar10824732 *foo = [[RDar10824732 alloc] initWithObj:obj]; // no-warning
1811*67e74705SXin Li    [foo release];
1812*67e74705SXin Li  }
1813*67e74705SXin Li}
1814*67e74705SXin Li
1815*67e74705SXin Li// Stop tracking objects passed to functions, which take callbacks as parameters.
1816*67e74705SXin Li// radar://10973977
1817*67e74705SXin Litypedef int (*CloseCallback) (void *);
1818*67e74705SXin Livoid ReaderForIO(CloseCallback ioclose, void *ioctx);
1819*67e74705SXin Liint IOClose(void *context);
1820*67e74705SXin Li
1821*67e74705SXin Li@protocol SInS <NSObject>
1822*67e74705SXin Li@end
1823*67e74705SXin Li
1824*67e74705SXin Li@interface radar10973977 : NSObject
1825*67e74705SXin Li- (id<SInS>)inputS;
1826*67e74705SXin Li- (void)reader;
1827*67e74705SXin Li@end
1828*67e74705SXin Li
1829*67e74705SXin Li@implementation radar10973977
1830*67e74705SXin Li- (void)reader
1831*67e74705SXin Li{
1832*67e74705SXin Li    id<SInS> inputS = [[self inputS] retain];
1833*67e74705SXin Li    ReaderForIO(IOClose, inputS);
1834*67e74705SXin Li}
1835*67e74705SXin Li- (id<SInS>)inputS
1836*67e74705SXin Li{
1837*67e74705SXin Li    return 0;
1838*67e74705SXin Li}
1839*67e74705SXin Li@end
1840*67e74705SXin Li
1841*67e74705SXin Li// Object escapes through a selector callback: radar://11398514
1842*67e74705SXin Liextern id NSApp;
1843*67e74705SXin Li@interface MySheetController
1844*67e74705SXin Li- (id<SInS>)inputS;
1845*67e74705SXin Li- (void)showDoSomethingSheetAction:(id)action;
1846*67e74705SXin Li- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo;
1847*67e74705SXin Li@end
1848*67e74705SXin Li
1849*67e74705SXin Li@implementation MySheetController
1850*67e74705SXin Li- (id<SInS>)inputS {
1851*67e74705SXin Li    return 0;
1852*67e74705SXin Li}
1853*67e74705SXin Li- (void)showDoSomethingSheetAction:(id)action {
1854*67e74705SXin Li  id<SInS> inputS = [[self inputS] retain];
1855*67e74705SXin Li  [NSApp beginSheet:0
1856*67e74705SXin Li         modalForWindow:0
1857*67e74705SXin Li         modalDelegate:0
1858*67e74705SXin Li         didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
1859*67e74705SXin Li         contextInfo:(void *)inputS]; // no - warning
1860*67e74705SXin Li}
1861*67e74705SXin Li- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo {
1862*67e74705SXin Li
1863*67e74705SXin Li      id contextObject = (id)contextInfo;
1864*67e74705SXin Li      [contextObject release];
1865*67e74705SXin Li}
1866*67e74705SXin Li
1867*67e74705SXin Li- (id)copyAutoreleaseRadar13081402 {
1868*67e74705SXin Li  id x = [[[NSString alloc] initWithUTF8String:"foo"] autorelease];
1869*67e74705SXin Li  [x retain];
1870*67e74705SXin Li  return x; // no warning
1871*67e74705SXin Li}
1872*67e74705SXin Li
1873*67e74705SXin Li@end
1874*67e74705SXin Li//===----------------------------------------------------------------------===//
1875*67e74705SXin Li// Test returning allocated memory in a struct.
1876*67e74705SXin Li//
1877*67e74705SXin Li// We currently don't have a general way to track pointers that "escape".
1878*67e74705SXin Li// Here we test that RetainCountChecker doesn't get excited about returning
1879*67e74705SXin Li// allocated CF objects in struct fields.
1880*67e74705SXin Li//===----------------------------------------------------------------------===//
1881*67e74705SXin Livoid *malloc(size_t);
1882*67e74705SXin Listruct rdar11104566 { CFStringRef myStr; };
1883*67e74705SXin Listruct rdar11104566 test_rdar11104566() {
1884*67e74705SXin Li  CFStringRef cf = CFStringCreateWithCString( ((CFAllocatorRef)0), "test", kCFStringEncodingUTF8 ); // no-warning
1885*67e74705SXin Li  struct rdar11104566 V;
1886*67e74705SXin Li  V.myStr = cf;
1887*67e74705SXin Li  return V; // no-warning
1888*67e74705SXin Li}
1889*67e74705SXin Li
1890*67e74705SXin Listruct rdar11104566 *test_2_rdar11104566() {
1891*67e74705SXin Li  CFStringRef cf = CFStringCreateWithCString( ((CFAllocatorRef)0), "test", kCFStringEncodingUTF8 ); // no-warning
1892*67e74705SXin Li  struct rdar11104566 *V = (struct rdar11104566 *) malloc(sizeof(*V));
1893*67e74705SXin Li  V->myStr = cf;
1894*67e74705SXin Li  return V; // no-warning
1895*67e74705SXin Li}
1896*67e74705SXin Li
1897*67e74705SXin Li//===----------------------------------------------------------------------===//
1898*67e74705SXin Li// ObjC literals support.
1899*67e74705SXin Li//===----------------------------------------------------------------------===//
1900*67e74705SXin Li
1901*67e74705SXin Livoid test_objc_arrays() {
1902*67e74705SXin Li    { // CASE ONE -- OBJECT IN ARRAY CREATED DIRECTLY
1903*67e74705SXin Li        NSObject *o = [[NSObject alloc] init];
1904*67e74705SXin Li        NSArray *a = [[NSArray alloc] initWithObjects:o, (void*)0]; // expected-warning {{leak}}
1905*67e74705SXin Li        [o release];
1906*67e74705SXin Li        [a description];
1907*67e74705SXin Li        [o description];
1908*67e74705SXin Li    }
1909*67e74705SXin Li
1910*67e74705SXin Li    { // CASE TWO -- OBJECT IN ARRAY CREATED BY DUPING AUTORELEASED ARRAY
1911*67e74705SXin Li        NSObject *o = [[NSObject alloc] init];
1912*67e74705SXin Li        NSArray *a1 = [NSArray arrayWithObjects:o, (void*)0];
1913*67e74705SXin Li        NSArray *a2 = [[NSArray alloc] initWithArray:a1]; // expected-warning {{leak}}
1914*67e74705SXin Li        [o release];
1915*67e74705SXin Li        [a2 description];
1916*67e74705SXin Li        [o description];
1917*67e74705SXin Li    }
1918*67e74705SXin Li
1919*67e74705SXin Li    { // CASE THREE -- OBJECT IN RETAINED @[]
1920*67e74705SXin Li        NSObject *o = [[NSObject alloc] init];
1921*67e74705SXin Li        NSArray *a3 = [@[o] retain]; // expected-warning {{leak}}
1922*67e74705SXin Li        [o release];
1923*67e74705SXin Li        [a3 description];
1924*67e74705SXin Li        [o description];
1925*67e74705SXin Li    }
1926*67e74705SXin Li
1927*67e74705SXin Li    { // CASE FOUR -- OBJECT IN ARRAY CREATED BY DUPING @[]
1928*67e74705SXin Li        NSObject *o = [[NSObject alloc] init];
1929*67e74705SXin Li        NSArray *a = [[NSArray alloc] initWithArray:@[o]]; // expected-warning {{leak}}
1930*67e74705SXin Li        [o release];
1931*67e74705SXin Li
1932*67e74705SXin Li        [a description];
1933*67e74705SXin Li        [o description];
1934*67e74705SXin Li    }
1935*67e74705SXin Li
1936*67e74705SXin Li    { // CASE FIVE -- OBJECT IN RETAINED @{}
1937*67e74705SXin Li        NSValue *o = [[NSValue alloc] init];
1938*67e74705SXin Li        NSDictionary *a = [@{o : o} retain]; // expected-warning {{leak}}
1939*67e74705SXin Li        [o release];
1940*67e74705SXin Li
1941*67e74705SXin Li        [a description];
1942*67e74705SXin Li        [o description];
1943*67e74705SXin Li    }
1944*67e74705SXin Li}
1945*67e74705SXin Li
1946*67e74705SXin Livoid test_objc_integer_literals() {
1947*67e74705SXin Li  id value = [@1 retain]; // expected-warning {{leak}}
1948*67e74705SXin Li  [value description];
1949*67e74705SXin Li}
1950*67e74705SXin Li
1951*67e74705SXin Livoid test_objc_boxed_expressions(int x, const char *y) {
1952*67e74705SXin Li  id value = [@(x) retain]; // expected-warning {{leak}}
1953*67e74705SXin Li  [value description];
1954*67e74705SXin Li
1955*67e74705SXin Li  value = [@(y) retain]; // expected-warning {{leak}}
1956*67e74705SXin Li  [value description];
1957*67e74705SXin Li}
1958*67e74705SXin Li
1959*67e74705SXin Li// Test NSLog doesn't escape tracked objects.
1960*67e74705SXin Livoid rdar11400885(int y)
1961*67e74705SXin Li{
1962*67e74705SXin Li  @autoreleasepool {
1963*67e74705SXin Li    NSString *printString;
1964*67e74705SXin Li    if(y > 2)
1965*67e74705SXin Li      printString = [[NSString alloc] init];
1966*67e74705SXin Li    else
1967*67e74705SXin Li      printString = [[NSString alloc] init];
1968*67e74705SXin Li    NSLog(@"Once %@", printString);
1969*67e74705SXin Li    [printString release];
1970*67e74705SXin Li    NSLog(@"Again: %@", printString); // expected-warning {{Reference-counted object is used after it is released}}
1971*67e74705SXin Li  }
1972*67e74705SXin Li}
1973*67e74705SXin Li
1974*67e74705SXin Liid makeCollectableNonLeak() {
1975*67e74705SXin Li  extern CFTypeRef CFCreateSomething();
1976*67e74705SXin Li
1977*67e74705SXin Li  CFTypeRef object = CFCreateSomething(); // +1
1978*67e74705SXin Li  CFRetain(object); // +2
1979*67e74705SXin Li  id objCObject = NSMakeCollectable(object); // +2
1980*67e74705SXin Li  [objCObject release]; // +1
1981*67e74705SXin Li  return [objCObject autorelease]; // +0
1982*67e74705SXin Li}
1983*67e74705SXin Li
1984*67e74705SXin Li
1985*67e74705SXin Livoid consumeAndStopTracking(id NS_CONSUMED obj, void (^callback)(void));
1986*67e74705SXin Livoid CFConsumeAndStopTracking(CFTypeRef CF_CONSUMED obj, void (^callback)(void));
1987*67e74705SXin Li
1988*67e74705SXin Livoid testConsumeAndStopTracking() {
1989*67e74705SXin Li  id retained = [@[] retain]; // +1
1990*67e74705SXin Li  consumeAndStopTracking(retained, ^{}); // no-warning
1991*67e74705SXin Li
1992*67e74705SXin Li  id doubleRetained = [[@[] retain] retain]; // +2
1993*67e74705SXin Li  consumeAndStopTracking(doubleRetained, ^{
1994*67e74705SXin Li    [doubleRetained release];
1995*67e74705SXin Li  }); // no-warning
1996*67e74705SXin Li
1997*67e74705SXin Li  id unretained = @[]; // +0
1998*67e74705SXin Li  consumeAndStopTracking(unretained, ^{}); // expected-warning {{Incorrect decrement of the reference count of an object that is not owned at this point by the caller}}
1999*67e74705SXin Li}
2000*67e74705SXin Li
2001*67e74705SXin Livoid testCFConsumeAndStopTracking() {
2002*67e74705SXin Li  id retained = [@[] retain]; // +1
2003*67e74705SXin Li  CFConsumeAndStopTracking((CFTypeRef)retained, ^{}); // no-warning
2004*67e74705SXin Li
2005*67e74705SXin Li  id doubleRetained = [[@[] retain] retain]; // +2
2006*67e74705SXin Li  CFConsumeAndStopTracking((CFTypeRef)doubleRetained, ^{
2007*67e74705SXin Li    [doubleRetained release];
2008*67e74705SXin Li  }); // no-warning
2009*67e74705SXin Li
2010*67e74705SXin Li  id unretained = @[]; // +0
2011*67e74705SXin Li  CFConsumeAndStopTracking((CFTypeRef)unretained, ^{}); // expected-warning {{Incorrect decrement of the reference count of an object that is not owned at this point by the caller}}
2012*67e74705SXin Li}
2013*67e74705SXin Li//===----------------------------------------------------------------------===//
2014*67e74705SXin Li// Test 'pragma clang arc_cf_code_audited' support.
2015*67e74705SXin Li//===----------------------------------------------------------------------===//
2016*67e74705SXin Li
2017*67e74705SXin Litypedef void *MyCFType;
2018*67e74705SXin Li#pragma clang arc_cf_code_audited begin
2019*67e74705SXin LiMyCFType CreateMyCFType();
2020*67e74705SXin Li#pragma clang arc_cf_code_audited end
2021*67e74705SXin Li
2022*67e74705SXin Livoid test_custom_cf() {
2023*67e74705SXin Li  MyCFType x = CreateMyCFType(); // expected-warning {{leak of an object stored into 'x'}}
2024*67e74705SXin Li}
2025*67e74705SXin Li
2026*67e74705SXin Li//===----------------------------------------------------------------------===//
2027*67e74705SXin Li// Test calling CFPlugInInstanceCreate, which appears in CF but doesn't
2028*67e74705SXin Li// return a CF object.
2029*67e74705SXin Li//===----------------------------------------------------------------------===//
2030*67e74705SXin Li
2031*67e74705SXin Livoid test_CFPlugInInstanceCreate(CFUUIDRef factoryUUID, CFUUIDRef typeUUID) {
2032*67e74705SXin Li  CFPlugInInstanceCreate(kCFAllocatorDefault, factoryUUID, typeUUID); // no-warning
2033*67e74705SXin Li}
2034*67e74705SXin Li
2035*67e74705SXin Li//===----------------------------------------------------------------------===//
2036*67e74705SXin Li// PR14927: -drain only has retain-count semantics on NSAutoreleasePool.
2037*67e74705SXin Li//===----------------------------------------------------------------------===//
2038*67e74705SXin Li
2039*67e74705SXin Li@interface PR14927 : NSObject
2040*67e74705SXin Li- (void)drain;
2041*67e74705SXin Li@end
2042*67e74705SXin Li
2043*67e74705SXin Livoid test_drain() {
2044*67e74705SXin Li  PR14927 *obj = [[PR14927 alloc] init];
2045*67e74705SXin Li  [obj drain];
2046*67e74705SXin Li  [obj release]; // no-warning
2047*67e74705SXin Li}
2048*67e74705SXin Li
2049*67e74705SXin Li//===----------------------------------------------------------------------===//
2050*67e74705SXin Li// Allow cf_returns_retained and cf_returns_not_retained to mark a return
2051*67e74705SXin Li// value as tracked, even if the object isn't a known CF type.
2052*67e74705SXin Li//===----------------------------------------------------------------------===//
2053*67e74705SXin Li
2054*67e74705SXin LiMyCFType getCustom() __attribute__((cf_returns_not_retained));
2055*67e74705SXin LiMyCFType makeCustom() __attribute__((cf_returns_retained));
2056*67e74705SXin Li
2057*67e74705SXin Livoid testCustomReturnsRetained() {
2058*67e74705SXin Li  MyCFType obj = makeCustom(); // expected-warning {{leak of an object stored into 'obj'}}
2059*67e74705SXin Li}
2060*67e74705SXin Li
2061*67e74705SXin Livoid testCustomReturnsNotRetained() {
2062*67e74705SXin Li  CFRelease(getCustom()); // expected-warning {{Incorrect decrement of the reference count of an object that is not owned at this point by the caller}}
2063*67e74705SXin Li}
2064*67e74705SXin Li
2065*67e74705SXin Li//===----------------------------------------------------------------------===//
2066*67e74705SXin Li// Don't print variables which are out of the current scope.
2067*67e74705SXin Li//===----------------------------------------------------------------------===//
2068*67e74705SXin Li@interface MyObj12706177 : NSObject
2069*67e74705SXin Li-(instancetype)initX;
2070*67e74705SXin Li+(void)test12706177;
2071*67e74705SXin Li@end
2072*67e74705SXin Listatic int Cond;
2073*67e74705SXin Li@implementation MyObj12706177
2074*67e74705SXin Li-(instancetype)initX {
2075*67e74705SXin Li  if (Cond)
2076*67e74705SXin Li    return 0;
2077*67e74705SXin Li  self = [super init];
2078*67e74705SXin Li  return self;
2079*67e74705SXin Li}
2080*67e74705SXin Li+(void)test12706177 {
2081*67e74705SXin Li  id x = [[MyObj12706177 alloc] initX]; //expected-warning {{Potential leak of an object}}
2082*67e74705SXin Li  [x release];
2083*67e74705SXin Li}
2084*67e74705SXin Li@end
2085*67e74705SXin Li
2086*67e74705SXin Li//===----------------------------------------------------------------------===//
2087*67e74705SXin Li// <rdar://problem/13783514> xpc_connection_set_finalizer_f
2088*67e74705SXin Li//===----------------------------------------------------------------------===//
2089*67e74705SXin Li
2090*67e74705SXin Litypedef xpc_object_t xpc_connection_t;
2091*67e74705SXin Litypedef void (*xpc_finalizer_t)(void *value);
2092*67e74705SXin Livoid xpc_connection_set_context(xpc_connection_t connection, void *ctx);
2093*67e74705SXin Livoid xpc_connection_set_finalizer_f(xpc_connection_t connection,
2094*67e74705SXin Li                                    xpc_finalizer_t finalizer);
2095*67e74705SXin Livoid releaseAfterXPC(void *context) {
2096*67e74705SXin Li  [(NSArray *)context release];
2097*67e74705SXin Li}
2098*67e74705SXin Li
2099*67e74705SXin Livoid rdar13783514(xpc_connection_t connection) {
2100*67e74705SXin Li  xpc_connection_set_context(connection, [[NSMutableArray alloc] init]);
2101*67e74705SXin Li  xpc_connection_set_finalizer_f(connection, releaseAfterXPC);
2102*67e74705SXin Li} // no-warning
2103*67e74705SXin Li
2104*67e74705SXin LiCFAttributedStringRef CFAttributedCreate(void *CFObj CF_CONSUMED) CF_RETURNS_RETAINED;
2105*67e74705SXin Li
2106*67e74705SXin Li@interface Action
2107*67e74705SXin Li@property (nonatomic) SEL action;
2108*67e74705SXin Li@property (nonatomic, assign) id target;
2109*67e74705SXin Li@end
2110