Debian Codegraph @

clang/test/SemaObjC/arc-objc-lifetime.m

raw
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
4 typedef const struct __CFString * CFStringRef;
5 @class NSString;
6
7 NSString *CFBridgingRelease(void);
8
9 typedef NSString * PNSString;
10
11 typedef __autoreleasing NSString * AUTORELEASEPNSString;
12
13 @interface I @end
14
15 @implementation I
16 - (CFStringRef)myString
17 {
18 CFStringRef myString =
19 (__bridge CFStringRef) (__strong NSString *)CFBridgingRelease(); // expected-error {{explicit ownership qualifier on cast result has no effect}}
20
21 myString =
22 (__bridge CFStringRef) (__autoreleasing PNSString) CFBridgingRelease(); // expected-error {{explicit ownership qualifier on cast result has no effect}}
23 myString =
24 (__bridge CFStringRef) (AUTORELEASEPNSString) CFBridgingRelease(); // OK
25 myString =
26 (__bridge CFStringRef) (typeof(__strong NSString *)) CFBridgingRelease(); // expected-error {{explicit ownership qualifier on cast result has no effect}}
27 return myString;
28 }
29
30 - (void)decodeValueOfObjCType:(const char *)type at:(void *)addr {
31 __autoreleasing id *stuff = (__autoreleasing id *)addr;
32 }
33 @end
34
35 __strong I *__strong test1; // expected-error {{the type 'I *__strong' is already explicitly ownership-qualified}}
36 __strong I *(__strong test2); // expected-error {{the type 'I *__strong' is already explicitly ownership-qualified}}
37 __strong I *(__strong (test3)); // expected-error {{the type 'I *__strong' is already explicitly ownership-qualified}}
38 __unsafe_unretained __typeof__(test3) test4;
39 typedef __strong I *strong_I;
40 __unsafe_unretained strong_I test5;
41
42 typedef void (^T) (void);
43 @interface NSObject @end
44 @protocol P;
45 @interface Radar10907090 @end
46
47 @implementation Radar10907090
48 - (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}} \
49 // expected-warning {{method parameter of type '__autoreleasing id<P> *' with no explicit ownership}}
50 - (void) MM : (NSObject*) arg0 : (__strong NSObject**)arg : (id) arg1 : (__strong id*) arg2 {}
51 - (void) M : (NSObject**)arg0 : (id*)arg {} // expected-warning {{method parameter of type 'NSObject *__autoreleasing *' with no explicit ownership}} \
52 // expected-warning {{method parameter of type '__autoreleasing id *' with no explicit ownership}}
53 - (void) N : (__strong NSObject***) arg0 : (__strong NSObject<P>***)arg : (float**) arg1 : (double) arg2 {}
54 - (void) BLOCK : (T*) arg0 : (T)arg : (__strong T*) arg1 {} // expected-warning-re {{method parameter of type '__autoreleasing T *' (aka 'void (^__autoreleasing *)({{(void)?}})') with no explicit ownership}}
55 @end
56
57 @class NSMutableDictionary, NSError;
58 @interface Radar12280826
59 - (void)createInferiorTransportAndSetEnvironment:(NSMutableDictionary*)environment error:(__autoreleasing NSError**)error;
60 @end
61
62 @implementation Radar12280826
63 - (void)createInferiorTransportAndSetEnvironment:(NSMutableDictionary*)environment error:(__autoreleasing NSError**)error {}
64 @end
65
66 typedef __strong id strong_id;
67 typedef NSObject *NSObject_ptr;
68 typedef __strong NSObject *strong_NSObject_ptr;
69
70 // Warn
71 __strong id f1(void); // expected-warning{{ARC __strong lifetime qualifier on return type is ignored}}
72 NSObject __unsafe_unretained *f2(int); // expected-warning{{ARC __unsafe_unretained lifetime qualifier on return type is ignored}}
73 __autoreleasing NSObject *f3(void); // expected-warning{{ARC __autoreleasing lifetime qualifier on return type is ignored}}
74 NSObject * __strong f4(void); // expected-warning{{ARC __strong lifetime qualifier on return type is ignored}}
75 NSObject_ptr __strong f5(void); // expected-warning{{ARC __strong lifetime qualifier on return type is ignored}}
76
77 typedef __strong id (*fptr)(int); // expected-warning{{ARC __strong lifetime qualifier on return type is ignored}}
78
79 // Don't warn
80 strong_id f6(void);
81 strong_NSObject_ptr f7(void);
82 typedef __strong id (^block_ptr)(int);
83
84 void test8_a(void) {
85 __weak id *(^myBlock)(void);
86 __weak id *var = myBlock();
87 (void) (__strong id *) &myBlock;
88 (void) (__weak id *) &myBlock; // expected-error {{cast}}
89 }
90 void test8_b(void) {
91 __weak id (^myBlock)(void);
92 (void) (__weak id *) &myBlock;
93 (void) (__strong id *) &myBlock; // expected-error {{cast}}
94 }
95 void test8_c(void) {
96 __weak id (^*(^myBlock)(void))(void);
97 (void) (__weak id*) myBlock();
98 (void) (__strong id*) myBlock(); // expected-error {{cast}}
99 (void) (__weak id*) &myBlock; // expected-error {{cast}}
100 (void) (__strong id*) &myBlock;
101 }
102
103 @class Test9;
104 void test9_a(void) {
105 __weak Test9 **(^myBlock)(void);
106 __weak Test9 **var = myBlock();
107 (void) (__strong Test9 **) &myBlock;
108 (void) (__weak Test9 **) &myBlock; // expected-error {{cast}}
109 }
110 void test9_b(void) {
111 __weak Test9 *(^myBlock)(void);
112 (void) (__weak Test9**) &myBlock;
113 (void) (__strong Test9**) &myBlock; // expected-error {{cast}}
114 }
115 void test9_c(void) {
116 __weak Test9 *(^*(^myBlock)(void))(void);
117 (void) (__weak Test9 **) myBlock();
118 (void) (__strong Test9 **) myBlock(); // expected-error {{cast}}
119 (void) (__weak Test9 **) &myBlock; // expected-error {{cast}}
120 (void) (__strong Test9 **) &myBlock;
121 }