constant-folding.c
5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify -analyzer-config eagerly-assume=false %s
void clang_analyzer_eval(int);
// There should be no warnings unless otherwise indicated.
void testComparisons (int a) {
// Sema can already catch the simple comparison a==a,
// since that's usually a logic error (and not path-dependent).
int b = a;
clang_analyzer_eval(b == a); // expected-warning{{TRUE}}
clang_analyzer_eval(b >= a); // expected-warning{{TRUE}}
clang_analyzer_eval(b <= a); // expected-warning{{TRUE}}
clang_analyzer_eval(b != a); // expected-warning{{FALSE}}
clang_analyzer_eval(b > a); // expected-warning{{FALSE}}
clang_analyzer_eval(b < a); // expected-warning{{FALSE}}
}
void testSelfOperations (int a) {
clang_analyzer_eval((a|a) == a); // expected-warning{{TRUE}}
clang_analyzer_eval((a&a) == a); // expected-warning{{TRUE}}
clang_analyzer_eval((a^a) == 0); // expected-warning{{TRUE}}
clang_analyzer_eval((a-a) == 0); // expected-warning{{TRUE}}
}
void testIdempotent (int a) {
clang_analyzer_eval((a*1) == a); // expected-warning{{TRUE}}
clang_analyzer_eval((a/1) == a); // expected-warning{{TRUE}}
clang_analyzer_eval((a+0) == a); // expected-warning{{TRUE}}
clang_analyzer_eval((a-0) == a); // expected-warning{{TRUE}}
clang_analyzer_eval((a<<0) == a); // expected-warning{{TRUE}}
clang_analyzer_eval((a>>0) == a); // expected-warning{{TRUE}}
clang_analyzer_eval((a^0) == a); // expected-warning{{TRUE}}
clang_analyzer_eval((a&(~0)) == a); // expected-warning{{TRUE}}
clang_analyzer_eval((a|0) == a); // expected-warning{{TRUE}}
}
void testReductionToConstant (int a) {
clang_analyzer_eval((a*0) == 0); // expected-warning{{TRUE}}
clang_analyzer_eval((a&0) == 0); // expected-warning{{TRUE}}
clang_analyzer_eval((a|(~0)) == (~0)); // expected-warning{{TRUE}}
}
void testSymmetricIntSymOperations (int a) {
clang_analyzer_eval((2+a) == (a+2)); // expected-warning{{TRUE}}
clang_analyzer_eval((2*a) == (a*2)); // expected-warning{{TRUE}}
clang_analyzer_eval((2&a) == (a&2)); // expected-warning{{TRUE}}
clang_analyzer_eval((2^a) == (a^2)); // expected-warning{{TRUE}}
clang_analyzer_eval((2|a) == (a|2)); // expected-warning{{TRUE}}
}
void testAsymmetricIntSymOperations (int a) {
clang_analyzer_eval(((~0) >> a) == (~0)); // expected-warning{{TRUE}}
clang_analyzer_eval((0 >> a) == 0); // expected-warning{{TRUE}}
clang_analyzer_eval((0 << a) == 0); // expected-warning{{TRUE}}
// Unsigned right shift shifts in zeroes.
clang_analyzer_eval(((~0U) >> a) != (~0U)); // expected-warning{{UNKNOWN}}
}
void testLocations (char *a) {
char *b = a;
clang_analyzer_eval(b == a); // expected-warning{{TRUE}}
clang_analyzer_eval(b >= a); // expected-warning{{TRUE}}
clang_analyzer_eval(b <= a); // expected-warning{{TRUE}}
clang_analyzer_eval(b != a); // expected-warning{{FALSE}}
clang_analyzer_eval(b > a); // expected-warning{{FALSE}}
clang_analyzer_eval(b < a); // expected-warning{{FALSE}}
}
void testMixedTypeComparisons (char a, unsigned long b) {
if (a != 0) return;
if (b != 0x100) return;
clang_analyzer_eval(a <= b); // expected-warning{{TRUE}}
clang_analyzer_eval(b >= a); // expected-warning{{TRUE}}
clang_analyzer_eval(a != b); // expected-warning{{TRUE}}
}
void testBitwiseRules(unsigned int a, int b) {
clang_analyzer_eval((a | 1) >= 1); // expected-warning{{TRUE}}
clang_analyzer_eval((a | -1) >= -1); // expected-warning{{TRUE}}
clang_analyzer_eval((a | 2) >= 2); // expected-warning{{TRUE}}
clang_analyzer_eval((a | 5) >= 5); // expected-warning{{TRUE}}
clang_analyzer_eval((a | 10) >= 10); // expected-warning{{TRUE}}
// Argument order should not influence this
clang_analyzer_eval((1 | a) >= 1); // expected-warning{{TRUE}}
clang_analyzer_eval((a & 1) <= 1); // expected-warning{{TRUE}}
clang_analyzer_eval((a & 2) <= 2); // expected-warning{{TRUE}}
clang_analyzer_eval((a & 5) <= 5); // expected-warning{{TRUE}}
clang_analyzer_eval((a & 10) <= 10); // expected-warning{{TRUE}}
clang_analyzer_eval((a & -10) <= 10); // expected-warning{{UNKNOWN}}
// Again, check for different argument order.
clang_analyzer_eval((1 & a) <= 1); // expected-warning{{TRUE}}
unsigned int c = a;
c |= 1;
clang_analyzer_eval((c | 0) == 0); // expected-warning{{FALSE}}
// Rules don't apply to signed typed, as the values might be negative.
clang_analyzer_eval((b | 1) > 0); // expected-warning{{UNKNOWN}}
// Even for signed values, bitwise OR with a non-zero is always non-zero.
clang_analyzer_eval((b | 1) == 0); // expected-warning{{FALSE}}
clang_analyzer_eval((b | -2) == 0); // expected-warning{{FALSE}}
clang_analyzer_eval((b | 10) == 0); // expected-warning{{FALSE}}
clang_analyzer_eval((b | 0) == 0); // expected-warning{{UNKNOWN}}
#ifdef ANALYZER_CM_Z3
clang_analyzer_eval((b | -2) >= 0); // expected-warning{{FALSE}}
#else
clang_analyzer_eval((b | -2) >= 0); // expected-warning{{UNKNOWN}}
#endif
// Check that dynamically computed constants also work.
int constant = 1 << 3;
unsigned int d = a | constant;
clang_analyzer_eval(constant > 0); // expected-warning{{TRUE}}
}