1 /* $NetBSD: lsym_for.c,v 1.9 2023/06/26 20:23:40 rillig Exp $ */ 2 3 /* 4 * Tests for the token lsym_for, which represents the keyword 'for' that 5 * starts a 'for' loop. 6 * 7 * Most 'for' loops have 3 expressions in their head. Each of these 8 * expressions is optional though. 9 * 10 * When all 3 expressions are omitted, the 'for' loop is often called a 11 * 'forever' loop. 12 */ 13 14 //indent input 15 void example(void)16example(void) 17 { 18 for (;;) 19 break; 20 for (var = value;;) 21 break; 22 for (; cond;) 23 break; 24 for (;; i++) 25 break; 26 } 27 //indent end 28 29 //indent run-equals-input 30 31 32 //indent input 33 void function(void)34function(void) 35 { 36 for (int i = 0; i < 6; i++) 37 print_char("hello\n"[i]); 38 forever { 39 stmt(); 40 } 41 } 42 //indent end 43 44 //indent run-equals-input 45 46 47 /* 48 * Indent can cope with various syntax errors, which may be caused by 49 * syntactic macros like 'forever' or 'foreach'. 50 */ 51 //indent input 52 #define forever for (;;) 53 #define foreach(list, it) for (it = list.first; it != NULL; it = it->next) 54 55 void function(void)56function(void) 57 { 58 forever 59 stmt(); 60 61 forever { 62 stmt(); 63 } 64 65 /* $ No space after 'foreach' since it looks like a function name. */ 66 foreach(list, it) 67 println(it->data); 68 69 /* $ No space after 'foreach' since it looks like a function name. */ 70 foreach(list, it) { 71 println(it->data); 72 } 73 } 74 //indent end 75 76 //indent run-equals-input 77 78 79 /* 80 * Another variant of a 'for' loop, seen in sys/arch/arm/apple/apple_intc.c. 81 */ 82 //indent input 83 { 84 for (CPU_INFO_FOREACH(cii, ci)) { 85 } 86 } 87 //indent end 88 89 //indent run-equals-input 90 91 92 /* Ensure that the '*' after 'list_item' is a unary operator. */ 93 //indent input 94 { 95 for (const list_item *i = first; i != NULL; i = i->next) { 96 } 97 for (list_item **i = first; i != NULL; i = i->next) { 98 } 99 for (list_item *const *i = first; i != NULL; i = i->next) { 100 } 101 for (const char *const *i = first; i != NULL; i = i->next) { 102 } 103 } 104 //indent end 105 106 //indent run-equals-input 107