xref: /aosp_15_r20/external/clang/test/ARCMT/objcmt-subscripting-literals.m (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li// RUN: rm -rf %t
2*67e74705SXin Li// RUN: %clang_cc1 -objcmt-migrate-literals -objcmt-migrate-subscripting -mt-migrate-directory %t %s -x objective-c -triple x86_64-apple-darwin11
3*67e74705SXin Li// RUN: c-arcmt-test -mt-migrate-directory %t | arcmt-test -verify-transformed-files %s.result
4*67e74705SXin Li// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -x objective-c %s.result
5*67e74705SXin Li
6*67e74705SXin Litypedef signed char BOOL;
7*67e74705SXin Li#define nil ((void*) 0)
8*67e74705SXin Li
9*67e74705SXin Litypedef const struct __CFString * CFStringRef;
10*67e74705SXin Li
11*67e74705SXin Li@interface NSObject
12*67e74705SXin Li+ (id)alloc;
13*67e74705SXin Li@end
14*67e74705SXin Li
15*67e74705SXin Li@protocol NSCopying
16*67e74705SXin Li@end
17*67e74705SXin Li
18*67e74705SXin Li@interface NSString : NSObject
19*67e74705SXin Li+ (id)stringWithString:(NSString *)string;
20*67e74705SXin Li- (id)initWithString:(NSString *)aString;
21*67e74705SXin Li@end
22*67e74705SXin Li
23*67e74705SXin Li@interface NSArray : NSObject
24*67e74705SXin Li- (id)objectAtIndex:(unsigned long)index;
25*67e74705SXin Li@end
26*67e74705SXin Li
27*67e74705SXin Li@interface NSArray (NSExtendedArray)
28*67e74705SXin Li- (id)objectAtIndexedSubscript:(unsigned)idx;
29*67e74705SXin Li@end
30*67e74705SXin Li
31*67e74705SXin Li@interface NSArray (NSArrayCreation)
32*67e74705SXin Li+ (id)array;
33*67e74705SXin Li+ (id)arrayWithObject:(id)anObject;
34*67e74705SXin Li+ (id)arrayWithObjects:(const id [])objects count:(unsigned long)cnt;
35*67e74705SXin Li+ (id)arrayWithObjects:(id)firstObj, ...;
36*67e74705SXin Li+ (id)arrayWithArray:(NSArray *)array;
37*67e74705SXin Li
38*67e74705SXin Li- (id)initWithObjects:(const id [])objects count:(unsigned long)cnt;
39*67e74705SXin Li- (id)initWithObjects:(id)firstObj, ...;
40*67e74705SXin Li- (id)initWithArray:(NSArray *)array;
41*67e74705SXin Li@end
42*67e74705SXin Li
43*67e74705SXin Li@interface NSMutableArray : NSArray
44*67e74705SXin Li- (void)replaceObjectAtIndex:(unsigned long)index withObject:(id)anObject;
45*67e74705SXin Li@end
46*67e74705SXin Li
47*67e74705SXin Li@interface NSMutableArray (NSExtendedMutableArray)
48*67e74705SXin Li- (void)setObject:(id)obj atIndexedSubscript:(unsigned)idx;
49*67e74705SXin Li@end
50*67e74705SXin Li
51*67e74705SXin Li@interface NSDictionary : NSObject
52*67e74705SXin Li- (id)objectForKey:(id)aKey;
53*67e74705SXin Li@end
54*67e74705SXin Li
55*67e74705SXin Li@interface NSDictionary (NSExtendedDictionary)
56*67e74705SXin Li- (id)objectForKeyedSubscript:(id)key;
57*67e74705SXin Li@end
58*67e74705SXin Li
59*67e74705SXin Li@interface NSDictionary (NSDictionaryCreation)
60*67e74705SXin Li+ (id)dictionary;
61*67e74705SXin Li+ (id)dictionaryWithObject:(id)object forKey:(id)key;
62*67e74705SXin Li+ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt;
63*67e74705SXin Li+ (id)dictionaryWithObjectsAndKeys:(id)firstObject, ...;
64*67e74705SXin Li+ (id)dictionaryWithDictionary:(NSDictionary *)dict;
65*67e74705SXin Li+ (id)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
66*67e74705SXin Li
67*67e74705SXin Li- (id)initWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt;
68*67e74705SXin Li- (id)initWithObjectsAndKeys:(id)firstObject, ...;
69*67e74705SXin Li- (id)initWithDictionary:(NSDictionary *)otherDictionary;
70*67e74705SXin Li- (id)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
71*67e74705SXin Li@end
72*67e74705SXin Li
73*67e74705SXin Li@interface NSMutableDictionary : NSDictionary
74*67e74705SXin Li- (void)setObject:(id)anObject forKey:(id)aKey;
75*67e74705SXin Li@end
76*67e74705SXin Li
77*67e74705SXin Li@interface NSMutableDictionary (NSExtendedMutableDictionary)
78*67e74705SXin Li- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;
79*67e74705SXin Li@end
80*67e74705SXin Li
81*67e74705SXin Li@interface NSNumber : NSObject
82*67e74705SXin Li@end
83*67e74705SXin Li
84*67e74705SXin Li@interface NSNumber (NSNumberCreation)
85*67e74705SXin Li+ (NSNumber *)numberWithInt:(int)value;
86*67e74705SXin Li@end
87*67e74705SXin Li
88*67e74705SXin Li#define M(x) (x)
89*67e74705SXin Li#define PAIR(x) @#x, [NSNumber numberWithInt:(x)]
90*67e74705SXin Li#define TWO(x) ((x), (x))
91*67e74705SXin Li#define TWO_SEP(x,y) ((x), (y))
92*67e74705SXin Li
93*67e74705SXin Li@interface I {
94*67e74705SXin Li  NSArray *ivarArr;
95*67e74705SXin Li}
96*67e74705SXin Li@end
97*67e74705SXin Li@implementation I
98*67e74705SXin Li-(void) foo {
99*67e74705SXin Li  NSString *str;
100*67e74705SXin Li  NSArray *arr;
101*67e74705SXin Li  NSDictionary *dict;
102*67e74705SXin Li
103*67e74705SXin Li  arr = [NSArray array];
104*67e74705SXin Li  arr = [NSArray arrayWithObject:str];
105*67e74705SXin Li  arr = [NSArray arrayWithObjects:str, str, nil];
106*67e74705SXin Li  dict = [NSDictionary dictionary];
107*67e74705SXin Li  dict = [NSDictionary dictionaryWithObject:arr forKey:str];
108*67e74705SXin Li  dict = [NSDictionary dictionaryWithObjectsAndKeys: @"value1", @"key1", @"value2", @"key2", nil];
109*67e74705SXin Li  dict = [NSDictionary dictionaryWithObjectsAndKeys: PAIR(1), PAIR(2), nil];
110*67e74705SXin Li  dict = [NSDictionary dictionaryWithObjectsAndKeys:
111*67e74705SXin Li                                               @"value1", @"key1",
112*67e74705SXin Li#ifdef BLAH
113*67e74705SXin Li                                               @"value2", @"key2",
114*67e74705SXin Li#else
115*67e74705SXin Li                                               @"value3", @"key3",
116*67e74705SXin Li#endif
117*67e74705SXin Li                                               nil ];
118*67e74705SXin Li
119*67e74705SXin Li  id o = [arr objectAtIndex:2];
120*67e74705SXin Li  o = [dict objectForKey:@"key"];
121*67e74705SXin Li  o = TWO([dict objectForKey:@"key"]);
122*67e74705SXin Li  o = TWO_SEP([dict objectForKey:@"key"], [arr objectAtIndex:2]);
123*67e74705SXin Li  o = [NSDictionary dictionaryWithObject:[NSDictionary dictionary] forKey:@"key"];
124*67e74705SXin Li  NSMutableArray *marr = 0;
125*67e74705SXin Li  NSMutableDictionary *mdict = 0;
126*67e74705SXin Li  [marr replaceObjectAtIndex:2 withObject:@"val"];
127*67e74705SXin Li  [mdict setObject:@"value" forKey:@"key"];
128*67e74705SXin Li  [marr replaceObjectAtIndex:2 withObject:[arr objectAtIndex:4]];
129*67e74705SXin Li  [mdict setObject:[dict objectForKey:@"key2"] forKey:@"key"];
130*67e74705SXin Li  [mdict setObject:[dict objectForKey:@"key2"] forKey:
131*67e74705SXin Li#if 1
132*67e74705SXin Li                     @"key1"
133*67e74705SXin Li#else
134*67e74705SXin Li                     @"key2"
135*67e74705SXin Li#endif
136*67e74705SXin Li                    ];
137*67e74705SXin Li  [mdict setObject:[dict objectForKey:
138*67e74705SXin Li#if 2
139*67e74705SXin Li                     @"key3"
140*67e74705SXin Li#else
141*67e74705SXin Li                     @"key4"
142*67e74705SXin Li#endif
143*67e74705SXin Li                   ] forKey:@"key"];
144*67e74705SXin Li  [mdict setObject:@"value" forKey:[dict objectForKey:
145*67e74705SXin Li#if 3
146*67e74705SXin Li                     @"key5"
147*67e74705SXin Li#else
148*67e74705SXin Li                     @"key6"
149*67e74705SXin Li#endif
150*67e74705SXin Li                   ] ];
151*67e74705SXin Li  [mdict setObject:@"val" forKey:[dict objectForKey:@"key2"]];
152*67e74705SXin Li  [mdict setObject:[dict objectForKey:@"key1"] forKey:[dict objectForKey:[NSArray arrayWithObject:@"arrkey"]]];
153*67e74705SXin Li  __strong NSArray **parr = 0;
154*67e74705SXin Li  o = [*parr objectAtIndex:2];
155*67e74705SXin Li  void *hd;
156*67e74705SXin Li  o = [(NSArray*)hd objectAtIndex:2];
157*67e74705SXin Li  o = [ivarArr objectAtIndex:2];
158*67e74705SXin Li
159*67e74705SXin Li  dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1", [NSArray array], nil] forKeys:[NSArray arrayWithObjects:@"A", [arr objectAtIndex:2], nil]];
160*67e74705SXin Li  dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1", @"2", nil] forKeys:arr];
161*67e74705SXin Li  dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1", @"2", nil] forKeys:@[@"A", @"B"]];
162*67e74705SXin Li  dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSArray array], @"A", [NSArray array], @"B", nil];
163*67e74705SXin Li}
164*67e74705SXin Li@end
165*67e74705SXin Li
166*67e74705SXin Liextern const CFStringRef globStr;
167*67e74705SXin Li
168*67e74705SXin Livoid test1(NSString *str) {
169*67e74705SXin Li  NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: str, globStr, nil];
170*67e74705SXin Li  dict = [NSDictionary dictionaryWithObjectsAndKeys: globStr, str, nil];
171*67e74705SXin Li  dict = [NSDictionary dictionaryWithObject:str forKey:globStr];
172*67e74705SXin Li  dict = [NSDictionary dictionaryWithObject:globStr forKey:str];
173*67e74705SXin Li
174*67e74705SXin Li  NSArray *arr = [NSArray arrayWithObjects: globStr, globStr, nil];
175*67e74705SXin Li  arr = [NSArray arrayWithObjects: str, globStr, nil];
176*67e74705SXin Li  arr = [NSArray arrayWithObjects: globStr, str, nil];
177*67e74705SXin Li  arr = [NSArray arrayWithObject:globStr];
178*67e74705SXin Li}
179*67e74705SXin Li
180*67e74705SXin Li@interface Custom : NSObject
181*67e74705SXin Li- (id)objectAtIndex:(unsigned long)index;
182*67e74705SXin Li@end
183*67e74705SXin Li
184*67e74705SXin Li@interface Custom (Extended)
185*67e74705SXin Li- (id)objectAtIndexedSubscript:(unsigned)idx;
186*67e74705SXin Li@end
187*67e74705SXin Li
188*67e74705SXin Li@interface MutableCustom : Custom
189*67e74705SXin Li- (void)replaceObjectAtIndex:(unsigned long)index withObject:(id)anObject;
190*67e74705SXin Li@end
191*67e74705SXin Li
192*67e74705SXin Li@interface MutableCustom (Extended)
193*67e74705SXin Li- (void)setObject:(id)obj atIndexedSubscript:(unsigned)idx;
194*67e74705SXin Li@end
195*67e74705SXin Li
196*67e74705SXin Li@interface CustomUnavail : NSObject
197*67e74705SXin Li- (id)objectAtIndex:(unsigned long)index;
198*67e74705SXin Li@end
199*67e74705SXin Li
200*67e74705SXin Li@interface CustomUnavail (Extended)
201*67e74705SXin Li- (id)objectAtIndexedSubscript:(unsigned)idx __attribute__((unavailable));
202*67e74705SXin Li@end
203*67e74705SXin Li
204*67e74705SXin Li@interface MutableCustomUnavail : CustomUnavail
205*67e74705SXin Li- (void)replaceObjectAtIndex:(unsigned long)index withObject:(id)anObject;
206*67e74705SXin Li@end
207*67e74705SXin Li
208*67e74705SXin Li@interface MutableCustomUnavail (Extended)
209*67e74705SXin Li- (void)setObject:(id)obj atIndexedSubscript:(unsigned)idx __attribute__((unavailable));
210*67e74705SXin Li@end
211*67e74705SXin Li
212*67e74705SXin Livoid test2() {
213*67e74705SXin Li  MutableCustom *mutc;
214*67e74705SXin Li  id o = [mutc objectAtIndex:4];
215*67e74705SXin Li  [mutc replaceObjectAtIndex:2 withObject:@"val"];
216*67e74705SXin Li
217*67e74705SXin Li  MutableCustomUnavail *mutcunaval;
218*67e74705SXin Li  o = [mutcunaval objectAtIndex:4];
219*67e74705SXin Li  [mutcunaval replaceObjectAtIndex:2 withObject:@"val"];
220*67e74705SXin Li}
221*67e74705SXin Li
222*67e74705SXin Li@interface NSLocale : NSObject
223*67e74705SXin Li+ (id)systemLocale;
224*67e74705SXin Li+ (id)currentLocale;
225*67e74705SXin Li- (id)objectForKey:(id)key;
226*67e74705SXin Li@end
227*67e74705SXin Li
228*67e74705SXin Livoid test3(id key) {
229*67e74705SXin Li  id o = [[NSLocale currentLocale] objectForKey:key];
230*67e74705SXin Li}
231