NSContainers.m revision b095782ec09329b474a4e0d0ccdad4c15d515b39
1// RUN: %clang_cc1 -analyze -analyzer-checker=core,osx.cocoa.NilArg -verify -Wno-objc-root-class %s 2typedef unsigned long NSUInteger; 3typedef signed char BOOL; 4typedef struct _NSZone NSZone; 5@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; 6@protocol NSObject 7@end 8@protocol NSCopying 9- (id)copyWithZone:(NSZone *)zone; 10@end 11@protocol NSMutableCopying 12- (id)mutableCopyWithZone:(NSZone *)zone; 13@end 14@protocol NSCoding 15- (void)encodeWithCoder:(NSCoder *)aCoder; 16@end 17@protocol NSFastEnumeration 18@end 19@protocol NSSecureCoding <NSCoding> 20@required 21+ (BOOL)supportsSecureCoding; 22@end 23@interface NSObject <NSObject> {} 24- (id)init; 25+ (id)alloc; 26@end 27@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration> 28 29- (NSUInteger)count; 30- (id)objectAtIndex:(NSUInteger)index; 31 32@end 33 34@interface NSArray (NSExtendedArray) 35- (NSArray *)arrayByAddingObject:(id)anObject; 36- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx __attribute__((availability(macosx,introduced=10.8))); 37@end 38 39@interface NSMutableArray : NSArray 40 41- (void)addObject:(id)anObject; 42- (void)insertObject:(id)anObject atIndex:(NSUInteger)index; 43- (void)removeLastObject; 44- (void)removeObjectAtIndex:(NSUInteger)index; 45- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject; 46 47@end 48 49@interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration> 50 51- (NSUInteger)count; 52- (id)objectForKey:(id)aKey; 53- (NSEnumerator *)keyEnumerator; 54 55@end 56 57@interface NSDictionary (NSDictionaryCreation) 58 59+ (id)dictionary; 60+ (id)dictionaryWithObject:(id)object forKey:(id <NSCopying>)key; 61@end 62 63@interface NSMutableDictionary : NSDictionary 64 65- (void)removeObjectForKey:(id)aKey; 66- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey; 67 68@end 69 70@interface NSMutableDictionary (NSExtendedMutableDictionary) 71 72- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary; 73- (void)removeAllObjects; 74- (void)removeObjectsForKeys:(NSArray *)keyArray; 75- (void)setDictionary:(NSDictionary *)otherDictionary; 76- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key __attribute__((availability(macosx,introduced=10.8))); 77 78@end 79 80@interface NSString : NSObject <NSCopying, NSMutableCopying, NSSecureCoding> 81 82@end 83 84// NSMutableArray API 85void testNilArgNSMutableArray1() { 86 NSMutableArray *marray = [[NSMutableArray alloc] init]; 87 [marray addObject:0]; // expected-warning {{Argument to 'NSMutableArray' method 'addObject:' cannot be nil}} 88} 89 90void testNilArgNSMutableArray2() { 91 NSMutableArray *marray = [[NSMutableArray alloc] init]; 92 [marray insertObject:0 atIndex:1]; // expected-warning {{Argument to 'NSMutableArray' method 'insertObject:atIndex:' cannot be nil}} 93} 94 95void testNilArgNSMutableArray3() { 96 NSMutableArray *marray = [[NSMutableArray alloc] init]; 97 [marray replaceObjectAtIndex:1 withObject:0]; // expected-warning {{Argument to 'NSMutableArray' method 'replaceObjectAtIndex:withObject:' cannot be nil}} 98} 99 100void testNilArgNSMutableArray4() { 101 NSMutableArray *marray = [[NSMutableArray alloc] init]; 102 [marray setObject:0 atIndexedSubscript:1]; // expected-warning {{Argument to 'NSMutableArray' method 'setObject:atIndexedSubscript:' cannot be nil}} 103} 104 105void testNilArgNSMutableArray5() { 106 NSMutableArray *marray = [[NSMutableArray alloc] init]; 107 marray[1] = 0; // expected-warning {{Array element cannot be nil}} 108} 109 110// NSArray API 111void testNilArgNSArray1() { 112 NSArray *array = [[NSArray alloc] init]; 113 NSArray *copyArray = [array arrayByAddingObject:0]; // expected-warning {{Argument to 'NSArray' method 'arrayByAddingObject:' cannot be nil}} 114} 115 116// NSMutableDictionary and NSDictionary APIs. 117void testNilArgNSMutableDictionary1(NSMutableDictionary *d, NSString* key) { 118 [d setObject:0 forKey:key]; // expected-warning {{Argument to 'NSMutableDictionary' method 'setObject:forKey:' cannot be nil}} 119} 120 121void testNilArgNSMutableDictionary2(NSMutableDictionary *d, NSObject *obj) { 122 [d setObject:obj forKey:0]; // expected-warning {{Argument to 'NSMutableDictionary' method 'setObject:forKey:' cannot be nil}} 123} 124 125void testNilArgNSMutableDictionary3(NSMutableDictionary *d) { 126 [d removeObjectForKey:0]; // expected-warning {{Argument to 'NSMutableDictionary' method 'removeObjectForKey:' cannot be nil}} 127} 128 129void testNilArgNSMutableDictionary5(NSMutableDictionary *d, NSString* key) { 130 d[key] = 0; // expected-warning {{Dictionary object cannot be nil}} 131} 132void testNilArgNSMutableDictionary6(NSMutableDictionary *d, NSString *key) { 133 if (key) 134 ; 135 d[key] = 0; // expected-warning {{Dictionary key cannot be nil}} 136 // expected-warning@-1 {{Dictionary object cannot be nil}} 137} 138 139NSDictionary *testNilArgNSDictionary1(NSString* key) { 140 return [NSDictionary dictionaryWithObject:0 forKey:key]; // expected-warning {{Argument to 'NSDictionary' method 'dictionaryWithObject:forKey:' cannot be nil}} 141} 142NSDictionary *testNilArgNSDictionary2(NSObject *obj) { 143 return [NSDictionary dictionaryWithObject:obj forKey:0]; // expected-warning {{Argument to 'NSDictionary' method 'dictionaryWithObject:forKey:' cannot be nil}} 144} 145