Fix some reversed if conditions
[smatch.git] / smatch_ranges.c
bloba2b3e41c2db5c7d3ddd9c24c0c0886ac6b90522c
1 /*
2 * sparse/smatch_ranges.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "parse.h"
11 #include "smatch.h"
12 #include "smatch_extra.h"
13 #include "smatch_slist.h"
15 ALLOCATOR(data_info, "smatch extra data");
16 ALLOCATOR(data_range, "data range");
17 __DO_ALLOCATOR(struct data_range, sizeof(struct data_range), __alignof__(struct data_range),
18 "permanent ranges", perm_data_range);
20 static char *show_num(long long num)
22 static char buff[256];
24 if (num == whole_range.min) {
25 snprintf(buff, 255, "min");
26 } else if (num == whole_range.max) {
27 snprintf(buff, 255, "max");
28 } else if (num < 0) {
29 snprintf(buff, 255, "(%lld)", num);
30 } else {
31 snprintf(buff, 255, "%lld", num);
33 buff[255] = '\0';
34 return buff;
37 char *show_ranges(struct range_list *list)
39 struct data_range *tmp;
40 char full[256];
41 int i = 0;
43 full[0] = '\0';
44 full[255] = '\0';
45 FOR_EACH_PTR(list, tmp) {
46 if (i++)
47 strncat(full, ",", 254 - strlen(full));
48 if (tmp->min == tmp->max) {
49 strncat(full, show_num(tmp->min), 254 - strlen(full));
50 continue;
52 strncat(full, show_num(tmp->min), 254 - strlen(full));
53 strncat(full, "-", 254 - strlen(full));
54 strncat(full, show_num(tmp->max), 254 - strlen(full));
55 } END_FOR_EACH_PTR(tmp);
56 return alloc_sname(full);
59 static struct data_range range_zero = {
60 .min = 0,
61 .max = 0,
64 static struct data_range range_one = {
65 .min = 1,
66 .max = 1,
69 struct data_range *alloc_range(long long min, long long max)
71 struct data_range *ret;
73 if (min > max) {
74 printf("Error invalid range %lld to %lld\n", min, max);
76 if (min == whole_range.min && max == whole_range.max)
77 return &whole_range;
78 if (min == 0 && max == 0)
79 return &range_zero;
80 if (min == 1 && max == 1)
81 return &range_one;
82 ret = __alloc_data_range(0);
83 ret->min = min;
84 ret->max = max;
85 return ret;
88 struct data_range *alloc_range_perm(long long min, long long max)
90 struct data_range *ret;
92 if (min > max) {
93 printf("Error invalid range %lld to %lld\n", min, max);
95 if (min == whole_range.min && max == whole_range.max)
96 return &whole_range;
97 if (min == 0 && max == 0)
98 return &range_zero;
99 if (min == 1 && max == 1)
100 return &range_one;
101 ret = __alloc_perm_data_range(0);
102 ret->min = min;
103 ret->max = max;
104 return ret;
107 struct data_info *alloc_dinfo_range(long long min, long long max)
109 struct data_info *ret;
111 ret = __alloc_data_info(0);
112 ret->type = DATA_RANGE;
113 ret->value_ranges = NULL;
114 add_range(&ret->value_ranges, min, max);
115 return ret;
118 struct data_info *alloc_dinfo_range_list(struct range_list *rl)
120 struct data_info *ret;
122 ret = __alloc_data_info(0);
123 ret->type = DATA_RANGE;
124 ret->value_ranges = rl;
125 return ret;
128 void add_range(struct range_list **list, long long min, long long max)
130 struct data_range *tmp = NULL;
131 struct data_range *new;
132 int check_next = 0;
134 FOR_EACH_PTR(*list, tmp) {
135 if (check_next) {
136 /* Sometimes we overlap with more than one range
137 so we have to delete or modify the next range. */
139 /* Doesn't overlap with the next one. */
140 if (max < tmp->min)
141 return;
142 /* Partially overlaps with the next one. */
143 if (max < tmp->max) {
144 tmp->min = max + 1;
145 return;
147 /* Completely overlaps with the next one. */
148 if (max > tmp->max) {
149 DELETE_CURRENT_PTR(tmp);
150 continue;
153 if (max != whole_range.max && max + 1 == tmp->min) {
154 /* join 2 ranges into a big range */
155 new = alloc_range(min, tmp->max);
156 REPLACE_CURRENT_PTR(tmp, new);
157 return;
159 if (max < tmp->min) { /* new range entirely below */
160 new = alloc_range(min, max);
161 INSERT_CURRENT(new, tmp);
162 return;
164 if (min < tmp->min) { /* new range partially below */
165 if (max < tmp->max)
166 max = tmp->max;
167 else
168 check_next = 1;
169 new = alloc_range(min, max);
170 REPLACE_CURRENT_PTR(tmp, new);
171 if (!check_next)
172 return;
174 if (max <= tmp->max) /* new range already included */
175 return;
176 if (min <= tmp->max) { /* new range partially above */
177 min = tmp->min;
178 new = alloc_range(min, max);
179 REPLACE_CURRENT_PTR(tmp, new);
180 check_next = 1;
182 if (min != whole_range.min && min - 1 == tmp->max) {
183 /* join 2 ranges into a big range */
184 new = alloc_range(tmp->min, max);
185 REPLACE_CURRENT_PTR(tmp, new);
186 check_next = 1;
188 } END_FOR_EACH_PTR(tmp);
189 if (check_next)
190 return;
191 new = alloc_range(min, max);
192 add_ptr_list(list, new);
195 struct range_list *clone_range_list(struct range_list *list)
197 struct data_range *tmp;
198 struct range_list *ret = NULL;
200 FOR_EACH_PTR(list, tmp) {
201 add_ptr_list(&ret, tmp);
202 } END_FOR_EACH_PTR(tmp);
203 return ret;
206 struct range_list *range_list_union(struct range_list *one, struct range_list *two)
208 struct data_range *tmp;
209 struct range_list *ret = NULL;
211 if (!one || !two) /*having nothing in a list means everything is in */
212 return NULL;
214 FOR_EACH_PTR(one, tmp) {
215 add_range(&ret, tmp->min, tmp->max);
216 } END_FOR_EACH_PTR(tmp);
217 FOR_EACH_PTR(two, tmp) {
218 add_range(&ret, tmp->min, tmp->max);
219 } END_FOR_EACH_PTR(tmp);
220 return ret;
223 struct range_list *remove_range(struct range_list *list, long long min, long long max)
225 struct data_range *tmp;
226 struct range_list *ret = NULL;
228 FOR_EACH_PTR(list, tmp) {
229 if (tmp->max < min) {
230 add_range(&ret, tmp->min, tmp->max);
231 continue;
233 if (tmp->min > max) {
234 add_range(&ret, tmp->min, tmp->max);
235 continue;
237 if (tmp->min >= min && tmp->max <= max)
238 continue;
239 if (tmp->min >= min) {
240 add_range(&ret, max + 1, tmp->max);
241 } else if (tmp->max <= max) {
242 add_range(&ret, tmp->min, min - 1);
243 } else {
244 add_range(&ret, tmp->min, min - 1);
245 add_range(&ret, max + 1, tmp->max);
247 } END_FOR_EACH_PTR(tmp);
248 return ret;
251 long long get_dinfo_min(struct data_info *dinfo)
253 struct data_range *drange;
255 if (!dinfo || !dinfo->value_ranges)
256 return whole_range.min;
257 drange = first_ptr_list((struct ptr_list *)dinfo->value_ranges);
258 return drange->min;
261 long long get_dinfo_max(struct data_info *dinfo)
263 struct data_range *drange;
265 if (!dinfo || !dinfo->value_ranges)
266 return whole_range.max;
267 drange = first_ptr_list((struct ptr_list *)dinfo->value_ranges);
268 return drange->max;
272 * if it can be only one and only value return 1, else return 0
274 int get_single_value_from_range(struct data_info *dinfo, long long *val)
276 struct data_range *tmp;
277 int count = 0;
279 if (dinfo->type != DATA_RANGE)
280 return 0;
282 FOR_EACH_PTR(dinfo->value_ranges, tmp) {
283 if (!count++) {
284 if (tmp->min != tmp->max)
285 return 0;
286 *val = tmp->min;
287 } else {
288 return 0;
290 } END_FOR_EACH_PTR(tmp);
291 return 1;
294 int true_comparison_range(struct data_range *left, int comparison, struct data_range *right)
296 switch(comparison){
297 case '<':
298 case SPECIAL_UNSIGNED_LT:
299 if (left->min < right->max)
300 return 1;
301 return 0;
302 case SPECIAL_UNSIGNED_LTE:
303 case SPECIAL_LTE:
304 if (left->min <= right->max)
305 return 1;
306 return 0;
307 case SPECIAL_EQUAL:
308 if (left->max < right->min)
309 return 0;
310 if (left->min > right->max)
311 return 0;
312 return 1;
313 case SPECIAL_UNSIGNED_GTE:
314 case SPECIAL_GTE:
315 if (left->max >= right->min)
316 return 1;
317 return 0;
318 case '>':
319 case SPECIAL_UNSIGNED_GT:
320 if (left->max > right->min)
321 return 1;
322 return 0;
323 case SPECIAL_NOTEQUAL:
324 if (left->min != left->max)
325 return 1;
326 if (right->min != right->max)
327 return 1;
328 if (left->min != right->min)
329 return 1;
330 return 0;
331 default:
332 sm_msg("unhandled comparison %d\n", comparison);
333 return 0;
335 return 0;
338 int true_comparison_range_lr(int comparison, struct data_range *var, struct data_range *val, int left)
340 if (left)
341 return true_comparison_range(var, comparison, val);
342 else
343 return true_comparison_range(val, comparison, var);
346 static int false_comparison_range(struct data_range *left, int comparison, struct data_range *right)
348 switch(comparison){
349 case '<':
350 case SPECIAL_UNSIGNED_LT:
351 if (left->max >= right->min)
352 return 1;
353 return 0;
354 case SPECIAL_UNSIGNED_LTE:
355 case SPECIAL_LTE:
356 if (left->max > right->min)
357 return 1;
358 return 0;
359 case SPECIAL_EQUAL:
360 if (left->min != left->max)
361 return 1;
362 if (right->min != right->max)
363 return 1;
364 if (left->min != right->min)
365 return 1;
366 return 0;
367 case SPECIAL_UNSIGNED_GTE:
368 case SPECIAL_GTE:
369 if (left->min < right->max)
370 return 1;
371 return 0;
372 case '>':
373 case SPECIAL_UNSIGNED_GT:
374 if (left->min <= right->max)
375 return 1;
376 return 0;
377 case SPECIAL_NOTEQUAL:
378 if (left->max < right->min)
379 return 0;
380 if (left->min > right->max)
381 return 0;
382 return 1;
383 default:
384 sm_msg("unhandled comparison %d\n", comparison);
385 return 0;
387 return 0;
390 int false_comparison_range_lr(int comparison, struct data_range *var, struct data_range *val, int left)
392 if (left)
393 return false_comparison_range(var, comparison, val);
394 else
395 return false_comparison_range(val, comparison, var);
398 int possibly_true(int comparison, struct data_info *dinfo, int num, int left)
400 struct data_range *tmp;
401 struct data_range drange;
403 drange.min = num;
404 drange.max = num;
406 FOR_EACH_PTR(dinfo->value_ranges, tmp) {
407 if (true_comparison_range_lr(comparison, tmp, &drange, left))
408 return 1;
409 } END_FOR_EACH_PTR(tmp);
410 return 0;
413 int possibly_false(int comparison, struct data_info *dinfo, int num, int left)
415 struct data_range *tmp;
416 struct data_range drange;
418 drange.min = num;
419 drange.max = num;
421 FOR_EACH_PTR(dinfo->value_ranges, tmp) {
422 if (false_comparison_range_lr(comparison, tmp, &drange, left))
423 return 1;
424 } END_FOR_EACH_PTR(tmp);
425 return 0;
428 int possibly_true_range_list(int comparison, struct data_info *dinfo, struct range_list *values, int left)
430 struct data_range *tmp, *tmp2;
432 FOR_EACH_PTR(dinfo->value_ranges, tmp) {
433 FOR_EACH_PTR(values, tmp2) {
434 if (true_comparison_range_lr(comparison, tmp, tmp2, left))
435 return 1;
436 } END_FOR_EACH_PTR(tmp2);
437 } END_FOR_EACH_PTR(tmp);
438 return 0;
441 int possibly_false_range_list(int comparison, struct data_info *dinfo, struct range_list *values, int left)
443 struct data_range *tmp, *tmp2;
445 FOR_EACH_PTR(dinfo->value_ranges, tmp) {
446 FOR_EACH_PTR(values, tmp2) {
447 if (false_comparison_range_lr(comparison, tmp, tmp2, left))
448 return 1;
449 } END_FOR_EACH_PTR(tmp2);
450 } END_FOR_EACH_PTR(tmp);
451 return 0;
454 void tack_on(struct range_list **list, struct data_range *drange)
456 add_ptr_list(list, drange);
459 int in_list_exact(struct range_list *list, struct data_range *drange)
461 struct data_range *tmp;
463 FOR_EACH_PTR(list, tmp) {
464 if (tmp->min == drange->min && tmp->max == drange->max)
465 return 1;
466 } END_FOR_EACH_PTR(tmp);
467 return 0;
471 void push_range_list(struct range_list_stack **rl_stack, struct range_list *rl)
473 add_ptr_list(rl_stack, rl);
476 struct range_list *pop_range_list(struct range_list_stack **rl_stack)
478 struct range_list *rl;
480 rl = last_ptr_list((struct ptr_list *)*rl_stack);
481 delete_ptr_list_last((struct ptr_list **)rl_stack);
482 return rl;
485 struct range_list *top_range_list(struct range_list_stack *rl_stack)
487 struct range_list *rl;
489 rl = last_ptr_list((struct ptr_list *)rl_stack);
490 return rl;
493 void filter_top_range_list(struct range_list_stack **rl_stack, long long num)
495 struct range_list *rl;
497 rl = pop_range_list(rl_stack);
498 rl = remove_range(rl, num, num);
499 push_range_list(rl_stack, rl);
502 static void free_single_dinfo(struct data_info *dinfo)
504 if (dinfo->type == DATA_RANGE)
505 __free_ptr_list((struct ptr_list **)&dinfo->value_ranges);
508 static void free_dinfos(struct allocation_blob *blob)
510 unsigned int size = sizeof(struct data_info);
511 unsigned int offset = 0;
513 while (offset < blob->offset) {
514 free_single_dinfo((struct data_info *)(blob->data + offset));
515 offset += size;
519 void free_data_info_allocs(void)
521 struct allocator_struct *desc = &data_info_allocator;
522 struct allocation_blob *blob = desc->blobs;
524 desc->blobs = NULL;
525 desc->allocations = 0;
526 desc->total_bytes = 0;
527 desc->useful_bytes = 0;
528 desc->freelist = NULL;
529 while (blob) {
530 struct allocation_blob *next = blob->next;
531 free_dinfos(blob);
532 blob_free(blob, desc->chunking);
533 blob = next;
535 clear_data_range_alloc();