blob: 0974c3b8bb0f549657ee2b5365c1c59b264a693d [file] [log] [blame]
Ted Kremenekf7639e12012-03-06 20:06:33 +00001// RUN: rm -rf %t
Argyrios Kyrtzidis56527162012-03-06 22:03:39 +00002// RUN: %clang_cc1 -objcmt-migrate-literals -objcmt-migrate-subscripting -mt-migrate-directory %t %s -x objective-c -triple x86_64-apple-darwin11
Ted Kremenekf7639e12012-03-06 20:06:33 +00003// RUN: c-arcmt-test -mt-migrate-directory %t | arcmt-test -verify-transformed-files %s.result
Argyrios Kyrtzidisc1dfed62012-05-14 22:01:53 +00004// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -x objective-c %s.result
Ted Kremenekf7639e12012-03-06 20:06:33 +00005
6typedef signed char BOOL;
7#define nil ((void*) 0)
8
Argyrios Kyrtzidisc1dfed62012-05-14 22:01:53 +00009typedef const struct __CFString * CFStringRef;
10
Ted Kremenekf7639e12012-03-06 20:06:33 +000011@interface NSObject
12+ (id)alloc;
13@end
14
Argyrios Kyrtzidisa3fcbeb2012-06-19 02:22:02 +000015@protocol NSCopying
16@end
17
Ted Kremenekf7639e12012-03-06 20:06:33 +000018@interface NSString : NSObject
19+ (id)stringWithString:(NSString *)string;
20- (id)initWithString:(NSString *)aString;
21@end
22
23@interface NSArray : NSObject
24- (id)objectAtIndex:(unsigned long)index;
Argyrios Kyrtzidisa3fcbeb2012-06-19 02:22:02 +000025@end
26
27@interface NSArray (NSExtendedArray)
28- (id)objectAtIndexedSubscript:(unsigned)idx;
Ted Kremenekf7639e12012-03-06 20:06:33 +000029@end
30
31@interface NSArray (NSArrayCreation)
32+ (id)array;
33+ (id)arrayWithObject:(id)anObject;
34+ (id)arrayWithObjects:(const id [])objects count:(unsigned long)cnt;
35+ (id)arrayWithObjects:(id)firstObj, ...;
36+ (id)arrayWithArray:(NSArray *)array;
37
38- (id)initWithObjects:(const id [])objects count:(unsigned long)cnt;
39- (id)initWithObjects:(id)firstObj, ...;
40- (id)initWithArray:(NSArray *)array;
Ted Kremenekf7639e12012-03-06 20:06:33 +000041@end
42
43@interface NSMutableArray : NSArray
44- (void)replaceObjectAtIndex:(unsigned long)index withObject:(id)anObject;
Argyrios Kyrtzidisa3fcbeb2012-06-19 02:22:02 +000045@end
46
47@interface NSMutableArray (NSExtendedMutableArray)
48- (void)setObject:(id)obj atIndexedSubscript:(unsigned)idx;
Ted Kremenekf7639e12012-03-06 20:06:33 +000049@end
50
51@interface NSDictionary : NSObject
Argyrios Kyrtzidisa3fcbeb2012-06-19 02:22:02 +000052- (id)objectForKey:(id)aKey;
53@end
54
55@interface NSDictionary (NSExtendedDictionary)
Ted Kremenekf7639e12012-03-06 20:06:33 +000056- (id)objectForKeyedSubscript:(id)key;
57@end
58
59@interface NSDictionary (NSDictionaryCreation)
60+ (id)dictionary;
61+ (id)dictionaryWithObject:(id)object forKey:(id)key;
62+ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt;
63+ (id)dictionaryWithObjectsAndKeys:(id)firstObject, ...;
64+ (id)dictionaryWithDictionary:(NSDictionary *)dict;
65+ (id)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
66
67- (id)initWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt;
68- (id)initWithObjectsAndKeys:(id)firstObject, ...;
69- (id)initWithDictionary:(NSDictionary *)otherDictionary;
70- (id)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
Ted Kremenekf7639e12012-03-06 20:06:33 +000071@end
72
73@interface NSMutableDictionary : NSDictionary
74- (void)setObject:(id)anObject forKey:(id)aKey;
Argyrios Kyrtzidisa3fcbeb2012-06-19 02:22:02 +000075@end
76
77@interface NSMutableDictionary (NSExtendedMutableDictionary)
78- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;
Ted Kremenekf7639e12012-03-06 20:06:33 +000079@end
80
81@interface NSNumber : NSObject
82@end
83
84@interface NSNumber (NSNumberCreation)
85+ (NSNumber *)numberWithInt:(int)value;
86@end
87
88#define M(x) (x)
89#define PAIR(x) @#x, [NSNumber numberWithInt:(x)]
90#define TWO(x) ((x), (x))
Argyrios Kyrtzidis822e2882015-09-11 20:09:11 +000091#define TWO_SEP(x,y) ((x), (y))
Ted Kremenekf7639e12012-03-06 20:06:33 +000092
Argyrios Kyrtzidis94442982012-05-22 00:47:53 +000093@interface I {
94 NSArray *ivarArr;
95}
Ted Kremenekf7639e12012-03-06 20:06:33 +000096@end
97@implementation I
98-(void) foo {
99 NSString *str;
100 NSArray *arr;
101 NSDictionary *dict;
102
103 arr = [NSArray array];
104 arr = [NSArray arrayWithObject:str];
105 arr = [NSArray arrayWithObjects:str, str, nil];
106 dict = [NSDictionary dictionary];
107 dict = [NSDictionary dictionaryWithObject:arr forKey:str];
108 dict = [NSDictionary dictionaryWithObjectsAndKeys: @"value1", @"key1", @"value2", @"key2", nil];
109 dict = [NSDictionary dictionaryWithObjectsAndKeys: PAIR(1), PAIR(2), nil];
110 dict = [NSDictionary dictionaryWithObjectsAndKeys:
111 @"value1", @"key1",
112#ifdef BLAH
113 @"value2", @"key2",
114#else
115 @"value3", @"key3",
116#endif
117 nil ];
118
119 id o = [arr objectAtIndex:2];
120 o = [dict objectForKey:@"key"];
121 o = TWO([dict objectForKey:@"key"]);
Argyrios Kyrtzidis822e2882015-09-11 20:09:11 +0000122 o = TWO_SEP([dict objectForKey:@"key"], [arr objectAtIndex:2]);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000123 o = [NSDictionary dictionaryWithObject:[NSDictionary dictionary] forKey:@"key"];
124 NSMutableArray *marr = 0;
125 NSMutableDictionary *mdict = 0;
126 [marr replaceObjectAtIndex:2 withObject:@"val"];
127 [mdict setObject:@"value" forKey:@"key"];
128 [marr replaceObjectAtIndex:2 withObject:[arr objectAtIndex:4]];
129 [mdict setObject:[dict objectForKey:@"key2"] forKey:@"key"];
130 [mdict setObject:[dict objectForKey:@"key2"] forKey:
131#if 1
132 @"key1"
133#else
134 @"key2"
135#endif
136 ];
137 [mdict setObject:[dict objectForKey:
138#if 2
139 @"key3"
140#else
141 @"key4"
142#endif
143 ] forKey:@"key"];
144 [mdict setObject:@"value" forKey:[dict objectForKey:
145#if 3
146 @"key5"
147#else
148 @"key6"
149#endif
150 ] ];
151 [mdict setObject:@"val" forKey:[dict objectForKey:@"key2"]];
152 [mdict setObject:[dict objectForKey:@"key1"] forKey:[dict objectForKey:[NSArray arrayWithObject:@"arrkey"]]];
153 __strong NSArray **parr = 0;
154 o = [*parr objectAtIndex:2];
Argyrios Kyrtzidis0bbe94f2012-05-14 23:33:49 +0000155 void *hd;
156 o = [(NSArray*)hd objectAtIndex:2];
Argyrios Kyrtzidis94442982012-05-22 00:47:53 +0000157 o = [ivarArr objectAtIndex:2];
Argyrios Kyrtzidis6b4f3412013-01-16 23:54:48 +0000158
159 dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1", [NSArray array], nil] forKeys:[NSArray arrayWithObjects:@"A", [arr objectAtIndex:2], nil]];
160 dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1", @"2", nil] forKeys:arr];
161 dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1", @"2", nil] forKeys:@[@"A", @"B"]];
Argyrios Kyrtzidis501d90b2013-04-06 01:13:17 +0000162 dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSArray array], @"A", [NSArray array], @"B", nil];
Ted Kremenekf7639e12012-03-06 20:06:33 +0000163}
164@end
Argyrios Kyrtzidisc1dfed62012-05-14 22:01:53 +0000165
166extern const CFStringRef globStr;
167
168void test1(NSString *str) {
169 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: str, globStr, nil];
170 dict = [NSDictionary dictionaryWithObjectsAndKeys: globStr, str, nil];
171 dict = [NSDictionary dictionaryWithObject:str forKey:globStr];
172 dict = [NSDictionary dictionaryWithObject:globStr forKey:str];
173
174 NSArray *arr = [NSArray arrayWithObjects: globStr, globStr, nil];
175 arr = [NSArray arrayWithObjects: str, globStr, nil];
176 arr = [NSArray arrayWithObjects: globStr, str, nil];
177 arr = [NSArray arrayWithObject:globStr];
178}
Argyrios Kyrtzidis13b92922012-07-05 21:49:51 +0000179
180@interface Custom : NSObject
181- (id)objectAtIndex:(unsigned long)index;
182@end
183
184@interface Custom (Extended)
185- (id)objectAtIndexedSubscript:(unsigned)idx;
186@end
187
188@interface MutableCustom : Custom
189- (void)replaceObjectAtIndex:(unsigned long)index withObject:(id)anObject;
190@end
191
192@interface MutableCustom (Extended)
193- (void)setObject:(id)obj atIndexedSubscript:(unsigned)idx;
194@end
195
196@interface CustomUnavail : NSObject
197- (id)objectAtIndex:(unsigned long)index;
198@end
199
200@interface CustomUnavail (Extended)
201- (id)objectAtIndexedSubscript:(unsigned)idx __attribute__((unavailable));
202@end
203
204@interface MutableCustomUnavail : CustomUnavail
205- (void)replaceObjectAtIndex:(unsigned long)index withObject:(id)anObject;
206@end
207
208@interface MutableCustomUnavail (Extended)
209- (void)setObject:(id)obj atIndexedSubscript:(unsigned)idx __attribute__((unavailable));
210@end
211
212void test2() {
213 MutableCustom *mutc;
214 id o = [mutc objectAtIndex:4];
215 [mutc replaceObjectAtIndex:2 withObject:@"val"];
216
217 MutableCustomUnavail *mutcunaval;
218 o = [mutcunaval objectAtIndex:4];
219 [mutcunaval replaceObjectAtIndex:2 withObject:@"val"];
220}
Argyrios Kyrtzidis89b928e2012-07-06 00:07:09 +0000221
222@interface NSLocale : NSObject
223+ (id)systemLocale;
224+ (id)currentLocale;
225- (id)objectForKey:(id)key;
226@end
227
228void test3(id key) {
229 id o = [[NSLocale currentLocale] objectForKey:key];
230}