blob: b709afd234be1ff75488965610dd8ab4041ebdf7 [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}} \
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 Jahanian52b62362012-02-01 22:56:20 +000039 // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'CFStringRef' (aka 'const struct __CFString *') into ARC}}
Fariborz Jahanian304efd52012-07-26 23:17:04 +000040 void *vp = str; // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRetain call}}
John McCall8f0e8d22011-06-15 23:25:17 +000041}
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +000042
43void f2(NSString *s) {
44 CFStringRef ref;
45 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 +000046 // expected-error {{bad receiver type 'CFStringRef' (aka 'const struct __CFString *')}} \
Fariborz Jahanian52b62362012-02-01 22:56:20 +000047 // 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 +000048}
49
50CFStringRef f3() {
51 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}} \
52 // expected-note {{remove the cast and change return type of function to 'NSString *' to have the object automatically autoreleased}}
53}