1// RUN: %clang_cc1 -arcmt-check -verify -triple x86_64-apple-darwin10 %s
2// DISABLE: mingw32
3
4#include "Common.h"
5
6typedef const struct __CFString * CFStringRef;
7typedef const void * CFTypeRef;
8CFTypeRef CFBridgingRetain(id X);
9id CFBridgingRelease(CFTypeRef);
10
11struct StrS {
12  CFStringRef sref_member;
13};
14
15@interface NSString : NSObject {
16  CFStringRef sref;
17  struct StrS *strS;
18}
19-(id)string;
20-(id)newString;
21@end
22
23@implementation NSString
24-(id)string {
25  if (0)
26    return sref;
27  else
28    return strS->sref_member;
29}
30-(id)newString {
31  return sref; // expected-error {{implicit conversion of C pointer type 'CFStringRef' (aka 'const struct __CFString *') to Objective-C pointer type 'id' requires a bridged cast}} \
32    // expected-note{{use __bridge to convert directly (no change in ownership)}} \
33    // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'CFStringRef' (aka 'const struct __CFString *') into ARC}}
34}
35@end
36
37void f(BOOL b) {
38  CFStringRef cfstr;
39  NSString *str = (NSString *)cfstr; // expected-error {{cast of C pointer type 'CFStringRef' (aka 'const struct __CFString *') to Objective-C pointer type 'NSString *' requires a bridged cast}} \
40    // expected-note{{use __bridge to convert directly (no change in ownership)}} \
41    // expected-note{{use CFBridgingRelease call to transfer ownership of a +1 'CFStringRef' (aka 'const struct __CFString *') into ARC}}
42  void *vp = str;  // expected-error {{requires a bridged cast}} expected-note {{use CFBridgingRetain call}} expected-note {{use __bridge}}
43}
44
45void f2(NSString *s) {
46  CFStringRef ref;
47  ref = [(CFStringRef)[s string] retain]; // expected-error {{cast of Objective-C pointer type 'id' to C pointer type 'CFStringRef' (aka 'const struct __CFString *') requires a bridged cast}} \
48    // expected-error {{bad receiver type 'CFStringRef' (aka 'const struct __CFString *')}} \
49    // expected-note{{use __bridge to convert directly (no change in ownership)}} \
50    // expected-note{{use CFBridgingRetain call to make an ARC object available as a +1 'CFStringRef' (aka 'const struct __CFString *')}}
51}
52
53CFStringRef f3() {
54  return (CFStringRef)[[[NSString alloc] init] autorelease]; // expected-error {{it is not safe to cast to 'CFStringRef' the result of 'autorelease' message; a __bridge cast may result in a pointer to a destroyed object and a __bridge_retained may leak the object}} \
55    // expected-note {{remove the cast and change return type of function to 'NSString *' to have the object automatically autoreleased}}
56}
57
58extern void NSLog(NSString *format, ...);
59
60// rdar://13192395
61void f4(NSString *s) {
62  NSLog(@"%@", (CFStringRef)s); // expected-error {{cast of Objective-C pointer type 'NSString *' to C pointer type 'CFStringRef' (aka 'const struct __CFString *') requires a bridged cast}} \
63    // expected-note{{use __bridge to convert directly (no change in ownership)}} \
64    // expected-note{{use CFBridgingRetain call to make an ARC object available as a +1 'CFStringRef' (aka 'const struct __CFString *')}}
65}
66