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