| 1 |
/* { dg-additional-options "-O0" } */ |
| 2 |
|
| 3 |
extern int maybe_useful_work (); |
| 4 |
|
| 5 |
int global_var; |
| 6 |
volatile int volatile_global_var; |
| 7 |
|
| 8 |
struct st |
| 9 |
{ |
| 10 |
int x; |
| 11 |
int y; |
| 12 |
}; |
| 13 |
|
| 14 |
static void __attribute__((noinline)) |
| 15 |
do_nothing (void) |
| 16 |
{ |
| 17 |
} |
| 18 |
|
| 19 |
void test_empty_while_true () |
| 20 |
{ |
| 21 |
while (1) {} /* { dg-warning "infinite loop" } */ |
| 22 |
/* { dg-message "looping back\.\.\." "" { target *-*-* } .-1 } */ |
| 23 |
/* { dg-message "\.\.\.to here" "" { target *-*-* } .-2 } */ |
| 24 |
} |
| 25 |
|
| 26 |
void test_empty_do_while () |
| 27 |
{ |
| 28 |
do {} while (1); /* { dg-warning "infinite loop" } */ |
| 29 |
/* { dg-message "looping back\.\.\." "" { target *-*-* } .-1 } */ |
| 30 |
/* { dg-message "\.\.\.to here" "" { target *-*-* } .-2 } */ |
| 31 |
} |
| 32 |
|
| 33 |
void test_empty_for () |
| 34 |
{ |
| 35 |
for (;;) {} /* { dg-warning "infinite loop" } */ |
| 36 |
/* { dg-message "looping back\.\.\." "" { target *-*-* } .-1 } */ |
| 37 |
/* { dg-message "\.\.\.to here" "" { target *-*-* } .-2 } */ |
| 38 |
} |
| 39 |
|
| 40 |
void test_while_true_maybe_useful_work () |
| 41 |
{ |
| 42 |
while (1) |
| 43 |
maybe_useful_work (); |
| 44 |
} |
| 45 |
|
| 46 |
void test_while_true_interproc_empty () |
| 47 |
{ |
| 48 |
/* Depending on optimization level, location is sometimes |
| 49 |
on "while", sometimes on "do_nothing", and sometimes the |
| 50 |
unknown location. */ |
| 51 |
while (1) |
| 52 |
do_nothing (); /* { dg-warning "infinite loop" } */ |
| 53 |
} |
| 54 |
|
| 55 |
void test_while_true_increment_local () |
| 56 |
{ |
| 57 |
int i = 0; |
| 58 |
while (1) |
| 59 |
i++; /* { dg-warning "infinite loop" } */ |
| 60 |
} |
| 61 |
|
| 62 |
void test_while_true_increment_global () |
| 63 |
{ |
| 64 |
while (1) |
| 65 |
global_var++; |
| 66 |
} |
| 67 |
|
| 68 |
void test_guarded_while_true_increment_local (int flag) |
| 69 |
{ |
| 70 |
if (flag) |
| 71 |
{ |
| 72 |
int i = 0; |
| 73 |
while (1) |
| 74 |
i++; /* { dg-warning "infinite loop" } */ |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
void test_while_local_flag_increment_local (int flag) |
| 79 |
{ |
| 80 |
int i = 0; |
| 81 |
while (flag) /* { dg-warning "infinite loop" } */ |
| 82 |
i++; |
| 83 |
} |
| 84 |
|
| 85 |
extern int check_flag (void); |
| 86 |
|
| 87 |
void test_while_calling_fn (void) |
| 88 |
{ |
| 89 |
while (check_flag ()) |
| 90 |
do_nothing (); |
| 91 |
} |
| 92 |
|
| 93 |
void test_missing_parens_on_call (void) |
| 94 |
{ |
| 95 |
while (check_flag) |
| 96 |
do_nothing (); /* { dg-warning "infinite loop" } */ |
| 97 |
} |
| 98 |
|
| 99 |
void test_iteration_copy_and_paste_error (int m, int n) |
| 100 |
{ |
| 101 |
/* Wrong variable is incremented in inner "for" loop, thus |
| 102 |
effectively an infinite loop. */ |
| 103 |
float arr[m][n]; |
| 104 |
for (int i = 0; i < m; i++) |
| 105 |
for (int j = 0; j < n; i++) /* { dg-warning "infinite loop" } */ |
| 106 |
arr[i][j] = 0.f; |
| 107 |
} |
| 108 |
|
| 109 |
void test_missing_comparison_in_for_condition_1 (int n) |
| 110 |
{ |
| 111 |
/* Should have been "i < n", rather than just "n". */ |
| 112 |
for (int i = 0; n; i++) /* { dg-warning "infinite loop" } */ |
| 113 |
{ |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
int test_missing_comparison_in_for_condition_2 (int val, int *arr, int n) |
| 118 |
{ |
| 119 |
/* Should have been "i < n", rather than just "n". */ |
| 120 |
int acc = 0; |
| 121 |
for (int i = 0; n; i++) /* { dg-warning "infinite loop" } */ |
| 122 |
acc += arr[i]; |
| 123 |
return acc; |
| 124 |
} |
| 125 |
|
| 126 |
void test_non_volatile_local_1 (void) |
| 127 |
{ |
| 128 |
int flag = 0; |
| 129 |
while (!flag) /* { dg-warning "infinite loop" } */ |
| 130 |
{ |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
void test_non_volatile_local_2a (void) |
| 135 |
{ |
| 136 |
int flag = 0; |
| 137 |
|
| 138 |
/* Perhaps should complain about this. |
| 139 |
Although the infinite loop might be doing useful work, |
| 140 |
"while (!flag)" is a misleading way to spell "infinite loop". */ |
| 141 |
while (!flag) |
| 142 |
maybe_useful_work (); |
| 143 |
} |
| 144 |
|
| 145 |
void test_non_volatile_local_2b (void) |
| 146 |
{ |
| 147 |
int flag = 0; |
| 148 |
|
| 149 |
while (!flag) |
| 150 |
flag = maybe_useful_work (); |
| 151 |
} |
| 152 |
|
| 153 |
void test_non_volatile_local_3a (int n) |
| 154 |
{ |
| 155 |
int i = 0; |
| 156 |
|
| 157 |
/* Perhaps should complain about this. |
| 158 |
Although the infinite loop might be doing useful work, |
| 159 |
"while (i < n)" is a misleading way to spell "infinite loop". */ |
| 160 |
while (i < n) |
| 161 |
maybe_useful_work (); |
| 162 |
} |
| 163 |
|
| 164 |
void test_non_volatile_local_3b (int n) |
| 165 |
{ |
| 166 |
int i = 0; |
| 167 |
|
| 168 |
while (i < n) |
| 169 |
{ |
| 170 |
maybe_useful_work (); |
| 171 |
i++; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
void test_volatile_local (void) |
| 176 |
{ |
| 177 |
volatile int flag = 0; |
| 178 |
while (!flag) /* { dg-bogus "infinite loop" } */ |
| 179 |
{ |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
void test_non_volatile_global (void) |
| 184 |
{ |
| 185 |
/* Not sure if we should warn here. */ |
| 186 |
while (!global_var) /* { dg-warning "infinite loop" } */ |
| 187 |
{ |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
void test_volatile_global (void) |
| 192 |
{ |
| 193 |
while (!volatile_global_var) /* { dg-bogus "infinite loop" } */ |
| 194 |
{ |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
void test_field_1 (struct st *p) |
| 199 |
{ |
| 200 |
/* Not sure if we should warn here. */ |
| 201 |
while (!p->x) /* { dg-warning "infinite loop" } */ |
| 202 |
{ |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
void test_field_2 (struct st *p) |
| 207 |
{ |
| 208 |
while (!p->x) /* { dg-bogus "infinite loop" } */ |
| 209 |
maybe_useful_work (); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
int missing_init_of_i (int *arr, unsigned n) |
| 214 |
{ |
| 215 |
int sum = 0; |
| 216 |
for (int i; i < n; i--) /* { dg-warning "use of uninitialized value 'i'" } */ |
| 217 |
sum += arr[i]; |
| 218 |
return sum; |
| 219 |
} |
| 220 |
|
| 221 |
void test_switch (char *pc) |
| 222 |
{ |
| 223 |
while (1) |
| 224 |
{ |
| 225 |
char opcode = *pc; /* { dg-warning "infinite loop" } */ |
| 226 |
switch (opcode) /* { dg-message "if it ever follows 'default:' branch, it will always do so\.\.\." } */ |
| 227 |
{ |
| 228 |
case 'A': |
| 229 |
pc++; |
| 230 |
break; |
| 231 |
case 'B': |
| 232 |
return; |
| 233 |
} |
| 234 |
} |
| 235 |
} |