2012-10-12 04:58:21 +08:00
|
|
|
// RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,osx.cocoa.NilArg,osx.cocoa.RetainCount,alpha.core -analyzer-store=region -analyzer-constraints=range -verify -Wno-objc-root-class %s
|
2013-02-22 10:59:24 +08:00
|
|
|
// RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,osx.cocoa.NilArg,osx.cocoa.RetainCount,alpha.core -analyzer-store=region -analyzer-constraints=range -analyzer-config mode=shallow -verify -Wno-objc-root-class %s
|
2012-10-12 04:58:21 +08:00
|
|
|
// RUN: %clang_cc1 -DTEST_64 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,osx.cocoa.NilArg,osx.cocoa.RetainCount,alpha.core -analyzer-store=region -analyzer-constraints=range -verify -Wno-objc-root-class %s
|
2016-03-29 07:55:58 +08:00
|
|
|
// RUN: %clang_cc1 -DOSATOMIC_USE_INLINED -triple i386-apple-darwin10 -analyze -analyzer-checker=core,osx.cocoa.NilArg,osx.cocoa.RetainCount,alpha.core -analyzer-store=region -analyzer-constraints=range -verify -Wno-objc-root-class %s
|
2008-06-17 04:37:30 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// The following code is reduced using delta-debugging from
|
|
|
|
// Foundation.h (Mac OS X).
|
|
|
|
//
|
|
|
|
// It includes the basic definitions for the test cases below.
|
|
|
|
// Not directly including Foundation.h directly makes this test case
|
|
|
|
// both svelte and portable to non-Mac platforms.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-10-15 09:40:34 +08:00
|
|
|
#ifdef TEST_64
|
|
|
|
typedef long long int64_t;
|
|
|
|
_Bool OSAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue );
|
|
|
|
#define COMPARE_SWAP_BARRIER OSAtomicCompareAndSwap64Barrier
|
|
|
|
typedef int64_t intptr_t;
|
|
|
|
#else
|
2009-04-30 00:03:59 +08:00
|
|
|
typedef int int32_t;
|
2009-10-15 09:40:34 +08:00
|
|
|
_Bool OSAtomicCompareAndSwap32Barrier( int32_t __oldValue, int32_t __newValue, volatile int32_t *__theValue );
|
|
|
|
#define COMPARE_SWAP_BARRIER OSAtomicCompareAndSwap32Barrier
|
|
|
|
typedef int32_t intptr_t;
|
|
|
|
#endif
|
|
|
|
|
2008-06-17 04:37:30 +08:00
|
|
|
typedef const void * CFTypeRef;
|
|
|
|
typedef const struct __CFString * CFStringRef;
|
|
|
|
typedef const struct __CFAllocator * CFAllocatorRef;
|
|
|
|
extern const CFAllocatorRef kCFAllocatorDefault;
|
|
|
|
extern CFTypeRef CFRetain(CFTypeRef cf);
|
2009-04-22 04:01:03 +08:00
|
|
|
void CFRelease(CFTypeRef cf);
|
2008-06-17 04:37:30 +08:00
|
|
|
typedef const struct __CFDictionary * CFDictionaryRef;
|
2008-12-18 05:50:35 +08:00
|
|
|
const void *CFDictionaryGetValue(CFDictionaryRef theDict, const void *key);
|
2008-06-17 04:37:30 +08:00
|
|
|
extern CFStringRef CFStringCreateWithFormat(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, ...);
|
|
|
|
typedef signed char BOOL;
|
|
|
|
typedef int NSInteger;
|
|
|
|
typedef unsigned int NSUInteger;
|
|
|
|
@class NSString, Protocol;
|
|
|
|
extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
|
|
|
|
typedef NSInteger NSComparisonResult;
|
|
|
|
typedef struct _NSZone NSZone;
|
|
|
|
@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
|
|
|
|
@protocol NSObject
|
|
|
|
- (BOOL)isEqual:(id)object;
|
|
|
|
- (oneway void)release;
|
2008-12-18 05:50:35 +08:00
|
|
|
- (id)retain;
|
2009-05-11 23:26:06 +08:00
|
|
|
- (id)autorelease;
|
2008-06-17 04:37:30 +08:00
|
|
|
@end
|
|
|
|
@protocol NSCopying
|
|
|
|
- (id)copyWithZone:(NSZone *)zone;
|
|
|
|
@end
|
|
|
|
@protocol NSMutableCopying
|
|
|
|
- (id)mutableCopyWithZone:(NSZone *)zone;
|
|
|
|
@end
|
|
|
|
@protocol NSCoding
|
|
|
|
- (void)encodeWithCoder:(NSCoder *)aCoder;
|
|
|
|
@end
|
|
|
|
@interface NSObject <NSObject> {}
|
|
|
|
- (id)init;
|
|
|
|
+ (id)alloc;
|
|
|
|
@end
|
|
|
|
extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
|
|
|
|
typedef struct {} NSFastEnumerationState;
|
|
|
|
@protocol NSFastEnumeration
|
|
|
|
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
|
|
|
|
@end
|
|
|
|
@class NSString;
|
|
|
|
typedef struct _NSRange {} NSRange;
|
|
|
|
@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>
|
|
|
|
- (NSUInteger)count;
|
|
|
|
@end
|
|
|
|
@interface NSMutableArray : NSArray
|
|
|
|
- (void)addObject:(id)anObject;
|
|
|
|
- (id)initWithCapacity:(NSUInteger)numItems;
|
|
|
|
@end
|
|
|
|
typedef unsigned short unichar;
|
|
|
|
@class NSData, NSArray, NSDictionary, NSCharacterSet, NSData, NSURL, NSError, NSLocale;
|
|
|
|
typedef NSUInteger NSStringCompareOptions;
|
|
|
|
@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length;
|
|
|
|
- (NSComparisonResult)compare:(NSString *)string;
|
|
|
|
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;
|
|
|
|
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange;
|
|
|
|
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale;
|
|
|
|
- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
|
|
|
|
- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator;
|
2009-04-24 03:11:35 +08:00
|
|
|
+ (id)stringWithFormat:(NSString *)format, ... __attribute__((format(__NSString__, 1, 2)));
|
2008-06-17 04:37:30 +08:00
|
|
|
@end
|
|
|
|
@interface NSSimpleCString : NSString {} @end
|
|
|
|
@interface NSConstantString : NSSimpleCString @end
|
|
|
|
extern void *_NSConstantStringClassReference;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Test cases.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
NSComparisonResult f1(NSString* s) {
|
|
|
|
NSString *aString = 0;
|
2011-12-15 08:38:15 +08:00
|
|
|
return [s compare:aString]; // expected-warning {{Argument to 'NSString' method 'compare:' cannot be nil}}
|
2008-06-17 04:37:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NSComparisonResult f2(NSString* s) {
|
|
|
|
NSString *aString = 0;
|
2011-12-15 08:38:15 +08:00
|
|
|
return [s caseInsensitiveCompare:aString]; // expected-warning {{Argument to 'NSString' method 'caseInsensitiveCompare:' cannot be nil}}
|
2008-06-17 04:37:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NSComparisonResult f3(NSString* s, NSStringCompareOptions op) {
|
|
|
|
NSString *aString = 0;
|
2011-12-15 08:38:15 +08:00
|
|
|
return [s compare:aString options:op]; // expected-warning {{Argument to 'NSString' method 'compare:options:' cannot be nil}}
|
2008-06-17 04:37:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NSComparisonResult f4(NSString* s, NSStringCompareOptions op, NSRange R) {
|
|
|
|
NSString *aString = 0;
|
2011-12-15 08:38:15 +08:00
|
|
|
return [s compare:aString options:op range:R]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:' cannot be nil}}
|
2008-06-17 04:37:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NSComparisonResult f5(NSString* s, NSStringCompareOptions op, NSRange R) {
|
|
|
|
NSString *aString = 0;
|
2011-12-15 08:38:15 +08:00
|
|
|
return [s compare:aString options:op range:R locale:0]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:locale:' cannot be nil}}
|
2008-06-17 04:37:30 +08:00
|
|
|
}
|
|
|
|
|
2008-08-05 08:07:51 +08:00
|
|
|
NSArray *f6(NSString* s) {
|
2011-12-15 08:38:15 +08:00
|
|
|
return [s componentsSeparatedByCharactersInSet:0]; // expected-warning {{Argument to 'NSString' method 'componentsSeparatedByCharactersInSet:' cannot be nil}}
|
2008-06-17 04:37:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NSString* f7(NSString* s1, NSString* s2, NSString* s3) {
|
|
|
|
|
2008-08-05 08:07:51 +08:00
|
|
|
NSString* s4 = (NSString*)
|
2009-02-05 07:49:09 +08:00
|
|
|
CFStringCreateWithFormat(kCFAllocatorDefault, 0, // expected-warning{{leak}}
|
2008-08-05 08:07:51 +08:00
|
|
|
(CFStringRef) __builtin___CFStringMakeConstantString("%@ %@ (%@)"),
|
|
|
|
s1, s2, s3);
|
2008-06-17 04:37:30 +08:00
|
|
|
|
|
|
|
CFRetain(s4);
|
2009-02-05 07:49:09 +08:00
|
|
|
return s4;
|
2008-06-17 04:37:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NSMutableArray* f8() {
|
|
|
|
|
|
|
|
NSString* s = [[NSString alloc] init];
|
|
|
|
NSMutableArray* a = [[NSMutableArray alloc] initWithCapacity:2];
|
|
|
|
[a addObject:s];
|
|
|
|
[s release]; // no-warning
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
void f9() {
|
|
|
|
|
|
|
|
NSString* s = [[NSString alloc] init];
|
|
|
|
NSString* q = s;
|
|
|
|
[s release];
|
|
|
|
[q release]; // expected-warning {{used after it is released}}
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString* f10() {
|
|
|
|
static NSString* s = 0;
|
|
|
|
if (!s) s = [[NSString alloc] init];
|
|
|
|
return s; // no-warning
|
|
|
|
}
|
|
|
|
|
2008-12-18 05:50:35 +08:00
|
|
|
// Test case for regression reported in <rdar://problem/6452745>.
|
|
|
|
// Essentially 's' should not be considered allocated on the false branch.
|
|
|
|
// This exercises the 'EvalAssume' logic in GRTransferFuncs (CFRefCount.cpp).
|
|
|
|
NSString* f11(CFDictionaryRef dict, const char* key) {
|
|
|
|
NSString* s = (NSString*) CFDictionaryGetValue(dict, key);
|
|
|
|
[s retain];
|
|
|
|
if (s) {
|
|
|
|
[s release];
|
|
|
|
}
|
2009-07-22 02:46:15 +08:00
|
|
|
return 0;
|
2008-12-18 05:50:35 +08:00
|
|
|
}
|
|
|
|
|
2008-12-19 07:40:58 +08:00
|
|
|
// Test case for passing a tracked object by-reference to a function we
|
2009-04-22 04:01:03 +08:00
|
|
|
// don't understand.
|
2008-12-19 07:40:58 +08:00
|
|
|
void unknown_function_f12(NSString** s);
|
|
|
|
void f12() {
|
|
|
|
NSString *string = [[NSString alloc] init];
|
|
|
|
unknown_function_f12(&string); // no-warning
|
|
|
|
}
|
|
|
|
|
2009-04-22 04:01:03 +08:00
|
|
|
// Test double release of CFString (PR 4014).
|
|
|
|
void f13(void) {
|
|
|
|
CFStringRef ref = CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100);
|
|
|
|
CFRelease(ref);
|
|
|
|
CFRelease(ref); // expected-warning{{Reference-counted object is used after it is released}}
|
|
|
|
}
|
2008-12-19 07:40:58 +08:00
|
|
|
|
2011-03-09 04:05:26 +08:00
|
|
|
@interface MyString : NSString
|
|
|
|
@end
|
|
|
|
|
|
|
|
void f14(MyString *s) {
|
2011-12-15 08:38:15 +08:00
|
|
|
[s compare:0]; // expected-warning {{Argument to 'MyString' method 'compare:' cannot be nil}}
|
2011-03-09 04:05:26 +08:00
|
|
|
}
|
|
|
|
|
2009-05-11 23:26:06 +08:00
|
|
|
// Test regular use of -autorelease
|
|
|
|
@interface TestAutorelease
|
|
|
|
-(NSString*) getString;
|
|
|
|
@end
|
|
|
|
@implementation TestAutorelease
|
|
|
|
-(NSString*) getString {
|
|
|
|
NSString *str = [[NSString alloc] init];
|
|
|
|
return [str autorelease]; // no-warning
|
|
|
|
}
|
2009-05-13 15:12:33 +08:00
|
|
|
- (void)m1
|
|
|
|
{
|
|
|
|
NSString *s = [[NSString alloc] init]; // expected-warning{{leak}}
|
|
|
|
[s retain];
|
|
|
|
[s autorelease];
|
|
|
|
}
|
|
|
|
- (void)m2
|
|
|
|
{
|
|
|
|
NSString *s = [[[NSString alloc] init] autorelease]; // expected-warning{{leak}}
|
|
|
|
[s retain];
|
|
|
|
}
|
|
|
|
- (void)m3
|
|
|
|
{
|
|
|
|
NSString *s = [[[NSString alloc] init] autorelease];
|
|
|
|
[s retain];
|
|
|
|
[s autorelease];
|
|
|
|
}
|
|
|
|
- (void)m4
|
|
|
|
{
|
|
|
|
NSString *s = [[NSString alloc] init]; // expected-warning{{leak}}
|
|
|
|
[s retain];
|
|
|
|
}
|
|
|
|
- (void)m5
|
|
|
|
{
|
|
|
|
NSString *s = [[NSString alloc] init];
|
|
|
|
[s autorelease];
|
|
|
|
}
|
2009-05-11 23:26:06 +08:00
|
|
|
@end
|
|
|
|
|
2008-06-17 04:37:30 +08:00
|
|
|
@interface C1 : NSObject {}
|
|
|
|
- (NSString*) getShared;
|
|
|
|
+ (C1*) sharedInstance;
|
|
|
|
@end
|
2008-07-03 23:37:02 +08:00
|
|
|
@implementation C1 : NSObject {}
|
2008-06-17 04:37:30 +08:00
|
|
|
- (NSString*) getShared {
|
|
|
|
static NSString* s = 0;
|
|
|
|
if (!s) s = [[NSString alloc] init];
|
|
|
|
return s; // no-warning
|
|
|
|
}
|
|
|
|
+ (C1 *)sharedInstance {
|
|
|
|
static C1 *sharedInstance = 0;
|
|
|
|
if (!sharedInstance) {
|
|
|
|
sharedInstance = [[C1 alloc] init];
|
|
|
|
}
|
|
|
|
return sharedInstance; // no-warning
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface SharedClass : NSObject
|
|
|
|
+ (id)sharedInstance;
|
2009-01-07 08:39:56 +08:00
|
|
|
- (id)notShared;
|
2008-06-17 04:37:30 +08:00
|
|
|
@end
|
2009-01-07 08:39:56 +08:00
|
|
|
|
2008-07-03 23:37:02 +08:00
|
|
|
@implementation SharedClass
|
2008-06-17 04:37:30 +08:00
|
|
|
|
|
|
|
- (id)_init {
|
|
|
|
if ((self = [super init])) {
|
|
|
|
NSLog(@"Bar");
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2009-01-07 08:39:56 +08:00
|
|
|
- (id)notShared {
|
2009-02-08 06:55:48 +08:00
|
|
|
return [[SharedClass alloc] _init]; // expected-warning{{leak}}
|
2009-01-07 08:39:56 +08:00
|
|
|
}
|
|
|
|
|
2008-06-17 04:37:30 +08:00
|
|
|
+ (id)sharedInstance {
|
|
|
|
static SharedClass *_sharedInstance = 0;
|
|
|
|
if (!_sharedInstance) {
|
|
|
|
_sharedInstance = [[SharedClass alloc] _init];
|
|
|
|
}
|
|
|
|
return _sharedInstance; // no-warning
|
|
|
|
}
|
|
|
|
@end
|
2009-01-07 08:39:56 +08:00
|
|
|
|
|
|
|
id testSharedClassFromFunction() {
|
|
|
|
return [[SharedClass alloc] _init]; // no-warning
|
|
|
|
}
|
|
|
|
|
2016-03-29 07:55:58 +08:00
|
|
|
#if !(defined(OSATOMIC_USE_INLINED) && OSATOMIC_USE_INLINED)
|
2009-04-11 08:11:10 +08:00
|
|
|
// Test OSCompareAndSwap
|
|
|
|
_Bool OSAtomicCompareAndSwapPtr( void *__oldValue, void *__newValue, void * volatile *__theValue );
|
2009-04-11 08:54:13 +08:00
|
|
|
extern BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation);
|
2016-03-29 07:55:58 +08:00
|
|
|
#else
|
|
|
|
// Test that the body farm models are still used even when a body is available.
|
|
|
|
_Bool opaque_OSAtomicCompareAndSwapPtr( void *__oldValue, void *__newValue, void * volatile *__theValue );
|
|
|
|
_Bool OSAtomicCompareAndSwapPtr( void *__oldValue, void *__newValue, void * volatile *__theValue ) {
|
|
|
|
return opaque_OSAtomicCompareAndSwapPtr(__oldValue, __newValue, __theValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern BOOL opaque_objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation);
|
|
|
|
extern BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation) {
|
|
|
|
return opaque_objc_atomicCompareAndSwapPtr(predicate, replacement, objectLocation);
|
|
|
|
}
|
|
|
|
#endif
|
2009-04-11 08:11:10 +08:00
|
|
|
|
|
|
|
void testOSCompareAndSwap() {
|
|
|
|
NSString *old = 0;
|
2009-04-11 08:54:13 +08:00
|
|
|
NSString *s = [[NSString alloc] init]; // no-warning
|
2009-04-11 08:11:10 +08:00
|
|
|
if (!OSAtomicCompareAndSwapPtr(0, s, (void**) &old))
|
|
|
|
[s release];
|
|
|
|
else
|
|
|
|
[old release];
|
|
|
|
}
|
|
|
|
|
2009-12-10 07:29:55 +08:00
|
|
|
void testOSCompareAndSwapXXBarrier_local() {
|
2009-04-30 00:03:59 +08:00
|
|
|
NSString *old = 0;
|
|
|
|
NSString *s = [[NSString alloc] init]; // no-warning
|
2009-10-15 09:40:34 +08:00
|
|
|
if (!COMPARE_SWAP_BARRIER((intptr_t) 0, (intptr_t) s, (intptr_t*) &old))
|
2009-04-30 00:03:59 +08:00
|
|
|
[s release];
|
|
|
|
else
|
|
|
|
[old release];
|
|
|
|
}
|
|
|
|
|
2009-12-10 07:29:55 +08:00
|
|
|
void testOSCompareAndSwapXXBarrier_local_no_direct_release() {
|
2009-10-15 09:40:34 +08:00
|
|
|
NSString *old = 0;
|
2009-12-09 16:32:57 +08:00
|
|
|
NSString *s = [[NSString alloc] init]; // no-warning
|
2009-10-15 09:40:34 +08:00
|
|
|
if (!COMPARE_SWAP_BARRIER((intptr_t) 0, (intptr_t) s, (intptr_t*) &old))
|
|
|
|
return;
|
|
|
|
else
|
|
|
|
[old release];
|
|
|
|
}
|
|
|
|
|
|
|
|
int testOSCompareAndSwapXXBarrier_id(Class myclass, id xclass) {
|
|
|
|
if (COMPARE_SWAP_BARRIER(0, (intptr_t) myclass, (intptr_t*) &xclass))
|
2009-07-30 02:18:25 +08:00
|
|
|
return 1;
|
|
|
|
return 0;
|
2009-10-15 09:40:34 +08:00
|
|
|
}
|
2009-07-30 02:18:25 +08:00
|
|
|
|
2009-12-10 07:29:55 +08:00
|
|
|
void test_objc_atomicCompareAndSwap_local() {
|
2009-04-11 08:54:13 +08:00
|
|
|
NSString *old = 0;
|
|
|
|
NSString *s = [[NSString alloc] init]; // no-warning
|
|
|
|
if (!objc_atomicCompareAndSwapPtr(0, s, &old))
|
|
|
|
[s release];
|
|
|
|
else
|
|
|
|
[old release];
|
|
|
|
}
|
|
|
|
|
2009-12-10 07:29:55 +08:00
|
|
|
void test_objc_atomicCompareAndSwap_local_no_direct_release() {
|
2009-10-15 09:40:34 +08:00
|
|
|
NSString *old = 0;
|
2009-12-09 16:32:57 +08:00
|
|
|
NSString *s = [[NSString alloc] init]; // no-warning
|
2009-10-15 09:40:34 +08:00
|
|
|
if (!objc_atomicCompareAndSwapPtr(0, s, &old))
|
|
|
|
return;
|
|
|
|
else
|
|
|
|
[old release];
|
|
|
|
}
|
|
|
|
|
2009-12-10 07:29:55 +08:00
|
|
|
void test_objc_atomicCompareAndSwap_parameter(NSString **old) {
|
|
|
|
NSString *s = [[NSString alloc] init]; // no-warning
|
|
|
|
if (!objc_atomicCompareAndSwapPtr(0, s, old))
|
|
|
|
[s release];
|
|
|
|
else
|
|
|
|
[*old release];
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_objc_atomicCompareAndSwap_parameter_no_direct_release(NSString **old) {
|
|
|
|
NSString *s = [[NSString alloc] init]; // expected-warning{{leak}}
|
|
|
|
if (!objc_atomicCompareAndSwapPtr(0, s, old))
|
|
|
|
return;
|
|
|
|
else
|
|
|
|
[*old release];
|
|
|
|
}
|
|
|
|
|
2009-10-15 09:40:34 +08:00
|
|
|
|
2009-04-24 03:11:35 +08:00
|
|
|
// Test stringWithFormat (<rdar://problem/6815234>)
|
|
|
|
void test_stringWithFormat() {
|
|
|
|
NSString *string = [[NSString stringWithFormat:@"%ld", (long) 100] retain];
|
|
|
|
[string release];
|
2009-04-24 05:25:57 +08:00
|
|
|
[string release]; // expected-warning{{Incorrect decrement of the reference count}}
|
2009-04-24 03:11:35 +08:00
|
|
|
}
|
|
|
|
|
2009-05-03 14:08:32 +08:00
|
|
|
// Test isTrackedObjectType().
|
2009-04-24 06:11:07 +08:00
|
|
|
typedef NSString* WonkyTypedef;
|
|
|
|
@interface TestIsTracked
|
|
|
|
+ (WonkyTypedef)newString;
|
|
|
|
@end
|
|
|
|
|
|
|
|
void test_isTrackedObjectType(void) {
|
|
|
|
NSString *str = [TestIsTracked newString]; // expected-warning{{Potential leak}}
|
|
|
|
}
|
2009-05-02 09:49:13 +08:00
|
|
|
|
2009-05-03 14:08:32 +08:00
|
|
|
// Test isTrackedCFObjectType().
|
|
|
|
@interface TestIsCFTracked
|
|
|
|
+ (CFStringRef) badNewCFString;
|
|
|
|
+ (CFStringRef) newCFString;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation TestIsCFTracked
|
|
|
|
+ (CFStringRef) newCFString {
|
|
|
|
return CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100); // no-warning
|
|
|
|
}
|
|
|
|
+ (CFStringRef) badNewCFString {
|
|
|
|
return CFStringCreateWithFormat(kCFAllocatorDefault, ((void*)0), ((CFStringRef) __builtin___CFStringMakeConstantString ("" "%d" "")), 100); // expected-warning{{leak}}
|
|
|
|
}
|
|
|
|
|
2009-05-02 09:49:13 +08:00
|
|
|
// Test @synchronized
|
|
|
|
void test_synchronized(id x) {
|
|
|
|
@synchronized(x) {
|
|
|
|
NSString *string = [[NSString stringWithFormat:@"%ld", (long) 100] retain]; // expected-warning {{leak}}
|
|
|
|
}
|
|
|
|
}
|
2009-11-17 02:57:01 +08:00
|
|
|
@end
|
2010-01-11 14:52:53 +08:00
|
|
|
|
|
|
|
void testOSCompareAndSwapXXBarrier_parameter(NSString **old) {
|
|
|
|
NSString *s = [[NSString alloc] init]; // no-warning
|
|
|
|
if (!COMPARE_SWAP_BARRIER((intptr_t) 0, (intptr_t) s, (intptr_t*) old))
|
|
|
|
[s release];
|
|
|
|
else
|
|
|
|
[*old release];
|
|
|
|
}
|
2010-01-11 15:40:00 +08:00
|
|
|
|
|
|
|
void testOSCompareAndSwapXXBarrier_parameter_no_direct_release(NSString **old) {
|
|
|
|
NSString *s = [[NSString alloc] init]; // no-warning
|
|
|
|
if (!COMPARE_SWAP_BARRIER((intptr_t) 0, (intptr_t) s, (intptr_t*) old))
|
|
|
|
[s release];
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
2013-02-02 08:30:04 +08:00
|
|
|
|
|
|
|
@interface AlwaysInlineBodyFarmBodies : NSObject {
|
|
|
|
NSString *_value;
|
|
|
|
}
|
|
|
|
- (NSString *)_value;
|
|
|
|
- (void)callValue;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation AlwaysInlineBodyFarmBodies
|
|
|
|
|
|
|
|
- (NSString *)_value {
|
|
|
|
if (!_value) {
|
|
|
|
NSString *s = [[NSString alloc] init];
|
|
|
|
if (!OSAtomicCompareAndSwapPtr(0, s, (void**)&_value)) {
|
|
|
|
[s release];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)callValue {
|
|
|
|
[self _value];
|
|
|
|
}
|
|
|
|
@end
|