xref: /llvm-project/clang/test/SemaObjC/foreach.m (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1/* RUN: %clang_cc1 -Wall -Wno-unused-but-set-variable -fsyntax-only -verify -std=c89 -pedantic %s
2 */
3
4@class NSArray;
5
6void f(NSArray *a) {
7    id keys;
8    for (int i in a); /* expected-error{{selector element type 'int' is not a valid object}} */
9    for ((id)2 in a); /* expected-error{{selector element is not a valid lvalue}} */
10    for (2 in a); /* expected-error{{selector element is not a valid lvalue}} */
11
12  /* This should be ok, 'thisKey' should be scoped to the loop in question,
13   * and no diagnostics even in pedantic mode should happen.
14   */
15  for (id thisKey in keys); /* expected-warning {{unused variable 'thisKey'}} */
16  for (id thisKey in keys); /* expected-warning {{unused variable 'thisKey'}} */
17}
18
19@protocol NSObject @end
20
21@interface NSObject <NSObject> {
22    Class isa;
23}
24@end
25
26typedef struct {
27    unsigned long state;
28    id *itemsPtr;
29    unsigned long *mutationsPtr;
30    unsigned long extra[5];
31} NSFastEnumerationState;
32
33@protocol NSFastEnumeration
34
35- (unsigned long)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(unsigned long)len;
36
37@end
38
39int main (void)
40{
41 NSObject<NSFastEnumeration>* collection = ((void*)0);
42 for (id thing in collection) { } /* expected-warning {{unused variable 'thing'}} */
43
44 return 0;
45}
46
47@interface Test2
48@property (assign) id prop;
49@end
50void test2(NSObject<NSFastEnumeration> *collection) {
51  Test2 *obj;
52  for (obj.prop in collection) { /* expected-error {{selector element is not a valid lvalue}} */
53  }
54}
55
56int cond(void);
57
58void test3(NSObject<NSFastEnumeration> *a0, NSObject<NSFastEnumeration> *a1) {
59  for (id i in a0) { /* expected-note 2 {{jump enters Objective-C fast enumeration loop}} */
60    for (id j in a1) { /* expected-note 2 {{jump enters Objective-C fast enumeration loop}} */
61      (void)i, (void)j;
62label0:
63      if (cond())
64        goto label1;
65    }
66label1:
67    if (cond())
68      goto label0; /* expected-error {{cannot jump from this goto statement to its label}} */
69    if (cond())
70      goto label2;
71  }
72
73label2:
74  if (cond())
75    goto label0; /* expected-error {{cannot jump from this goto statement to its label}} */
76  if (cond())
77    goto label1; /* expected-error{{cannot jump from this goto statement to its label}} */
78}
79