constraint_manager_negate_difference.c
4.65 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// RUN: %clang_analyze_cc1 -analyzer-checker=debug.ExprInspection,core.builtin -analyzer-config aggressive-binary-operation-simplification=true -verify %s
void clang_analyzer_eval(int);
void exit(int);
#define UINT_MIN (0U)
#define UINT_MAX (~UINT_MIN)
#define UINT_MID (UINT_MAX / 2 + 1)
#define INT_MAX (UINT_MAX & (UINT_MAX >> 1))
#define INT_MIN (UINT_MAX & ~(UINT_MAX >> 1))
extern void __assert_fail (__const char *__assertion, __const char *__file,
unsigned int __line, __const char *__function)
__attribute__ ((__noreturn__));
#define assert(expr) \
((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__))
void assert_in_range(int x) {
assert(x <= ((int)INT_MAX / 4));
assert(x >= -(((int)INT_MAX) / 4));
}
void assert_in_wide_range(int x) {
assert(x <= ((int)INT_MAX / 2));
assert(x >= -(((int)INT_MAX) / 2));
}
void assert_in_range_2(int m, int n) {
assert_in_range(m);
assert_in_range(n);
}
void equal(int m, int n) {
assert_in_range_2(m, n);
if (m != n)
return;
assert_in_wide_range(m - n);
clang_analyzer_eval(n == m); // expected-warning{{TRUE}}
}
void non_equal(int m, int n) {
assert_in_range_2(m, n);
if (m == n)
return;
assert_in_wide_range(m - n);
clang_analyzer_eval(n != m); // expected-warning{{TRUE}}
}
void less_or_equal(int m, int n) {
assert_in_range_2(m, n);
if (m < n)
return;
assert_in_wide_range(m - n);
clang_analyzer_eval(n <= m); // expected-warning{{TRUE}}
}
void less(int m, int n) {
assert_in_range_2(m, n);
if (m <= n)
return;
assert_in_wide_range(m - n);
clang_analyzer_eval(n < m); // expected-warning{{TRUE}}
}
void greater_or_equal(int m, int n) {
assert_in_range_2(m, n);
if (m > n)
return;
assert_in_wide_range(m - n);
clang_analyzer_eval(n >= m); // expected-warning{{TRUE}}
}
void greater(int m, int n) {
assert_in_range_2(m, n);
if (m >= n)
return;
assert_in_wide_range(m - n);
clang_analyzer_eval(n > m); // expected-warning{{TRUE}}
}
void negate_positive_range(int m, int n) {
if (m - n <= 0)
return;
clang_analyzer_eval(n - m < 0); // expected-warning{{TRUE}}
clang_analyzer_eval(n - m > INT_MIN); // expected-warning{{TRUE}}
clang_analyzer_eval(n - m == INT_MIN); // expected-warning{{FALSE}}
}
void negate_int_min(int m, int n) {
if (m - n != INT_MIN)
return;
clang_analyzer_eval(n - m == INT_MIN); // expected-warning{{TRUE}}
}
void negate_mixed(int m, int n) {
if (m -n > INT_MIN && m - n <= 0)
return;
clang_analyzer_eval(n - m <= 0); // expected-warning{{TRUE}}
}
void effective_range(int m, int n) {
assert(m - n >= 0);
assert(n - m >= 0);
clang_analyzer_eval(m - n == 0); // expected-warning{{TRUE}}
clang_analyzer_eval(n - m == 0); // expected-warning{{TRUE}}
}
void effective_range_2(int m, int n) {
assert(m - n <= 0);
assert(n - m <= 0);
clang_analyzer_eval(m - n == 0); // expected-warning{{TRUE}} expected-warning{{FALSE}}
clang_analyzer_eval(n - m == 0); // expected-warning{{TRUE}} expected-warning{{FALSE}}
}
void negate_unsigned_min(unsigned m, unsigned n) {
if (m - n == UINT_MIN) {
clang_analyzer_eval(n - m == UINT_MIN); // expected-warning{{TRUE}}
clang_analyzer_eval(n - m != UINT_MIN); // expected-warning{{FALSE}}
clang_analyzer_eval(n - m > UINT_MIN); // expected-warning{{FALSE}}
clang_analyzer_eval(n - m < UINT_MIN); // expected-warning{{FALSE}}
}
}
void negate_unsigned_mid(unsigned m, unsigned n) {
if (m - n == UINT_MID) {
clang_analyzer_eval(n - m == UINT_MID); // expected-warning{{TRUE}}
clang_analyzer_eval(n - m != UINT_MID); // expected-warning{{FALSE}}
}
}
void negate_unsigned_mid2(unsigned m, unsigned n) {
if (m - n < UINT_MID && m - n > UINT_MIN) {
clang_analyzer_eval(n - m > UINT_MID); // expected-warning{{TRUE}}
clang_analyzer_eval(n - m < UINT_MID); // expected-warning{{FALSE}}
}
}
void negate_unsigned_max(unsigned m, unsigned n) {
if (m - n == UINT_MAX) {
clang_analyzer_eval(n - m == 1); // expected-warning{{TRUE}}
clang_analyzer_eval(n - m != 1); // expected-warning{{FALSE}}
}
}
void negate_unsigned_one(unsigned m, unsigned n) {
if (m - n == 1) {
clang_analyzer_eval(n - m == UINT_MAX); // expected-warning{{TRUE}}
clang_analyzer_eval(n - m < UINT_MAX); // expected-warning{{FALSE}}
}
}
// The next code is a repro for the bug PR41588
void negated_unsigned_range(unsigned x, unsigned y) {
clang_analyzer_eval(x - y != 0); // expected-warning{{FALSE}} expected-warning{{TRUE}}
clang_analyzer_eval(y - x != 0); // expected-warning{{FALSE}} expected-warning{{TRUE}}
// expected no assertion on the next line
clang_analyzer_eval(x - y != 0); // expected-warning{{FALSE}} expected-warning{{TRUE}}
}