blob: bd1a4b7967cad5dd92b5a29f1905e79e19580d14 [file] [log] [blame]
Argyrios Kyrtzidisd655ab22011-02-28 19:49:17 +00001// 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 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
Chris Lattnere0303582010-01-09 20:43:19 +000039CFAbsoluteTime CFAbsoluteTimeGetCurrent();
40
Ted Kremenek12619382009-01-12 21:45:02 +000041CFAbsoluteTime f1_use_after_release() {
Ted Kremenek88739bf2008-06-16 18:46:17 +000042 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 Kremenek12619382009-01-12 21:45:02 +000052// The following two test cases verifies that CFMakeCollectable is a no-op
53// in non-GC mode and a "release" in GC mode.
Ted Kremenek23b8eaa2009-01-28 06:25:48 +000054CFAbsoluteTime f2_use_after_release() {
Ted Kremenek12619382009-01-12 21:45:02 +000055 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() {
Ted Kremenek2eff0f92008-11-05 22:17:39 +000066 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 Kremenek23b8eaa2009-01-28 06:25:48 +000076void f3_leak_with_gc() {
Ted Kremenek7a101812009-02-13 00:39:34 +000077 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // expected-warning 2 {{leak}}
Ted Kremenekcf118d42009-02-04 23:49:09 +000078 [[(id) date retain] release];
Ted Kremenek23b8eaa2009-01-28 06:25:48 +000079}
80
Ted Kremenek12619382009-01-12 21:45:02 +000081// 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}