blob: 2b421b0757ade8ae547efa9de1aefaf5ff4a4826 [file] [log] [blame]
John McCalld1e40d52011-10-02 01:16:38 +00001// RUN: %clang_cc1 -arcmt-check -verify -triple x86_64-apple-darwin10 %s
John McCall8f0e8d22011-06-15 23:25:17 +00002
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +00003#include "Common.h"
John McCall8f0e8d22011-06-15 23:25:17 +00004
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +00005typedef const struct __CFString * CFStringRef;
Fariborz Jahanian52b62362012-02-01 22:56:20 +00006typedef const void * CFTypeRef;
7CFTypeRef CFBridgingRetain(id X);
8id CFBridgingRelease(CFTypeRef);
John McCall8f0e8d22011-06-15 23:25:17 +00009
Argyrios Kyrtzidis76a52452012-06-07 00:44:06 +000010struct 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 Jahanian607f5872012-07-27 21:34:23 +000031 // expected-note{{use __bridge to convert directly (no change in ownership)}} \
Argyrios Kyrtzidis76a52452012-06-07 00:44:06 +000032 // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'CFStringRef' (aka 'const struct __CFString *') into ARC}}
33}
34@end
35
John McCall8f0e8d22011-06-15 23:25:17 +000036void 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 Jahanian607f5872012-07-27 21:34:23 +000039 // expected-note{{use __bridge to convert directly (no change in ownership)}} \
Fariborz Jahanian52b62362012-02-01 22:56:20 +000040 // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'CFStringRef' (aka 'const struct __CFString *') into ARC}}
Fariborz Jahanian607f5872012-07-27 21:34:23 +000041 void *vp = str; // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRetain call}} expected-note {{use __bridge}}
John McCall8f0e8d22011-06-15 23:25:17 +000042}
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +000043
44void 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 Trieu2fe9b7f2011-12-15 00:38:15 +000047 // expected-error {{bad receiver type 'CFStringRef' (aka 'const struct __CFString *')}} \
Fariborz Jahanian607f5872012-07-27 21:34:23 +000048 // expected-note{{use __bridge to convert directly (no change in ownership)}} \
Fariborz Jahanian52b62362012-02-01 22:56:20 +000049 // expected-note{{use CFBridgingRetain call to make an ARC object available as a +1 'CFStringRef' (aka 'const struct __CFString *')}}
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +000050}
51
52CFStringRef 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 Kyrtzidis33ace062013-02-14 17:29:16 +000056
57extern void NSLog(NSString *format, ...);
58
59// rdar://13192395
60void 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}