2011-09-02 13:55:19 +08:00
|
|
|
// RUN: %clang_cc1 -analyze -analyzer-checker=core,osx.cocoa.RetainCount -analyzer-store=region -analyzer-constraints=range -verify -fobjc-gc %s -Wno-implicit-function-declaration
|
2008-06-17 02:46:17 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// The following code is reduced using delta-debugging from
|
|
|
|
// Foundation.h and CoreFoundation.h (Mac OS X).
|
|
|
|
//
|
|
|
|
// It includes the basic definitions for the test cases below.
|
|
|
|
// Not directly including [Core]Foundation.h directly makes this test case
|
2008-06-17 03:51:41 +08:00
|
|
|
// both svelte and portable to non-Mac platforms.
|
2008-06-17 02:46:17 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
typedef const void * CFTypeRef;
|
2009-01-13 05:45:02 +08:00
|
|
|
void CFRelease(CFTypeRef cf);
|
|
|
|
CFTypeRef CFRetain(CFTypeRef cf);
|
|
|
|
CFTypeRef CFMakeCollectable(CFTypeRef cf);
|
2008-06-17 02:46:17 +08:00
|
|
|
typedef const struct __CFAllocator * CFAllocatorRef;
|
|
|
|
typedef double CFTimeInterval;
|
|
|
|
typedef CFTimeInterval CFAbsoluteTime;
|
|
|
|
typedef const struct __CFDate * CFDateRef;
|
|
|
|
extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at);
|
2009-01-13 05:45:02 +08:00
|
|
|
extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate);
|
|
|
|
typedef struct objc_object {} *id;
|
2008-06-17 02:46:17 +08:00
|
|
|
typedef signed char BOOL;
|
2009-07-22 03:01:48 +08:00
|
|
|
static __inline__ __attribute__((always_inline)) id NSMakeCollectable(CFTypeRef cf) { return 0; }
|
2009-01-13 05:45:02 +08:00
|
|
|
@protocol NSObject - (BOOL)isEqual:(id)object;
|
|
|
|
- (oneway void)release;
|
2009-01-28 14:25:48 +08:00
|
|
|
- (id)retain;
|
2009-01-13 05:45:02 +08:00
|
|
|
@end
|
|
|
|
@class NSArray;
|
2008-06-17 02:46:17 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Test cases.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-01-10 04:43:19 +08:00
|
|
|
CFAbsoluteTime CFAbsoluteTimeGetCurrent();
|
|
|
|
|
2009-01-13 05:45:02 +08:00
|
|
|
CFAbsoluteTime f1_use_after_release() {
|
2008-06-17 02:46:17 +08:00
|
|
|
CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
|
|
|
|
CFDateRef date = CFDateCreate(0, t);
|
|
|
|
CFRetain(date);
|
|
|
|
[NSMakeCollectable(date) release];
|
|
|
|
CFDateGetAbsoluteTime(date); // no-warning
|
|
|
|
CFRelease(date);
|
2011-12-15 08:38:15 +08:00
|
|
|
t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released}}
|
2008-06-17 02:46:17 +08:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2009-01-13 05:45:02 +08:00
|
|
|
// The following two test cases verifies that CFMakeCollectable is a no-op
|
|
|
|
// in non-GC mode and a "release" in GC mode.
|
2009-01-28 14:25:48 +08:00
|
|
|
CFAbsoluteTime f2_use_after_release() {
|
2009-01-13 05:45:02 +08:00
|
|
|
CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
|
|
|
|
CFDateRef date = CFDateCreate(0, t);
|
|
|
|
CFRetain(date);
|
|
|
|
[(id) CFMakeCollectable(date) release];
|
|
|
|
CFDateGetAbsoluteTime(date); // no-warning
|
|
|
|
CFRelease(date);
|
2011-12-15 08:38:15 +08:00
|
|
|
t = CFDateGetAbsoluteTime(date); // expected-warning{{Reference-counted object is used after it is released}}
|
2009-01-13 05:45:02 +08:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFAbsoluteTime f2_noleak() {
|
2008-11-06 06:17:39 +08:00
|
|
|
CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
|
|
|
|
CFDateRef date = CFDateCreate(0, t);
|
|
|
|
CFRetain(date);
|
|
|
|
[(id) CFMakeCollectable(date) release];
|
|
|
|
CFDateGetAbsoluteTime(date); // no-warning
|
|
|
|
t = CFDateGetAbsoluteTime(date); // no-warning
|
|
|
|
CFRelease(date); // no-warning
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2009-01-28 14:25:48 +08:00
|
|
|
void f3_leak_with_gc() {
|
2009-02-13 08:39:34 +08:00
|
|
|
CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // expected-warning 2 {{leak}}
|
2009-02-05 07:49:09 +08:00
|
|
|
[[(id) date retain] release];
|
2009-01-28 14:25:48 +08:00
|
|
|
}
|
|
|
|
|
2009-01-13 05:45:02 +08:00
|
|
|
// The following test case verifies that we "stop tracking" a retained object
|
|
|
|
// when it is passed as an argument to an implicitly defined function.
|
|
|
|
CFAbsoluteTime f4() {
|
|
|
|
CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
|
|
|
|
CFDateRef date = CFDateCreate(0, t);
|
|
|
|
CFRetain(date);
|
|
|
|
some_implicitly_defined_function_stop_tracking(date); // no-warning
|
|
|
|
return t;
|
|
|
|
}
|