xref: /minix3/external/bsd/llvm/dist/clang/test/SemaObjC/foreach.m (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc/* RUN: %clang_cc1 -Wall -fsyntax-only -verify -std=c89 -pedantic %s
2*f4a2713aSLionel Sambuc */
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 in a); /* expected-error{{selector element type 'int' is not a valid object}} */
9*f4a2713aSLionel Sambuc    for ((id)2 in a); /* expected-error{{selector element is not a valid lvalue}} */
10*f4a2713aSLionel Sambuc    for (2 in a); /* expected-error{{selector element is not a valid lvalue}} */
11*f4a2713aSLionel Sambuc
12*f4a2713aSLionel Sambuc  /* This should be ok, 'thisKey' should be scoped to the loop in question,
13*f4a2713aSLionel Sambuc   * and no diagnostics even in pedantic mode should happen.
14*f4a2713aSLionel Sambuc   * rdar://6814674
15*f4a2713aSLionel Sambuc   */
16*f4a2713aSLionel Sambuc  for (id thisKey in keys); /* expected-warning {{unused variable 'thisKey'}} */
17*f4a2713aSLionel Sambuc  for (id thisKey in keys); /* expected-warning {{unused variable 'thisKey'}} */
18*f4a2713aSLionel Sambuc}
19*f4a2713aSLionel Sambuc
20*f4a2713aSLionel Sambuc/* // rdar://9072298 */
21*f4a2713aSLionel Sambuc@protocol NSObject @end
22*f4a2713aSLionel Sambuc
23*f4a2713aSLionel Sambuc@interface NSObject <NSObject> {
24*f4a2713aSLionel Sambuc    Class isa;
25*f4a2713aSLionel Sambuc}
26*f4a2713aSLionel Sambuc@end
27*f4a2713aSLionel Sambuc
28*f4a2713aSLionel Sambuctypedef struct {
29*f4a2713aSLionel Sambuc    unsigned long state;
30*f4a2713aSLionel Sambuc    id *itemsPtr;
31*f4a2713aSLionel Sambuc    unsigned long *mutationsPtr;
32*f4a2713aSLionel Sambuc    unsigned long extra[5];
33*f4a2713aSLionel Sambuc} NSFastEnumerationState;
34*f4a2713aSLionel Sambuc
35*f4a2713aSLionel Sambuc@protocol NSFastEnumeration
36*f4a2713aSLionel Sambuc
37*f4a2713aSLionel Sambuc- (unsigned long)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(unsigned long)len;
38*f4a2713aSLionel Sambuc
39*f4a2713aSLionel Sambuc@end
40*f4a2713aSLionel Sambuc
41*f4a2713aSLionel Sambucint main ()
42*f4a2713aSLionel Sambuc{
43*f4a2713aSLionel Sambuc NSObject<NSFastEnumeration>* collection = ((void*)0);
44*f4a2713aSLionel Sambuc for (id thing in collection) { } /* expected-warning {{unused variable 'thing'}} */
45*f4a2713aSLionel Sambuc
46*f4a2713aSLionel Sambuc return 0;
47*f4a2713aSLionel Sambuc}
48*f4a2713aSLionel Sambuc
49*f4a2713aSLionel Sambuc/* rdar://problem/11068137 */
50*f4a2713aSLionel Sambuc@interface Test2
51*f4a2713aSLionel Sambuc@property (assign) id prop;
52*f4a2713aSLionel Sambuc@end
53*f4a2713aSLionel Sambucvoid test2(NSObject<NSFastEnumeration> *collection) {
54*f4a2713aSLionel Sambuc  Test2 *obj;
55*f4a2713aSLionel Sambuc  for (obj.prop in collection) { /* expected-error {{selector element is not a valid lvalue}} */
56*f4a2713aSLionel Sambuc  }
57*f4a2713aSLionel Sambuc}
58