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