Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Analysis / additive-folding-range-constraints.c
blobe75223eab05bcf7467e3d66ef082d44b3cc02d27
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -analyzer-config eagerly-assume=false -verify %s
3 void clang_analyzer_eval(int);
5 #define UINT_MAX (~0U)
6 #define INT_MAX (UINT_MAX & (UINT_MAX >> 1))
7 #define INT_MIN (-INT_MAX - 1)
10 // Each of these adjusted ranges has an adjustment small enough to split the
11 // solution range across an overflow boundary (Min for <, Max for >).
12 // This corresponds to one set of branches in RangeConstraintManager.
13 void smallAdjustmentGT (unsigned a) {
14 if (a+2 > 1)
15 clang_analyzer_eval(a < UINT_MAX-1); // expected-warning{{TRUE}}
16 else
17 clang_analyzer_eval(a == UINT_MAX-1 || a == UINT_MAX); // expected-warning{{TRUE}}
20 void smallAdjustmentGE (unsigned a) {
21 if (a+2 >= 1)
22 clang_analyzer_eval(a < UINT_MAX-1 || a == UINT_MAX); // expected-warning{{TRUE}}
23 else
24 clang_analyzer_eval(a == UINT_MAX-1); // expected-warning{{TRUE}}
27 void smallAdjustmentLT (unsigned a) {
28 if (a+1 < 2)
29 clang_analyzer_eval(a == 0 || a == UINT_MAX); // expected-warning{{TRUE}}
32 void smallAdjustmentLE (unsigned a) {
33 if (a+1 <= 2)
34 clang_analyzer_eval(a == 0 || a == 1 || a == UINT_MAX); // expected-warning{{TRUE}}
38 // Each of these adjusted ranges has an adjustment large enough to push the
39 // comparison value over an overflow boundary (Min for <, Max for >).
40 // This corresponds to one set of branches in RangeConstraintManager.
41 void largeAdjustmentGT (unsigned a) {
42 if (a-2 > UINT_MAX-1)
43 clang_analyzer_eval(a == 1); // expected-warning{{TRUE}}
44 else
45 clang_analyzer_eval(a != 1); // expected-warning{{TRUE}}
48 void largeAdjustmentGE (unsigned a) {
49 if (a-2 >= UINT_MAX-1)
50 clang_analyzer_eval(a == 1 || a == 0); // expected-warning{{TRUE}}
51 else
52 clang_analyzer_eval(a > 1); // expected-warning{{TRUE}}
55 void largeAdjustmentLT (unsigned a) {
56 if (a+2 < 1)
57 clang_analyzer_eval(a == UINT_MAX-1); // expected-warning{{TRUE}}
58 else
59 clang_analyzer_eval(a != UINT_MAX-1); // expected-warning{{TRUE}}
62 void largeAdjustmentLE (unsigned a) {
63 if (a+2 <= 1)
64 clang_analyzer_eval(a == UINT_MAX-1 || a == UINT_MAX); // expected-warning{{TRUE}}
65 else
66 clang_analyzer_eval(a < UINT_MAX-1); // expected-warning{{TRUE}}
70 // Test the nine cases in RangeConstraintManager's pinning logic.
71 // For out-of-range tautologies, it may be the negation that actually
72 // triggers the case in question.
73 void mixedComparisons1(signed char a) {
74 // Case 1: The range is entirely below the symbol's range.
75 int min = INT_MIN;
77 clang_analyzer_eval((a - 2) >= (min + 5LL)); // expected-warning{{TRUE}}
79 clang_analyzer_eval(a == 0); // expected-warning{{UNKNOWN}}
80 clang_analyzer_eval(a == 0x7F); // expected-warning{{UNKNOWN}}
81 clang_analyzer_eval(a == -0x80); // expected-warning{{UNKNOWN}}
84 void mixedComparisons2(signed char a) {
85 // Case 2: Only the lower end of the range is outside.
86 clang_analyzer_eval((a - 5) < (-0x81LL)); // expected-warning{{UNKNOWN}}
88 if ((a - 5) < (-0x81LL)) {
89 clang_analyzer_eval(a == 0); // expected-warning{{FALSE}}
90 clang_analyzer_eval(a == 0x7F); // expected-warning{{FALSE}}
91 clang_analyzer_eval(a == -0x80); // expected-warning{{UNKNOWN}}
95 void mixedComparisons3(signed char a) {
96 // Case 3: The entire symbol range is covered.
97 clang_analyzer_eval((a - 0x200) < -0x100LL); // expected-warning{{TRUE}}
99 clang_analyzer_eval(a == 0); // expected-warning{{UNKNOWN}}
100 clang_analyzer_eval(a == 0x7F); // expected-warning{{UNKNOWN}}
101 clang_analyzer_eval(a == -0x80); // expected-warning{{UNKNOWN}}
104 void mixedComparisons4(signed char a) {
105 // Case 4: The range wraps around, but the lower wrap is out-of-range.
106 clang_analyzer_eval((a - 5) > 0LL); // expected-warning{{UNKNOWN}}
108 if ((a - 5) > 0LL) {
109 clang_analyzer_eval(a == 0); // expected-warning{{FALSE}}
110 clang_analyzer_eval(a == 0x7F); // expected-warning{{UNKNOWN}}
111 clang_analyzer_eval(a == -0x80); // expected-warning{{FALSE}}
115 void mixedComparisons5(signed char a) {
116 // Case 5: The range is inside and may or may not wrap.
117 clang_analyzer_eval((a + 5) == 0LL); // expected-warning{{UNKNOWN}}
119 if ((a + 5) == 0LL) {
120 clang_analyzer_eval(a == 0); // expected-warning{{FALSE}}
121 clang_analyzer_eval(a == 0x7F); // expected-warning{{FALSE}}
122 clang_analyzer_eval(a == -0x80); // expected-warning{{FALSE}}
123 } else {
124 clang_analyzer_eval(a == 0); // expected-warning{{UNKNOWN}}
125 clang_analyzer_eval(a == 0x7F); // expected-warning{{UNKNOWN}}
126 clang_analyzer_eval(a == -0x80); // expected-warning{{UNKNOWN}}
130 void mixedComparisons6(signed char a) {
131 // Case 6: Only the upper end of the range is outside.
132 clang_analyzer_eval((a + 5) > 0x81LL); // expected-warning{{UNKNOWN}}
134 if ((a + 5) > 0x81LL) {
135 clang_analyzer_eval(a == 0); // expected-warning{{FALSE}}
136 clang_analyzer_eval(a == 0x7F); // expected-warning{{UNKNOWN}}
137 clang_analyzer_eval(a == -0x80); // expected-warning{{FALSE}}
141 void mixedComparisons7(signed char a) {
142 // Case 7: The range wraps around but is entirely outside the symbol's range.
143 int min = INT_MIN;
145 clang_analyzer_eval((a + 2) >= (min + 5LL)); // expected-warning{{TRUE}}
147 clang_analyzer_eval(a == 0); // expected-warning{{UNKNOWN}}
148 clang_analyzer_eval(a == 0x7F); // expected-warning{{UNKNOWN}}
149 clang_analyzer_eval(a == -0x80); // expected-warning{{UNKNOWN}}
152 void mixedComparisons8(signed char a) {
153 // Case 8: The range wraps, but the upper wrap is out of range.
154 clang_analyzer_eval((a + 5) < 0LL); // expected-warning{{UNKNOWN}}
156 if ((a + 5) < 0LL) {
157 clang_analyzer_eval(a == 0); // expected-warning{{FALSE}}
158 clang_analyzer_eval(a == 0x7F); // expected-warning{{FALSE}}
159 clang_analyzer_eval(a == -0x80); // expected-warning{{UNKNOWN}}
163 void mixedComparisons9(signed char a) {
164 // Case 9: The range is entirely above the symbol's range.
165 int max = INT_MAX;
167 clang_analyzer_eval((a + 2) <= (max - 5LL)); // expected-warning{{TRUE}}
169 clang_analyzer_eval(a == 0); // expected-warning{{UNKNOWN}}
170 clang_analyzer_eval(a == 0x7F); // expected-warning{{UNKNOWN}}
171 clang_analyzer_eval(a == -0x80); // expected-warning{{UNKNOWN}}
175 void mixedSignedness1(int a) {
176 unsigned max = UINT_MAX;
177 clang_analyzer_eval(a < max); // expected-warning{{UNKNOWN}}
178 clang_analyzer_eval((a + 2) < max); // expected-warning{{UNKNOWN}}
179 clang_analyzer_eval((a + 2U) < max); // expected-warning{{UNKNOWN}}
182 void mixedSignedness2(int a) {
183 unsigned max = UINT_MAX;
184 clang_analyzer_eval(a <= max); // expected-warning{{TRUE}}
185 clang_analyzer_eval((a + 2) <= max); // expected-warning{{TRUE}}
186 clang_analyzer_eval((a + 2U) <= max); // expected-warning{{TRUE}}
189 void mixedSignedness3(unsigned a) {
190 int max = INT_MAX;
191 clang_analyzer_eval(a < max); // expected-warning{{UNKNOWN}}
192 clang_analyzer_eval((a + 2) < max); // expected-warning{{UNKNOWN}}
193 clang_analyzer_eval((a + 2U) < max); // expected-warning{{UNKNOWN}}
196 void mixedSignedness4(unsigned a) {
197 int max = INT_MAX;
198 clang_analyzer_eval(a <= max); // expected-warning{{UNKNOWN}}
199 clang_analyzer_eval((a + 2) <= max); // expected-warning{{UNKNOWN}}
200 clang_analyzer_eval((a + 2U) <= max); // expected-warning{{UNKNOWN}}
203 void mixedSignedness5(unsigned a) {
204 int min = INT_MIN;
205 clang_analyzer_eval(a < min); // expected-warning{{UNKNOWN}}
206 clang_analyzer_eval((a + 2) < min); // expected-warning{{UNKNOWN}}
207 clang_analyzer_eval((a + 2U) < min); // expected-warning{{UNKNOWN}}
210 void mixedSignedness6(unsigned a) {
211 int min = INT_MIN;
212 clang_analyzer_eval(a <= min); // expected-warning{{UNKNOWN}}
213 clang_analyzer_eval((a + 2) <= min); // expected-warning{{UNKNOWN}}
214 clang_analyzer_eval((a + 2U) <= min); // expected-warning{{UNKNOWN}}
217 void mixedSignedness7(unsigned a) {
218 unsigned min = 0;
219 clang_analyzer_eval(a < min); // expected-warning{{FALSE}}
220 clang_analyzer_eval((a + 2) < min); // expected-warning{{FALSE}}
221 clang_analyzer_eval((a + 2U) < min); // expected-warning{{FALSE}}
224 void mixedSignedness8(unsigned a) {
225 unsigned min = 0;
226 clang_analyzer_eval(a <= min); // expected-warning{{UNKNOWN}}
227 clang_analyzer_eval((a + 2) <= min); // expected-warning{{UNKNOWN}}
228 clang_analyzer_eval((a + 2U) <= min); // expected-warning{{UNKNOWN}}
231 void mixedSignedness9(unsigned a) {
232 int min = 0;
233 clang_analyzer_eval(a < min); // expected-warning{{FALSE}}
234 clang_analyzer_eval((a + 2) < min); // expected-warning{{FALSE}}
235 clang_analyzer_eval((a + 2U) < min); // expected-warning{{FALSE}}
238 void mixedSignedness10(unsigned a) {
239 int min = 0;
240 clang_analyzer_eval(a <= min); // expected-warning{{UNKNOWN}}
241 clang_analyzer_eval((a + 2) <= min); // expected-warning{{UNKNOWN}}
242 clang_analyzer_eval((a + 2U) <= min); // expected-warning{{UNKNOWN}}
245 void mixedSignedness11(int a) {
246 int min = 0;
247 clang_analyzer_eval(a < min); // expected-warning{{UNKNOWN}}
248 clang_analyzer_eval((a + 2) < min); // expected-warning{{UNKNOWN}}
249 clang_analyzer_eval((a + 2U) < min); // expected-warning{{FALSE}}
252 void mixedSignedness12(int a) {
253 int min = 0;
254 clang_analyzer_eval(a <= min); // expected-warning{{UNKNOWN}}
255 clang_analyzer_eval((a + 2) <= min); // expected-warning{{UNKNOWN}}
256 clang_analyzer_eval((a + 2U) <= min); // expected-warning{{UNKNOWN}}
259 void mixedSignedness13(int a) {
260 unsigned max = INT_MAX;
261 clang_analyzer_eval(a < max); // expected-warning{{UNKNOWN}}
262 clang_analyzer_eval((a + 2) < max); // expected-warning{{UNKNOWN}}
263 clang_analyzer_eval((a + 2U) < max); // expected-warning{{UNKNOWN}}
266 void mixedSignedness14(int a) {
267 unsigned max = INT_MAX;
268 clang_analyzer_eval(a <= max); // expected-warning{{UNKNOWN}}
269 clang_analyzer_eval((a + 2) <= max); // expected-warning{{UNKNOWN}}
270 clang_analyzer_eval((a + 2U) <= max); // expected-warning{{UNKNOWN}}
273 void mixedSignedness15(int a) {
274 unsigned min = INT_MIN;
275 clang_analyzer_eval(a < min); // expected-warning{{UNKNOWN}}
276 clang_analyzer_eval((a + 2) < min); // expected-warning{{UNKNOWN}}
277 clang_analyzer_eval((a + 2U) < min); // expected-warning{{UNKNOWN}}
280 void mixedSignedness16(int a) {
281 unsigned min = INT_MIN;
282 clang_analyzer_eval(a <= min); // expected-warning{{UNKNOWN}}
283 clang_analyzer_eval((a + 2) <= min); // expected-warning{{UNKNOWN}}
284 clang_analyzer_eval((a + 2U) <= min); // expected-warning{{UNKNOWN}}
287 void mixedSignedness17(int a) {
288 unsigned max = INT_MAX;
289 if (a < max)
290 return;
292 clang_analyzer_eval(a < 0); // expected-warning{{UNKNOWN}}
293 clang_analyzer_eval(a == 0); // expected-warning{{FALSE}}
294 clang_analyzer_eval(a == INT_MAX); // expected-warning{{UNKNOWN}}
297 void mixedSignedness18(int a) {
298 if (a >= 0)
299 return;
301 clang_analyzer_eval(a < 0); // expected-warning{{TRUE}}
302 clang_analyzer_eval(a == (unsigned)INT_MIN); // expected-warning{{UNKNOWN}}
303 clang_analyzer_eval(a == UINT_MAX); // expected-warning{{UNKNOWN}}