| 1 |
// RUN: %clang_analyze_cc1 -analyzer-checker=core,osx.cocoa.RetainCount -Wno-objc-root-class -verify %s |
| 2 |
// expected-no-diagnostics |
| 3 |
|
| 4 |
//===----------------------------------------------------------------------===// |
| 5 |
// The following code is reduced using delta-debugging from |
| 6 |
// Foundation.h (Mac OS X). |
| 7 |
// |
| 8 |
// It includes the basic definitions for the test cases below. |
| 9 |
// Not directly including Foundation.h directly makes this test case |
| 10 |
// both svelte and portable to non-Mac platforms. |
| 11 |
//===----------------------------------------------------------------------===// |
| 12 |
|
| 13 |
typedef const void * CFTypeRef; |
| 14 |
typedef const struct __CFString * CFStringRef; |
| 15 |
typedef const struct __CFAllocator * CFAllocatorRef; |
| 16 |
extern const CFAllocatorRef kCFAllocatorDefault; |
| 17 |
extern CFTypeRef CFRetain(CFTypeRef cf); |
| 18 |
void CFRelease(CFTypeRef cf); |
| 19 |
typedef const struct __CFDictionary * CFDictionaryRef; |
| 20 |
const void *CFDictionaryGetValue(CFDictionaryRef theDict, const void *key); |
| 21 |
extern CFStringRef CFStringCreateWithFormat(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, ...); |
| 22 |
typedef signed char BOOL; |
| 23 |
typedef int NSInteger; |
| 24 |
typedef unsigned int NSUInteger; |
| 25 |
typedef struct objc_selector *SEL; |
| 26 |
@class NSString, Protocol; |
| 27 |
extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); |
| 28 |
typedef NSInteger NSComparisonResult; |
| 29 |
typedef struct _NSZone NSZone; |
| 30 |
@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 31 |
@protocol NSObject |
| 32 |
- (BOOL)isEqual:(id)object; |
| 33 |
- (oneway void)release; |
| 34 |
- (Class)class; |
| 35 |
- (id)retain; |
| 36 |
@end |
| 37 |
@protocol NSCopying |
| 38 |
- (id)copyWithZone:(NSZone *)zone; |
| 39 |
@end |
| 40 |
@protocol NSMutableCopying |
| 41 |
- (id)mutableCopyWithZone:(NSZone *)zone; |
| 42 |
@end |
| 43 |
@protocol NSCoding |
| 44 |
- (void)encodeWithCoder:(NSCoder *)aCoder; |
| 45 |
@end |
| 46 |
@interface NSObject <NSObject> {} |
| 47 |
- (id)init; |
| 48 |
+ (id)alloc; |
| 49 |
+ (Class)class; |
| 50 |
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait; |
| 51 |
@end |
| 52 |
extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); |
| 53 |
typedef struct {} NSFastEnumerationState; |
| 54 |
@protocol NSFastEnumeration |
| 55 |
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len; |
| 56 |
@end |
| 57 |
@class NSString; |
| 58 |
typedef struct _NSRange {} NSRange; |
| 59 |
@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration> |
| 60 |
- (NSUInteger)count; |
| 61 |
@end |
| 62 |
@interface NSMutableArray : NSArray |
| 63 |
- (void)addObject:(id)anObject; |
| 64 |
- (id)initWithCapacity:(NSUInteger)numItems; |
| 65 |
@end |
| 66 |
typedef unsigned short unichar; |
| 67 |
@class NSData, NSArray, NSDictionary, NSCharacterSet, NSData, NSURL, NSError, NSLocale; |
| 68 |
typedef NSUInteger NSStringCompareOptions; |
| 69 |
@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length; |
| 70 |
- (NSComparisonResult)compare:(NSString *)string; |
| 71 |
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask; |
| 72 |
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange; |
| 73 |
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale; |
| 74 |
- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string; |
| 75 |
- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator; |
| 76 |
@end |
| 77 |
@interface NSSimpleCString : NSString {} @end |
| 78 |
@interface NSConstantString : NSSimpleCString @end |
| 79 |
extern void *_NSConstantStringClassReference; |
| 80 |
|
| 81 |
//===----------------------------------------------------------------------===// |
| 82 |
// Test cases. |
| 83 |
//===----------------------------------------------------------------------===// |
| 84 |
|
| 85 |
// The analyzer doesn't perform any inter-procedural analysis, so delegates |
| 86 |
// involving [NSObject performSelector...] tend to lead to false positives. |
| 87 |
// For now the analyzer just stops tracking the reference count of the |
| 88 |
// receiver until we have better support for delegates. |
| 89 |
|
| 90 |
@interface test_6062730 : NSObject |
| 91 |
+ (void)postNotification:(NSString *)str; |
| 92 |
- (void)foo; |
| 93 |
- (void)bar; |
| 94 |
@end |
| 95 |
|
| 96 |
@implementation test_6062730 |
| 97 |
- (void) foo { |
| 98 |
NSString *str = [[NSString alloc] init]; // no-warning |
| 99 |
[test_6062730 performSelectorOnMainThread:@selector(postNotification:) withObject:str waitUntilDone:1]; |
| 100 |
} |
| 101 |
|
| 102 |
- (void) bar { |
| 103 |
NSString *str = [[NSString alloc] init]; // no-warning |
| 104 |
[[self class] performSelectorOnMainThread:@selector(postNotification:) withObject:str waitUntilDone:1]; |
| 105 |
} |
| 106 |
|
| 107 |
+ (void) postNotification:(NSString *)str { |
| 108 |
[str release]; // no-warning |
| 109 |
} |
| 110 |
@end |
| 111 |
|
| 112 |
|
| 113 |
@interface ObjectThatRequiresDelegate : NSObject |
| 114 |
- (id)initWithDelegate:(id)delegate; |
| 115 |
- (id)initWithNumber:(int)num delegate:(id)delegate; |
| 116 |
@end |
| 117 |
|
| 118 |
|
| 119 |
@interface DelegateRequirerTest |
| 120 |
@end |
| 121 |
@implementation DelegateRequirerTest |
| 122 |
|
| 123 |
- (void)test { |
| 124 |
(void)[[ObjectThatRequiresDelegate alloc] initWithDelegate:self]; |
| 125 |
(void)[[ObjectThatRequiresDelegate alloc] initWithNumber:0 delegate:self]; |
| 126 |
// no leak warnings -- these objects could be released in callback methods |
| 127 |
} |
| 128 |
|
| 129 |
@end |