blob: 6a39e707717ee76a1d50146dd3429ad320bfd992 [file] [log] [blame]
John McCall9b0a7ce2011-10-02 01:16:38 +00001// // RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fsyntax-only -fobjc-arc -verify %s
Fariborz Jahanian4ad56862011-06-20 20:54:42 +00002
3typedef const struct __CFString * CFStringRef;
Fariborz Jahanian30febeb2012-02-01 22:56:20 +00004typedef const void * CFTypeRef;
5CFTypeRef CFBridgingRetain(id X);
6id CFBridgingRelease(CFTypeRef);
7
Fariborz Jahanian4ad56862011-06-20 20:54:42 +00008
John McCalle4fe2452011-10-01 01:01:08 +00009@interface Object
10@property CFStringRef property;
11- (CFStringRef) implicitProperty;
12- (CFStringRef) newString;
13- (CFStringRef) makeString;
Fariborz Jahanian4ad56862011-06-20 20:54:42 +000014@end
15
John McCalle4fe2452011-10-01 01:01:08 +000016extern Object *object;
17
18// rdar://9744349
19id test0(void) {
20 id p1 = (id)[object property];
21 id p2 = (__bridge_transfer id)[object property];
22 id p3 = (__bridge id)[object property];
23 return (id) object.property;
Fariborz Jahanian4ad56862011-06-20 20:54:42 +000024}
John McCalle4fe2452011-10-01 01:01:08 +000025
26// rdar://10140692
27CFStringRef unauditedString(void);
28CFStringRef plusOneString(void) __attribute__((cf_returns_retained));
29
30#pragma clang arc_cf_code_audited begin
31CFStringRef auditedString(void);
32CFStringRef auditedCreateString(void);
33#pragma clang arc_cf_code_audited end
34
35void test1(int cond) {
36 id x;
37 x = (id) auditedString();
38 x = (id) (cond ? auditedString() : (void*) 0);
39 x = (id) (cond ? (void*) 0 : auditedString());
40 x = (id) (cond ? (CFStringRef) @"help" : auditedString());
41
Fariborz Jahanian84c97ca2012-07-27 21:34:23 +000042 x = (id) unauditedString(); // expected-error {{requires a bridged cast}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRelease call to}}
43 x = (id) (cond ? unauditedString() : (void*) 0); // expected-error {{requires a bridged cast}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRelease call to}}
44 x = (id) (cond ? (void*) 0 : unauditedString()); // expected-error {{requires a bridged cast}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRelease call to}}
45 x = (id) (cond ? (CFStringRef) @"help" : unauditedString()); // expected-error {{requires a bridged cast}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRelease call to}}
John McCalle4fe2452011-10-01 01:01:08 +000046
Fariborz Jahanian36986c62012-07-27 22:37:07 +000047 x = (id) auditedCreateString(); // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRelease call to}}
48 x = (id) (cond ? auditedCreateString() : (void*) 0); // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRelease call to}}
49 x = (id) (cond ? (void*) 0 : auditedCreateString()); // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRelease call to}}
50 x = (id) (cond ? (CFStringRef) @"help" : auditedCreateString()); // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRelease call to}}
John McCalle4fe2452011-10-01 01:01:08 +000051
52 x = (id) [object property];
53 x = (id) (cond ? [object property] : (void*) 0);
54 x = (id) (cond ? (void*) 0 : [object property]);
55 x = (id) (cond ? (CFStringRef) @"help" : [object property]);
56
57 x = (id) object.property;
58 x = (id) (cond ? object.property : (void*) 0);
59 x = (id) (cond ? (void*) 0 : object.property);
60 x = (id) (cond ? (CFStringRef) @"help" : object.property);
61
62 x = (id) object.implicitProperty;
63 x = (id) (cond ? object.implicitProperty : (void*) 0);
64 x = (id) (cond ? (void*) 0 : object.implicitProperty);
65 x = (id) (cond ? (CFStringRef) @"help" : object.implicitProperty);
66
67 x = (id) [object makeString];
68 x = (id) (cond ? [object makeString] : (void*) 0);
69 x = (id) (cond ? (void*) 0 : [object makeString]);
70 x = (id) (cond ? (CFStringRef) @"help" : [object makeString]);
71
72 x = (id) [object newString];
73 x = (id) (cond ? [object newString] : (void*) 0);
74 x = (id) (cond ? (void*) 0 : [object newString]);
75 x = (id) (cond ? (CFStringRef) @"help" : [object newString]); // a bit questionable
76}
John McCall4124c492011-10-17 18:40:02 +000077
78// rdar://problem/10246264
79@interface CFTaker
80- (void) takeOrdinary: (CFStringRef) arg;
81- (void) takeVariadic: (int) n, ...;
82- (void) takeConsumed: (CFStringRef __attribute__((cf_consumed))) arg;
83@end
84void testCFTaker(CFTaker *taker, id string) {
85 [taker takeOrdinary: (CFStringRef) string];
86 [taker takeVariadic: 1, (CFStringRef) string];
Fariborz Jahanian84c97ca2012-07-27 21:34:23 +000087 [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}}
John McCall4124c492011-10-17 18:40:02 +000088}
89
90void takeCFOrdinaryUnaudited(CFStringRef arg);
91void takeCFVariadicUnaudited(int n, ...);
92void takeCFConsumedUnaudited(CFStringRef __attribute__((cf_consumed)) arg);
93#pragma clang arc_cf_code_audited begin
94void takeCFOrdinaryAudited(CFStringRef arg);
95void takeCFVariadicAudited(int n, ...);
96void takeCFConsumedAudited(CFStringRef __attribute__((cf_consumed)) arg);
97#pragma clang arc_cf_code_audited end
98
99void testTakerFunctions(id string) {
Fariborz Jahanian84c97ca2012-07-27 21:34:23 +0000100 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 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 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}}
John McCall4124c492011-10-17 18:40:02 +0000103
104 void (*taker)(CFStringRef) = 0;
Fariborz Jahanian84c97ca2012-07-27 21:34:23 +0000105 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}}
John McCall4124c492011-10-17 18:40:02 +0000106
107 takeCFOrdinaryAudited((CFStringRef) string);
108 takeCFVariadicAudited(1, (CFStringRef) string);
Fariborz Jahanian84c97ca2012-07-27 21:34:23 +0000109 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}}
John McCall4124c492011-10-17 18:40:02 +0000110}
111
112void testTakerFunctions_parens(id string) {
Fariborz Jahanian84c97ca2012-07-27 21:34:23 +0000113 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 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 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}}
John McCall4124c492011-10-17 18:40:02 +0000116
117 void (*taker)(CFStringRef) = 0;
Fariborz Jahanian84c97ca2012-07-27 21:34:23 +0000118 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}}
John McCall4124c492011-10-17 18:40:02 +0000119
120 takeCFOrdinaryAudited(((CFStringRef) string));
121 takeCFVariadicAudited(1, ((CFStringRef) string));
Fariborz Jahanian84c97ca2012-07-27 21:34:23 +0000122 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}}
John McCall4124c492011-10-17 18:40:02 +0000123}