blob: 70ad54f8aa4a82ecd7327b81cd69b5a905d84847 [file] [log] [blame]
Daniel Dunbard7d5f022009-03-24 02:24:46 +00001// 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
Ted Kremenekabf43972009-01-28 21:44:40 +00003
4//===----------------------------------------------------------------------===//
5// Header stuff.
6//===----------------------------------------------------------------------===//
Ted Kremeneke798e7c2009-04-27 19:14:45 +00007
Ted Kremenekabf43972009-01-28 21:44:40 +00008typedef unsigned int __darwin_natural_t;
Ted Kremeneke798e7c2009-04-27 19:14:45 +00009typedef struct {} div_t;
Ted Kremenekabf43972009-01-28 21:44:40 +000010typedef unsigned long UInt32;
11typedef signed long CFIndex;
12typedef const void * CFTypeRef;
13typedef const struct __CFString * CFStringRef;
14typedef const struct __CFAllocator * CFAllocatorRef;
15extern const CFAllocatorRef kCFAllocatorDefault;
16extern CFTypeRef CFRetain(CFTypeRef cf);
17extern void CFRelease(CFTypeRef cf);
18typedef struct {
19}
Ted Kremeneke798e7c2009-04-27 19:14:45 +000020CFArrayCallBacks;
Ted Kremenekabf43972009-01-28 21:44:40 +000021extern const CFArrayCallBacks kCFTypeArrayCallBacks;
22typedef const struct __CFArray * CFArrayRef;
23typedef struct __CFArray * CFMutableArrayRef;
24extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks);
25extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx);
Ted Kremeneke798e7c2009-04-27 19:14:45 +000026extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value);
Ted Kremenekabf43972009-01-28 21:44:40 +000027typedef const struct __CFDictionary * CFDictionaryRef;
28typedef UInt32 CFStringEncoding;
29enum {
30kCFStringEncodingMacRoman = 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 };
31extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding);
32typedef double CFTimeInterval;
33typedef CFTimeInterval CFAbsoluteTime;
Ted Kremeneke798e7c2009-04-27 19:14:45 +000034extern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void);
Ted Kremenekabf43972009-01-28 21:44:40 +000035typedef const struct __CFDate * CFDateRef;
36extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at);
37extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate);
38typedef __darwin_natural_t natural_t;
39typedef natural_t mach_port_name_t;
40typedef mach_port_name_t mach_port_t;
Ted Kremeneke798e7c2009-04-27 19:14:45 +000041typedef struct {
42}
43CFRunLoopObserverContext;
Ted Kremenekabf43972009-01-28 21:44:40 +000044typedef signed char BOOL;
Ted Kremeneke798e7c2009-04-27 19:14:45 +000045typedef unsigned int NSUInteger;
46@class NSString, Protocol;
47extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
Ted Kremenekabf43972009-01-28 21:44:40 +000048typedef struct _NSZone NSZone;
49@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
50@protocol NSObject - (BOOL)isEqual:(id)object;
51- (id)retain;
52- (oneway void)release;
53- (id)autorelease;
54@end @protocol NSCopying - (id)copyWithZone:(NSZone *)zone;
Ted Kremeneke798e7c2009-04-27 19:14:45 +000055@end @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone;
Ted Kremenekabf43972009-01-28 21:44:40 +000056@end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder;
Ted Kremeneke798e7c2009-04-27 19:14:45 +000057@end
58@interface NSObject <NSObject> {}
59+ (id)alloc;
60+ (id)allocWithZone:(NSZone *)zone;
61@end typedef float CGFloat;
62@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length;
63- (const char *)UTF8String;
64- (id)initWithUTF8String:(const char *)nullTerminatedCString;
65+ (id)stringWithUTF8String:(const char *)nullTerminatedCString;
66- (id)init;
67- (void)dealloc;
68@end extern NSString * const NSCurrentLocaleDidChangeNotification ;
69@protocol NSLocking - (void)lock;
70@end extern NSString * const NSUndoManagerCheckpointNotification;
71typedef enum {
72ACL_READ_DATA = (1<<1), ACL_LIST_DIRECTORY = (1<<1), ACL_WRITE_DATA = (1<<2), ACL_ADD_FILE = (1<<2), ACL_EXECUTE = (1<<3), ACL_SEARCH = (1<<3), ACL_DELETE = (1<<4), ACL_APPEND_DATA = (1<<5), ACL_ADD_SUBDIRECTORY = (1<<5), ACL_DELETE_CHILD = (1<<6), ACL_READ_ATTRIBUTES = (1<<7), ACL_WRITE_ATTRIBUTES = (1<<8), ACL_READ_EXTATTRIBUTES = (1<<9), ACL_WRITE_EXTATTRIBUTES = (1<<10), ACL_READ_SECURITY = (1<<11), ACL_WRITE_SECURITY = (1<<12), ACL_CHANGE_OWNER = (1<<13) }
73acl_entry_id_t;
Ted Kremenekabf43972009-01-28 21:44:40 +000074typedef int kern_return_t;
75typedef kern_return_t mach_error_t;
76typedef mach_port_t io_object_t;
77typedef io_object_t io_service_t;
78typedef struct __DASession * DASessionRef;
79extern DASessionRef DASessionCreate( CFAllocatorRef allocator );
80typedef struct __DADisk * DADiskRef;
81extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name );
82extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media );
83extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk );
84extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk );
Ted Kremeneke798e7c2009-04-27 19:14:45 +000085@interface NSResponder : NSObject <NSCoding> {
86}
87@end @class NSColor, NSFont, NSNotification;
88typedef struct __CFlags {
89}
90_CFlags;
91@interface NSCell : NSObject <NSCopying, NSCoding> {
92}
93@end @class NSDate, NSDictionary, NSError, NSException, NSNotification;
94@interface NSManagedObjectContext : NSObject <NSCoding, NSLocking> {
Ted Kremenekabf43972009-01-28 21:44:40 +000095}
96@end enum {
97kDAReturnSuccess = 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 };
98typedef mach_error_t DAReturn;
99typedef const struct __DADissenter * DADissenterRef;
100extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string );
101
Ted Kremeneke798e7c2009-04-27 19:14:45 +0000102CFTypeRef CFMakeCollectable(CFTypeRef cf) ;
103
Ted Kremenekabf43972009-01-28 21:44:40 +0000104//===----------------------------------------------------------------------===//
105// Test cases.
106//===----------------------------------------------------------------------===//
107
108void f1() {
Ted Kremenekb2b14d72009-02-18 22:11:23 +0000109 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
Ted Kremenekabf43972009-01-28 21:44:40 +0000110 id x = [(id) A autorelease];
Ted Kremenekb2b14d72009-02-18 22:11:23 +0000111 CFRelease((CFMutableArrayRef) x);
112}
113
114void f2() {
115 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}}
116 id x = [(id) A retain];
117 [x release];
118 [x release];
119}
120
121void f3() {
122 CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}}
123 CFMakeCollectable(A);
124 CFRetain(A);
Ted Kremenekabf43972009-01-28 21:44:40 +0000125}
Ted Kremenek4064de92009-04-27 18:27:22 +0000126
Ted Kremeneke8720ce2009-05-10 06:25:57 +0000127// Test return of non-owned objects in contexts where an owned object
128// is expected.
129@interface TestReturnNotOwnedWhenExpectedOwned
130- (NSString*)newString;
Ted Kremenek82f2be52009-05-10 16:52:15 +0000131- (CFMutableArrayRef)newArray;
Ted Kremeneke8720ce2009-05-10 06:25:57 +0000132@end
133
134@implementation TestReturnNotOwnedWhenExpectedOwned
135- (NSString*)newString {
Ted Kremenek82f2be52009-05-10 16:52:15 +0000136 NSString *s = [NSString stringWithUTF8String:"hello"]; // expected-warning{{Potential leak (when using garbage collection) of an object allocated on line 136 and stored into 's'}}
137 CFRetain(s);
138 return s;
139}
140- (CFMutableArrayRef)newArray{
141 return CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning
Ted Kremeneke8720ce2009-05-10 06:25:57 +0000142}
143@end
144
Ted Kremenek8c6096e2009-05-09 03:10:32 +0000145//===----------------------------------------------------------------------===//
146// Tests of ownership attributes.
147//===----------------------------------------------------------------------===//
148
149@interface TestOwnershipAttr : NSObject
150- (NSString*) returnsAnOwnedString __attribute__((ns_returns_retained));
151- (NSString*) returnsAnOwnedCFString __attribute__((cf_returns_retained));
152@end
153
154void test_attr_1(TestOwnershipAttr *X) {
155 NSString *str = [X returnsAnOwnedString]; // no-warning
156}
157
158void test_attr_1b(TestOwnershipAttr *X) {
159 NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}}
160}
161