xref: /minix3/external/bsd/llvm/dist/clang/test/SemaObjC/arc-unbridged-cast.m (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc// // RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fsyntax-only -fobjc-arc -verify %s
2*f4a2713aSLionel Sambuc
3*f4a2713aSLionel Sambuctypedef const struct __CFString * CFStringRef;
4*f4a2713aSLionel Sambuctypedef const void * CFTypeRef;
5*f4a2713aSLionel SambucCFTypeRef CFBridgingRetain(id X);
6*f4a2713aSLionel Sambucid CFBridgingRelease(CFTypeRef);
7*f4a2713aSLionel Sambuc
8*f4a2713aSLionel Sambuc
9*f4a2713aSLionel Sambuc@interface Object
10*f4a2713aSLionel Sambuc@property CFStringRef property;
11*f4a2713aSLionel Sambuc- (CFStringRef) implicitProperty;
12*f4a2713aSLionel Sambuc- (CFStringRef) newString;
13*f4a2713aSLionel Sambuc- (CFStringRef) makeString;
14*f4a2713aSLionel Sambuc@end
15*f4a2713aSLionel Sambuc
16*f4a2713aSLionel Sambucextern Object *object;
17*f4a2713aSLionel Sambuc
18*f4a2713aSLionel Sambuc// rdar://9744349
19*f4a2713aSLionel Sambucid test0(void) {
20*f4a2713aSLionel Sambuc  id p1 = (id)[object property];
21*f4a2713aSLionel Sambuc  id p2 = (__bridge_transfer id)[object property];
22*f4a2713aSLionel Sambuc  id p3 = (__bridge id)[object property];
23*f4a2713aSLionel Sambuc  return (id) object.property;
24*f4a2713aSLionel Sambuc}
25*f4a2713aSLionel Sambuc
26*f4a2713aSLionel Sambuc// rdar://10140692
27*f4a2713aSLionel SambucCFStringRef unauditedString(void);
28*f4a2713aSLionel SambucCFStringRef plusOneString(void) __attribute__((cf_returns_retained));
29*f4a2713aSLionel Sambuc
30*f4a2713aSLionel Sambuc#pragma clang arc_cf_code_audited begin
31*f4a2713aSLionel SambucCFStringRef auditedString(void);
32*f4a2713aSLionel SambucCFStringRef auditedCreateString(void);
33*f4a2713aSLionel Sambuc#pragma clang arc_cf_code_audited end
34*f4a2713aSLionel Sambuc
35*f4a2713aSLionel Sambucvoid test1(int cond) {
36*f4a2713aSLionel Sambuc  id x;
37*f4a2713aSLionel Sambuc  x = (id) auditedString();
38*f4a2713aSLionel Sambuc  x = (id) (cond ? auditedString() : (void*) 0);
39*f4a2713aSLionel Sambuc  x = (id) (cond ? (void*) 0 : auditedString());
40*f4a2713aSLionel Sambuc  x = (id) (cond ? (CFStringRef) @"help" : auditedString());
41*f4a2713aSLionel Sambuc
42*f4a2713aSLionel Sambuc  x = (id) unauditedString(); // expected-error {{requires a bridged cast}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRelease call to}}
43*f4a2713aSLionel Sambuc  x = (id) (cond ? unauditedString() : (void*) 0); // expected-error {{requires a bridged cast}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRelease call to}}
44*f4a2713aSLionel Sambuc  x = (id) (cond ? (void*) 0 : unauditedString()); // expected-error {{requires a bridged cast}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRelease call to}}
45*f4a2713aSLionel Sambuc  x = (id) (cond ? (CFStringRef) @"help" : unauditedString()); // expected-error {{requires a bridged cast}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRelease call to}}
46*f4a2713aSLionel Sambuc
47*f4a2713aSLionel Sambuc  x = (id) auditedCreateString(); // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRelease call to}}
48*f4a2713aSLionel Sambuc  x = (id) (cond ? auditedCreateString() : (void*) 0); // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRelease call to}}
49*f4a2713aSLionel Sambuc  x = (id) (cond ? (void*) 0 : auditedCreateString()); // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRelease call to}}
50*f4a2713aSLionel Sambuc  x = (id) (cond ? (CFStringRef) @"help" : auditedCreateString()); // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRelease call to}}
51*f4a2713aSLionel Sambuc
52*f4a2713aSLionel Sambuc  x = (id) [object property];
53*f4a2713aSLionel Sambuc  x = (id) (cond ? [object property] : (void*) 0);
54*f4a2713aSLionel Sambuc  x = (id) (cond ? (void*) 0 : [object property]);
55*f4a2713aSLionel Sambuc  x = (id) (cond ? (CFStringRef) @"help" : [object property]);
56*f4a2713aSLionel Sambuc
57*f4a2713aSLionel Sambuc  x = (id) object.property;
58*f4a2713aSLionel Sambuc  x = (id) (cond ? object.property : (void*) 0);
59*f4a2713aSLionel Sambuc  x = (id) (cond ? (void*) 0 : object.property);
60*f4a2713aSLionel Sambuc  x = (id) (cond ? (CFStringRef) @"help" : object.property);
61*f4a2713aSLionel Sambuc
62*f4a2713aSLionel Sambuc  x = (id) object.implicitProperty;
63*f4a2713aSLionel Sambuc  x = (id) (cond ? object.implicitProperty : (void*) 0);
64*f4a2713aSLionel Sambuc  x = (id) (cond ? (void*) 0 : object.implicitProperty);
65*f4a2713aSLionel Sambuc  x = (id) (cond ? (CFStringRef) @"help" : object.implicitProperty);
66*f4a2713aSLionel Sambuc
67*f4a2713aSLionel Sambuc  x = (id) [object makeString];
68*f4a2713aSLionel Sambuc  x = (id) (cond ? [object makeString] : (void*) 0);
69*f4a2713aSLionel Sambuc  x = (id) (cond ? (void*) 0 : [object makeString]);
70*f4a2713aSLionel Sambuc  x = (id) (cond ? (CFStringRef) @"help" : [object makeString]);
71*f4a2713aSLionel Sambuc
72*f4a2713aSLionel Sambuc  x = (id) [object newString];
73*f4a2713aSLionel Sambuc  x = (id) (cond ? [object newString] : (void*) 0);
74*f4a2713aSLionel Sambuc  x = (id) (cond ? (void*) 0 : [object newString]);
75*f4a2713aSLionel Sambuc  x = (id) (cond ? (CFStringRef) @"help" : [object newString]); // a bit questionable
76*f4a2713aSLionel Sambuc}
77*f4a2713aSLionel Sambuc
78*f4a2713aSLionel Sambuc// rdar://problem/10246264
79*f4a2713aSLionel Sambuc@interface CFTaker
80*f4a2713aSLionel Sambuc- (void) takeOrdinary: (CFStringRef) arg;
81*f4a2713aSLionel Sambuc- (void) takeVariadic: (int) n, ...;
82*f4a2713aSLionel Sambuc- (void) takeConsumed: (CFStringRef __attribute__((cf_consumed))) arg;
83*f4a2713aSLionel Sambuc@end
84*f4a2713aSLionel Sambucvoid testCFTaker(CFTaker *taker, id string) {
85*f4a2713aSLionel Sambuc  [taker takeOrdinary: (CFStringRef) string];
86*f4a2713aSLionel Sambuc  [taker takeVariadic: 1, (CFStringRef) string];
87*f4a2713aSLionel Sambuc  [taker takeConsumed: (CFStringRef) string]; // expected-error {{cast of Objective-C pointer type 'id' to C pointer type 'CFStringRef'}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRetain call to}}
88*f4a2713aSLionel Sambuc}
89*f4a2713aSLionel Sambuc
90*f4a2713aSLionel Sambucvoid takeCFOrdinaryUnaudited(CFStringRef arg);
91*f4a2713aSLionel Sambucvoid takeCFVariadicUnaudited(int n, ...);
92*f4a2713aSLionel Sambucvoid takeCFConsumedUnaudited(CFStringRef __attribute__((cf_consumed)) arg);
93*f4a2713aSLionel Sambuc#pragma clang arc_cf_code_audited begin
94*f4a2713aSLionel Sambucvoid takeCFOrdinaryAudited(CFStringRef arg);
95*f4a2713aSLionel Sambucvoid takeCFVariadicAudited(int n, ...);
96*f4a2713aSLionel Sambucvoid takeCFConsumedAudited(CFStringRef __attribute__((cf_consumed)) arg);
97*f4a2713aSLionel Sambuc#pragma clang arc_cf_code_audited end
98*f4a2713aSLionel Sambuc
99*f4a2713aSLionel Sambucvoid testTakerFunctions(id string) {
100*f4a2713aSLionel Sambuc  takeCFOrdinaryUnaudited((CFStringRef) string); // expected-error {{cast of Objective-C pointer type 'id' to C pointer type 'CFStringRef'}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRetain call to}}
101*f4a2713aSLionel Sambuc  takeCFVariadicUnaudited(1, (CFStringRef) string); // expected-error {{cast of Objective-C pointer type 'id' to C pointer type 'CFStringRef'}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRetain call to}}
102*f4a2713aSLionel Sambuc  takeCFConsumedUnaudited((CFStringRef) string); // expected-error {{cast of Objective-C pointer type 'id' to C pointer type 'CFStringRef'}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRetain call to}}
103*f4a2713aSLionel Sambuc
104*f4a2713aSLionel Sambuc  void (*taker)(CFStringRef) = 0;
105*f4a2713aSLionel Sambuc  taker((CFStringRef) string); // expected-error {{cast of Objective-C pointer type 'id' to C pointer type 'CFStringRef'}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRetain call to}}
106*f4a2713aSLionel Sambuc
107*f4a2713aSLionel Sambuc  takeCFOrdinaryAudited((CFStringRef) string);
108*f4a2713aSLionel Sambuc  takeCFVariadicAudited(1, (CFStringRef) string);
109*f4a2713aSLionel Sambuc  takeCFConsumedAudited((CFStringRef) string); // expected-error {{cast of Objective-C pointer type 'id' to C pointer type 'CFStringRef'}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRetain call to}}
110*f4a2713aSLionel Sambuc}
111*f4a2713aSLionel Sambuc
112*f4a2713aSLionel Sambucvoid testTakerFunctions_parens(id string) {
113*f4a2713aSLionel Sambuc  takeCFOrdinaryUnaudited(((CFStringRef) string)); // expected-error {{cast of Objective-C pointer type 'id' to C pointer type 'CFStringRef'}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRetain call to}}
114*f4a2713aSLionel Sambuc  takeCFVariadicUnaudited(1, ((CFStringRef) string)); // expected-error {{cast of Objective-C pointer type 'id' to C pointer type 'CFStringRef'}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRetain call to}}
115*f4a2713aSLionel Sambuc  takeCFConsumedUnaudited(((CFStringRef) string)); // expected-error {{cast of Objective-C pointer type 'id' to C pointer type 'CFStringRef'}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRetain call to}}
116*f4a2713aSLionel Sambuc
117*f4a2713aSLionel Sambuc  void (*taker)(CFStringRef) = 0;
118*f4a2713aSLionel Sambuc  taker(((CFStringRef) string)); // expected-error {{cast of Objective-C pointer type 'id' to C pointer type 'CFStringRef'}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRetain call to}}
119*f4a2713aSLionel Sambuc
120*f4a2713aSLionel Sambuc  takeCFOrdinaryAudited(((CFStringRef) string));
121*f4a2713aSLionel Sambuc  takeCFVariadicAudited(1, ((CFStringRef) string));
122*f4a2713aSLionel Sambuc  takeCFConsumedAudited(((CFStringRef) string)); // expected-error {{cast of Objective-C pointer type 'id' to C pointer type 'CFStringRef'}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRetain call to}}
123*f4a2713aSLionel Sambuc}
124