jit: Fix Darwin bootstrap after r15-1699.
[official-gcc.git] / gcc / testsuite / c-c++-common / analyzer / infinite-loop-linked-list.c
blobaaa387fd310419f57d58e85998576ee2c0414707
1 /* Various correct and incorrect ways to walk a linked list, accumulating
2 a value. */
4 /* { dg-additional-options "-O0" } */
6 extern int maybe_useful_work ();
8 struct node
10 struct node *next;
11 int val;
14 /* Various "while" loops. */
16 int correct_while_loop (struct node *n)
18 int sum = 0;
19 while (n)
21 sum += n->val;
22 n = n->next;
24 return sum;
27 int while_loop_missing_next (struct node *n)
29 int sum = 0;
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 */
35 return sum;
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)
46 int sum = 0;
47 while (n) /* { dg-bogus "infinite loop" } */
49 sum += n->val;
50 /* We're missing: "n = n->next;", so n does not change */
51 /* But here we do something that could be doing useful work. */
52 maybe_useful_work ();
54 return sum;
57 /* BUG: missing: "sum += ", so sum does not change. */
59 int while_loop_missing_add (struct node *n)
61 int sum = 0;
62 while (n) /* { dg-warning "infinite loop" } */
63 n->val;
64 return sum;
67 /* Various "for" loops. */
69 int correct_for_loop (struct node *n)
71 int sum = 0;
72 for (struct node *iter = n; iter; iter = iter->next)
73 sum += n->val;
74 return sum;
77 int for_loop_missing_condition (struct node *n)
79 int sum = 0;
80 for (struct node *iter = n; ; iter = iter->next)
81 sum += n->val; /* { dg-warning "infinite loop" } */
82 return sum;
85 int for_loop_missing_next (struct node *n)
87 int sum = 0;
88 for (struct node *iter = n; iter; ) /* { dg-warning "infinite loop" } */
89 sum += n->val;
90 return sum;
93 /* BUG: missing: "sum += ", so sum does not change.
94 Not an infinite loop though, as we do iterate to the
95 end of the list. */
97 int for_loop_missing_add (struct node *n)
99 int sum = 0;
100 for (struct node *iter = n; iter; iter = iter->next)
101 n->val; /* { dg-bogus "infinite loop" } */
102 return sum;
105 /* BUG: "iter->next" should be "iter = iter->next". */
107 int for_loop_noop_next (struct node *n)
109 int sum = 0;
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 } */
112 return sum;
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)
126 int sum = 0;
127 for (struct node *iter = n; iter; iter = n->next) /* { dg-warning "infinite loop" "" { xfail *-*-* } } */
128 /* TODO: xfail. */
129 sum += n->val;
130 return sum;