70 lines · plain
1// RUN: %clang_analyze_cc1 -analyzer-checker=core,osx.cocoa.ObjCGenerics -verify %s2 3#if !__has_feature(objc_generics)4# error Compiler does not support Objective-C generics?5#endif6 7typedef __typeof(sizeof(int)) size_t;8void *memset(void *, int, size_t);9 10#define nil 011typedef unsigned long NSUInteger;12typedef int BOOL;13 14@protocol NSCopying15@end16 17__attribute__((objc_root_class))18@interface NSObject19- (void) myFunction:(int*)p myParam:(int) n;20@end21 22@interface MyType : NSObject <NSCopying>23- (void) myFunction:(int*)p myParam:(int) n;24@end25 26@interface NSArray<ObjectType> : NSObject27- (void) init;28- (BOOL)contains:(ObjectType)obj;29- (ObjectType)getObjAtIndex:(NSUInteger)idx;30- (ObjectType)objectAtIndexedSubscript:(NSUInteger)idx;31@property(readonly) ObjectType firstObject;32@end33 34@implementation NSObject35- (void) myFunction:(int*)p myParam:(int) n {36 (void)*p;// no warning37}38@end39 40@implementation MyType41- (void) myFunction:(int*)p myParam:(int) n {42 int i = 5/n; // expected-warning {{}}43 (void)i;44}45@end46 47void testReturnType(NSArray<MyType *> *arr) {48 NSArray *erased = arr;49 NSObject *element = [erased firstObject];50 // TODO: myFunction currently dispatches to NSObject. Make it dispatch to51 // MyType instead!52 [element myFunction:0 myParam:0 ];53}54 55void testArgument(NSArray<MyType *> *arr, id element) {56 NSArray *erased = arr;57 [erased contains: element];58 // TODO: myFunction currently is not dispatched to MyType. Make it dispatch to59 // MyType!60 [element myFunction:0 myParam:0 ];61}62 63// Do not try this at home! The analyzer shouldn't crash though when it64// tries to figure out the dynamic type behind the alloca's return value.65void testAlloca(size_t NSArrayClassSizeWeKnowSomehow) {66 NSArray *arr = __builtin_alloca(NSArrayClassSizeWeKnowSomehow);67 memset(arr, 0, NSArrayClassSizeWeKnowSomehow);68 [arr init]; // no-crash69}70