Clang Project

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