Ted Kremenek | 0964a06 | 2009-01-21 06:57:53 +0000 | [diff] [blame] | 1 | // RUN: clang -analyze -checker-cfref -verify -fobjc-gc %s && |
| 2 | // RUN: clang -analyze -checker-cfref -analyzer-store-region -verify -fobjc-gc %s |
| 3 | |
Ted Kremenek | 88739bf | 2008-06-16 18:46:17 +0000 | [diff] [blame] | 4 | |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | // The following code is reduced using delta-debugging from |
| 7 | // Foundation.h and CoreFoundation.h (Mac OS X). |
| 8 | // |
| 9 | // It includes the basic definitions for the test cases below. |
| 10 | // Not directly including [Core]Foundation.h directly makes this test case |
Ted Kremenek | dfc996c | 2008-06-16 19:51:41 +0000 | [diff] [blame] | 11 | // both svelte and portable to non-Mac platforms. |
Ted Kremenek | 88739bf | 2008-06-16 18:46:17 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | typedef const void * CFTypeRef; |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 15 | void CFRelease(CFTypeRef cf); |
| 16 | CFTypeRef CFRetain(CFTypeRef cf); |
| 17 | CFTypeRef CFMakeCollectable(CFTypeRef cf); |
Ted Kremenek | 88739bf | 2008-06-16 18:46:17 +0000 | [diff] [blame] | 18 | typedef const struct __CFAllocator * CFAllocatorRef; |
| 19 | typedef double CFTimeInterval; |
| 20 | typedef CFTimeInterval CFAbsoluteTime; |
| 21 | typedef const struct __CFDate * CFDateRef; |
| 22 | extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 23 | extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate); |
| 24 | typedef struct objc_object {} *id; |
Ted Kremenek | 88739bf | 2008-06-16 18:46:17 +0000 | [diff] [blame] | 25 | typedef signed char BOOL; |
Ted Kremenek | 88739bf | 2008-06-16 18:46:17 +0000 | [diff] [blame] | 26 | static __inline__ __attribute__((always_inline)) id NSMakeCollectable(CFTypeRef cf) {} |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 27 | @protocol NSObject - (BOOL)isEqual:(id)object; |
| 28 | - (oneway void)release; |
Ted Kremenek | 23b8eaa | 2009-01-28 06:25:48 +0000 | [diff] [blame^] | 29 | - (id)retain; |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 30 | @end |
| 31 | @class NSArray; |
Ted Kremenek | 88739bf | 2008-06-16 18:46:17 +0000 | [diff] [blame] | 32 | |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | // Test cases. |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 37 | CFAbsoluteTime f1_use_after_release() { |
Ted Kremenek | 88739bf | 2008-06-16 18:46:17 +0000 | [diff] [blame] | 38 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 39 | CFDateRef date = CFDateCreate(0, t); |
| 40 | CFRetain(date); |
| 41 | [NSMakeCollectable(date) release]; |
| 42 | CFDateGetAbsoluteTime(date); // no-warning |
| 43 | CFRelease(date); |
| 44 | t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}} |
| 45 | return t; |
| 46 | } |
| 47 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 48 | // The following two test cases verifies that CFMakeCollectable is a no-op |
| 49 | // in non-GC mode and a "release" in GC mode. |
Ted Kremenek | 23b8eaa | 2009-01-28 06:25:48 +0000 | [diff] [blame^] | 50 | CFAbsoluteTime f2_use_after_release() { |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 51 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 52 | CFDateRef date = CFDateCreate(0, t); |
| 53 | CFRetain(date); |
| 54 | [(id) CFMakeCollectable(date) release]; |
| 55 | CFDateGetAbsoluteTime(date); // no-warning |
| 56 | CFRelease(date); |
| 57 | t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}} |
| 58 | return t; |
| 59 | } |
| 60 | |
| 61 | CFAbsoluteTime f2_noleak() { |
Ted Kremenek | 2eff0f9 | 2008-11-05 22:17:39 +0000 | [diff] [blame] | 62 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 63 | CFDateRef date = CFDateCreate(0, t); |
| 64 | CFRetain(date); |
| 65 | [(id) CFMakeCollectable(date) release]; |
| 66 | CFDateGetAbsoluteTime(date); // no-warning |
| 67 | t = CFDateGetAbsoluteTime(date); // no-warning |
| 68 | CFRelease(date); // no-warning |
| 69 | return t; |
| 70 | } |
| 71 | |
Ted Kremenek | 23b8eaa | 2009-01-28 06:25:48 +0000 | [diff] [blame^] | 72 | void f3_leak_with_gc() { |
| 73 | CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); |
| 74 | [[(id) date retain] release]; // expected-warning{{leak}} |
| 75 | } |
| 76 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 77 | // The following test case verifies that we "stop tracking" a retained object |
| 78 | // when it is passed as an argument to an implicitly defined function. |
| 79 | CFAbsoluteTime f4() { |
| 80 | CFAbsoluteTime t = CFAbsoluteTimeGetCurrent(); |
| 81 | CFDateRef date = CFDateCreate(0, t); |
| 82 | CFRetain(date); |
| 83 | some_implicitly_defined_function_stop_tracking(date); // no-warning |
| 84 | return t; |
| 85 | } |