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