| John McCall | 9b0a7ce | 2011-10-02 01:16:38 +0000 | [diff] [blame] | 1 | // // RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fsyntax-only -fobjc-arc -verify %s |
| Fariborz Jahanian | 4ad5686 | 2011-06-20 20:54:42 +0000 | [diff] [blame] | 2 | |
| 3 | typedef const struct __CFString * CFStringRef; |
| Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 4 | typedef const void * CFTypeRef; |
| 5 | CFTypeRef CFBridgingRetain(id X); |
| 6 | id CFBridgingRelease(CFTypeRef); |
| 7 | |
| Fariborz Jahanian | 4ad5686 | 2011-06-20 20:54:42 +0000 | [diff] [blame] | 8 | |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 9 | @interface Object |
| 10 | @property CFStringRef property; |
| 11 | - (CFStringRef) implicitProperty; |
| 12 | - (CFStringRef) newString; |
| 13 | - (CFStringRef) makeString; |
| Fariborz Jahanian | 4ad5686 | 2011-06-20 20:54:42 +0000 | [diff] [blame] | 14 | @end |
| 15 | |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 16 | extern Object *object; |
| 17 | |
| 18 | // rdar://9744349 |
| 19 | id 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 Jahanian | 4ad5686 | 2011-06-20 20:54:42 +0000 | [diff] [blame] | 24 | } |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 25 | |
| 26 | // rdar://10140692 |
| 27 | CFStringRef unauditedString(void); |
| 28 | CFStringRef plusOneString(void) __attribute__((cf_returns_retained)); |
| 29 | |
| 30 | #pragma clang arc_cf_code_audited begin |
| 31 | CFStringRef auditedString(void); |
| 32 | CFStringRef auditedCreateString(void); |
| 33 | #pragma clang arc_cf_code_audited end |
| 34 | |
| Ben Langmuir | 443aa4b | 2015-02-25 20:09:06 +0000 | [diff] [blame] | 35 | extern const CFStringRef kUserConst; |
| 36 | |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 37 | void test1(int cond) { |
| 38 | id x; |
| 39 | x = (id) auditedString(); |
| Ben Langmuir | 443aa4b | 2015-02-25 20:09:06 +0000 | [diff] [blame] | 40 | x = (id) kUserConst; |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 41 | x = (id) (cond ? auditedString() : (void*) 0); |
| 42 | x = (id) (cond ? (void*) 0 : auditedString()); |
| 43 | x = (id) (cond ? (CFStringRef) @"help" : auditedString()); |
| Ben Langmuir | 443aa4b | 2015-02-25 20:09:06 +0000 | [diff] [blame] | 44 | x = (id) (cond ? (CFStringRef) @"help" : kUserConst); |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 45 | |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 46 | x = (id) unauditedString(); // expected-error {{requires a bridged cast}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRelease call to}} |
| 47 | x = (id) (cond ? unauditedString() : (void*) 0); // expected-error {{requires a bridged cast}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRelease call to}} |
| Ben Langmuir | 443aa4b | 2015-02-25 20:09:06 +0000 | [diff] [blame] | 48 | x = (id) (cond ? unauditedString() : kUserConst); // expected-error {{requires a bridged cast}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRelease call to}} |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 49 | x = (id) (cond ? (void*) 0 : unauditedString()); // expected-error {{requires a bridged cast}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRelease call to}} |
| 50 | x = (id) (cond ? (CFStringRef) @"help" : unauditedString()); // expected-error {{requires a bridged cast}} expected-note {{use __bridge to}} expected-note {{use CFBridgingRelease call to}} |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 51 | |
| Fariborz Jahanian | 36986c6 | 2012-07-27 22:37:07 +0000 | [diff] [blame] | 52 | x = (id) auditedCreateString(); // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRelease call to}} |
| 53 | x = (id) (cond ? auditedCreateString() : (void*) 0); // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRelease call to}} |
| 54 | x = (id) (cond ? (void*) 0 : auditedCreateString()); // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRelease call to}} |
| 55 | x = (id) (cond ? (CFStringRef) @"help" : auditedCreateString()); // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRelease call to}} |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 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.property; |
| 63 | x = (id) (cond ? object.property : (void*) 0); |
| 64 | x = (id) (cond ? (void*) 0 : object.property); |
| 65 | x = (id) (cond ? (CFStringRef) @"help" : object.property); |
| 66 | |
| 67 | x = (id) object.implicitProperty; |
| 68 | x = (id) (cond ? object.implicitProperty : (void*) 0); |
| 69 | x = (id) (cond ? (void*) 0 : object.implicitProperty); |
| 70 | x = (id) (cond ? (CFStringRef) @"help" : object.implicitProperty); |
| 71 | |
| 72 | x = (id) [object makeString]; |
| 73 | x = (id) (cond ? [object makeString] : (void*) 0); |
| 74 | x = (id) (cond ? (void*) 0 : [object makeString]); |
| 75 | x = (id) (cond ? (CFStringRef) @"help" : [object makeString]); |
| Ben Langmuir | 443aa4b | 2015-02-25 20:09:06 +0000 | [diff] [blame] | 76 | x = (id) (cond ? kUserConst : [object makeString]); |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 77 | |
| 78 | x = (id) [object newString]; |
| 79 | x = (id) (cond ? [object newString] : (void*) 0); |
| 80 | x = (id) (cond ? (void*) 0 : [object newString]); |
| 81 | x = (id) (cond ? (CFStringRef) @"help" : [object newString]); // a bit questionable |
| Ben Langmuir | 443aa4b | 2015-02-25 20:09:06 +0000 | [diff] [blame] | 82 | x = (id) (cond ? kUserConst : [object newString]); // expected-error{{requires a bridged cast}} expected-note{{use __bridge to}} expected-note{{use CFBridgingRelease call to}} |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 83 | } |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 84 | |
| 85 | // rdar://problem/10246264 |
| 86 | @interface CFTaker |
| 87 | - (void) takeOrdinary: (CFStringRef) arg; |
| 88 | - (void) takeVariadic: (int) n, ...; |
| 89 | - (void) takeConsumed: (CFStringRef __attribute__((cf_consumed))) arg; |
| 90 | @end |
| 91 | void testCFTaker(CFTaker *taker, id string) { |
| 92 | [taker takeOrdinary: (CFStringRef) string]; |
| 93 | [taker takeVariadic: 1, (CFStringRef) string]; |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 94 | [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 McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void takeCFOrdinaryUnaudited(CFStringRef arg); |
| 98 | void takeCFVariadicUnaudited(int n, ...); |
| 99 | void takeCFConsumedUnaudited(CFStringRef __attribute__((cf_consumed)) arg); |
| 100 | #pragma clang arc_cf_code_audited begin |
| 101 | void takeCFOrdinaryAudited(CFStringRef arg); |
| 102 | void takeCFVariadicAudited(int n, ...); |
| 103 | void takeCFConsumedAudited(CFStringRef __attribute__((cf_consumed)) arg); |
| 104 | #pragma clang arc_cf_code_audited end |
| 105 | |
| 106 | void testTakerFunctions(id string) { |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 107 | 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}} |
| 108 | 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}} |
| 109 | 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 McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 110 | |
| 111 | void (*taker)(CFStringRef) = 0; |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 112 | 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 McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 113 | |
| 114 | takeCFOrdinaryAudited((CFStringRef) string); |
| 115 | takeCFVariadicAudited(1, (CFStringRef) string); |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 116 | 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 McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void testTakerFunctions_parens(id string) { |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 120 | 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}} |
| 121 | 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}} |
| 122 | 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 McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 123 | |
| 124 | void (*taker)(CFStringRef) = 0; |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 125 | 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 McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 126 | |
| 127 | takeCFOrdinaryAudited(((CFStringRef) string)); |
| 128 | takeCFVariadicAudited(1, ((CFStringRef) string)); |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 129 | 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 McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 130 | } |