Debian Codegraph @

clang/test/SemaObjC/arc-type-conversion.m

raw
1 // RUN: %clang_cc1 -fsyntax-only -fobjc-arc -fobjc-runtime-has-weak -verify -fblocks -Wno-incompatible-pointer-types %s
2
3 typedef const void * CFTypeRef;
4 CFTypeRef CFBridgingRetain(id X);
5 id CFBridgingRelease(CFTypeRef);
6
7 void * cvt(id arg)
8 {
9 void* voidp_val;
10 (void)(int*)arg; // expected-error {{cast of an Objective-C pointer to 'int *' is disallowed with ARC}}
11 (void)(id)arg;
12 (void)(__autoreleasing id*)arg; // expected-error {{cast of an Objective-C pointer to '__autoreleasing id *' is disallowed with ARC}}
13 (void)(id*)arg; // expected-error {{cast of an Objective-C pointer to '__strong id *' is disallowed with ARC}}
14
15 (void)(__autoreleasing id**)voidp_val;
16 (void)(void*)voidp_val;
17 (void)(void**)arg; // expected-error {{cast of an Objective-C pointer to 'void **' is disallowed with ARC}}
18 cvt((void*)arg); // expected-error {{cast of Objective-C pointer type 'id' to C pointer type 'void *' requires a bridged cast}} \
19 // expected-error {{implicit conversion of C pointer type 'void *' to Objective-C pointer type 'id' requires a bridged cast}} \
20 // expected-note 2 {{use __bridge to convert directly (no change in ownership)}} \
21 // expected-note {{use CFBridgingRetain call to make an ARC object available as a +1 'void *'}} \
22 // expected-note {{use CFBridgingRelease call to transfer ownership of a +1 'void *' into ARC}}
23 cvt(0);
24 (void)(__strong id**)(0);
25 return arg; // expected-error {{implicit conversion of Objective-C pointer type 'id' to C pointer type 'void *' requires a bridged cast}} \
26 // expected-note {{use __bridge to convert directly (no change in ownership)}} \
27 // expected-note {{use CFBridgingRetain call to make an ARC object available as a +1 'void *'}}
28 }
29
30 void to_void(__strong id *sip, __weak id *wip,
31 __autoreleasing id *aip,
32 __unsafe_unretained id *uip) {
33 void *vp1 = sip;
34 void *vp2 = wip;
35 void *vp3 = aip;
36 void *vp4 = uip;
37 (void)(void*)sip;
38 (void)(void*)wip;
39 (void)(void*)aip;
40 (void)(void*)uip;
41 (void)(void*)&sip;
42 (void)(void*)&wip;
43 (void)(void*)&aip;
44 (void)(void*)&uip;
45 }
46
47 void from_void(void *vp) {
48 __strong id *sip = (__strong id *)vp;
49 __weak id *wip = (__weak id *)vp;
50 __autoreleasing id *aip = (__autoreleasing id *)vp;
51 __unsafe_unretained id *uip = (__unsafe_unretained id *)vp;
52
53 __strong id **sipp = (__strong id **)vp;
54 __weak id **wipp = (__weak id **)vp;
55 __autoreleasing id **aipp = (__autoreleasing id **)vp;
56 __unsafe_unretained id **uipp = (__unsafe_unretained id **)vp;
57
58 sip = vp; // expected-error{{implicit conversion of a non-Objective-C pointer type 'void *' to '__strong id *' is disallowed with ARC}}
59 wip = vp; // expected-error{{implicit conversion of a non-Objective-C pointer type 'void *' to '__weak id *' is disallowed with ARC}}
60 aip = vp; // expected-error{{implicit conversion of a non-Objective-C pointer type 'void *' to '__autoreleasing id *' is disallowed with ARC}}
61 uip = vp; // expected-error{{implicit conversion of a non-Objective-C pointer type 'void *' to '__unsafe_unretained id *' is disallowed with ARC}}
62 }
63
64 typedef void (^Block)(void);
65 typedef void (^Block_strong)(void) __strong;
66 typedef void (^Block_autoreleasing)(void) __autoreleasing;
67
68 @class NSString;
69
70 void ownership_transfer_in_cast(void *vp, Block *pblk) {
71 __strong NSString **sip = (NSString**)(__strong id *)vp;
72 __weak NSString **wip = (NSString**)(__weak id *)vp;
73 __autoreleasing id *aip = (id*)(__autoreleasing id *)vp;
74 __unsafe_unretained id *uip = (id*)(__unsafe_unretained id *)vp;
75
76 __strong id **sipp = (id**)(__strong id **)vp;
77 __weak id **wipp = (id**)(__weak id **)vp;
78 __autoreleasing id **aipp = (id**)(__autoreleasing id **)vp;
79 __unsafe_unretained id **uipp = (id**)(__unsafe_unretained id **)vp;
80
81 Block_strong blk_strong1;
82 Block_strong blk_strong2 = (Block)blk_strong1;
83 Block_autoreleasing *blk_auto = (Block*)pblk;
84
85 id lv;
86 (void)(id)&lv; // expected-error {{cast of an indirect pointer to an Objective-C pointer to 'id'}}
87 (void)(id*)lv; // expected-error {{cast of an Objective-C pointer to '__strong id *'}}
88 (void)(NSString*)&lv; // expected-error {{cast of an indirect pointer to an Objective-C pointer to 'NSString *'}}
89 (void)(NSString**)lv; // expected-error {{cast of an Objective-C pointer to 'NSString *__strong *'}}
90 (void)(Block)&lv; // expected-error {{cast of an indirect pointer to an Objective-C pointer to 'Block'}}
91 (void)(Block*)lv; // expected-error {{cast of an Objective-C pointer to '__strong Block *'}}
92 }
93
94 void conversion_in_conditional(id a, void* b) {
95 id c = 1 ? a : b; // expected-error {{operands to conditional of types 'id' and 'void *' are incompatible in ARC mode}}
96 id d = 1 ? b : a; // expected-error {{operands to conditional of types 'void *' and 'id' are incompatible in ARC mode}}
97 }
98
99 void conversion_pointer_to_id(__strong id *x) {
100 struct S {
101 int a[2];
102 } s, *p;
103
104 x = (__strong id *)&s;
105 x = &s; // expected-error {{implicit conversion of a non-Objective-C pointer type 'struct S *' to '__strong id *' is disallowed with ARC}}
106 p = (struct S *)x;
107 p = x; // expected-error {{implicit conversion of an indirect pointer to an Objective-C pointer to 'struct S *' is disallowed with ARC}}
108 }