blob: 9a0b94ddcaece139dc8d3ccdff57a4dd623bc4de [file] [log] [blame]
Ted Kremenek859be3b2008-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
102NSComparisonResult f6(NSString* s) {
103 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
108 NSString* s4 = CFStringCreateWithFormat(kCFAllocatorDefault, 0,
109 L"%@ %@ (%@)",
110 s1, s2, s3);
111
112 CFRetain(s4);
113 return s4; // expected-warning{{leak}}
114}
115
116NSMutableArray* f8() {
117
118 NSString* s = [[NSString alloc] init];
119 NSMutableArray* a = [[NSMutableArray alloc] initWithCapacity:2];
120 [a addObject:s];
121 [s release]; // no-warning
122 return a;
123}
124
125void f9() {
126
127 NSString* s = [[NSString alloc] init];
128 NSString* q = s;
129 [s release];
130 [q release]; // expected-warning {{used after it is released}}
131}
132
133NSString* f10() {
134
135 static NSString* s = 0;
136
137 if (!s) s = [[NSString alloc] init];
138
139 return s; // no-warning
140}
141
142@interface C1 : NSObject {}
143- (NSString*) getShared;
144+ (C1*) sharedInstance;
145@end
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000146@implementation C1 : NSObject {} // expected-warning{{Objective-C class 'C1' lacks a 'dealloc' instance method}}
Ted Kremenek859be3b2008-06-16 20:37:30 +0000147- (NSString*) getShared {
148 static NSString* s = 0;
149 if (!s) s = [[NSString alloc] init];
150 return s; // no-warning
151}
152+ (C1 *)sharedInstance {
153 static C1 *sharedInstance = 0;
154 if (!sharedInstance) {
155 sharedInstance = [[C1 alloc] init];
156 }
157 return sharedInstance; // no-warning
158}
159@end
160
161@interface SharedClass : NSObject
162+ (id)sharedInstance;
163@end
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000164@implementation SharedClass // expected-warning {{Objective-C class 'SharedClass' lacks a 'dealloc' instance method}}
Ted Kremenek859be3b2008-06-16 20:37:30 +0000165
166- (id)_init {
167 if ((self = [super init])) {
168 NSLog(@"Bar");
169 }
170 return self;
171}
172
173+ (id)sharedInstance {
174 static SharedClass *_sharedInstance = 0;
175 if (!_sharedInstance) {
176 _sharedInstance = [[SharedClass alloc] _init];
177 }
178 return _sharedInstance; // no-warning
179}
180@end