math: handle logical expressions
[smatch.git] / smatch_ranges.c
blob6a98db2d105a874d90b85c33b8a81602547bd1c6
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 struct data_range whole_range = {
21 .min = LLONG_MIN,
22 .max = LLONG_MAX,
25 static char *show_num(long long num)
27 static char buff[256];
29 if (num == whole_range.min)
30 snprintf(buff, 255, "min");
31 else if (num == whole_range.max)
32 snprintf(buff, 255, "max");
33 else if (num < 0)
34 snprintf(buff, 255, "(%lld)", num);
35 else
36 snprintf(buff, 255, "%lld", num);
38 buff[255] = '\0';
39 return buff;
42 char *show_ranges(struct range_list *list)
44 struct data_range *tmp;
45 char full[256];
46 int i = 0;
48 full[0] = '\0';
49 full[255] = '\0';
50 FOR_EACH_PTR(list, tmp) {
51 if (i++)
52 strncat(full, ",", 254 - strlen(full));
53 if (tmp->min == tmp->max) {
54 strncat(full, show_num(tmp->min), 254 - strlen(full));
55 continue;
57 strncat(full, show_num(tmp->min), 254 - strlen(full));
58 strncat(full, "-", 254 - strlen(full));
59 strncat(full, show_num(tmp->max), 254 - strlen(full));
60 } END_FOR_EACH_PTR(tmp);
61 return alloc_sname(full);
64 void get_value_ranges(char *value, struct range_list **rl)
66 long long val1, val2;
67 char *start;
68 char *c;
70 *rl = NULL;
72 c = value;
73 while (*c) {
74 if (*c == '(')
75 c++;
76 start = c;
77 if (!strncmp(start, "min", 3)) {
78 val1 = LLONG_MIN;
79 c += 3;
80 } else {
81 while (*c && *c != ',' && *c != '-')
82 c++;
83 val1 = strtoll(start, &c, 10);
85 if (*c == ')')
86 c++;
87 if (!*c) {
88 add_range(rl, val1, val1);
89 break;
91 if (*c == ',') {
92 add_range(rl, val1, val1);
93 c++;
94 start = c;
95 continue;
97 c++; /* skip the dash in eg. 4-5 */
98 if (*c == '(')
99 c++;
100 start = c;
101 if (!strncmp(start, "max", 3)) {
102 val2 = LLONG_MAX;
103 c += 3;
104 } else {
106 while (*c && *c != ',' && *c != '-')
107 c++;
108 val2 = strtoll(start, &c, 10);
110 add_range(rl, val1, val2);
111 if (!*c)
112 break;
113 if (*c == ')')
114 c++;
115 c++; /* skip the comma in eg: 4-5,7 */
120 static struct data_range range_zero = {
121 .min = 0,
122 .max = 0,
125 static struct data_range range_one = {
126 .min = 1,
127 .max = 1,
130 int is_whole_range_rl(struct range_list *rl)
132 struct data_range *drange;
134 if (ptr_list_empty(rl))
135 return 1;
136 drange = first_ptr_list((struct ptr_list *)rl);
137 if (drange->min == whole_range.min && drange->max == whole_range.max)
138 return 1;
139 return 0;
142 long long rl_min(struct range_list *rl)
144 struct data_range *drange;
146 if (ptr_list_empty(rl))
147 return whole_range.min;
148 drange = first_ptr_list((struct ptr_list *)rl);
149 return drange->min;
152 long long rl_max(struct range_list *rl)
154 struct data_range *drange;
156 if (ptr_list_empty(rl))
157 return whole_range.max;
158 drange = last_ptr_list((struct ptr_list *)rl);
159 return drange->max;
162 static struct data_range *alloc_range_helper(long long min, long long max, int perm)
164 struct data_range *ret;
166 if (min > max) {
167 sm_msg("Error invalid range %lld to %lld", min, max);
168 min = whole_range.min;
169 max = whole_range.max;
171 if (min == whole_range.min && max == whole_range.max)
172 return &whole_range;
173 if (min == 0 && max == 0)
174 return &range_zero;
175 if (min == 1 && max == 1)
176 return &range_one;
178 if (perm)
179 ret = __alloc_perm_data_range(0);
180 else
181 ret = __alloc_data_range(0);
182 ret->min = min;
183 ret->max = max;
184 return ret;
187 struct data_range *alloc_range(long long min, long long max)
189 return alloc_range_helper(min, max, 0);
192 struct data_range *alloc_range_perm(long long min, long long max)
194 return alloc_range_helper(min, max, 1);
197 struct range_list *alloc_range_list(long long min, long long max)
199 struct range_list *rl = NULL;
201 add_range(&rl, min, max);
202 return rl;
205 struct range_list *whole_range_list(void)
207 return alloc_range_list(whole_range.min, whole_range.max);
210 void add_range(struct range_list **list, long long min, long long max)
212 struct data_range *tmp = NULL;
213 struct data_range *new = NULL;
214 int check_next = 0;
217 * FIXME: This has a problem merging a range_list like: min-0,3-max
218 * with a range like 1-2. You end up with min-2,3-max instead of
219 * just min-max.
221 FOR_EACH_PTR(*list, tmp) {
222 if (check_next) {
223 /* Sometimes we overlap with more than one range
224 so we have to delete or modify the next range. */
225 if (max + 1 == tmp->min) {
226 /* join 2 ranges here */
227 new->max = tmp->max;
228 DELETE_CURRENT_PTR(tmp);
229 return;
232 /* Doesn't overlap with the next one. */
233 if (max < tmp->min)
234 return;
235 /* Partially overlaps with the next one. */
236 if (max < tmp->max) {
237 tmp->min = max + 1;
238 return;
240 /* Completely overlaps with the next one. */
241 if (max >= tmp->max) {
242 DELETE_CURRENT_PTR(tmp);
243 /* there could be more ranges to delete */
244 continue;
247 if (max != whole_range.max && max + 1 == tmp->min) {
248 /* join 2 ranges into a big range */
249 new = alloc_range(min, tmp->max);
250 REPLACE_CURRENT_PTR(tmp, new);
251 return;
253 if (max < tmp->min) { /* new range entirely below */
254 new = alloc_range(min, max);
255 INSERT_CURRENT(new, tmp);
256 return;
258 if (min < tmp->min) { /* new range partially below */
259 if (max < tmp->max)
260 max = tmp->max;
261 else
262 check_next = 1;
263 new = alloc_range(min, max);
264 REPLACE_CURRENT_PTR(tmp, new);
265 if (!check_next)
266 return;
267 continue;
269 if (max <= tmp->max) /* new range already included */
270 return;
271 if (min <= tmp->max) { /* new range partially above */
272 min = tmp->min;
273 new = alloc_range(min, max);
274 REPLACE_CURRENT_PTR(tmp, new);
275 check_next = 1;
276 continue;
278 if (min != whole_range.min && min - 1 == tmp->max) {
279 /* join 2 ranges into a big range */
280 new = alloc_range(tmp->min, max);
281 REPLACE_CURRENT_PTR(tmp, new);
282 check_next = 1;
283 continue;
285 /* the new range is entirely above the existing ranges */
286 } END_FOR_EACH_PTR(tmp);
287 if (check_next)
288 return;
289 new = alloc_range(min, max);
290 add_ptr_list(list, new);
293 struct range_list *clone_range_list(struct range_list *list)
295 struct data_range *tmp;
296 struct range_list *ret = NULL;
298 FOR_EACH_PTR(list, tmp) {
299 add_ptr_list(&ret, tmp);
300 } END_FOR_EACH_PTR(tmp);
301 return ret;
304 struct range_list *clone_permanent(struct range_list *list)
306 struct data_range *tmp;
307 struct data_range *new;
308 struct range_list *ret = NULL;
310 FOR_EACH_PTR(list, tmp) {
311 new = alloc_range_perm(tmp->min, tmp->max);
312 add_ptr_list(&ret, new);
313 } END_FOR_EACH_PTR(tmp);
314 return ret;
317 struct range_list *range_list_union(struct range_list *one, struct range_list *two)
319 struct data_range *tmp;
320 struct range_list *ret = NULL;
322 FOR_EACH_PTR(one, tmp) {
323 add_range(&ret, tmp->min, tmp->max);
324 } END_FOR_EACH_PTR(tmp);
325 FOR_EACH_PTR(two, tmp) {
326 add_range(&ret, tmp->min, tmp->max);
327 } END_FOR_EACH_PTR(tmp);
328 return ret;
331 struct range_list *remove_range(struct range_list *list, long long min, long long max)
333 struct data_range *tmp;
334 struct range_list *ret = NULL;
336 FOR_EACH_PTR(list, tmp) {
337 if (tmp->max < min) {
338 add_range(&ret, tmp->min, tmp->max);
339 continue;
341 if (tmp->min > max) {
342 add_range(&ret, tmp->min, tmp->max);
343 continue;
345 if (tmp->min >= min && tmp->max <= max)
346 continue;
347 if (tmp->min >= min) {
348 add_range(&ret, max + 1, tmp->max);
349 } else if (tmp->max <= max) {
350 add_range(&ret, tmp->min, min - 1);
351 } else {
352 add_range(&ret, tmp->min, min - 1);
353 add_range(&ret, max + 1, tmp->max);
355 } END_FOR_EACH_PTR(tmp);
356 return ret;
360 * if it can be only one and only value return 1, else return 0
362 int estate_get_single_value(struct smatch_state *state, long long *val)
364 struct data_info *dinfo;
365 struct data_range *tmp;
366 int count = 0;
368 dinfo = get_dinfo(state);
369 if (!dinfo || dinfo->type != DATA_RANGE)
370 return 0;
372 FOR_EACH_PTR(dinfo->value_ranges, tmp) {
373 if (!count++) {
374 if (tmp->min != tmp->max)
375 return 0;
376 *val = tmp->min;
377 } else {
378 return 0;
380 } END_FOR_EACH_PTR(tmp);
381 return count;
384 int true_comparison_range(struct data_range *left, int comparison, struct data_range *right)
386 switch (comparison) {
387 case '<':
388 case SPECIAL_UNSIGNED_LT:
389 if (left->min < right->max)
390 return 1;
391 return 0;
392 case SPECIAL_UNSIGNED_LTE:
393 case SPECIAL_LTE:
394 if (left->min <= right->max)
395 return 1;
396 return 0;
397 case SPECIAL_EQUAL:
398 if (left->max < right->min)
399 return 0;
400 if (left->min > right->max)
401 return 0;
402 return 1;
403 case SPECIAL_UNSIGNED_GTE:
404 case SPECIAL_GTE:
405 if (left->max >= right->min)
406 return 1;
407 return 0;
408 case '>':
409 case SPECIAL_UNSIGNED_GT:
410 if (left->max > right->min)
411 return 1;
412 return 0;
413 case SPECIAL_NOTEQUAL:
414 if (left->min != left->max)
415 return 1;
416 if (right->min != right->max)
417 return 1;
418 if (left->min != right->min)
419 return 1;
420 return 0;
421 default:
422 sm_msg("unhandled comparison %d\n", comparison);
423 return 0;
425 return 0;
428 int true_comparison_range_lr(int comparison, struct data_range *var, struct data_range *val, int left)
430 if (left)
431 return true_comparison_range(var, comparison, val);
432 else
433 return true_comparison_range(val, comparison, var);
436 static int false_comparison_range(struct data_range *left, int comparison, struct data_range *right)
438 switch (comparison) {
439 case '<':
440 case SPECIAL_UNSIGNED_LT:
441 if (left->max >= right->min)
442 return 1;
443 return 0;
444 case SPECIAL_UNSIGNED_LTE:
445 case SPECIAL_LTE:
446 if (left->max > right->min)
447 return 1;
448 return 0;
449 case SPECIAL_EQUAL:
450 if (left->min != left->max)
451 return 1;
452 if (right->min != right->max)
453 return 1;
454 if (left->min != right->min)
455 return 1;
456 return 0;
457 case SPECIAL_UNSIGNED_GTE:
458 case SPECIAL_GTE:
459 if (left->min < right->max)
460 return 1;
461 return 0;
462 case '>':
463 case SPECIAL_UNSIGNED_GT:
464 if (left->min <= right->max)
465 return 1;
466 return 0;
467 case SPECIAL_NOTEQUAL:
468 if (left->max < right->min)
469 return 0;
470 if (left->min > right->max)
471 return 0;
472 return 1;
473 default:
474 sm_msg("unhandled comparison %d\n", comparison);
475 return 0;
477 return 0;
480 int false_comparison_range_lr(int comparison, struct data_range *var, struct data_range *val, int left)
482 if (left)
483 return false_comparison_range(var, comparison, val);
484 else
485 return false_comparison_range(val, comparison, var);
488 int possibly_true(struct expression *left, int comparison, struct expression *right)
490 struct range_list *rl_left, *rl_right;
491 struct data_range *tmp_left, *tmp_right;
493 if (!get_implied_range_list(left, &rl_left))
494 return 1;
495 if (!get_implied_range_list(right, &rl_right))
496 return 1;
498 FOR_EACH_PTR(rl_left, tmp_left) {
499 FOR_EACH_PTR(rl_right, tmp_right) {
500 if (true_comparison_range(tmp_left, comparison, tmp_right))
501 return 1;
502 } END_FOR_EACH_PTR(tmp_right);
503 } END_FOR_EACH_PTR(tmp_left);
504 return 0;
507 int possibly_false(struct expression *left, int comparison, struct expression *right)
509 struct range_list *rl_left, *rl_right;
510 struct data_range *tmp_left, *tmp_right;
512 if (!get_implied_range_list(left, &rl_left))
513 return 1;
514 if (!get_implied_range_list(right, &rl_right))
515 return 1;
517 FOR_EACH_PTR(rl_left, tmp_left) {
518 FOR_EACH_PTR(rl_right, tmp_right) {
519 if (false_comparison_range(tmp_left, comparison, tmp_right))
520 return 1;
521 } END_FOR_EACH_PTR(tmp_right);
522 } END_FOR_EACH_PTR(tmp_left);
523 return 0;
526 int possibly_true_range_lists(struct range_list *left_ranges, int comparison, struct range_list *right_ranges)
528 struct data_range *left_tmp, *right_tmp;
530 if (!left_ranges || !right_ranges)
531 return 1;
533 FOR_EACH_PTR(left_ranges, left_tmp) {
534 FOR_EACH_PTR(right_ranges, right_tmp) {
535 if (true_comparison_range(left_tmp, comparison, right_tmp))
536 return 1;
537 } END_FOR_EACH_PTR(right_tmp);
538 } END_FOR_EACH_PTR(left_tmp);
539 return 0;
542 int possibly_false_range_lists(struct range_list *left_ranges, int comparison, struct range_list *right_ranges)
544 struct data_range *left_tmp, *right_tmp;
546 if (!left_ranges || !right_ranges)
547 return 1;
549 FOR_EACH_PTR(left_ranges, left_tmp) {
550 FOR_EACH_PTR(right_ranges, right_tmp) {
551 if (false_comparison_range(left_tmp, comparison, right_tmp))
552 return 1;
553 } END_FOR_EACH_PTR(right_tmp);
554 } END_FOR_EACH_PTR(left_tmp);
555 return 0;
558 void tack_on(struct range_list **list, struct data_range *drange)
560 add_ptr_list(list, drange);
563 int in_list_exact(struct range_list *list, struct data_range *drange)
565 struct data_range *tmp;
567 FOR_EACH_PTR(list, tmp) {
568 if (tmp->min == drange->min && tmp->max == drange->max)
569 return 1;
570 } END_FOR_EACH_PTR(tmp);
571 return 0;
575 void push_range_list(struct range_list_stack **rl_stack, struct range_list *rl)
577 add_ptr_list(rl_stack, rl);
580 struct range_list *pop_range_list(struct range_list_stack **rl_stack)
582 struct range_list *rl;
584 rl = last_ptr_list((struct ptr_list *)*rl_stack);
585 delete_ptr_list_last((struct ptr_list **)rl_stack);
586 return rl;
589 struct range_list *top_range_list(struct range_list_stack *rl_stack)
591 struct range_list *rl;
593 rl = last_ptr_list((struct ptr_list *)rl_stack);
594 return rl;
597 void filter_top_range_list(struct range_list_stack **rl_stack, long long num)
599 struct range_list *rl;
601 rl = pop_range_list(rl_stack);
602 rl = remove_range(rl, num, num);
603 push_range_list(rl_stack, rl);
606 void free_range_list(struct range_list **rlist)
608 __free_ptr_list((struct ptr_list **)rlist);
611 static void free_single_dinfo(struct data_info *dinfo)
613 if (dinfo->type == DATA_RANGE)
614 free_range_list(&dinfo->value_ranges);
617 static void free_dinfos(struct allocation_blob *blob)
619 unsigned int size = sizeof(struct data_info);
620 unsigned int offset = 0;
622 while (offset < blob->offset) {
623 free_single_dinfo((struct data_info *)(blob->data + offset));
624 offset += size;
628 void free_data_info_allocs(void)
630 struct allocator_struct *desc = &data_info_allocator;
631 struct allocation_blob *blob = desc->blobs;
633 desc->blobs = NULL;
634 desc->allocations = 0;
635 desc->total_bytes = 0;
636 desc->useful_bytes = 0;
637 desc->freelist = NULL;
638 while (blob) {
639 struct allocation_blob *next = blob->next;
640 free_dinfos(blob);
641 blob_free(blob, desc->chunking);
642 blob = next;
644 clear_data_range_alloc();