blob: 28e9006c18e5daf9222362be0893640519759e1c [file] [log] [blame]
Ted Kremenek34653182012-07-25 07:26:32 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core,osx.cocoa.RetainCount -analyzer-store=region -Wno-objc-root-class -verify %s
Andy Gibbs8e8fb3b2012-10-19 12:44:48 +00002// expected-no-diagnostics
Ted Kremenekde4d5332009-04-24 17:50:11 +00003
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 {
Jordan Rose4531b7d2012-07-02 19:27:43 +0000100 NSString *str = [[NSString alloc] init]; // no-warning
Ted Kremenekde4d5332009-04-24 17:50:11 +0000101 [test_6062730 performSelectorOnMainThread:@selector(postNotification:) withObject:str waitUntilDone:1];
102}
103
104- (void) bar {
Jordan Rose4531b7d2012-07-02 19:27:43 +0000105 NSString *str = [[NSString alloc] init]; // no-warning
Ted Kremenekde4d5332009-04-24 17:50:11 +0000106 [[self class] performSelectorOnMainThread:@selector(postNotification:) withObject:str waitUntilDone:1];
107}
108
109+ (void) postNotification:(NSString *)str {
110 [str release]; // no-warning
111}
112@end
113
Jordan Rose50571a92012-06-15 18:19:52 +0000114
115@interface ObjectThatRequiresDelegate : NSObject
116- (id)initWithDelegate:(id)delegate;
117- (id)initWithNumber:(int)num delegate:(id)delegate;
118@end
119
120
121@interface DelegateRequirerTest
122@end
123@implementation DelegateRequirerTest
124
125- (void)test {
126 (void)[[ObjectThatRequiresDelegate alloc] initWithDelegate:self];
127 (void)[[ObjectThatRequiresDelegate alloc] initWithNumber:0 delegate:self];
128 // no leak warnings -- these objects could be released in callback methods
129}
130
131@end