blob: 0ba7792146c6c919b2bc5f9003fb45520faf5f43 [file] [log] [blame]
John McCalld1e40d52011-10-02 01:16:38 +00001// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fsyntax-only -fobjc-arc -fblocks -verify %s
Rafael Espindola96e78132013-07-04 16:16:58 +00002// RUN: not %clang_cc1 -triple x86_64-apple-darwin11 -fsyntax-only -fobjc-arc -fblocks -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
John McCallf85e1932011-06-15 23:02:42 +00003
4typedef const void *CFTypeRef;
Fariborz Jahanian52b62362012-02-01 22:56:20 +00005CFTypeRef CFBridgingRetain(id X);
6id CFBridgingRelease(CFTypeRef);
John McCallf85e1932011-06-15 23:02:42 +00007typedef const struct __CFString *CFStringRef;
8
9@interface NSString
10@end
11
12CFTypeRef CFCreateSomething();
13CFStringRef CFCreateString();
14CFTypeRef CFGetSomething();
15CFStringRef CFGetString();
16
17id CreateSomething();
18NSString *CreateNSString();
19
20void from_cf() {
21 id obj1 = (__bridge_transfer id)CFCreateSomething();
22 id obj2 = (__bridge_transfer NSString*)CFCreateString();
23 (__bridge int*)CFCreateSomething(); // expected-error{{incompatible types casting 'CFTypeRef' (aka 'const void *') to 'int *' with a __bridge cast}}
24 id obj3 = (__bridge id)CFGetSomething();
25 id obj4 = (__bridge NSString*)CFGetString();
26}
27
28void to_cf(id obj) {
29 CFTypeRef cf1 = (__bridge_retained CFTypeRef)CreateSomething();
30 CFStringRef cf2 = (__bridge_retained CFStringRef)CreateNSString();
31 CFTypeRef cf3 = (__bridge CFTypeRef)CreateSomething();
John McCall18164422011-06-17 21:23:37 +000032 CFStringRef cf4 = (__bridge CFStringRef)CreateNSString();
33
34 // rdar://problem/9629566 - temporary workaround
John McCallb64915a2011-06-17 21:56:12 +000035 CFTypeRef cf5 = (__bridge_retain CFTypeRef)CreateSomething(); // expected-error {{unknown cast annotation __bridge_retain; did you mean __bridge_retained?}}
Jordan Rosed880b3a2012-06-07 01:10:31 +000036 // CHECK: fix-it:"{{.*}}":{35:20-35:35}:"__bridge_retained"
John McCallf85e1932011-06-15 23:02:42 +000037}
38
Jordan Rosed880b3a2012-06-07 01:10:31 +000039CFTypeRef fixits() {
John McCallf85e1932011-06-15 23:02:42 +000040 id obj1 = (id)CFCreateSomething(); // expected-error{{cast of C pointer type 'CFTypeRef' (aka 'const void *') to Objective-C pointer type 'id' requires a bridged cast}} \
Fariborz Jahanian24b2ab72012-07-27 23:55:46 +000041 // expected-note{{use __bridge to convert directly (no change in ownership)}} expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'CFTypeRef' (aka 'const void *') into ARC}}
Jordan Rosed880b3a2012-06-07 01:10:31 +000042 // CHECK: fix-it:"{{.*}}":{40:17-40:17}:"CFBridgingRelease("
43 // CHECK: fix-it:"{{.*}}":{40:36-40:36}:")"
44
John McCallf85e1932011-06-15 23:02:42 +000045 CFTypeRef cf1 = (CFTypeRef)CreateSomething(); // expected-error{{cast of Objective-C pointer type 'id' to C pointer type 'CFTypeRef' (aka 'const void *') requires a bridged cast}} \
Fariborz Jahanian607f5872012-07-27 21:34:23 +000046 // expected-note{{use __bridge to convert directly (no change in ownership)}} \
Fariborz Jahanian52b62362012-02-01 22:56:20 +000047 // expected-note{{use CFBridgingRetain call to make an ARC object available as a +1 'CFTypeRef' (aka 'const void *')}}
Fariborz Jahanian533b34f2012-07-27 22:37:07 +000048 // CHECK: fix-it:"{{.*}}":{45:30-45:30}:"CFBridgingRetain("
49 // CHECK: fix-it:"{{.*}}":{45:47-45:47}:")"
Jordan Rosed880b3a2012-06-07 01:10:31 +000050
51 return (obj1); // expected-error{{implicit conversion of Objective-C pointer type 'id' to C pointer type 'CFTypeRef' (aka 'const void *') requires a bridged cast}} \
Fariborz Jahanian607f5872012-07-27 21:34:23 +000052 // expected-note{{use __bridge to convert directly (no change in ownership)}} \
Jordan Rosed880b3a2012-06-07 01:10:31 +000053 // expected-note{{use CFBridgingRetain call to make an ARC object available as a +1 'CFTypeRef' (aka 'const void *')}}
Fariborz Jahanian533b34f2012-07-27 22:37:07 +000054 // CHECK: fix-it:"{{.*}}":{51:10-51:10}:"(__bridge CFTypeRef)"
55 // CHECK: fix-it:"{{.*}}":{51:10-51:10}:"CFBridgingRetain"
Jordan Rosed880b3a2012-06-07 01:10:31 +000056}
57
58CFTypeRef fixitsWithSpace(id obj) {
59 return(obj); // expected-error{{implicit conversion of Objective-C pointer type 'id' to C pointer type 'CFTypeRef' (aka 'const void *') requires a bridged cast}} \
Fariborz Jahanian607f5872012-07-27 21:34:23 +000060 // expected-note{{use __bridge to convert directly (no change in ownership)}} \
Jordan Rosed880b3a2012-06-07 01:10:31 +000061 // expected-note{{use CFBridgingRetain call to make an ARC object available as a +1 'CFTypeRef' (aka 'const void *')}}
Fariborz Jahanian533b34f2012-07-27 22:37:07 +000062 // CHECK: fix-it:"{{.*}}":{59:9-59:9}:"(__bridge CFTypeRef)"
63 // CHECK: fix-it:"{{.*}}":{59:9-59:9}:" CFBridgingRetain"
John McCallf85e1932011-06-15 23:02:42 +000064}
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070065
66// rdar://problem/20107345
67typedef const struct __attribute__((objc_bridge(id))) __CFAnnotatedObject *CFAnnotatedObjectRef;
68CFAnnotatedObjectRef CFGetAnnotated();
69
70void testObjCBridgeId() {
71 id obj;
72 obj = (__bridge id)CFGetAnnotated();
73 obj = (__bridge NSString*)CFGetAnnotated();
74 obj = (__bridge_transfer id)CFGetAnnotated();
75 obj = (__bridge_transfer NSString*)CFGetAnnotated();
76
77 CFAnnotatedObjectRef ref;
78 ref = (__bridge CFAnnotatedObjectRef) CreateSomething();
79 ref = (__bridge CFAnnotatedObjectRef) CreateNSString();
80 ref = (__bridge_retained CFAnnotatedObjectRef) CreateSomething();
81 ref = (__bridge_retained CFAnnotatedObjectRef) CreateNSString();
82}
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -070083
84// rdar://20113785
85typedef const struct __attribute__((objc_bridge(UIFont))) __CTFont * CTFontRef;
86
87id testObjCBridgeUnknownTypeToId(CTFontRef font) {
88 id x = (__bridge id)font;
89 return x;
90}
91