1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,osx.cocoa.ObjCGenerics -verify %s
3 #if !__has_feature(objc_generics)
4 # error Compiler does not support Objective-C generics?
7 typedef __typeof(sizeof(int)) size_t;
8 void *memset(void *, int, size_t);
11 typedef unsigned long NSUInteger;
17 __attribute__((objc_root_class))
19 - (void) myFunction:(int*)p myParam:(int) n;
22 @interface MyType : NSObject <NSCopying>
23 - (void) myFunction:(int*)p myParam:(int) n;
26 @interface NSArray<ObjectType> : NSObject
28 - (BOOL)contains:(ObjectType)obj;
29 - (ObjectType)getObjAtIndex:(NSUInteger)idx;
30 - (ObjectType)objectAtIndexedSubscript:(NSUInteger)idx;
31 @property(readonly) ObjectType firstObject;
34 @implementation NSObject
35 - (void) myFunction:(int*)p myParam:(int) n {
36 (void)*p;// no warning
40 @implementation MyType
41 - (void) myFunction:(int*)p myParam:(int) n {
42 int i = 5/n; // expected-warning {{}}
47 void testReturnType(NSArray<MyType *> *arr) {
48 NSArray *erased = arr;
49 NSObject *element = [erased firstObject];
50 // TODO: myFunction currently dispatches to NSObject. Make it dispatch to
52 [element myFunction:0 myParam:0 ];
55 void 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 to
60 [element myFunction:0 myParam:0 ];
63 // Do not try this at home! The analyzer shouldn't crash though when it
64 // tries to figure out the dynamic type behind the alloca's return value.
65 void testAlloca(size_t NSArrayClassSizeWeKnowSomehow) {
66 NSArray *arr = __builtin_alloca(NSArrayClassSizeWeKnowSomehow);
67 memset(arr, 0, NSArrayClassSizeWeKnowSomehow);
68 [arr init]; // no-crash