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