Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 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 |
Ted Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 3 | |
| 4 | //===----------------------------------------------------------------------===// |
| 5 | // Header stuff. |
| 6 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e798e7c | 2009-04-27 19:14:45 +0000 | [diff] [blame] | 7 | |
Ted Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 8 | typedef unsigned int __darwin_natural_t; |
Ted Kremenek | e798e7c | 2009-04-27 19:14:45 +0000 | [diff] [blame] | 9 | typedef struct {} div_t; |
Ted Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 10 | typedef unsigned long UInt32; |
| 11 | typedef signed long CFIndex; |
| 12 | typedef const void * CFTypeRef; |
| 13 | typedef const struct __CFString * CFStringRef; |
| 14 | typedef const struct __CFAllocator * CFAllocatorRef; |
| 15 | extern const CFAllocatorRef kCFAllocatorDefault; |
| 16 | extern CFTypeRef CFRetain(CFTypeRef cf); |
| 17 | extern void CFRelease(CFTypeRef cf); |
| 18 | typedef struct { |
| 19 | } |
Ted Kremenek | e798e7c | 2009-04-27 19:14:45 +0000 | [diff] [blame] | 20 | CFArrayCallBacks; |
Ted Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 21 | extern const CFArrayCallBacks kCFTypeArrayCallBacks; |
| 22 | typedef const struct __CFArray * CFArrayRef; |
| 23 | typedef struct __CFArray * CFMutableArrayRef; |
| 24 | extern CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks); |
| 25 | extern const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx); |
Ted Kremenek | e798e7c | 2009-04-27 19:14:45 +0000 | [diff] [blame] | 26 | extern void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value); |
Ted Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 27 | typedef const struct __CFDictionary * CFDictionaryRef; |
| 28 | typedef UInt32 CFStringEncoding; |
| 29 | enum { |
| 30 | kCFStringEncodingMacRoman = 0, kCFStringEncodingWindowsLatin1 = 0x0500, kCFStringEncodingISOLatin1 = 0x0201, kCFStringEncodingNextStepLatin = 0x0B01, kCFStringEncodingASCII = 0x0600, kCFStringEncodingUnicode = 0x0100, kCFStringEncodingUTF8 = 0x08000100, kCFStringEncodingNonLossyASCII = 0x0BFF , kCFStringEncodingUTF16 = 0x0100, kCFStringEncodingUTF16BE = 0x10000100, kCFStringEncodingUTF16LE = 0x14000100, kCFStringEncodingUTF32 = 0x0c000100, kCFStringEncodingUTF32BE = 0x18000100, kCFStringEncodingUTF32LE = 0x1c000100 }; |
| 31 | extern CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding); |
| 32 | typedef double CFTimeInterval; |
| 33 | typedef CFTimeInterval CFAbsoluteTime; |
Ted Kremenek | e798e7c | 2009-04-27 19:14:45 +0000 | [diff] [blame] | 34 | extern CFAbsoluteTime CFAbsoluteTimeGetCurrent(void); |
Ted Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 35 | typedef const struct __CFDate * CFDateRef; |
| 36 | extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at); |
| 37 | extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate); |
| 38 | typedef __darwin_natural_t natural_t; |
| 39 | typedef natural_t mach_port_name_t; |
| 40 | typedef mach_port_name_t mach_port_t; |
Ted Kremenek | e798e7c | 2009-04-27 19:14:45 +0000 | [diff] [blame] | 41 | typedef struct { |
| 42 | } |
| 43 | CFRunLoopObserverContext; |
Ted Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 44 | typedef signed char BOOL; |
Ted Kremenek | e798e7c | 2009-04-27 19:14:45 +0000 | [diff] [blame] | 45 | typedef unsigned int NSUInteger; |
| 46 | @class NSString, Protocol; |
| 47 | extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); |
Ted Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 48 | typedef 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 Kremenek | e798e7c | 2009-04-27 19:14:45 +0000 | [diff] [blame] | 55 | @end @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; |
Ted Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 56 | @end @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; |
Ted Kremenek | e798e7c | 2009-04-27 19:14:45 +0000 | [diff] [blame] | 57 | @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; |
| 71 | typedef enum { |
| 72 | ACL_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) } |
| 73 | acl_entry_id_t; |
Ted Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 74 | typedef int kern_return_t; |
| 75 | typedef kern_return_t mach_error_t; |
| 76 | typedef mach_port_t io_object_t; |
| 77 | typedef io_object_t io_service_t; |
| 78 | typedef struct __DASession * DASessionRef; |
| 79 | extern DASessionRef DASessionCreate( CFAllocatorRef allocator ); |
| 80 | typedef struct __DADisk * DADiskRef; |
| 81 | extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef session, const char * name ); |
| 82 | extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media ); |
| 83 | extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk ); |
| 84 | extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk ); |
Ted Kremenek | e798e7c | 2009-04-27 19:14:45 +0000 | [diff] [blame] | 85 | @interface NSResponder : NSObject <NSCoding> { |
| 86 | } |
| 87 | @end @class NSColor, NSFont, NSNotification; |
| 88 | typedef 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 Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 95 | } |
| 96 | @end enum { |
| 97 | kDAReturnSuccess = 0, kDAReturnError = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x01, kDAReturnBusy = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x02, kDAReturnBadArgument = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x03, kDAReturnExclusiveAccess = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x04, kDAReturnNoResources = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x05, kDAReturnNotFound = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x06, kDAReturnNotMounted = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x07, kDAReturnNotPermitted = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x08, kDAReturnNotPrivileged = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x09, kDAReturnNotReady = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0A, kDAReturnNotWritable = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0B, kDAReturnUnsupported = (((0x3e)&0x3f)<<26) | (((0x368)&0xfff)<<14) | 0x0C }; |
| 98 | typedef mach_error_t DAReturn; |
| 99 | typedef const struct __DADissenter * DADissenterRef; |
| 100 | extern DADissenterRef DADissenterCreate( CFAllocatorRef allocator, DAReturn status, CFStringRef string ); |
| 101 | |
Ted Kremenek | e798e7c | 2009-04-27 19:14:45 +0000 | [diff] [blame] | 102 | CFTypeRef CFMakeCollectable(CFTypeRef cf) ; |
| 103 | |
Ted Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 104 | //===----------------------------------------------------------------------===// |
| 105 | // Test cases. |
| 106 | //===----------------------------------------------------------------------===// |
| 107 | |
| 108 | void f1() { |
Ted Kremenek | b2b14d7 | 2009-02-18 22:11:23 +0000 | [diff] [blame] | 109 | CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // no-warning |
Ted Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 110 | id x = [(id) A autorelease]; |
Ted Kremenek | b2b14d7 | 2009-02-18 22:11:23 +0000 | [diff] [blame] | 111 | CFRelease((CFMutableArrayRef) x); |
| 112 | } |
| 113 | |
| 114 | void 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 | |
| 121 | void f3() { |
| 122 | CFMutableArrayRef A = CFArrayCreateMutable(0, 10, &kCFTypeArrayCallBacks); // expected-warning{{leak}} |
| 123 | CFMakeCollectable(A); |
| 124 | CFRetain(A); |
Ted Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 125 | } |
Ted Kremenek | 4064de9 | 2009-04-27 18:27:22 +0000 | [diff] [blame] | 126 | |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 127 | // Test return of non-owned objects in contexts where an owned object |
| 128 | // is expected. |
| 129 | @interface TestReturnNotOwnedWhenExpectedOwned |
| 130 | - (NSString*)newString; |
Ted Kremenek | 82f2be5 | 2009-05-10 16:52:15 +0000 | [diff] [blame] | 131 | - (CFMutableArrayRef)newArray; |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 132 | @end |
| 133 | |
| 134 | @implementation TestReturnNotOwnedWhenExpectedOwned |
| 135 | - (NSString*)newString { |
Ted Kremenek | 82f2be5 | 2009-05-10 16:52:15 +0000 | [diff] [blame] | 136 | 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 Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 142 | } |
| 143 | @end |
| 144 | |
Ted Kremenek | 8c6096e | 2009-05-09 03:10:32 +0000 | [diff] [blame] | 145 | //===----------------------------------------------------------------------===// |
| 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 | |
| 154 | void test_attr_1(TestOwnershipAttr *X) { |
| 155 | NSString *str = [X returnsAnOwnedString]; // no-warning |
| 156 | } |
| 157 | |
| 158 | void test_attr_1b(TestOwnershipAttr *X) { |
| 159 | NSString *str = [X returnsAnOwnedCFString]; // expected-warning{{leak}} |
| 160 | } |
| 161 | |