xref: /aosp_15_r20/external/clang/test/SemaObjCXX/foreach.mm (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li// RUN: %clang_cc1 -fsyntax-only -fblocks -verify -std=c++11 %s
2*67e74705SXin Li// rdar://9293227
3*67e74705SXin Li
4*67e74705SXin Li@class NSArray;
5*67e74705SXin Li
6*67e74705SXin Livoid f(NSArray *a) {
7*67e74705SXin Li    id keys;
8*67e74705SXin Li    for (int i : a); // expected-error{{selector element type 'int' is not a valid object}}
9*67e74705SXin Li    for ((id)2 : a);  // expected-error {{for range declaration must declare a variable}}
10*67e74705SXin Li    for (2 : a); // expected-error {{for range declaration must declare a variable}}
11*67e74705SXin Li
12*67e74705SXin Li  for (id thisKey : keys);
13*67e74705SXin Li
14*67e74705SXin Li  for (auto thisKey : keys) { } // expected-warning{{'auto' deduced as 'id' in declaration of 'thisKey'}}
15*67e74705SXin Li}
16*67e74705SXin Li
17*67e74705SXin Litemplate<typename Collection>
18*67e74705SXin Livoid ft(Collection col) {
19*67e74705SXin Li  for (id x : col) { }
20*67e74705SXin Li  for (auto x : col) { }
21*67e74705SXin Li}
22*67e74705SXin Li
23*67e74705SXin Litemplate void ft(NSArray *);
24*67e74705SXin Li
25*67e74705SXin Li/* // rdar://9072298 */
26*67e74705SXin Li@protocol NSObject @end
27*67e74705SXin Li
28*67e74705SXin Li@interface NSObject <NSObject> {
29*67e74705SXin Li    Class isa;
30*67e74705SXin Li}
31*67e74705SXin Li@end
32*67e74705SXin Li
33*67e74705SXin Litypedef struct {
34*67e74705SXin Li    unsigned long state;
35*67e74705SXin Li    id *itemsPtr;
36*67e74705SXin Li    unsigned long *mutationsPtr;
37*67e74705SXin Li    unsigned long extra[5];
38*67e74705SXin Li} NSFastEnumerationState;
39*67e74705SXin Li
40*67e74705SXin Li@protocol NSFastEnumeration
41*67e74705SXin Li
42*67e74705SXin Li- (unsigned long)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(unsigned long)len;
43*67e74705SXin Li
44*67e74705SXin Li@end
45*67e74705SXin Li
46*67e74705SXin Liint main ()
47*67e74705SXin Li{
48*67e74705SXin Li NSObject<NSFastEnumeration>* collection = 0;
49*67e74705SXin Li for (id thing : collection) { }
50*67e74705SXin Li
51*67e74705SXin Li id array;
52*67e74705SXin Li for (int (^b)(void) : array) {
53*67e74705SXin Li    if (b() == 10000) {
54*67e74705SXin Li        return 1;
55*67e74705SXin Li    }
56*67e74705SXin Li }
57*67e74705SXin Li return 0;
58*67e74705SXin Li}
59*67e74705SXin Li
60*67e74705SXin Li/* rdar://problem/11068137 */
61*67e74705SXin Li@interface Test2
62*67e74705SXin Li@property (assign) id prop;
63*67e74705SXin Li@end
64*67e74705SXin Livoid test2(NSObject<NSFastEnumeration> *collection) {
65*67e74705SXin Li  Test2 *obj;
66*67e74705SXin Li  for (obj.prop : collection) { // expected-error {{for range declaration must declare a variable}}
67*67e74705SXin Li  }
68*67e74705SXin Li}
69*67e74705SXin Li
70*67e74705SXin Livoid testErrors(NSArray *array) {
71*67e74705SXin Li  typedef int fn(int);
72*67e74705SXin Li
73*67e74705SXin Li  for (fn x in array) { } // expected-error{{non-variable declaration in 'for' loop}}
74*67e74705SXin Li}
75