]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/SemaObjC/arc-objc-lifetime.m
Vendor import of clang release_34 branch r197841 (effectively, 3.4 RC3):
[FreeBSD/FreeBSD.git] / test / SemaObjC / arc-objc-lifetime.m
1 // RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fsyntax-only -fobjc-arc -fblocks -fobjc-runtime-has-weak -Wexplicit-ownership-type  -verify -Wno-objc-root-class %s
2 // RUN: %clang_cc1 -x objective-c++ -triple x86_64-apple-darwin11 -fsyntax-only -fobjc-arc -fblocks -fobjc-runtime-has-weak -Wexplicit-ownership-type -verify -Wno-objc-root-class %s
3 // rdar://10244607
4
5 typedef const struct __CFString * CFStringRef;
6 @class NSString;
7
8 NSString *CFBridgingRelease();
9
10 typedef NSString * PNSString;
11
12 typedef __autoreleasing NSString * AUTORELEASEPNSString;
13
14 @interface I @end
15
16 @implementation I
17 - (CFStringRef)myString
18 {
19     CFStringRef myString =
20       (__bridge CFStringRef) (__strong NSString *)CFBridgingRelease(); // expected-error {{explicit ownership qualifier on cast result has no effect}}
21
22     myString =
23       (__bridge CFStringRef) (__autoreleasing PNSString) CFBridgingRelease(); // expected-error {{explicit ownership qualifier on cast result has no effect}}
24     myString =
25       (__bridge CFStringRef) (AUTORELEASEPNSString) CFBridgingRelease(); // OK
26     myString =
27       (__bridge CFStringRef) (typeof(__strong NSString *)) CFBridgingRelease(); // expected-error {{explicit ownership qualifier on cast result has no effect}}
28     return myString;
29 }
30
31 - (void)decodeValueOfObjCType:(const char *)type at:(void *)addr {
32         __autoreleasing id *stuff = (__autoreleasing id *)addr;
33 }
34 @end
35
36 // rdar://problem/10711456
37 __strong I *__strong test1; // expected-error {{the type 'I *__strong' is already explicitly ownership-qualified}}
38 __strong I *(__strong test2); // expected-error {{the type 'I *__strong' is already explicitly ownership-qualified}}
39 __strong I *(__strong (test3)); // expected-error {{the type 'I *__strong' is already explicitly ownership-qualified}}
40 __unsafe_unretained __typeof__(test3) test4;
41 typedef __strong I *strong_I;
42 __unsafe_unretained strong_I test5;
43
44 // rdar://10907090
45 typedef void (^T) ();
46 @interface NSObject @end
47 @protocol P;
48 @interface Radar10907090 @end
49
50 @implementation Radar10907090
51 - (void) MMM : (NSObject*) arg0 : (NSObject<P>**)arg : (id) arg1 : (id<P>*) arg2 {} // expected-warning {{method parameter of type 'NSObject<P> *__autoreleasing *' with no explicit ownership}} \
52                                         // expected-warning {{method parameter of type '__autoreleasing id<P> *' with no explicit ownership}}
53 - (void) MM : (NSObject*) arg0 : (__strong NSObject**)arg : (id) arg1 : (__strong id*) arg2 {}
54 - (void) M : (NSObject**)arg0 : (id*)arg {} // expected-warning {{method parameter of type 'NSObject *__autoreleasing *' with no explicit ownership}} \
55                                             // expected-warning {{method parameter of type '__autoreleasing id *' with no explicit ownership}}
56 - (void) N : (__strong NSObject***) arg0 : (__strong NSObject<P>***)arg : (float**) arg1 : (double) arg2 {} 
57 - (void) BLOCK : (T*) arg0 : (T)arg  : (__strong T*) arg1 {} // expected-warning {{method parameter of type '__autoreleasing T *' (aka 'void (^__autoreleasing *)()') with no explicit ownership}}
58 @end
59
60 // rdar://12280826
61 @class NSMutableDictionary, NSError;
62 @interface Radar12280826
63 - (void)createInferiorTransportAndSetEnvironment:(NSMutableDictionary*)environment error:(__autoreleasing NSError**)error;
64 @end
65
66 @implementation Radar12280826
67 - (void)createInferiorTransportAndSetEnvironment:(NSMutableDictionary*)environment error:(__autoreleasing NSError**)error {}
68 @end
69
70 // <rdar://problem/12367446>
71 typedef __strong id strong_id;
72 typedef NSObject *NSObject_ptr;
73 typedef __strong NSObject *strong_NSObject_ptr;
74
75 // Warn
76 __strong id f1(); // expected-warning{{ARC __strong lifetime qualifier on return type is ignored}}
77 NSObject __unsafe_unretained *f2(int); // expected-warning{{ARC __unsafe_unretained lifetime qualifier on return type is ignored}}
78 __autoreleasing NSObject *f3(void); // expected-warning{{ARC __autoreleasing lifetime qualifier on return type is ignored}}
79 NSObject * __strong f4(void); // expected-warning{{ARC __strong lifetime qualifier on return type is ignored}}
80 NSObject_ptr __strong f5(); // expected-warning{{ARC __strong lifetime qualifier on return type is ignored}}
81
82 typedef __strong id (*fptr)(int); // expected-warning{{ARC __strong lifetime qualifier on return type is ignored}}
83
84 // Don't warn
85 strong_id f6();
86 strong_NSObject_ptr f7();
87 typedef __strong id (^block_ptr)(int);
88
89 // rdar://10127067
90 void test8_a() {
91   __weak id *(^myBlock)(void);
92   __weak id *var = myBlock();
93   (void) (__strong id *) &myBlock;
94   (void) (__weak id *) &myBlock; // expected-error {{cast}}
95 }
96 void test8_b() {
97   __weak id (^myBlock)(void);
98   (void) (__weak id *) &myBlock;
99   (void) (__strong id *) &myBlock; // expected-error {{cast}}
100 }
101 void test8_c() {
102   __weak id (^*(^myBlock)(void))(void);
103   (void) (__weak id*) myBlock();
104   (void) (__strong id*) myBlock(); // expected-error {{cast}}
105   (void) (__weak id*) &myBlock; // expected-error {{cast}}
106   (void) (__strong id*) &myBlock;
107 }
108
109 @class Test9;
110 void test9_a() {
111   __weak Test9 **(^myBlock)(void);
112   __weak Test9 **var = myBlock();
113   (void) (__strong Test9 **) &myBlock;
114   (void) (__weak Test9 **) &myBlock; // expected-error {{cast}}
115 }
116 void test9_b() {
117   __weak Test9 *(^myBlock)(void);
118   (void) (__weak Test9**) &myBlock;
119   (void) (__strong Test9**) &myBlock; // expected-error {{cast}}
120 }
121 void test9_c() {
122   __weak Test9 *(^*(^myBlock)(void))(void);
123   (void) (__weak Test9 **) myBlock();
124   (void) (__strong Test9 **) myBlock(); // expected-error {{cast}}
125   (void) (__weak Test9 **) &myBlock; // expected-error {{cast}}
126   (void) (__strong Test9 **) &myBlock;
127 }