[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaObjCXX / arc-bridged-cast.mm
blob581eadae96bd3cf594ae3fde79a43bc380023b10
1 // RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fsyntax-only -fobjc-arc -fblocks -verify %s
3 typedef const void *CFTypeRef;
4 typedef const struct __CFString *CFStringRef;
6 @interface NSString
7 @end
9 CFTypeRef CFCreateSomething();
10 CFStringRef CFCreateString();
11 CFTypeRef CFGetSomething();
12 CFStringRef CFGetString();
14 id CreateSomething();
15 NSString *CreateNSString();
17 template<typename IdType, typename StringType, typename IntPtrType>
18 void from_cf() {
19   id obj1 = (__bridge_transfer IdType)CFCreateSomething();
20   id obj2 = (__bridge_transfer StringType)CFCreateString();
21   (__bridge IntPtrType)CFCreateSomething(); // expected-error{{incompatible types casting 'CFTypeRef' (aka 'const void *') to 'int *' with a __bridge cast}}
22   id obj3 = (__bridge IdType)CFGetSomething();
23   id obj4 = (__bridge StringType)CFGetString();
26 template void from_cf<id, NSString*, int*>(); // expected-note{{in instantiation of function template specialization}}
28 template<typename IdType, typename StringType>
29 void to_cf(id obj) {
30   CFTypeRef cf1 = (__bridge_retained IdType)CreateSomething();
31   CFStringRef cf2 = (__bridge_retained StringType)CreateNSString();
32   CFTypeRef cf3 = (__bridge IdType)CreateSomething();
33   CFStringRef cf4 = (__bridge StringType)CreateNSString(); 
36 template void to_cf<CFTypeRef, CFStringRef>(id);
38 typedef const struct __attribute__((objc_bridge(id))) __CFAnnotatedObject *CFAnnotatedObjectRef;
39 CFAnnotatedObjectRef CFGetAnnotated();
41 void testObjCBridgeId() {
42   id obj;
43   obj = (__bridge id)CFGetAnnotated();
44   obj = (__bridge NSString*)CFGetAnnotated();
45   obj = (__bridge_transfer id)CFGetAnnotated();
46   obj = (__bridge_transfer NSString*)CFGetAnnotated();
48   CFAnnotatedObjectRef ref;
49   ref = (__bridge CFAnnotatedObjectRef) CreateSomething();
50   ref = (__bridge CFAnnotatedObjectRef) CreateNSString();
51   ref = (__bridge_retained CFAnnotatedObjectRef) CreateSomething();
52   ref = (__bridge_retained CFAnnotatedObjectRef) CreateNSString();
55 struct __CFAnnotatedObject {
56 } cf0;
58 extern const CFAnnotatedObjectRef r0;
59 extern const CFAnnotatedObjectRef r1 = &cf0;
60 extern "C" const CFAnnotatedObjectRef r2;
61 extern "C" const CFAnnotatedObjectRef r3 = &cf0;
63 void testExternC() {
64   id obj;
65   obj = (id)r0;
66   obj = (id)r1; // expected-error{{cast of C pointer type 'CFAnnotatedObjectRef' (aka 'const __CFAnnotatedObject *') to Objective-C pointer type 'id' requires a bridged cast}} expected-note{{use __bridge to convert directly}} expected-note{{use __bridge_transfer to transfer ownership of a +1 'CFAnnotatedObjectRef'}}
67   obj = (id)r2;
68   obj = (id)r3; // expected-error{{cast of C pointer type 'CFAnnotatedObjectRef' (aka 'const __CFAnnotatedObject *') to Objective-C pointer type 'id' requires a bridged cast}} expected-note{{use __bridge to convert directly}} expected-note{{use __bridge_transfer to transfer ownership of a +1 'CFAnnotatedObjectRef'}}