blob: a56c5b858521838ece99b74835928b7cc88dc29b [file] [log] [blame]
Ted Kremenekf4653c02008-06-16 20:37:30 +00001// RUN: clang -checker-cfref -verify %s
2
3//===----------------------------------------------------------------------===//
4// The following code is reduced using delta-debugging from
5// Foundation.h (Mac OS X).
6//
7// It includes the basic definitions for the test cases below.
8// Not directly including Foundation.h directly makes this test case
9// both svelte and portable to non-Mac platforms.
10//===----------------------------------------------------------------------===//
11
12typedef const void * CFTypeRef;
13typedef const struct __CFString * CFStringRef;
14typedef const struct __CFAllocator * CFAllocatorRef;
15extern const CFAllocatorRef kCFAllocatorDefault;
16extern CFTypeRef CFRetain(CFTypeRef cf);
17typedef const struct __CFDictionary * CFDictionaryRef;
18extern CFStringRef CFStringCreateWithFormat(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, ...);
19typedef signed char BOOL;
20typedef int NSInteger;
21typedef unsigned int NSUInteger;
22@class NSString, Protocol;
23extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
24typedef NSInteger NSComparisonResult;
25typedef struct _NSZone NSZone;
26@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
27@protocol NSObject
28- (BOOL)isEqual:(id)object;
29- (oneway void)release;
30@end
31@protocol NSCopying
32- (id)copyWithZone:(NSZone *)zone;
33@end
34@protocol NSMutableCopying
35- (id)mutableCopyWithZone:(NSZone *)zone;
36@end
37@protocol NSCoding
38- (void)encodeWithCoder:(NSCoder *)aCoder;
39@end
40@interface NSObject <NSObject> {}
41- (id)init;
42+ (id)alloc;
43@end
44extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
45typedef struct {} NSFastEnumerationState;
46@protocol NSFastEnumeration
47- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
48@end
49@class NSString;
50typedef struct _NSRange {} NSRange;
51@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>
52- (NSUInteger)count;
53@end
54@interface NSMutableArray : NSArray
55- (void)addObject:(id)anObject;
56- (id)initWithCapacity:(NSUInteger)numItems;
57@end
58typedef unsigned short unichar;
59@class NSData, NSArray, NSDictionary, NSCharacterSet, NSData, NSURL, NSError, NSLocale;
60typedef NSUInteger NSStringCompareOptions;
61@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> - (NSUInteger)length;
62- (NSComparisonResult)compare:(NSString *)string;
63- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;
64- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange;
65- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale;
66- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
67- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator;
68@end
69@interface NSSimpleCString : NSString {} @end
70@interface NSConstantString : NSSimpleCString @end
71extern void *_NSConstantStringClassReference;
72
73//===----------------------------------------------------------------------===//
74// Test cases.
75//===----------------------------------------------------------------------===//
76
77NSComparisonResult f1(NSString* s) {
78 NSString *aString = 0;
79 return [s compare:aString]; // expected-warning {{Argument to 'NSString' method 'compare:' cannot be nil.}}
80}
81
82NSComparisonResult f2(NSString* s) {
83 NSString *aString = 0;
84 return [s caseInsensitiveCompare:aString]; // expected-warning {{Argument to 'NSString' method 'caseInsensitiveCompare:' cannot be nil.}}
85}
86
87NSComparisonResult f3(NSString* s, NSStringCompareOptions op) {
88 NSString *aString = 0;
89 return [s compare:aString options:op]; // expected-warning {{Argument to 'NSString' method 'compare:options:' cannot be nil.}}
90}
91
92NSComparisonResult f4(NSString* s, NSStringCompareOptions op, NSRange R) {
93 NSString *aString = 0;
94 return [s compare:aString options:op range:R]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:' cannot be nil.}}
95}
96
97NSComparisonResult f5(NSString* s, NSStringCompareOptions op, NSRange R) {
98 NSString *aString = 0;
99 return [s compare:aString options:op range:R locale:0]; // expected-warning {{Argument to 'NSString' method 'compare:options:range:locale:' cannot be nil.}}
100}
101
Daniel Dunbar103baef2008-08-05 00:07:51 +0000102NSArray *f6(NSString* s) {
Ted Kremenekf4653c02008-06-16 20:37:30 +0000103 return [s componentsSeparatedByCharactersInSet:0]; // expected-warning {{Argument to 'NSString' method 'componentsSeparatedByCharactersInSet:' cannot be nil.}}
104}
105
106NSString* f7(NSString* s1, NSString* s2, NSString* s3) {
107
Daniel Dunbar103baef2008-08-05 00:07:51 +0000108 NSString* s4 = (NSString*)
109 CFStringCreateWithFormat(kCFAllocatorDefault, 0,
110 (CFStringRef) __builtin___CFStringMakeConstantString("%@ %@ (%@)"),
111 s1, s2, s3);
Ted Kremenekf4653c02008-06-16 20:37:30 +0000112
113 CFRetain(s4);
114 return s4; // expected-warning{{leak}}
115}
116
117NSMutableArray* f8() {
118
119 NSString* s = [[NSString alloc] init];
120 NSMutableArray* a = [[NSMutableArray alloc] initWithCapacity:2];
121 [a addObject:s];
122 [s release]; // no-warning
123 return a;
124}
125
126void f9() {
127
128 NSString* s = [[NSString alloc] init];
129 NSString* q = s;
130 [s release];
131 [q release]; // expected-warning {{used after it is released}}
132}
133
134NSString* f10() {
135
136 static NSString* s = 0;
137
138 if (!s) s = [[NSString alloc] init];
139
140 return s; // no-warning
141}
142
143@interface C1 : NSObject {}
144- (NSString*) getShared;
145+ (C1*) sharedInstance;
146@end
Ted Kremenekf7ff2ce2008-07-03 15:37:02 +0000147@implementation C1 : NSObject {}
Ted Kremenekf4653c02008-06-16 20:37:30 +0000148- (NSString*) getShared {
149 static NSString* s = 0;
150 if (!s) s = [[NSString alloc] init];
151 return s; // no-warning
152}
153+ (C1 *)sharedInstance {
154 static C1 *sharedInstance = 0;
155 if (!sharedInstance) {
156 sharedInstance = [[C1 alloc] init];
157 }
158 return sharedInstance; // no-warning
159}
160@end
161
162@interface SharedClass : NSObject
163+ (id)sharedInstance;
164@end
Ted Kremenekf7ff2ce2008-07-03 15:37:02 +0000165@implementation SharedClass
Ted Kremenekf4653c02008-06-16 20:37:30 +0000166
167- (id)_init {
168 if ((self = [super init])) {
169 NSLog(@"Bar");
170 }
171 return self;
172}
173
174+ (id)sharedInstance {
175 static SharedClass *_sharedInstance = 0;
176 if (!_sharedInstance) {
177 _sharedInstance = [[SharedClass alloc] _init];
178 }
179 return _sharedInstance; // no-warning
180}
181@end