John McCall | d1e40d5 | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -arcmt-check -verify -triple x86_64-apple-darwin10 %s |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 2 | |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 3 | #include "Common.h" |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 4 | |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 5 | typedef const struct __CFString * CFStringRef; |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 6 | typedef const void * CFTypeRef; |
| 7 | CFTypeRef CFBridgingRetain(id X); |
| 8 | id CFBridgingRelease(CFTypeRef); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 9 | |
Argyrios Kyrtzidis | 76a5245 | 2012-06-07 00:44:06 +0000 | [diff] [blame] | 10 | struct StrS { |
| 11 | CFStringRef sref_member; |
| 12 | }; |
| 13 | |
| 14 | @interface NSString : NSObject { |
| 15 | CFStringRef sref; |
| 16 | struct StrS *strS; |
| 17 | } |
| 18 | -(id)string; |
| 19 | -(id)newString; |
| 20 | @end |
| 21 | |
| 22 | @implementation NSString |
| 23 | -(id)string { |
| 24 | if (0) |
| 25 | return sref; |
| 26 | else |
| 27 | return strS->sref_member; |
| 28 | } |
| 29 | -(id)newString { |
| 30 | return sref; // expected-error {{implicit conversion of C pointer type 'CFStringRef' (aka 'const struct __CFString *') to Objective-C pointer type 'id' requires a bridged cast}} \ |
Fariborz Jahanian | 607f587 | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 31 | // expected-note{{use __bridge to convert directly (no change in ownership)}} \ |
Argyrios Kyrtzidis | 76a5245 | 2012-06-07 00:44:06 +0000 | [diff] [blame] | 32 | // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'CFStringRef' (aka 'const struct __CFString *') into ARC}} |
| 33 | } |
| 34 | @end |
| 35 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 36 | void f(BOOL b) { |
| 37 | CFStringRef cfstr; |
| 38 | NSString *str = (NSString *)cfstr; // expected-error {{cast of C pointer type 'CFStringRef' (aka 'const struct __CFString *') to Objective-C pointer type 'NSString *' requires a bridged cast}} \ |
Fariborz Jahanian | 607f587 | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 39 | // expected-note{{use __bridge to convert directly (no change in ownership)}} \ |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 40 | // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'CFStringRef' (aka 'const struct __CFString *') into ARC}} |
Fariborz Jahanian | 607f587 | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 41 | void *vp = str; // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRetain call}} expected-note {{use __bridge}} |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 42 | } |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 43 | |
| 44 | void f2(NSString *s) { |
| 45 | CFStringRef ref; |
| 46 | ref = [(CFStringRef)[s string] retain]; // expected-error {{cast of Objective-C pointer type 'id' to C pointer type 'CFStringRef' (aka 'const struct __CFString *') requires a bridged cast}} \ |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 47 | // expected-error {{bad receiver type 'CFStringRef' (aka 'const struct __CFString *')}} \ |
Fariborz Jahanian | 607f587 | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 48 | // expected-note{{use __bridge to convert directly (no change in ownership)}} \ |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 49 | // expected-note{{use CFBridgingRetain call to make an ARC object available as a +1 'CFStringRef' (aka 'const struct __CFString *')}} |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | CFStringRef f3() { |
| 53 | return (CFStringRef)[[[NSString alloc] init] autorelease]; // expected-error {{it is not safe to cast to 'CFStringRef' the result of 'autorelease' message; a __bridge cast may result in a pointer to a destroyed object and a __bridge_retained may leak the object}} \ |
| 54 | // expected-note {{remove the cast and change return type of function to 'NSString *' to have the object automatically autoreleased}} |
| 55 | } |
Argyrios Kyrtzidis | 33ace06 | 2013-02-14 17:29:16 +0000 | [diff] [blame] | 56 | |
| 57 | extern void NSLog(NSString *format, ...); |
| 58 | |
| 59 | // rdar://13192395 |
| 60 | void f4(NSString *s) { |
| 61 | NSLog(@"%@", (CFStringRef)s); // expected-error {{cast of Objective-C pointer type 'NSString *' to C pointer type 'CFStringRef' (aka 'const struct __CFString *') requires a bridged cast}} \ |
| 62 | // expected-note{{use __bridge to convert directly (no change in ownership)}} \ |
| 63 | // expected-note{{use CFBridgingRetain call to make an ARC object available as a +1 'CFStringRef' (aka 'const struct __CFString *')}} |
| 64 | } |