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