1 /* Various correct and incorrect ways to walk a linked list, accumulating
4 /* { dg-additional-options "-O0" } */
6 extern int maybe_useful_work ();
14 /* Various "while" loops. */
16 int correct_while_loop (struct node
*n
)
27 int while_loop_missing_next (struct node
*n
)
30 while (n
) /* { dg-line "while_loop_missing_next_WHILE_LINE" } */
32 sum
+= n
->val
; /* { dg-line "while_loop_missing_next_LOOP_BODY" } */
33 /* We're missing: "n = n->next;", so n does not change */
37 /* { dg-warning "10: infinite loop" "" { target *-*-* } while_loop_missing_next_WHILE_LINE } */
38 /* { dg-message "10: \\(2\\) when 'n' is non-NULL: always following 'true' branch\.\.\." "" { target *-*-* } while_loop_missing_next_WHILE_LINE } */
39 /* { dg-message "\\(3\\) \.\.\.to here" "" { target *-*-* } while_loop_missing_next_LOOP_BODY } */
40 /* { dg-message "\\(4\\) looping back\.\.\." "" { target *-*-* } while_loop_missing_next_LOOP_BODY } */
41 /* { dg-message "\\(5\\) \.\.\.to here" "" { target *-*-* } while_loop_missing_next_WHILE_LINE } */
44 int while_loop_missing_next_with_work (struct node
*n
)
47 while (n
) /* { dg-bogus "infinite loop" } */
50 /* We're missing: "n = n->next;", so n does not change */
51 /* But here we do something that could be doing useful work. */
57 /* BUG: missing: "sum += ", so sum does not change. */
59 int while_loop_missing_add (struct node
*n
)
62 while (n
) /* { dg-warning "infinite loop" } */
67 /* Various "for" loops. */
69 int correct_for_loop (struct node
*n
)
72 for (struct node
*iter
= n
; iter
; iter
= iter
->next
)
77 int for_loop_missing_condition (struct node
*n
)
80 for (struct node
*iter
= n
; ; iter
= iter
->next
)
81 sum
+= n
->val
; /* { dg-warning "infinite loop" } */
85 int for_loop_missing_next (struct node
*n
)
88 for (struct node
*iter
= n
; iter
; ) /* { dg-warning "infinite loop" } */
93 /* BUG: missing: "sum += ", so sum does not change.
94 Not an infinite loop though, as we do iterate to the
97 int for_loop_missing_add (struct node
*n
)
100 for (struct node
*iter
= n
; iter
; iter
= iter
->next
)
101 n
->val
; /* { dg-bogus "infinite loop" } */
105 /* BUG: "iter->next" should be "iter = iter->next". */
107 int for_loop_noop_next (struct node
*n
)
110 for (struct node
*iter
= n
; iter
; iter
->next
) /* { dg-line for_loop_noop_next_FOR_LINE } */
111 sum
+= n
->val
; /* { dg-line for_loop_noop_next_LOOP_BODY } */
114 /* { dg-warning "31: infinite loop" "" { target *-*-* } for_loop_noop_next_FOR_LINE } */
115 /* { dg-message "31: \\(2\\) when 'iter' is non-NULL: always following 'true' branch\.\.\." "" { target *-*-* } for_loop_noop_next_FOR_LINE } */
116 /* { dg-message "\\(3\\) \.\.\.to here" "" { target *-*-* } for_loop_noop_next_LOOP_BODY } */
117 /* { dg-message "\\(4\\) looping back\.\.\." "" { target *-*-* } for_loop_noop_next_LOOP_BODY } */
118 /* { dg-message "\\(5\\) \.\.\.to here" "" { target *-*-* } for_loop_noop_next_FOR_LINE } */
121 /* BUG: "iter = n->next" should be "iter = iter->next"
122 and so it becomes an infinite loop at the 2nd element. */
124 int for_loop_wrong_next (struct node
*n
)
127 for (struct node
*iter
= n
; iter
; iter
= n
->next
) /* { dg-warning "infinite loop" "" { xfail *-*-* } } */