1 // RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -verify -fobjc-gc -analyzer-constraints=basic %s -Wno-implicit-function-declaration
2 // RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -verify -fobjc-gc -analyzer-constraints=range %s -Wno-implicit-function-declaration
3 // RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -verify -fobjc-gc -disable-free %s -Wno-implicit-function-declaration
4 // RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=basic -verify -fobjc-gc %s -Wno-implicit-function-declaration
5 // RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=range -verify -fobjc-gc %s -Wno-implicit-function-declaration
7 //===----------------------------------------------------------------------===//
8 // The following code is reduced using delta-debugging from
9 // Foundation.h and CoreFoundation.h (Mac OS X).
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 //===----------------------------------------------------------------------===//
16 typedef const void * CFTypeRef;
17 void CFRelease(CFTypeRef cf);
18 CFTypeRef CFRetain(CFTypeRef cf);
19 CFTypeRef CFMakeCollectable(CFTypeRef cf);
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);
25 extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate);
26 typedef struct objc_object {} *id;
27 typedef signed char BOOL;
28 static __inline__ __attribute__((always_inline)) id NSMakeCollectable(CFTypeRef cf) { return 0; }
29 @protocol NSObject - (BOOL)isEqual:(id)object;
30 - (oneway void)release;
35 //===----------------------------------------------------------------------===//
37 //===----------------------------------------------------------------------===//
39 CFAbsoluteTime CFAbsoluteTimeGetCurrent();
41 CFAbsoluteTime f1_use_after_release() {
42 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
43 CFDateRef date = CFDateCreate(0, t);
45 [NSMakeCollectable(date) release];
46 CFDateGetAbsoluteTime(date); // no-warning
48 t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}}
52 // The following two test cases verifies that CFMakeCollectable is a no-op
53 // in non-GC mode and a "release" in GC mode.
54 CFAbsoluteTime f2_use_after_release() {
55 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
56 CFDateRef date = CFDateCreate(0, t);
58 [(id) CFMakeCollectable(date) release];
59 CFDateGetAbsoluteTime(date); // no-warning
61 t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released.}}
65 CFAbsoluteTime f2_noleak() {
66 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
67 CFDateRef date = CFDateCreate(0, t);
69 [(id) CFMakeCollectable(date) release];
70 CFDateGetAbsoluteTime(date); // no-warning
71 t = CFDateGetAbsoluteTime(date); // no-warning
72 CFRelease(date); // no-warning
76 void f3_leak_with_gc() {
77 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // expected-warning 2 {{leak}}
78 [[(id) date retain] release];
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.
84 CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
85 CFDateRef date = CFDateCreate(0, t);
87 some_implicitly_defined_function_stop_tracking(date); // no-warning