Debian Codegraph @

clang/test/Analysis/infeasible-sink.c

raw
1 // RUN: %clang_analyze_cc1 %s \
2 // RUN: -analyzer-checker=core \
3 // RUN: -analyzer-checker=debug.ExprInspection \
4 // RUN: -analyzer-config eagerly-assume=false \
5 // RUN: -verify
6
7 // Here we test that if it turns out that the parent state is infeasible then
8 // both children States (more precisely the ExplodedNodes) are marked as a
9 // Sink.
10 // We rely on existing defects of the underlying constraint solver. However,
11 // in the future we might strengthen the solver to discover the infeasibility
12 // right when we create the parent state. At that point some of these tests
13 // will fail, and either we shall find another solver weakness to have the test
14 // case functioning, or we shall simply remove that.
15
16 void clang_analyzer_warnIfReached();
17 void clang_analyzer_eval(int);
18
19 void test1(int x) {
20 if (x * x != 4)
21 return;
22 if (x < 0 || x > 1)
23 return;
24
25 // { x^2 == 4 and x:[0,1] }
26 // This state is already infeasible.
27
28 // Perfectly constraining 'x' will trigger constant folding,
29 // when we realize we were already infeasible.
30 // The same happens for the 'else' branch.
31 if (x == 0) {
32 clang_analyzer_warnIfReached(); // no-warning
33 } else {
34 clang_analyzer_warnIfReached(); // no-warning
35 }
36 clang_analyzer_warnIfReached(); // no-warning
37 (void)x;
38 }
39
40 int a, b, c, d, e;
41 void test2(void) {
42
43 if (a == 0)
44 return;
45
46 if (e != c)
47 return;
48
49 d = e - c;
50 b = d;
51 a -= d;
52
53 clang_analyzer_warnIfReached(); // expected-warning {{REACHABLE}}
54
55 if (a != 0)
56 return;
57
58 clang_analyzer_warnIfReached(); // no-warning: Unreachable due to contradiction.
59 }