xref: /aosp_15_r20/external/clang/test/ARCMT/atautorelease-check.m (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li// RUN: %clang_cc1 -arcmt-check -verify -triple x86_64-apple-darwin10 %s
2*67e74705SXin Li
3*67e74705SXin Li#if __has_feature(objc_arr)
4*67e74705SXin Li#define NS_AUTOMATED_REFCOUNT_UNAVAILABLE __attribute__((unavailable("not available in automatic reference counting mode")))
5*67e74705SXin Li#else
6*67e74705SXin Li#define NS_AUTOMATED_REFCOUNT_UNAVAILABLE
7*67e74705SXin Li#endif
8*67e74705SXin Li
9*67e74705SXin Litypedef struct _NSZone NSZone;
10*67e74705SXin Litypedef int BOOL;
11*67e74705SXin Litypedef unsigned NSUInteger;
12*67e74705SXin Li
13*67e74705SXin Li@protocol NSObject
14*67e74705SXin Li- (BOOL)isEqual:(id)object;
15*67e74705SXin Li- (id)retain NS_AUTOMATED_REFCOUNT_UNAVAILABLE;
16*67e74705SXin Li- (NSUInteger)retainCount NS_AUTOMATED_REFCOUNT_UNAVAILABLE;
17*67e74705SXin Li- (oneway void)release NS_AUTOMATED_REFCOUNT_UNAVAILABLE;
18*67e74705SXin Li- (id)autorelease NS_AUTOMATED_REFCOUNT_UNAVAILABLE;
19*67e74705SXin Li
20*67e74705SXin Li- (NSZone *)zone NS_AUTOMATED_REFCOUNT_UNAVAILABLE;
21*67e74705SXin Li@end
22*67e74705SXin Li
23*67e74705SXin Li@protocol NSCopying
24*67e74705SXin Li- (id)copyWithZone:(NSZone *)zone;
25*67e74705SXin Li@end
26*67e74705SXin Li
27*67e74705SXin Li@protocol NSMutableCopying
28*67e74705SXin Li- (id)mutableCopyWithZone:(NSZone *)zone;
29*67e74705SXin Li@end
30*67e74705SXin Li
31*67e74705SXin Li@interface NSObject <NSObject> {}
32*67e74705SXin Li- (id)init;
33*67e74705SXin Li
34*67e74705SXin Li+ (id)new;
35*67e74705SXin Li+ (id)allocWithZone:(NSZone *)zone NS_AUTOMATED_REFCOUNT_UNAVAILABLE;
36*67e74705SXin Li+ (id)alloc;
37*67e74705SXin Li- (void)dealloc;
38*67e74705SXin Li
39*67e74705SXin Li- (void)finalize;
40*67e74705SXin Li
41*67e74705SXin Li- (id)copy;
42*67e74705SXin Li- (id)mutableCopy;
43*67e74705SXin Li
44*67e74705SXin Li+ (id)copyWithZone:(NSZone *)zone NS_AUTOMATED_REFCOUNT_UNAVAILABLE;
45*67e74705SXin Li+ (id)mutableCopyWithZone:(NSZone *)zone NS_AUTOMATED_REFCOUNT_UNAVAILABLE;
46*67e74705SXin Li@end
47*67e74705SXin Li
48*67e74705SXin Liextern void NSRecycleZone(NSZone *zone);
49*67e74705SXin Li
50*67e74705SXin LiNS_AUTOMATED_REFCOUNT_UNAVAILABLE
51*67e74705SXin Li@interface NSAutoreleasePool : NSObject { // expected-note 13 {{marked unavailable here}}
52*67e74705SXin Li@private
53*67e74705SXin Li    void    *_token;
54*67e74705SXin Li    void    *_reserved3;
55*67e74705SXin Li    void    *_reserved2;
56*67e74705SXin Li    void    *_reserved;
57*67e74705SXin Li}
58*67e74705SXin Li
59*67e74705SXin Li+ (void)addObject:(id)anObject;
60*67e74705SXin Li
61*67e74705SXin Li- (void)addObject:(id)anObject;
62*67e74705SXin Li
63*67e74705SXin Li- (void)drain;
64*67e74705SXin Li
65*67e74705SXin Li@end
66*67e74705SXin Li
67*67e74705SXin Li
68*67e74705SXin Livoid NSLog(id, ...);
69*67e74705SXin Li
70*67e74705SXin Liint main (int argc, const char * argv[]) {
71*67e74705SXin Li    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
72*67e74705SXin Li    NSAutoreleasePool *chunkPool = [[NSAutoreleasePool alloc] init]; // expected-error 2 {{'NSAutoreleasePool' is unavailable}}
73*67e74705SXin Li
74*67e74705SXin Li    while (argc) {
75*67e74705SXin Li      [chunkPool release];
76*67e74705SXin Li      // the following pool was not released in this scope, don't touch it.
77*67e74705SXin Li      chunkPool = [[NSAutoreleasePool alloc] init]; // expected-error {{'NSAutoreleasePool' is unavailable}}
78*67e74705SXin Li    }
79*67e74705SXin Li
80*67e74705SXin Li    [chunkPool drain];
81*67e74705SXin Li    [pool drain];
82*67e74705SXin Li
83*67e74705SXin Li    return 0;
84*67e74705SXin Li}
85*67e74705SXin Li
86*67e74705SXin Livoid f(void) {
87*67e74705SXin Li    NSAutoreleasePool * pool;  // expected-error {{'NSAutoreleasePool' is unavailable}}
88*67e74705SXin Li
89*67e74705SXin Li    for (int i=0; i != 10; ++i) {
90*67e74705SXin Li      id x = pool; // We won't touch a NSAutoreleasePool if we can't safely
91*67e74705SXin Li                   // remove all the references to it.
92*67e74705SXin Li    }
93*67e74705SXin Li
94*67e74705SXin Li    pool = [[NSAutoreleasePool alloc] init];  // expected-error {{'NSAutoreleasePool' is unavailable}}
95*67e74705SXin Li    NSLog(@"%s", "YES");
96*67e74705SXin Li    [pool release];
97*67e74705SXin Li}
98*67e74705SXin Li
99*67e74705SXin Livoid f2(void) {
100*67e74705SXin Li    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // expected-error 2 {{'NSAutoreleasePool' is unavailable}} \
101*67e74705SXin Li                                            // expected-note {{scope begins here}}
102*67e74705SXin Li
103*67e74705SXin Li    // 'x' is declared inside the "pool scope" but used outside it, if we create
104*67e74705SXin Li    // a @autorelease scope it will be undefined outside it so don't touch the pool.
105*67e74705SXin Li    int x = 0; // expected-note {{declared here}}
106*67e74705SXin Li
107*67e74705SXin Li    [pool release]; // expected-note {{scope ends here}}
108*67e74705SXin Li
109*67e74705SXin Li    ++x; // expected-error {{a name is referenced outside the NSAutoreleasePool scope that it was declared in}}
110*67e74705SXin Li}
111*67e74705SXin Li
112*67e74705SXin Livoid f3(void) {
113*67e74705SXin Li    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // expected-error 2 {{'NSAutoreleasePool' is unavailable}} \
114*67e74705SXin Li                                            // expected-note {{scope begins here}}
115*67e74705SXin Li
116*67e74705SXin Li    struct S { int x; }; // expected-note {{declared here}}
117*67e74705SXin Li
118*67e74705SXin Li    [pool release]; // expected-note {{scope ends here}}
119*67e74705SXin Li
120*67e74705SXin Li    struct S *var; // expected-error {{a name is referenced outside the NSAutoreleasePool scope that it was declared in}}
121*67e74705SXin Li    var->x = 0;
122*67e74705SXin Li}
123*67e74705SXin Li
124*67e74705SXin Livoid f4(void) {
125*67e74705SXin Li    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // expected-error 2 {{'NSAutoreleasePool' is unavailable}} \
126*67e74705SXin Li                                            // expected-note {{scope begins here}}
127*67e74705SXin Li
128*67e74705SXin Li    enum { Bar }; // expected-note {{declared here}}
129*67e74705SXin Li
130*67e74705SXin Li    [pool release]; // expected-note {{scope ends here}}
131*67e74705SXin Li
132*67e74705SXin Li    int x = Bar; // expected-error {{a name is referenced outside the NSAutoreleasePool scope that it was declared in}}
133*67e74705SXin Li}
134*67e74705SXin Li
135*67e74705SXin Livoid f5(void) {
136*67e74705SXin Li    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // expected-error 2 {{'NSAutoreleasePool' is unavailable}} \
137*67e74705SXin Li                                            // expected-note {{scope begins here}}
138*67e74705SXin Li
139*67e74705SXin Li    typedef int Bar; // expected-note {{declared here}}
140*67e74705SXin Li
141*67e74705SXin Li    [pool release]; // expected-note {{scope ends here}}
142*67e74705SXin Li
143*67e74705SXin Li    Bar x; // expected-error {{a name is referenced outside the NSAutoreleasePool scope that it was declared in}}
144*67e74705SXin Li}
145