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