Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/loop-0-up-to-n-by-1-with-iter-obj.c

raw
1 /* { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } } */
2
3 #include <stdlib.h>
4
5 #include "../../gcc.dg/analyzer/analyzer-decls.h"
6
7 struct iter
8 {
9 int start;
10 int end;
11 int step;
12 int val;
13 };
14
15 struct iter * __attribute__((noinline))
16 iter_new (int start, int end, int step)
17 {
18 struct iter *it = (struct iter *)malloc (sizeof (struct iter));
19 if (!it)
20 abort ();
21 it->start = start;
22 it->end = end;
23 it->step = step;
24 it->val = start;
25 return it;
26 }
27
28 int __attribute__((noinline))
29 iter_done_p (struct iter *it)
30 {
31 return it->val >= it->end;
32 }
33
34 void __attribute__((noinline))
35 iter_next (struct iter *it)
36 {
37 it->val += it->step;
38 }
39
40 /* Example of an iterator object, to see how well we cope with a well-disguised
41 iteration from 0 to n with a step of 1. */
42
43 void test(int n)
44 {
45 __analyzer_dump_exploded_nodes (0); /* { dg-warning "1 processed enode" } */
46
47 struct iter *it = iter_new (0, n, 1);
48 while (!iter_done_p (it))
49 {
50 __analyzer_eval (it->val < n); /* { dg-warning "TRUE" "true" } */
51 /* { dg-bogus "UNKNOWN" "unknown" { xfail *-*-* } .-1 } */
52 /* TODO(xfail^^^): ideally we ought to figure out i > 0 after 1st iteration. */
53
54 __analyzer_eval (it->val == 0); /* { dg-warning "TRUE" "true on 1st iter" } */
55 /* { dg-warning "UNKNOWN" "unknown" { target *-*-* } .-1 } */
56 /* TODO: should we ought to figure out i > 0 after 1st iteration? */
57
58 __analyzer_eval (it->val >= 0); /* { dg-warning "TRUE" } */
59
60 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
61
62 iter_next (it);
63 }
64
65 __analyzer_eval (it->val >= n); /* { dg-warning "TRUE" "true" { xfail *-*-* } } */
66 /* { dg-bogus "UNKNOWN" "unknown" { xfail *-*-* } .-1 } */
67
68 __analyzer_eval (it->val == n); /* { dg-warning "TRUE" "desired" { xfail *-*-* } } */
69 /* { dg-warning "UNKNOWN" "status quo" { target *-*-* } .-1 } */
70 /* TODO(xfail^^^): it only figures out i >= 256, rather than i == 256. */
71
72 free (it);
73
74 __analyzer_dump_exploded_nodes (0); /* { dg-warning "1 processed enode" } */
75 }