2014-11-15 01:12:50 +08:00
|
|
|
// RUN: %clang_cc1 -Wno-objc-literal-conversion -analyze -analyzer-checker=core,osx.cocoa.NonNilReturnValue,osx.cocoa.NilArg,osx.cocoa.Loops,debug.ExprInspection -verify -Wno-objc-root-class %s
|
2014-02-08 08:04:14 +08:00
|
|
|
|
|
|
|
void clang_analyzer_eval(int);
|
|
|
|
|
2013-03-19 04:46:56 +08:00
|
|
|
typedef unsigned long NSUInteger;
|
|
|
|
typedef signed char BOOL;
|
|
|
|
typedef struct _NSZone NSZone;
|
|
|
|
@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
|
|
|
|
@protocol NSObject
|
|
|
|
@end
|
|
|
|
@protocol NSCopying
|
|
|
|
- (id)copyWithZone:(NSZone *)zone;
|
|
|
|
@end
|
|
|
|
@protocol NSMutableCopying
|
|
|
|
- (id)mutableCopyWithZone:(NSZone *)zone;
|
|
|
|
@end
|
|
|
|
@protocol NSCoding
|
|
|
|
- (void)encodeWithCoder:(NSCoder *)aCoder;
|
|
|
|
@end
|
|
|
|
@protocol NSSecureCoding <NSCoding>
|
|
|
|
@required
|
|
|
|
+ (BOOL)supportsSecureCoding;
|
|
|
|
@end
|
|
|
|
@interface NSObject <NSObject> {}
|
|
|
|
- (id)init;
|
|
|
|
+ (id)alloc;
|
2015-09-15 09:13:53 +08:00
|
|
|
|
|
|
|
- (id)mutableCopy;
|
2013-03-19 04:46:56 +08:00
|
|
|
@end
|
|
|
|
|
2013-06-22 08:23:26 +08:00
|
|
|
typedef struct {
|
|
|
|
unsigned long state;
|
|
|
|
id *itemsPtr;
|
|
|
|
unsigned long *mutationsPtr;
|
|
|
|
unsigned long extra[5];
|
|
|
|
} NSFastEnumerationState;
|
|
|
|
@protocol NSFastEnumeration
|
|
|
|
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id [])buffer count:(NSUInteger)len;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration>
|
2013-03-19 04:46:56 +08:00
|
|
|
- (NSUInteger)count;
|
|
|
|
- (id)objectAtIndex:(NSUInteger)index;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NSArray (NSExtendedArray)
|
|
|
|
- (NSArray *)arrayByAddingObject:(id)anObject;
|
|
|
|
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx __attribute__((availability(macosx,introduced=10.8)));
|
|
|
|
@end
|
|
|
|
|
2013-05-14 05:48:20 +08:00
|
|
|
@interface NSArray (NSArrayCreation)
|
|
|
|
+ (instancetype)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt;
|
|
|
|
@end
|
|
|
|
|
2013-03-19 04:46:56 +08:00
|
|
|
@interface NSMutableArray : NSArray
|
|
|
|
|
|
|
|
- (void)addObject:(id)anObject;
|
|
|
|
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
|
|
|
|
- (void)removeLastObject;
|
|
|
|
- (void)removeObjectAtIndex:(NSUInteger)index;
|
|
|
|
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2013-03-23 08:39:21 +08:00
|
|
|
@interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration>
|
|
|
|
|
|
|
|
- (NSUInteger)count;
|
|
|
|
- (id)objectForKey:(id)aKey;
|
|
|
|
- (NSEnumerator *)keyEnumerator;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NSDictionary (NSDictionaryCreation)
|
|
|
|
|
|
|
|
+ (id)dictionary;
|
|
|
|
+ (id)dictionaryWithObject:(id)object forKey:(id <NSCopying>)key;
|
2013-05-14 05:48:20 +08:00
|
|
|
+ (instancetype)dictionaryWithObjects:(const id [])objects forKeys:(const id <NSCopying> [])keys count:(NSUInteger)cnt;
|
|
|
|
|
2013-03-23 08:39:21 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NSMutableDictionary : NSDictionary
|
|
|
|
|
|
|
|
- (void)removeObjectForKey:(id)aKey;
|
|
|
|
- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NSMutableDictionary (NSExtendedMutableDictionary)
|
|
|
|
|
|
|
|
- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
|
|
|
|
- (void)removeAllObjects;
|
|
|
|
- (void)removeObjectsForKeys:(NSArray *)keyArray;
|
|
|
|
- (void)setDictionary:(NSDictionary *)otherDictionary;
|
|
|
|
- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key __attribute__((availability(macosx,introduced=10.8)));
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2013-11-05 03:13:08 +08:00
|
|
|
@interface NSOrderedSet : NSObject <NSFastEnumeration>
|
|
|
|
@end
|
|
|
|
@interface NSOrderedSet (NSOrderedSetCreation)
|
|
|
|
- (NSUInteger)count;
|
|
|
|
@end
|
|
|
|
|
2013-03-23 08:39:21 +08:00
|
|
|
@interface NSString : NSObject <NSCopying, NSMutableCopying, NSSecureCoding>
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2013-05-11 02:04:46 +08:00
|
|
|
@interface NSNull : NSObject <NSCopying, NSSecureCoding>
|
|
|
|
+ (NSNull *)null;
|
|
|
|
@end
|
|
|
|
|
2013-03-19 04:46:56 +08:00
|
|
|
// NSMutableArray API
|
2013-03-23 08:39:21 +08:00
|
|
|
void testNilArgNSMutableArray1() {
|
2013-03-19 04:46:56 +08:00
|
|
|
NSMutableArray *marray = [[NSMutableArray alloc] init];
|
|
|
|
[marray addObject:0]; // expected-warning {{Argument to 'NSMutableArray' method 'addObject:' cannot be nil}}
|
|
|
|
}
|
|
|
|
|
2013-03-23 08:39:21 +08:00
|
|
|
void testNilArgNSMutableArray2() {
|
2013-03-19 04:46:56 +08:00
|
|
|
NSMutableArray *marray = [[NSMutableArray alloc] init];
|
|
|
|
[marray insertObject:0 atIndex:1]; // expected-warning {{Argument to 'NSMutableArray' method 'insertObject:atIndex:' cannot be nil}}
|
|
|
|
}
|
|
|
|
|
2013-03-23 08:39:21 +08:00
|
|
|
void testNilArgNSMutableArray3() {
|
2013-03-19 04:46:56 +08:00
|
|
|
NSMutableArray *marray = [[NSMutableArray alloc] init];
|
|
|
|
[marray replaceObjectAtIndex:1 withObject:0]; // expected-warning {{Argument to 'NSMutableArray' method 'replaceObjectAtIndex:withObject:' cannot be nil}}
|
|
|
|
}
|
|
|
|
|
2013-03-23 08:39:21 +08:00
|
|
|
void testNilArgNSMutableArray4() {
|
2013-03-19 04:46:56 +08:00
|
|
|
NSMutableArray *marray = [[NSMutableArray alloc] init];
|
|
|
|
[marray setObject:0 atIndexedSubscript:1]; // expected-warning {{Argument to 'NSMutableArray' method 'setObject:atIndexedSubscript:' cannot be nil}}
|
|
|
|
}
|
|
|
|
|
2013-03-23 08:39:21 +08:00
|
|
|
void testNilArgNSMutableArray5() {
|
|
|
|
NSMutableArray *marray = [[NSMutableArray alloc] init];
|
|
|
|
marray[1] = 0; // expected-warning {{Array element cannot be nil}}
|
|
|
|
}
|
|
|
|
|
2013-03-19 04:46:56 +08:00
|
|
|
// NSArray API
|
2013-03-23 08:39:21 +08:00
|
|
|
void testNilArgNSArray1() {
|
2013-03-19 04:46:56 +08:00
|
|
|
NSArray *array = [[NSArray alloc] init];
|
|
|
|
NSArray *copyArray = [array arrayByAddingObject:0]; // expected-warning {{Argument to 'NSArray' method 'arrayByAddingObject:' cannot be nil}}
|
|
|
|
}
|
|
|
|
|
2013-03-23 08:39:21 +08:00
|
|
|
// NSMutableDictionary and NSDictionary APIs.
|
|
|
|
void testNilArgNSMutableDictionary1(NSMutableDictionary *d, NSString* key) {
|
2013-04-06 07:50:18 +08:00
|
|
|
[d setObject:0 forKey:key]; // expected-warning {{Value argument to 'setObject:forKey:' cannot be nil}}
|
2013-03-23 08:39:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void testNilArgNSMutableDictionary2(NSMutableDictionary *d, NSObject *obj) {
|
2013-04-06 07:50:18 +08:00
|
|
|
[d setObject:obj forKey:0]; // expected-warning {{Key argument to 'setObject:forKey:' cannot be nil}}
|
2013-03-23 08:39:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void testNilArgNSMutableDictionary3(NSMutableDictionary *d) {
|
2013-04-06 07:50:18 +08:00
|
|
|
[d removeObjectForKey:0]; // expected-warning {{Value argument to 'removeObjectForKey:' cannot be nil}}
|
2013-03-23 08:39:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void testNilArgNSMutableDictionary5(NSMutableDictionary *d, NSString* key) {
|
2015-11-11 08:49:22 +08:00
|
|
|
d[key] = 0; // no-warning - removing the mapping for the given key
|
2013-03-23 08:39:21 +08:00
|
|
|
}
|
|
|
|
void testNilArgNSMutableDictionary6(NSMutableDictionary *d, NSString *key) {
|
|
|
|
if (key)
|
|
|
|
;
|
2013-04-06 07:50:18 +08:00
|
|
|
d[key] = 0; // expected-warning {{'NSMutableDictionary' key cannot be nil}}
|
2013-03-23 08:39:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NSDictionary *testNilArgNSDictionary1(NSString* key) {
|
2013-04-06 07:50:18 +08:00
|
|
|
return [NSDictionary dictionaryWithObject:0 forKey:key]; // expected-warning {{Value argument to 'dictionaryWithObject:forKey:' cannot be nil}}
|
2013-03-23 08:39:21 +08:00
|
|
|
}
|
|
|
|
NSDictionary *testNilArgNSDictionary2(NSObject *obj) {
|
2013-04-06 07:50:18 +08:00
|
|
|
return [NSDictionary dictionaryWithObject:obj forKey:0]; // expected-warning {{Key argument to 'dictionaryWithObject:forKey:' cannot be nil}}
|
2013-03-23 08:39:21 +08:00
|
|
|
}
|
2013-03-27 07:58:49 +08:00
|
|
|
|
2013-05-14 05:48:20 +08:00
|
|
|
id testCreateDictionaryLiteralKey(id value, id nilKey) {
|
|
|
|
if (nilKey)
|
|
|
|
;
|
|
|
|
return @{@"abc":value, nilKey:@"abc"}; // expected-warning {{Dictionary key cannot be nil}}
|
|
|
|
}
|
|
|
|
|
|
|
|
id testCreateDictionaryLiteralValue(id nilValue) {
|
|
|
|
if (nilValue)
|
|
|
|
;
|
|
|
|
return @{@"abc":nilValue}; // expected-warning {{Dictionary value cannot be nil}}
|
|
|
|
}
|
|
|
|
|
|
|
|
id testCreateDictionaryLiteral(id nilValue, id nilKey) {
|
|
|
|
if (nilValue)
|
|
|
|
;
|
|
|
|
if (nilKey)
|
|
|
|
;
|
|
|
|
return @{@"abc":nilValue, nilKey:@"abc"}; // expected-warning {{Dictionary key cannot be nil}}
|
|
|
|
// expected-warning@-1 {{Dictionary value cannot be nil}}
|
|
|
|
}
|
|
|
|
|
|
|
|
id testCreateArrayLiteral(id myNil) {
|
|
|
|
if (myNil)
|
|
|
|
;
|
|
|
|
return @[ @"a", myNil, @"c" ]; // expected-warning {{Array element cannot be nil}}
|
|
|
|
}
|
|
|
|
|
2013-03-27 07:58:49 +08:00
|
|
|
// Test inline defensive checks suppression.
|
|
|
|
void idc(id x) {
|
|
|
|
if (x)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
void testIDC(NSMutableDictionary *d, NSString *key) {
|
|
|
|
idc(key);
|
|
|
|
d[key] = @"abc"; // no-warning
|
|
|
|
}
|
|
|
|
|
2013-03-29 07:15:22 +08:00
|
|
|
@interface Foo {
|
|
|
|
@public
|
|
|
|
int x;
|
|
|
|
}
|
2013-03-27 07:58:49 +08:00
|
|
|
- (int *)getPtr;
|
|
|
|
- (int)getInt;
|
2013-03-29 07:15:22 +08:00
|
|
|
- (NSMutableDictionary *)getDictPtr;
|
|
|
|
@property (retain, readonly, nonatomic) Foo* data;
|
|
|
|
- (NSString*) stringForKeyFE: (id<NSCopying>)key;
|
2013-03-27 07:58:49 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
void idc2(id x) {
|
|
|
|
if (!x)
|
|
|
|
return;
|
|
|
|
}
|
2013-03-29 07:15:22 +08:00
|
|
|
Foo *retNil() {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-03-27 07:58:49 +08:00
|
|
|
|
|
|
|
void testIDC2(Foo *obj) {
|
|
|
|
idc2(obj);
|
|
|
|
*[obj getPtr] = 1; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
int testIDC3(Foo *obj) {
|
|
|
|
idc2(obj);
|
|
|
|
return 1/[obj getInt];
|
|
|
|
}
|
|
|
|
|
2013-03-29 07:15:22 +08:00
|
|
|
void testNilReceiverIDC(Foo *obj, NSString *key) {
|
|
|
|
NSMutableDictionary *D = [obj getDictPtr];
|
|
|
|
idc(D);
|
|
|
|
D[key] = @"abc"; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void testNilReceiverRetNil2(NSMutableDictionary *D, Foo *FooPtrIn, id value) {
|
|
|
|
NSString* const kKeyIdentifier = @"key";
|
|
|
|
Foo *FooPtr = retNil();
|
|
|
|
NSString *key = [[FooPtr data] stringForKeyFE: kKeyIdentifier];
|
|
|
|
// key is nil because FooPtr is nil. However, FooPtr is set to nil inside an
|
|
|
|
// inlined function, so this error report should be suppressed.
|
|
|
|
[D setObject: value forKey: key]; // no-warning
|
|
|
|
}
|
|
|
|
|
2013-05-11 02:04:46 +08:00
|
|
|
void testAssumeNSNullNullReturnsNonNil(NSMutableDictionary *Table, id Object,
|
|
|
|
id InValue) {
|
|
|
|
id Value = Object ? [Table objectForKey:Object] : [NSNull null];
|
|
|
|
if (!Value) {
|
|
|
|
Value = InValue;
|
|
|
|
[Table setObject:Value forKey:Object]; // no warning
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-22 08:23:26 +08:00
|
|
|
void testCollectionIsNotEmptyWhenCountIsGreaterThanZero(NSMutableDictionary *D){
|
|
|
|
if ([D count] > 0) { // Count is greater than zero.
|
|
|
|
NSString *s = 0;
|
|
|
|
for (NSString *key in D) {
|
|
|
|
s = key; // Loop is always entered.
|
|
|
|
}
|
|
|
|
[D removeObjectForKey:s]; // no warning
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-05 03:13:08 +08:00
|
|
|
void testCountAwareNSOrderedSet(NSOrderedSet *containers, int *validptr) {
|
|
|
|
int *x = 0;
|
|
|
|
NSUInteger containerCount = [containers count];
|
|
|
|
if (containerCount > 0)
|
|
|
|
x = validptr;
|
|
|
|
for (id c in containers) {
|
|
|
|
*x = 1; // no warning
|
|
|
|
}
|
|
|
|
}
|
2013-03-29 07:15:22 +08:00
|
|
|
|
2014-02-08 08:04:14 +08:00
|
|
|
void testLiteralsNonNil() {
|
|
|
|
clang_analyzer_eval(!!@[]); // expected-warning{{TRUE}}
|
|
|
|
clang_analyzer_eval(!!@{}); // expected-warning{{TRUE}}
|
|
|
|
}
|
|
|
|
|
2014-04-09 09:39:22 +08:00
|
|
|
@interface NSMutableArray (MySafeAdd)
|
|
|
|
- (void)addObject:(id)obj safe:(BOOL)safe;
|
|
|
|
@end
|
|
|
|
|
|
|
|
void testArrayCategory(NSMutableArray *arr) {
|
|
|
|
[arr addObject:0 safe:1]; // no-warning
|
|
|
|
}
|
|
|
|
|
2015-09-15 09:13:53 +08:00
|
|
|
@interface MyView : NSObject
|
|
|
|
-(NSArray *)subviews;
|
|
|
|
@end
|
|
|
|
|
|
|
|
void testNoReportWhenReceiverNil(NSMutableArray *array, int b) {
|
|
|
|
// Don't warn about adding nil to a container when the receiver is also
|
|
|
|
// definitely nil.
|
|
|
|
if (array == 0) {
|
|
|
|
[array addObject:0]; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
MyView *view = b ? [[MyView alloc] init] : 0;
|
|
|
|
NSMutableArray *subviews = [[view subviews] mutableCopy];
|
|
|
|
// When view is nil, subviews is also nil so there should be no warning
|
|
|
|
// here either.
|
|
|
|
[subviews addObject:view]; // no-warning
|
|
|
|
}
|