| 1 | // REQUIRES: darwin |
| 2 | // |
| 3 | // RUN: %clang -framework Foundation -fsanitize=objc-cast %s -O1 -o %t |
| 4 | // RUN: %run %t 2>&1 | FileCheck %s |
| 5 | // |
| 6 | // RUN: %clang -framework Foundation -fsanitize=objc-cast -fno-sanitize-recover=objc-cast %s -O1 -o %t.trap |
| 7 | // RUN: not %run %t.trap 2>&1 | FileCheck %s |
| 8 | |
| 9 | #include <Foundation/Foundation.h> |
| 10 | |
| 11 | int main() { |
| 12 | NSArray *arrayOfInt = [NSArray arrayWithObjects:@1, @2, @3, (void *)0]; |
| 13 | // CHECK: objc-cast.m:[[@LINE+1]]:{{.*}}: runtime error: invalid ObjC cast, object is a '{{__NSCFNumber|NSConstantIntegerNumber}}', but expected a 'NSString' |
| 14 | for (NSString *str in arrayOfInt) { |
| 15 | NSLog(@"%@" , str); |
| 16 | } |
| 17 | |
| 18 | NSArray *arrayOfStr = [NSArray arrayWithObjects:@"a" , @"b" , @"c" , (void *)0]; |
| 19 | for (NSString *str in arrayOfStr) { |
| 20 | NSLog(@"%@" , str); |
| 21 | } |
| 22 | |
| 23 | // The diagnostic should only be printed once. |
| 24 | // CHECK-NOT: runtime error |
| 25 | |
| 26 | return 0; |
| 27 | } |
| 28 | |