blob: c405fe24e590bbd44075d3c752d8f0dce60ee335 [file] [log] [blame]
Ted Kremenek0964a062009-01-21 06:57:53 +00001// RUN: clang -analyze -checker-cfref -verify -fobjc-gc %s &&
2// RUN: clang -analyze -checker-cfref -analyzer-store-region -verify -fobjc-gc %s
3
Ted Kremenek88739bf2008-06-16 18:46:17 +00004
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 Kremenekdfc996c2008-06-16 19:51:41 +000011// both svelte and portable to non-Mac platforms.
Ted Kremenek88739bf2008-06-16 18:46:17 +000012//===----------------------------------------------------------------------===//
13
14typedef const void * CFTypeRef;
Ted Kremenek12619382009-01-12 21:45:02 +000015void CFRelease(CFTypeRef cf);
16CFTypeRef CFRetain(CFTypeRef cf);
17CFTypeRef CFMakeCollectable(CFTypeRef cf);
Ted Kremenek88739bf2008-06-16 18:46:17 +000018typedef const struct __CFAllocator * CFAllocatorRef;
19typedef double CFTimeInterval;
20typedef CFTimeInterval CFAbsoluteTime;
21typedef const struct __CFDate * CFDateRef;
22extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at);
Ted Kremenek12619382009-01-12 21:45:02 +000023extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate);
24typedef struct objc_object {} *id;
Ted Kremenek88739bf2008-06-16 18:46:17 +000025typedef signed char BOOL;
Ted Kremenek88739bf2008-06-16 18:46:17 +000026static __inline__ __attribute__((always_inline)) id NSMakeCollectable(CFTypeRef cf) {}
Ted Kremenek12619382009-01-12 21:45:02 +000027@protocol NSObject - (BOOL)isEqual:(id)object;
28- (oneway void)release;
Ted Kremenek23b8eaa2009-01-28 06:25:48 +000029- (id)retain;
Ted Kremenek12619382009-01-12 21:45:02 +000030@end
31@class NSArray;
Ted Kremenek88739bf2008-06-16 18:46:17 +000032
33//===----------------------------------------------------------------------===//
34// Test cases.
35//===----------------------------------------------------------------------===//
36
Ted Kremenek12619382009-01-12 21:45:02 +000037CFAbsoluteTime f1_use_after_release() {
Ted Kremenek88739bf2008-06-16 18:46:17 +000038 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 Kremenek12619382009-01-12 21:45:02 +000048// The following two test cases verifies that CFMakeCollectable is a no-op
49// in non-GC mode and a "release" in GC mode.
Ted Kremenek23b8eaa2009-01-28 06:25:48 +000050CFAbsoluteTime f2_use_after_release() {
Ted Kremenek12619382009-01-12 21:45:02 +000051 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
61CFAbsoluteTime f2_noleak() {
Ted Kremenek2eff0f92008-11-05 22:17:39 +000062 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 Kremenek23b8eaa2009-01-28 06:25:48 +000072void f3_leak_with_gc() {
73 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());
74 [[(id) date retain] release]; // expected-warning{{leak}}
75}
76
Ted Kremenek12619382009-01-12 21:45:02 +000077// 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.
79CFAbsoluteTime 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}