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 |
Argyrios Kyrtzidis | d8b4216 | 2012-01-12 02:34:32 +0000 | [diff] [blame] | 2 | // DISABLE: mingw32 |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 3 | |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 4 | #include "Common.h" |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 5 | |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 6 | typedef const struct __CFString * CFStringRef; |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 7 | typedef const void * CFTypeRef; |
| 8 | CFTypeRef CFBridgingRetain(id X); |
| 9 | id CFBridgingRelease(CFTypeRef); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 10 | |
Argyrios Kyrtzidis | 76a5245 | 2012-06-07 00:44:06 +0000 | [diff] [blame] | 11 | struct StrS { |
| 12 | CFStringRef sref_member; |
| 13 | }; |
| 14 | |
| 15 | @interface NSString : NSObject { |
| 16 | CFStringRef sref; |
| 17 | struct StrS *strS; |
| 18 | } |
| 19 | -(id)string; |
| 20 | -(id)newString; |
| 21 | @end |
| 22 | |
| 23 | @implementation NSString |
| 24 | -(id)string { |
| 25 | if (0) |
| 26 | return sref; |
| 27 | else |
| 28 | return strS->sref_member; |
| 29 | } |
| 30 | -(id)newString { |
| 31 | 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}} \ |
| 32 | // expected-note{{use __bridge to convert directly (no change in ownership)}} \ |
| 33 | // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'CFStringRef' (aka 'const struct __CFString *') into ARC}} |
| 34 | } |
| 35 | @end |
| 36 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 37 | void f(BOOL b) { |
| 38 | CFStringRef cfstr; |
| 39 | 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}} \ |
| 40 | // expected-note{{use __bridge to convert directly (no change in ownership)}} \ |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 41 | // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'CFStringRef' (aka 'const struct __CFString *') into ARC}} |
| 42 | 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] | 43 | } |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 44 | |
| 45 | void f2(NSString *s) { |
| 46 | CFStringRef ref; |
| 47 | 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] | 48 | // expected-error {{bad receiver type 'CFStringRef' (aka 'const struct __CFString *')}} \ |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 49 | // expected-note{{use __bridge to convert directly (no change in ownership)}} \ |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 50 | // 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] | 51 | } |
| 52 | |
| 53 | CFStringRef f3() { |
| 54 | 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}} \ |
| 55 | // expected-note {{remove the cast and change return type of function to 'NSString *' to have the object automatically autoreleased}} |
| 56 | } |