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