CFDateGC.m revision e030358cc06e1cbce3c2e00ca67c946f9164b2a8
1// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=basic -verify -fobjc-gc -analyzer-constraints=basic %s  -Wno-implicit-function-declaration
2// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=basic -verify -fobjc-gc -analyzer-constraints=range %s  -Wno-implicit-function-declaration
3// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=basic -verify -fobjc-gc -disable-free %s  -Wno-implicit-function-declaration
4// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=region -analyzer-constraints=basic -verify -fobjc-gc %s  -Wno-implicit-function-declaration
5// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=region -analyzer-constraints=range -verify -fobjc-gc %s  -Wno-implicit-function-declaration
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 
13// both svelte and portable to non-Mac platforms.
14//===----------------------------------------------------------------------===//
15
16typedef const void * CFTypeRef;
17void CFRelease(CFTypeRef cf);
18CFTypeRef CFRetain(CFTypeRef cf);
19CFTypeRef CFMakeCollectable(CFTypeRef cf);
20typedef const struct __CFAllocator * CFAllocatorRef;
21typedef double CFTimeInterval;
22typedef CFTimeInterval CFAbsoluteTime;
23typedef const struct __CFDate * CFDateRef;
24extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at);
25extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate);
26typedef struct objc_object {} *id;
27typedef signed char BOOL;
28static __inline__ __attribute__((always_inline)) id NSMakeCollectable(CFTypeRef cf) { return 0; }
29@protocol NSObject  - (BOOL)isEqual:(id)object;
30- (oneway void)release;
31- (id)retain;
32@end
33@class NSArray;
34
35//===----------------------------------------------------------------------===//
36// Test cases.
37//===----------------------------------------------------------------------===//
38
39CFAbsoluteTime CFAbsoluteTimeGetCurrent();
40
41CFAbsoluteTime f1_use_after_release() {
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
52// The following two test cases verifies that CFMakeCollectable is a no-op
53// in non-GC mode and a "release" in GC mode.
54CFAbsoluteTime f2_use_after_release() {
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
65CFAbsoluteTime f2_noleak() {
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
76void f3_leak_with_gc() {
77  CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // expected-warning 2 {{leak}}
78  [[(id) date retain] release];
79}
80
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.
83CFAbsoluteTime 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}
90