| 1 |
// RUN: %clang_cc1 -arcmt-action=check -verify -triple x86_64-apple-darwin10 %s |
| 2 |
|
| 3 |
#include "Common.h" |
| 4 |
|
| 5 |
typedef const struct __CFString * CFStringRef; |
| 6 |
typedef const void * CFTypeRef; |
| 7 |
CFTypeRef CFBridgingRetain(id X); |
| 8 |
id CFBridgingRelease(CFTypeRef); |
| 9 |
|
| 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}} \ |
| 31 |
// expected-note{{use __bridge to convert directly (no change in ownership)}} \ |
| 32 |
// expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'CFStringRef' (aka 'const struct __CFString *') into ARC}} |
| 33 |
} |
| 34 |
@end |
| 35 |
|
| 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}} \ |
| 39 |
// expected-note{{use __bridge to convert directly (no change in ownership)}} \ |
| 40 |
// expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'CFStringRef' (aka 'const struct __CFString *') into ARC}} |
| 41 |
void *vp = str; // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRetain call}} expected-note {{use __bridge}} |
| 42 |
} |
| 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}} \ |
| 47 |
// expected-error {{bad receiver type 'CFStringRef' (aka 'const struct __CFString *')}} \ |
| 48 |
// expected-note{{use __bridge to convert directly (no change in ownership)}} \ |
| 49 |
// expected-note{{use CFBridgingRetain call to make an ARC object available as a +1 'CFStringRef' (aka 'const struct __CFString *')}} |
| 50 |
} |
| 51 |
|
| 52 |
CFStringRef f3(void) { |
| 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 |
} |
| 56 |
|
| 57 |
extern void NSLog(NSString *format, ...); |
| 58 |
|
| 59 |
void f4(NSString *s) { |
| 60 |
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}} \ |
| 61 |
// expected-note{{use __bridge to convert directly (no change in ownership)}} \ |
| 62 |
// expected-note{{use CFBridgingRetain call to make an ARC object available as a +1 'CFStringRef' (aka 'const struct __CFString *')}} |
| 63 |
} |