retain-release-gc-only.m revision 4064de959853503d9b87065adac1b277fff8af20
1// RUN: clang-cc -analyze -checker-cfref -verify -fobjc-gc-only %s &&
2// RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -fobjc-gc-only -verify %s
3
4//===----------------------------------------------------------------------===//
5// Header stuff.
6//===----------------------------------------------------------------------===//
7typedef unsigned int __darwin_natural_t;
8typedef unsigned long UInt32;
9typedef signed long CFIndex;
10typedef const void * CFTypeRef;
11typedef const struct __CFString * CFStringRef;
12typedef const struct __CFAllocator * CFAllocatorRef;
13extern const CFAllocatorRef kCFAllocatorDefault;
14extern CFTypeRef CFRetain(CFTypeRef cf);
15extern void CFRelease(CFTypeRef cf);
16CFTypeRef CFMakeCollectable(CFTypeRef cf);
17typedef struct {
18}
19  CFArrayCallBacks;
20extern const CFArrayCallBacks kCFTypeArrayCallBacks;
21typedef const struct __CFArray * CFArrayRef;
22typedef struct __CFArray * CFMutableArrayRef;
23extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks);
24extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx);
25typedef const struct __CFDictionary * CFDictionaryRef;
26typedef UInt32 CFStringEncoding;
27enum {
28kCFStringEncodingMacRoman = 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  };
29extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding);
30typedef double CFTimeInterval;
31typedef CFTimeInterval CFAbsoluteTime;
32typedef const struct __CFDate * CFDateRef;
33extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at);
34extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate);
35typedef __darwin_natural_t natural_t;
36typedef natural_t mach_port_name_t;
37typedef mach_port_name_t mach_port_t;
38typedef signed char BOOL;
39typedef struct _NSZone NSZone;
40@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
41@protocol NSObject  - (BOOL)isEqual:(id)object;
42- (id)retain;
43- (oneway void)release;
44- (id)autorelease;
45@end  @protocol NSCopying  - (id)copyWithZone:(NSZone *)zone;
46@end  @protocol NSCoding  - (void)encodeWithCoder:(NSCoder *)aCoder;
47@end    @interface NSObject <NSObject> {
48}
49@end  typedef float CGFloat;
50typedef double NSTimeInterval;
51@interface NSDate : NSObject <NSCopying, NSCoding>  - (NSTimeInterval)timeIntervalSinceReferenceDate;
52@end      enum {
53NSObjCNoType = 0,     NSObjCVoidType = 'v',     NSObjCCharType = 'c',     NSObjCShortType = 's',     NSObjCLongType = 'l',     NSObjCLonglongType = 'q',     NSObjCFloatType = 'f',     NSObjCDoubleType = 'd',      NSObjCBoolType = 'B',      NSObjCSelectorType = ':',     NSObjCObjectType = '@',     NSObjCStructType = '{',     NSObjCPointerType = '^',     NSObjCStringType = '*',     NSObjCArrayType = '[',     NSObjCUnionType = '(',     NSObjCBitfield = 'b' }
54__attribute__((deprecated));
55typedef int kern_return_t;
56typedef kern_return_t mach_error_t;
57typedef mach_port_t io_object_t;
58typedef io_object_t io_service_t;
59typedef struct __DASession * DASessionRef;
60extern DASessionRef DASessionCreate( CFAllocatorRef allocator );
61typedef struct __DADisk * DADiskRef;
62extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name );
63extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media );
64extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk );
65extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk );
66@interface NSAppleEventManager : NSObject {
67}
68@end enum {
69kDAReturnSuccess = 0,     kDAReturnError = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x01,     kDAReturnBusy = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x02,     kDAReturnBadArgument = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x03,     kDAReturnExclusiveAccess = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x04,     kDAReturnNoResources = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x05,     kDAReturnNotFound = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x06,     kDAReturnNotMounted = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x07,     kDAReturnNotPermitted = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x08,     kDAReturnNotPrivileged = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x09,     kDAReturnNotReady = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0A,     kDAReturnNotWritable = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0B,     kDAReturnUnsupported = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0C };
70typedef mach_error_t DAReturn;
71typedef const struct __DADissenter * DADissenterRef;
72extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string );
73
74//===----------------------------------------------------------------------===//
75// Test cases.
76//===----------------------------------------------------------------------===//
77
78void f1() {
79  CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
80  id x = [(id) A autorelease];
81  CFRelease((CFMutableArrayRef) x);
82}
83
84void f2() {
85  CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}}
86  id x = [(id) A retain];
87  [x release];
88  [x release];
89}
90
91void f3() {
92  CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}}
93  CFMakeCollectable(A);
94  CFRetain(A);
95}
96
97//===----------------------------------------------------------------------===//
98// Tests of ownership attributes.
99//===----------------------------------------------------------------------===//
100
101@interface TestOwnershipAttr : NSObject
102- (NSString*) returnsAnOwnedString __attribute__((objc_ownership_returns));
103- (void) myRetain:(id)__attribute__((objc_ownership_retain))obj;
104- (void) myCFRetain:(id)__attribute__((objc_ownership_cfretain))obj;
105@end
106
107void test_attr_1(TestOwnershipAttr *X) {
108  NSString *str = [X returnsAnOwnedString]; // no-warning
109}
110
111void test_attr_2(TestOwnershipAttr *X) {
112  NSString *str = [X returnsAnOwnedString]; // no-warning
113  [X myRetain:str];
114  [str release];
115}
116
117void test_attr_3(TestOwnershipAttr *X) {
118  // FIXME: This should be a leak.  Need to change the analyzer to
119  // to track Objective-C objects retain counts even in GC mode.
120  NSString *str = [X returnsAnOwnedString]; // no-warning
121  [X myCFRetain:str];
122  [str release];
123}
124