Home | History | Annotate | Download | only in Analysis
      1 // RUN: %clang_cc1 -analyze -analyzer-checker=core,osx.cocoa.NilArg -verify -Wno-objc-root-class %s
      2 typedef unsigned long NSUInteger;
      3 typedef signed char BOOL;
      4 typedef 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 
     28 @interface NSArray : NSObject <NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration>
     29 
     30 - (NSUInteger)count;
     31 - (id)objectAtIndex:(NSUInteger)index;
     32 
     33 @end
     34 
     35 @interface NSArray (NSExtendedArray)
     36 - (NSArray *)arrayByAddingObject:(id)anObject;
     37 - (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx __attribute__((availability(macosx,introduced=10.8)));
     38 @end
     39 
     40 @interface NSMutableArray : NSArray
     41 
     42 - (void)addObject:(id)anObject;
     43 - (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
     44 - (void)removeLastObject;
     45 - (void)removeObjectAtIndex:(NSUInteger)index;
     46 - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
     47 
     48 @end
     49 
     50 // NSMutableArray API
     51 void testNilArg1() {
     52   NSMutableArray *marray = [[NSMutableArray alloc] init];
     53   [marray addObject:0]; // expected-warning {{Argument to 'NSMutableArray' method 'addObject:' cannot be nil}}
     54 }
     55 
     56 void testNilArg2() {
     57   NSMutableArray *marray = [[NSMutableArray alloc] init];
     58   [marray insertObject:0 atIndex:1]; // expected-warning {{Argument to 'NSMutableArray' method 'insertObject:atIndex:' cannot be nil}}
     59 }
     60 
     61 void testNilArg3() {
     62   NSMutableArray *marray = [[NSMutableArray alloc] init];
     63   [marray replaceObjectAtIndex:1 withObject:0]; // expected-warning {{Argument to 'NSMutableArray' method 'replaceObjectAtIndex:withObject:' cannot be nil}}
     64 }
     65 
     66 void testNilArg4() {
     67   NSMutableArray *marray = [[NSMutableArray alloc] init];
     68   [marray setObject:0 atIndexedSubscript:1]; // expected-warning {{Argument to 'NSMutableArray' method 'setObject:atIndexedSubscript:' cannot be nil}}
     69 }
     70 
     71 // NSArray API
     72 void testNilArg5() {
     73   NSArray *array = [[NSArray alloc] init];
     74   NSArray *copyArray = [array arrayByAddingObject:0]; // expected-warning {{Argument to 'NSArray' method 'arrayByAddingObject:' cannot be nil}}
     75 }
     76 
     77