blob: b23998cfc68f93972c9a3a63db6cf961a619482b [file] [log] [blame]
Ted Kremenek8382cf52009-11-13 18:46:29 +00001// RUN: clang-cc -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=basic -verify -fobjc-gc -analyzer-constraints=basic %s
2// RUN: clang-cc -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=basic -verify -fobjc-gc -analyzer-constraints=range %s
3// RUN: clang-cc -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=basic -verify -fobjc-gc -disable-free %s
4// RUN: clang-cc -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=region -analyzer-constraints=basic -verify -fobjc-gc %s
5// RUN: clang-cc -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=region -analyzer-constraints=range -verify -fobjc-gc %s
Ted Kremenek88739bf2008-06-16 18:46:17 +00006
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 Kremenekdfc996c2008-06-16 19:51:41 +000013// both svelte and portable to non-Mac platforms.
Ted Kremenek88739bf2008-06-16 18:46:17 +000014//===----------------------------------------------------------------------===//
15
16typedef const void * CFTypeRef;
Ted Kremenek12619382009-01-12 21:45:02 +000017void CFRelease(CFTypeRef cf);
18CFTypeRef CFRetain(CFTypeRef cf);
19CFTypeRef CFMakeCollectable(CFTypeRef cf);
Ted Kremenek88739bf2008-06-16 18:46:17 +000020typedef const struct __CFAllocator * CFAllocatorRef;
21typedef double CFTimeInterval;
22typedef CFTimeInterval CFAbsoluteTime;
23typedef const struct __CFDate * CFDateRef;
24extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at);
Ted Kremenek12619382009-01-12 21:45:02 +000025extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate);
26typedef struct objc_object {} *id;
Ted Kremenek88739bf2008-06-16 18:46:17 +000027typedef signed char BOOL;
Mike Stump655a63d2009-07-21 19:01:48 +000028static __inline__ __attribute__((always_inline)) id NSMakeCollectable(CFTypeRef cf) { return 0; }
Ted Kremenek12619382009-01-12 21:45:02 +000029@protocol NSObject - (BOOL)isEqual:(id)object;
30- (oneway void)release;
Ted Kremenek23b8eaa2009-01-28 06:25:48 +000031- (id)retain;
Ted Kremenek12619382009-01-12 21:45:02 +000032@end
33@class NSArray;
Ted Kremenek88739bf2008-06-16 18:46:17 +000034
35//===----------------------------------------------------------------------===//
36// Test cases.
37//===----------------------------------------------------------------------===//
38
Ted Kremenek12619382009-01-12 21:45:02 +000039CFAbsoluteTime f1_use_after_release() {
Ted Kremenek88739bf2008-06-16 18:46:17 +000040 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
41 CFDateRef date = CFDateCreate(0, t);
42 CFRetain(date);
43 [NSMakeCollectable(date) release];
44 CFDateGetAbsoluteTime(date); // no-warning
45 CFRelease(date);
46 t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}}
47 return t;
48}
49
Ted Kremenek12619382009-01-12 21:45:02 +000050// The following two test cases verifies that CFMakeCollectable is a no-op
51// in non-GC mode and a "release" in GC mode.
Ted Kremenek23b8eaa2009-01-28 06:25:48 +000052CFAbsoluteTime f2_use_after_release() {
Ted Kremenek12619382009-01-12 21:45:02 +000053 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
54 CFDateRef date = CFDateCreate(0, t);
55 CFRetain(date);
56 [(id) CFMakeCollectable(date) release];
57 CFDateGetAbsoluteTime(date); // no-warning
58 CFRelease(date);
59 t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}}
60 return t;
61}
62
63CFAbsoluteTime f2_noleak() {
Ted Kremenek2eff0f92008-11-05 22:17:39 +000064 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
65 CFDateRef date = CFDateCreate(0, t);
66 CFRetain(date);
67 [(id) CFMakeCollectable(date) release];
68 CFDateGetAbsoluteTime(date); // no-warning
69 t = CFDateGetAbsoluteTime(date); // no-warning
70 CFRelease(date); // no-warning
71 return t;
72}
73
Ted Kremenek23b8eaa2009-01-28 06:25:48 +000074void f3_leak_with_gc() {
Ted Kremenek7a101812009-02-13 00:39:34 +000075 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // expected-warning 2 {{leak}}
Ted Kremenekcf118d42009-02-04 23:49:09 +000076 [[(id) date retain] release];
Ted Kremenek23b8eaa2009-01-28 06:25:48 +000077}
78
Ted Kremenek12619382009-01-12 21:45:02 +000079// The following test case verifies that we "stop tracking" a retained object
80// when it is passed as an argument to an implicitly defined function.
81CFAbsoluteTime f4() {
82 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
83 CFDateRef date = CFDateCreate(0, t);
84 CFRetain(date);
85 some_implicitly_defined_function_stop_tracking(date); // no-warning
86 return t;
87}