blob: 80d694e586805f774351bd0284f52881726fdf95 [file] [log] [blame]
John McCalld1e40d52011-10-02 01:16:38 +00001// RUN: %clang_cc1 -arcmt-check -verify -triple x86_64-apple-darwin10 %s
Argyrios Kyrtzidisd8b42162012-01-12 02:34:32 +00002// DISABLE: mingw32
John McCall8f0e8d22011-06-15 23:25:17 +00003
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +00004#include "Common.h"
John McCall8f0e8d22011-06-15 23:25:17 +00005
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +00006typedef const struct __CFString * CFStringRef;
Fariborz Jahanian52b62362012-02-01 22:56:20 +00007typedef const void * CFTypeRef;
8CFTypeRef CFBridgingRetain(id X);
9id CFBridgingRelease(CFTypeRef);
John McCall8f0e8d22011-06-15 23:25:17 +000010
Argyrios Kyrtzidis76a52452012-06-07 00:44:06 +000011struct 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 McCall8f0e8d22011-06-15 23:25:17 +000037void 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 Jahanian52b62362012-02-01 22:56:20 +000041 // 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 McCall8f0e8d22011-06-15 23:25:17 +000043}
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +000044
45void 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 Trieu2fe9b7f2011-12-15 00:38:15 +000048 // expected-error {{bad receiver type 'CFStringRef' (aka 'const struct __CFString *')}} \
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +000049 // expected-note{{use __bridge to convert directly (no change in ownership)}} \
Fariborz Jahanian52b62362012-02-01 22:56:20 +000050 // 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 +000051}
52
53CFStringRef 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}