Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,osx.cocoa.NilArg,osx.cocoa.RetainCount,osx.AtomicCAS,experimental.core -analyzer-store=region -analyzer-constraints=basic -verify %s |
| 2 | // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,osx.cocoa.NilArg,osx.cocoa.RetainCount,osx.AtomicCAS,experimental.core -analyzer-store=region -analyzer-constraints=range -verify %s |
| 3 | // RUN: %clang_cc1 -DTEST_64 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,osx.cocoa.NilArg,osx.cocoa.RetainCount,osx.AtomicCAS,experimental.core -analyzer-store=region -analyzer-constraints=basic -verify %s |
| 4 | // RUN: %clang_cc1 -DTEST_64 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,osx.cocoa.NilArg,osx.cocoa.RetainCount,osx.AtomicCAS,experimental.core -analyzer-store=region -analyzer-constraints=range -verify %s |
Ted Kremenek | 45f7c27 | 2009-10-15 01:40:34 +0000 | [diff] [blame] | 5 | |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 6 | |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // The following code is reduced using delta-debugging from |
| 9 | // Foundation.h (Mac OS X). |
| 10 | // |
| 11 | // It includes the basic definitions for the test cases below. |
| 12 | // Not directly including Foundation.h directly makes this test case |
| 13 | // both svelte and portable to non-Mac platforms. |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Ted Kremenek | 45f7c27 | 2009-10-15 01:40:34 +0000 | [diff] [blame] | 16 | #ifdef TEST_64 |
| 17 | typedef long long int64_t; |
| 18 | _Bool OSAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue ); |
| 19 | #define COMPARE_SWAP_BARRIER OSAtomicCompareAndSwap64Barrier |
| 20 | typedef int64_t intptr_t; |
| 21 | #else |
Ted Kremenek | a3f4540 | 2009-04-29 16:03:59 +0000 | [diff] [blame] | 22 | typedef int int32_t; |
Ted Kremenek | 45f7c27 | 2009-10-15 01:40:34 +0000 | [diff] [blame] | 23 | _Bool OSAtomicCompareAndSwap32Barrier( int32_t __oldValue, int32_t __newValue, volatile int32_t *__theValue ); |
| 24 | #define COMPARE_SWAP_BARRIER OSAtomicCompareAndSwap32Barrier |
| 25 | typedef int32_t intptr_t; |
| 26 | #endif |
| 27 | |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 28 | typedef const void * CFTypeRef; |
| 29 | typedef const struct __CFString * CFStringRef; |
| 30 | typedef const struct __CFAllocator * CFAllocatorRef; |
| 31 | extern const CFAllocatorRef kCFAllocatorDefault; |
| 32 | extern CFTypeRef CFRetain(CFTypeRef cf); |
Ted Kremenek | 09f1419 | 2009-04-21 20:01:03 +0000 | [diff] [blame] | 33 | void CFRelease(CFTypeRef cf); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 34 | typedef const struct __CFDictionary * CFDictionaryRef; |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 35 | const void *CFDictionaryGetValue(CFDictionaryRef theDict, const void *key); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 36 | extern CFStringRef CFStringCreateWithFormat(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, ...); |
| 37 | typedef signed char BOOL; |
| 38 | typedef int NSInteger; |
| 39 | typedef unsigned int NSUInteger; |
| 40 | @class NSString, Protocol; |
| 41 | extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); |
| 42 | typedef NSInteger NSComparisonResult; |
| 43 | typedef struct _NSZone NSZone; |
| 44 | @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 45 | @protocol NSObject |
| 46 | - (BOOL)isEqual:(id)object; |
| 47 | - (oneway void)release; |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 48 | - (id)retain; |
Ted Kremenek | 95d3b90 | 2009-05-11 15:26:06 +0000 | [diff] [blame] | 49 | - (id)autorelease; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 50 | @end |
| 51 | @protocol NSCopying |
| 52 | - (id)copyWithZone:(NSZone *)zone; |
| 53 | @end |
| 54 | @protocol NSMutableCopying |
| 55 | - (id)mutableCopyWithZone:(NSZone *)zone; |
| 56 | @end |
| 57 | @protocol NSCoding |
| 58 | - (void)encodeWithCoder:(NSCoder *)aCoder; |
| 59 | @end |
| 60 | @interface NSObject <NSObject> {} |
| 61 | - (id)init; |
| 62 | + (id)alloc; |
| 63 | @end |
| 64 | extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); |
| 65 | typedef struct {} NSFastEnumerationState; |
| 66 | @protocol NSFastEnumeration |
| 67 | - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len; |
| 68 | @end |
| 69 | @class NSString; |
| 70 | typedef struct _NSRange {} NSRange; |
| 71 | @interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> |
| 72 | - (NSUInteger)count; |
| 73 | @end |
| 74 | @interface NSMutableArray : NSArray |
| 75 | - (void)addObject:(id)anObject; |
| 76 | - (id)initWithCapacity:(NSUInteger)numItems; |
| 77 | @end |
| 78 | typedef unsigned short unichar; |
| 79 | @class NSData, NSArray, NSDictionary, NSCharacterSet, NSData, NSURL, NSError, NSLocale; |
| 80 | typedef NSUInteger NSStringCompareOptions; |
| 81 | @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; |
| 82 | - (NSComparisonResult)compare:(NSString *)string; |
| 83 | - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask; |
| 84 | - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange; |
| 85 | - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale; |
| 86 | - (NSComparisonResult)caseInsensitiveCompare:(NSString *)string; |
| 87 | - (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator; |
Ted Kremenek | e87450e | 2009-04-23 19:11:35 +0000 | [diff] [blame] | 88 | + (id)stringWithFormat:(NSString *)format, ... __attribute__((format(__NSString__, 1, 2))); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 89 | @end |
| 90 | @interface NSSimpleCString : NSString {} @end |
| 91 | @interface NSConstantString : NSSimpleCString @end |
| 92 | extern void *_NSConstantStringClassReference; |
| 93 | |
| 94 | //===----------------------------------------------------------------------===// |
| 95 | // Test cases. |
| 96 | //===----------------------------------------------------------------------===// |
| 97 | |
| 98 | NSComparisonResult f1(NSString* s) { |
| 99 | NSString *aString = 0; |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 100 | return [s compare:aString]; // expected-warning {{Argument to 'NSString' method 'compare:' cannot be nil}} |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | NSComparisonResult f2(NSString* s) { |
| 104 | NSString *aString = 0; |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 105 | return [s caseInsensitiveCompare:aString]; // expected-warning {{Argument to 'NSString' method 'caseInsensitiveCompare:' cannot be nil}} |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | NSComparisonResult f3(NSString* s, NSStringCompareOptions op) { |
| 109 | NSString *aString = 0; |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 110 | return [s compare:aString options:op]; // expected-warning {{Argument to 'NSString' method 'compare:options:' cannot be nil}} |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | NSComparisonResult f4(NSString* s, NSStringCompareOptions op, NSRange R) { |
| 114 | NSString *aString = 0; |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 115 | return [s compare:aString options:op range:R]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:' cannot be nil}} |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | NSComparisonResult f5(NSString* s, NSStringCompareOptions op, NSRange R) { |
| 119 | NSString *aString = 0; |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 120 | return [s compare:aString options:op range:R locale:0]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:locale:' cannot be nil}} |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 123 | NSArray *f6(NSString* s) { |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 124 | return [s componentsSeparatedByCharactersInSet:0]; // expected-warning {{Argument to 'NSString' method 'componentsSeparatedByCharactersInSet:' cannot be nil}} |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | NSString* f7(NSString* s1, NSString* s2, NSString* s3) { |
| 128 | |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 129 | NSString* s4 = (NSString*) |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 130 | CFStringCreateWithFormat(kCFAllocatorDefault, 0, // expected-warning{{leak}} |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 131 | (CFStringRef) __builtin___CFStringMakeConstantString("%@ %@ (%@)"), |
| 132 | s1, s2, s3); |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 133 | |
| 134 | CFRetain(s4); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 135 | return s4; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | NSMutableArray* f8() { |
| 139 | |
| 140 | NSString* s = [[NSString alloc] init]; |
| 141 | NSMutableArray* a = [[NSMutableArray alloc] initWithCapacity:2]; |
| 142 | [a addObject:s]; |
| 143 | [s release]; // no-warning |
| 144 | return a; |
| 145 | } |
| 146 | |
| 147 | void f9() { |
| 148 | |
| 149 | NSString* s = [[NSString alloc] init]; |
| 150 | NSString* q = s; |
| 151 | [s release]; |
| 152 | [q release]; // expected-warning {{used after it is released}} |
| 153 | } |
| 154 | |
| 155 | NSString* f10() { |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 156 | static NSString* s = 0; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 157 | if (!s) s = [[NSString alloc] init]; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 158 | return s; // no-warning |
| 159 | } |
| 160 | |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 161 | // Test case for regression reported in <rdar://problem/6452745>. |
| 162 | // Essentially 's' should not be considered allocated on the false branch. |
| 163 | // This exercises the 'EvalAssume' logic in GRTransferFuncs (CFRefCount.cpp). |
| 164 | NSString* f11(CFDictionaryRef dict, const char* key) { |
| 165 | NSString* s = (NSString*) CFDictionaryGetValue(dict, key); |
| 166 | [s retain]; |
| 167 | if (s) { |
| 168 | [s release]; |
| 169 | } |
Mike Stump | 4393b3f | 2009-07-21 18:46:15 +0000 | [diff] [blame] | 170 | return 0; |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Ted Kremenek | 784606f | 2008-12-18 23:40:58 +0000 | [diff] [blame] | 173 | // Test case for passing a tracked object by-reference to a function we |
Ted Kremenek | 09f1419 | 2009-04-21 20:01:03 +0000 | [diff] [blame] | 174 | // don't understand. |
Ted Kremenek | 784606f | 2008-12-18 23:40:58 +0000 | [diff] [blame] | 175 | void unknown_function_f12(NSString** s); |
| 176 | void f12() { |
| 177 | NSString *string = [[NSString alloc] init]; |
| 178 | unknown_function_f12(&string); // no-warning |
| 179 | } |
| 180 | |
Ted Kremenek | 09f1419 | 2009-04-21 20:01:03 +0000 | [diff] [blame] | 181 | // Test double release of CFString (PR 4014). |
| 182 | void f13(void) { |
| 183 | CFStringRef ref = CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100); |
| 184 | CFRelease(ref); |
| 185 | CFRelease(ref); // expected-warning{{Reference-counted object is used after it is released}} |
| 186 | } |
Ted Kremenek | 784606f | 2008-12-18 23:40:58 +0000 | [diff] [blame] | 187 | |
Anders Carlsson | b62bdce | 2011-03-08 20:05:26 +0000 | [diff] [blame] | 188 | @interface MyString : NSString |
| 189 | @end |
| 190 | |
| 191 | void f14(MyString *s) { |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 192 | [s compare:0]; // expected-warning {{Argument to 'MyString' method 'compare:' cannot be nil}} |
Anders Carlsson | b62bdce | 2011-03-08 20:05:26 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Ted Kremenek | 95d3b90 | 2009-05-11 15:26:06 +0000 | [diff] [blame] | 195 | // Test regular use of -autorelease |
| 196 | @interface TestAutorelease |
| 197 | -(NSString*) getString; |
| 198 | @end |
| 199 | @implementation TestAutorelease |
| 200 | -(NSString*) getString { |
| 201 | NSString *str = [[NSString alloc] init]; |
| 202 | return [str autorelease]; // no-warning |
| 203 | } |
Ted Kremenek | 2033a95 | 2009-05-13 07:12:33 +0000 | [diff] [blame] | 204 | - (void)m1 |
| 205 | { |
| 206 | NSString *s = [[NSString alloc] init]; // expected-warning{{leak}} |
| 207 | [s retain]; |
| 208 | [s autorelease]; |
| 209 | } |
| 210 | - (void)m2 |
| 211 | { |
| 212 | NSString *s = [[[NSString alloc] init] autorelease]; // expected-warning{{leak}} |
| 213 | [s retain]; |
| 214 | } |
| 215 | - (void)m3 |
| 216 | { |
| 217 | NSString *s = [[[NSString alloc] init] autorelease]; |
| 218 | [s retain]; |
| 219 | [s autorelease]; |
| 220 | } |
| 221 | - (void)m4 |
| 222 | { |
| 223 | NSString *s = [[NSString alloc] init]; // expected-warning{{leak}} |
| 224 | [s retain]; |
| 225 | } |
| 226 | - (void)m5 |
| 227 | { |
| 228 | NSString *s = [[NSString alloc] init]; |
| 229 | [s autorelease]; |
| 230 | } |
Ted Kremenek | 95d3b90 | 2009-05-11 15:26:06 +0000 | [diff] [blame] | 231 | @end |
| 232 | |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 233 | @interface C1 : NSObject {} |
| 234 | - (NSString*) getShared; |
| 235 | + (C1*) sharedInstance; |
| 236 | @end |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 237 | @implementation C1 : NSObject {} |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 238 | - (NSString*) getShared { |
| 239 | static NSString* s = 0; |
| 240 | if (!s) s = [[NSString alloc] init]; |
| 241 | return s; // no-warning |
| 242 | } |
| 243 | + (C1 *)sharedInstance { |
| 244 | static C1 *sharedInstance = 0; |
| 245 | if (!sharedInstance) { |
| 246 | sharedInstance = [[C1 alloc] init]; |
| 247 | } |
| 248 | return sharedInstance; // no-warning |
| 249 | } |
| 250 | @end |
| 251 | |
| 252 | @interface SharedClass : NSObject |
| 253 | + (id)sharedInstance; |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 254 | - (id)notShared; |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 255 | @end |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 256 | |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 257 | @implementation SharedClass |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 258 | |
| 259 | - (id)_init { |
| 260 | if ((self = [super init])) { |
| 261 | NSLog(@"Bar"); |
| 262 | } |
| 263 | return self; |
| 264 | } |
| 265 | |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 266 | - (id)notShared { |
Ted Kremenek | 043254a | 2009-02-07 22:55:48 +0000 | [diff] [blame] | 267 | return [[SharedClass alloc] _init]; // expected-warning{{leak}} |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Ted Kremenek | 859be3b | 2008-06-16 20:37:30 +0000 | [diff] [blame] | 270 | + (id)sharedInstance { |
| 271 | static SharedClass *_sharedInstance = 0; |
| 272 | if (!_sharedInstance) { |
| 273 | _sharedInstance = [[SharedClass alloc] _init]; |
| 274 | } |
| 275 | return _sharedInstance; // no-warning |
| 276 | } |
| 277 | @end |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 278 | |
| 279 | id testSharedClassFromFunction() { |
| 280 | return [[SharedClass alloc] _init]; // no-warning |
| 281 | } |
| 282 | |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 283 | // Test OSCompareAndSwap |
| 284 | _Bool OSAtomicCompareAndSwapPtr( void *__oldValue, void *__newValue, void * volatile *__theValue ); |
Ted Kremenek | b3bf76f | 2009-04-11 00:54:13 +0000 | [diff] [blame] | 285 | extern BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 286 | |
| 287 | void testOSCompareAndSwap() { |
| 288 | NSString *old = 0; |
Ted Kremenek | b3bf76f | 2009-04-11 00:54:13 +0000 | [diff] [blame] | 289 | NSString *s = [[NSString alloc] init]; // no-warning |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 290 | if (!OSAtomicCompareAndSwapPtr(0, s, (void**) &old)) |
| 291 | [s release]; |
| 292 | else |
| 293 | [old release]; |
| 294 | } |
| 295 | |
Ted Kremenek | 6bcd5a0 | 2009-12-09 23:29:55 +0000 | [diff] [blame] | 296 | void testOSCompareAndSwapXXBarrier_local() { |
Ted Kremenek | a3f4540 | 2009-04-29 16:03:59 +0000 | [diff] [blame] | 297 | NSString *old = 0; |
| 298 | NSString *s = [[NSString alloc] init]; // no-warning |
Ted Kremenek | 45f7c27 | 2009-10-15 01:40:34 +0000 | [diff] [blame] | 299 | if (!COMPARE_SWAP_BARRIER((intptr_t) 0, (intptr_t) s, (intptr_t*) &old)) |
Ted Kremenek | a3f4540 | 2009-04-29 16:03:59 +0000 | [diff] [blame] | 300 | [s release]; |
| 301 | else |
| 302 | [old release]; |
| 303 | } |
| 304 | |
Ted Kremenek | 6bcd5a0 | 2009-12-09 23:29:55 +0000 | [diff] [blame] | 305 | void testOSCompareAndSwapXXBarrier_local_no_direct_release() { |
Ted Kremenek | 45f7c27 | 2009-10-15 01:40:34 +0000 | [diff] [blame] | 306 | NSString *old = 0; |
Zhongxing Xu | 2f4a6b2 | 2009-12-09 08:32:57 +0000 | [diff] [blame] | 307 | NSString *s = [[NSString alloc] init]; // no-warning |
Ted Kremenek | 45f7c27 | 2009-10-15 01:40:34 +0000 | [diff] [blame] | 308 | if (!COMPARE_SWAP_BARRIER((intptr_t) 0, (intptr_t) s, (intptr_t*) &old)) |
| 309 | return; |
| 310 | else |
| 311 | [old release]; |
| 312 | } |
| 313 | |
| 314 | int testOSCompareAndSwapXXBarrier_id(Class myclass, id xclass) { |
| 315 | if (COMPARE_SWAP_BARRIER(0, (intptr_t) myclass, (intptr_t*) &xclass)) |
Ted Kremenek | 0aeaf5a | 2009-07-29 18:18:25 +0000 | [diff] [blame] | 316 | return 1; |
| 317 | return 0; |
Ted Kremenek | 45f7c27 | 2009-10-15 01:40:34 +0000 | [diff] [blame] | 318 | } |
Ted Kremenek | 0aeaf5a | 2009-07-29 18:18:25 +0000 | [diff] [blame] | 319 | |
Ted Kremenek | 6bcd5a0 | 2009-12-09 23:29:55 +0000 | [diff] [blame] | 320 | void test_objc_atomicCompareAndSwap_local() { |
Ted Kremenek | b3bf76f | 2009-04-11 00:54:13 +0000 | [diff] [blame] | 321 | NSString *old = 0; |
| 322 | NSString *s = [[NSString alloc] init]; // no-warning |
| 323 | if (!objc_atomicCompareAndSwapPtr(0, s, &old)) |
| 324 | [s release]; |
| 325 | else |
| 326 | [old release]; |
| 327 | } |
| 328 | |
Ted Kremenek | 6bcd5a0 | 2009-12-09 23:29:55 +0000 | [diff] [blame] | 329 | void test_objc_atomicCompareAndSwap_local_no_direct_release() { |
Ted Kremenek | 45f7c27 | 2009-10-15 01:40:34 +0000 | [diff] [blame] | 330 | NSString *old = 0; |
Zhongxing Xu | 2f4a6b2 | 2009-12-09 08:32:57 +0000 | [diff] [blame] | 331 | NSString *s = [[NSString alloc] init]; // no-warning |
Ted Kremenek | 45f7c27 | 2009-10-15 01:40:34 +0000 | [diff] [blame] | 332 | if (!objc_atomicCompareAndSwapPtr(0, s, &old)) |
| 333 | return; |
| 334 | else |
| 335 | [old release]; |
| 336 | } |
| 337 | |
Ted Kremenek | 6bcd5a0 | 2009-12-09 23:29:55 +0000 | [diff] [blame] | 338 | void test_objc_atomicCompareAndSwap_parameter(NSString **old) { |
| 339 | NSString *s = [[NSString alloc] init]; // no-warning |
| 340 | if (!objc_atomicCompareAndSwapPtr(0, s, old)) |
| 341 | [s release]; |
| 342 | else |
| 343 | [*old release]; |
| 344 | } |
| 345 | |
| 346 | void test_objc_atomicCompareAndSwap_parameter_no_direct_release(NSString **old) { |
| 347 | NSString *s = [[NSString alloc] init]; // expected-warning{{leak}} |
| 348 | if (!objc_atomicCompareAndSwapPtr(0, s, old)) |
| 349 | return; |
| 350 | else |
| 351 | [*old release]; |
| 352 | } |
| 353 | |
Ted Kremenek | 45f7c27 | 2009-10-15 01:40:34 +0000 | [diff] [blame] | 354 | |
Ted Kremenek | e87450e | 2009-04-23 19:11:35 +0000 | [diff] [blame] | 355 | // Test stringWithFormat (<rdar://problem/6815234>) |
| 356 | void test_stringWithFormat() { |
| 357 | NSString *string = [[NSString stringWithFormat:@"%ld", (long) 100] retain]; |
| 358 | [string release]; |
Ted Kremenek | f9df136 | 2009-04-23 21:25:57 +0000 | [diff] [blame] | 359 | [string release]; // expected-warning{{Incorrect decrement of the reference count}} |
Ted Kremenek | e87450e | 2009-04-23 19:11:35 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Ted Kremenek | 9251143 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 362 | // Test isTrackedObjectType(). |
Ted Kremenek | 97d095f | 2009-04-23 22:11:07 +0000 | [diff] [blame] | 363 | typedef NSString* WonkyTypedef; |
| 364 | @interface TestIsTracked |
| 365 | + (WonkyTypedef)newString; |
| 366 | @end |
| 367 | |
| 368 | void test_isTrackedObjectType(void) { |
| 369 | NSString *str = [TestIsTracked newString]; // expected-warning{{Potential leak}} |
| 370 | } |
Ted Kremenek | b3b0b36 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 371 | |
Ted Kremenek | 9251143 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 372 | // Test isTrackedCFObjectType(). |
| 373 | @interface TestIsCFTracked |
| 374 | + (CFStringRef) badNewCFString; |
| 375 | + (CFStringRef) newCFString; |
| 376 | @end |
| 377 | |
| 378 | @implementation TestIsCFTracked |
| 379 | + (CFStringRef) newCFString { |
| 380 | return CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100); // no-warning |
| 381 | } |
| 382 | + (CFStringRef) badNewCFString { |
| 383 | return CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100); // expected-warning{{leak}} |
| 384 | } |
| 385 | |
Ted Kremenek | b3b0b36 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 386 | // Test @synchronized |
| 387 | void test_synchronized(id x) { |
| 388 | @synchronized(x) { |
| 389 | NSString *string = [[NSString stringWithFormat:@"%ld", (long) 100] retain]; // expected-warning {{leak}} |
| 390 | } |
| 391 | } |
Fariborz Jahanian | 63e963c | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 392 | @end |
Zhongxing Xu | 604848a | 2010-01-11 06:52:53 +0000 | [diff] [blame] | 393 | |
| 394 | void testOSCompareAndSwapXXBarrier_parameter(NSString **old) { |
| 395 | NSString *s = [[NSString alloc] init]; // no-warning |
| 396 | if (!COMPARE_SWAP_BARRIER((intptr_t) 0, (intptr_t) s, (intptr_t*) old)) |
| 397 | [s release]; |
| 398 | else |
| 399 | [*old release]; |
| 400 | } |
Zhongxing Xu | 951b334 | 2010-01-11 07:40:00 +0000 | [diff] [blame] | 401 | |
| 402 | void testOSCompareAndSwapXXBarrier_parameter_no_direct_release(NSString **old) { |
| 403 | NSString *s = [[NSString alloc] init]; // no-warning |
| 404 | if (!COMPARE_SWAP_BARRIER((intptr_t) 0, (intptr_t) s, (intptr_t*) old)) |
| 405 | [s release]; |
| 406 | else |
| 407 | return; |
| 408 | } |