xref: /llvm-project/clang/test/SemaObjCXX/arc-unbridged-cast.mm (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;
5extern "C" CFTypeRef CFBridgingRetain(id X);
6extern "C" id 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
33void test1(int cond) {
34  id x;
35  x = (id) auditedString();
36  x = (id) (cond ? auditedString() : (void*) 0);
37  x = (id) (cond ? (void*) 0 : auditedString());
38  x = (id) (cond ? (CFStringRef) @"help" : auditedString());
39
40  x = (id) unauditedString(); // expected-error {{requires a bridged cast}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRelease call to}}
41  x = (id) (cond ? unauditedString() : (void*) 0); // expected-error {{requires a bridged cast}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRelease call to}}
42  x = (id) (cond ? (void*) 0 : unauditedString()); // expected-error {{requires a bridged cast}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRelease call to}}
43  x = (id) (cond ? (CFStringRef) @"help" : unauditedString()); // expected-error {{requires a bridged cast}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRelease call to}}
44
45  x = (id) auditedCreateString(); // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRelease call to}}
46  x = (id) (cond ? auditedCreateString() : (void*) 0); // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRelease call to}}
47  x = (id) (cond ? (void*) 0 : auditedCreateString()); // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRelease call to}}
48  x = (id) (cond ? (CFStringRef) @"help" : auditedCreateString()); // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRelease call to}}
49
50  x = (id) [object property];
51  x = (id) (cond ? [object property] : (void*) 0);
52  x = (id) (cond ? (void*) 0 : [object property]);
53  x = (id) (cond ? (CFStringRef) @"help" : [object property]);
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.implicitProperty;
61  x = (id) (cond ? object.implicitProperty : (void*) 0);
62  x = (id) (cond ? (void*) 0 : object.implicitProperty);
63  x = (id) (cond ? (CFStringRef) @"help" : object.implicitProperty);
64
65  x = (id) [object makeString];
66  x = (id) (cond ? [object makeString] : (void*) 0);
67  x = (id) (cond ? (void*) 0 : [object makeString]);
68  x = (id) (cond ? (CFStringRef) @"help" : [object makeString]);
69
70  x = (id) [object newString];
71  x = (id) (cond ? [object newString] : (void*) 0);
72  x = (id) (cond ? (void*) 0 : [object newString]);
73  x = (id) (cond ? (CFStringRef) @"help" : [object newString]); // a bit questionable
74}
75
76@interface CFTaker
77- (void) takeOrdinary: (CFStringRef) arg;
78- (void) takeVariadic: (int) n, ...;
79- (void) takeConsumed: (CFStringRef __attribute__((cf_consumed))) arg;
80@end
81void testCFTaker(CFTaker *taker, id string) {
82  [taker takeOrdinary: (CFStringRef) string];
83  [taker takeVariadic: 1, (CFStringRef) string];
84  [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}}
85}
86
87void takeCFOrdinaryUnaudited(CFStringRef arg);
88void takeCFVariadicUnaudited(int n, ...);
89void takeCFConsumedUnaudited(CFStringRef __attribute__((cf_consumed)) arg);
90#pragma clang arc_cf_code_audited begin
91void takeCFOrdinaryAudited(CFStringRef arg);
92void takeCFVariadicAudited(int n, ...);
93void takeCFConsumedAudited(CFStringRef __attribute__((cf_consumed)) arg);
94#pragma clang arc_cf_code_audited end
95
96void testTakerFunctions(id string) {
97  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}}
98  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}}
99  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}}
100
101  void (*taker)(CFStringRef) = 0;
102  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}}
103
104  takeCFOrdinaryAudited((CFStringRef) string);
105  takeCFVariadicAudited(1, (CFStringRef) string);
106  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}}
107}
108
109id obj;
110
111void rdar12788838() {
112  void *foo = reinterpret_cast<void *>(obj); // expected-error {{cast of Objective-C pointer type 'id' to C pointer type 'void *' requires a bridged cast}} \
113		// expected-note {{use __bridge with C-style cast to convert directly}} \
114		// expected-note {{use CFBridgingRetain call to make an ARC object available as a +1 'void *'}}
115}
116