ranges: initialize the pointer in get_value_ranges()
[smatch.git] / smatch_ranges.c
bloba38a304c6184117c5e7ec365300e322440542e12
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 void add_range(struct range_list **list, long long min, long long max)
207 struct data_range *tmp = NULL;
208 struct data_range *new = NULL;
209 int check_next = 0;
212 * FIXME: This has a problem merging a range_list like: min-0,3-max
213 * with a range like 1-2. You end up with min-2,3-max instead of
214 * just min-max.
216 FOR_EACH_PTR(*list, tmp) {
217 if (check_next) {
218 /* Sometimes we overlap with more than one range
219 so we have to delete or modify the next range. */
220 if (max + 1 == tmp->min) {
221 /* join 2 ranges here */
222 new->max = tmp->max;
223 DELETE_CURRENT_PTR(tmp);
224 return;
227 /* Doesn't overlap with the next one. */
228 if (max < tmp->min)
229 return;
230 /* Partially overlaps with the next one. */
231 if (max < tmp->max) {
232 tmp->min = max + 1;
233 return;
235 /* Completely overlaps with the next one. */
236 if (max >= tmp->max) {
237 DELETE_CURRENT_PTR(tmp);
238 /* there could be more ranges to delete */
239 continue;
242 if (max != whole_range.max && max + 1 == tmp->min) {
243 /* join 2 ranges into a big range */
244 new = alloc_range(min, tmp->max);
245 REPLACE_CURRENT_PTR(tmp, new);
246 return;
248 if (max < tmp->min) { /* new range entirely below */
249 new = alloc_range(min, max);
250 INSERT_CURRENT(new, tmp);
251 return;
253 if (min < tmp->min) { /* new range partially below */
254 if (max < tmp->max)
255 max = tmp->max;
256 else
257 check_next = 1;
258 new = alloc_range(min, max);
259 REPLACE_CURRENT_PTR(tmp, new);
260 if (!check_next)
261 return;
262 continue;
264 if (max <= tmp->max) /* new range already included */
265 return;
266 if (min <= tmp->max) { /* new range partially above */
267 min = tmp->min;
268 new = alloc_range(min, max);
269 REPLACE_CURRENT_PTR(tmp, new);
270 check_next = 1;
271 continue;
273 if (min != whole_range.min && min - 1 == tmp->max) {
274 /* join 2 ranges into a big range */
275 new = alloc_range(tmp->min, max);
276 REPLACE_CURRENT_PTR(tmp, new);
277 check_next = 1;
278 continue;
280 /* the new range is entirely above the existing ranges */
281 } END_FOR_EACH_PTR(tmp);
282 if (check_next)
283 return;
284 new = alloc_range(min, max);
285 add_ptr_list(list, new);
288 struct range_list *clone_range_list(struct range_list *list)
290 struct data_range *tmp;
291 struct range_list *ret = NULL;
293 FOR_EACH_PTR(list, tmp) {
294 add_ptr_list(&ret, tmp);
295 } END_FOR_EACH_PTR(tmp);
296 return ret;
299 struct range_list *range_list_union(struct range_list *one, struct range_list *two)
301 struct data_range *tmp;
302 struct range_list *ret = NULL;
304 FOR_EACH_PTR(one, tmp) {
305 add_range(&ret, tmp->min, tmp->max);
306 } END_FOR_EACH_PTR(tmp);
307 FOR_EACH_PTR(two, tmp) {
308 add_range(&ret, tmp->min, tmp->max);
309 } END_FOR_EACH_PTR(tmp);
310 return ret;
313 struct range_list *remove_range(struct range_list *list, long long min, long long max)
315 struct data_range *tmp;
316 struct range_list *ret = NULL;
318 FOR_EACH_PTR(list, tmp) {
319 if (tmp->max < min) {
320 add_range(&ret, tmp->min, tmp->max);
321 continue;
323 if (tmp->min > max) {
324 add_range(&ret, tmp->min, tmp->max);
325 continue;
327 if (tmp->min >= min && tmp->max <= max)
328 continue;
329 if (tmp->min >= min) {
330 add_range(&ret, max + 1, tmp->max);
331 } else if (tmp->max <= max) {
332 add_range(&ret, tmp->min, min - 1);
333 } else {
334 add_range(&ret, tmp->min, min - 1);
335 add_range(&ret, max + 1, tmp->max);
337 } END_FOR_EACH_PTR(tmp);
338 return ret;
342 * if it can be only one and only value return 1, else return 0
344 int estate_get_single_value(struct smatch_state *state, long long *val)
346 struct data_info *dinfo;
347 struct data_range *tmp;
348 int count = 0;
350 dinfo = get_dinfo(state);
351 if (!dinfo || dinfo->type != DATA_RANGE)
352 return 0;
354 FOR_EACH_PTR(dinfo->value_ranges, tmp) {
355 if (!count++) {
356 if (tmp->min != tmp->max)
357 return 0;
358 *val = tmp->min;
359 } else {
360 return 0;
362 } END_FOR_EACH_PTR(tmp);
363 return count;
366 int true_comparison_range(struct data_range *left, int comparison, struct data_range *right)
368 switch (comparison) {
369 case '<':
370 case SPECIAL_UNSIGNED_LT:
371 if (left->min < right->max)
372 return 1;
373 return 0;
374 case SPECIAL_UNSIGNED_LTE:
375 case SPECIAL_LTE:
376 if (left->min <= right->max)
377 return 1;
378 return 0;
379 case SPECIAL_EQUAL:
380 if (left->max < right->min)
381 return 0;
382 if (left->min > right->max)
383 return 0;
384 return 1;
385 case SPECIAL_UNSIGNED_GTE:
386 case SPECIAL_GTE:
387 if (left->max >= right->min)
388 return 1;
389 return 0;
390 case '>':
391 case SPECIAL_UNSIGNED_GT:
392 if (left->max > right->min)
393 return 1;
394 return 0;
395 case SPECIAL_NOTEQUAL:
396 if (left->min != left->max)
397 return 1;
398 if (right->min != right->max)
399 return 1;
400 if (left->min != right->min)
401 return 1;
402 return 0;
403 default:
404 sm_msg("unhandled comparison %d\n", comparison);
405 return 0;
407 return 0;
410 int true_comparison_range_lr(int comparison, struct data_range *var, struct data_range *val, int left)
412 if (left)
413 return true_comparison_range(var, comparison, val);
414 else
415 return true_comparison_range(val, comparison, var);
418 static int false_comparison_range(struct data_range *left, int comparison, struct data_range *right)
420 switch (comparison) {
421 case '<':
422 case SPECIAL_UNSIGNED_LT:
423 if (left->max >= right->min)
424 return 1;
425 return 0;
426 case SPECIAL_UNSIGNED_LTE:
427 case SPECIAL_LTE:
428 if (left->max > right->min)
429 return 1;
430 return 0;
431 case SPECIAL_EQUAL:
432 if (left->min != left->max)
433 return 1;
434 if (right->min != right->max)
435 return 1;
436 if (left->min != right->min)
437 return 1;
438 return 0;
439 case SPECIAL_UNSIGNED_GTE:
440 case SPECIAL_GTE:
441 if (left->min < right->max)
442 return 1;
443 return 0;
444 case '>':
445 case SPECIAL_UNSIGNED_GT:
446 if (left->min <= right->max)
447 return 1;
448 return 0;
449 case SPECIAL_NOTEQUAL:
450 if (left->max < right->min)
451 return 0;
452 if (left->min > right->max)
453 return 0;
454 return 1;
455 default:
456 sm_msg("unhandled comparison %d\n", comparison);
457 return 0;
459 return 0;
462 int false_comparison_range_lr(int comparison, struct data_range *var, struct data_range *val, int left)
464 if (left)
465 return false_comparison_range(var, comparison, val);
466 else
467 return false_comparison_range(val, comparison, var);
470 int possibly_true(struct expression *left, int comparison, struct expression *right)
472 struct range_list *rl_left, *rl_right;
473 struct data_range *tmp_left, *tmp_right;
475 if (!get_implied_range_list(left, &rl_left))
476 return 1;
477 if (!get_implied_range_list(right, &rl_right))
478 return 1;
480 FOR_EACH_PTR(rl_left, tmp_left) {
481 FOR_EACH_PTR(rl_right, tmp_right) {
482 if (true_comparison_range(tmp_left, comparison, tmp_right))
483 return 1;
484 } END_FOR_EACH_PTR(tmp_right);
485 } END_FOR_EACH_PTR(tmp_left);
486 return 0;
489 int possibly_false(struct expression *left, int comparison, struct expression *right)
491 struct range_list *rl_left, *rl_right;
492 struct data_range *tmp_left, *tmp_right;
494 if (!get_implied_range_list(left, &rl_left))
495 return 1;
496 if (!get_implied_range_list(right, &rl_right))
497 return 1;
499 FOR_EACH_PTR(rl_left, tmp_left) {
500 FOR_EACH_PTR(rl_right, tmp_right) {
501 if (false_comparison_range(tmp_left, comparison, tmp_right))
502 return 1;
503 } END_FOR_EACH_PTR(tmp_right);
504 } END_FOR_EACH_PTR(tmp_left);
505 return 0;
508 int possibly_true_range_lists(struct range_list *left_ranges, int comparison, struct range_list *right_ranges)
510 struct data_range *left_tmp, *right_tmp;
512 if (!left_ranges || !right_ranges)
513 return 1;
515 FOR_EACH_PTR(left_ranges, left_tmp) {
516 FOR_EACH_PTR(right_ranges, right_tmp) {
517 if (true_comparison_range(left_tmp, comparison, right_tmp))
518 return 1;
519 } END_FOR_EACH_PTR(right_tmp);
520 } END_FOR_EACH_PTR(left_tmp);
521 return 0;
524 int possibly_true_range_list_lr(int comparison, struct smatch_state *state, struct range_list *values, int left)
526 struct data_range *tmp, *tmp2;
528 FOR_EACH_PTR(estate_ranges(state), tmp) {
529 FOR_EACH_PTR(values, tmp2) {
530 if (true_comparison_range_lr(comparison, tmp, tmp2, left))
531 return 1;
532 } END_FOR_EACH_PTR(tmp2);
533 } END_FOR_EACH_PTR(tmp);
534 return 0;
537 int possibly_false_range_lists(struct range_list *left_ranges, int comparison, struct range_list *right_ranges)
539 struct data_range *left_tmp, *right_tmp;
541 if (!left_ranges || !right_ranges)
542 return 1;
544 FOR_EACH_PTR(left_ranges, left_tmp) {
545 FOR_EACH_PTR(right_ranges, right_tmp) {
546 if (false_comparison_range(left_tmp, comparison, right_tmp))
547 return 1;
548 } END_FOR_EACH_PTR(right_tmp);
549 } END_FOR_EACH_PTR(left_tmp);
550 return 0;
553 int possibly_false_range_list_lr(int comparison, struct smatch_state *state, struct range_list *values, int left)
555 struct data_range *tmp, *tmp2;
557 FOR_EACH_PTR(estate_ranges(state), tmp) {
558 FOR_EACH_PTR(values, tmp2) {
559 if (false_comparison_range_lr(comparison, tmp, tmp2, left))
560 return 1;
561 } END_FOR_EACH_PTR(tmp2);
562 } END_FOR_EACH_PTR(tmp);
563 return 0;
566 void tack_on(struct range_list **list, struct data_range *drange)
568 add_ptr_list(list, drange);
571 int in_list_exact(struct range_list *list, struct data_range *drange)
573 struct data_range *tmp;
575 FOR_EACH_PTR(list, tmp) {
576 if (tmp->min == drange->min && tmp->max == drange->max)
577 return 1;
578 } END_FOR_EACH_PTR(tmp);
579 return 0;
583 void push_range_list(struct range_list_stack **rl_stack, struct range_list *rl)
585 add_ptr_list(rl_stack, rl);
588 struct range_list *pop_range_list(struct range_list_stack **rl_stack)
590 struct range_list *rl;
592 rl = last_ptr_list((struct ptr_list *)*rl_stack);
593 delete_ptr_list_last((struct ptr_list **)rl_stack);
594 return rl;
597 struct range_list *top_range_list(struct range_list_stack *rl_stack)
599 struct range_list *rl;
601 rl = last_ptr_list((struct ptr_list *)rl_stack);
602 return rl;
605 void filter_top_range_list(struct range_list_stack **rl_stack, long long num)
607 struct range_list *rl;
609 rl = pop_range_list(rl_stack);
610 rl = remove_range(rl, num, num);
611 push_range_list(rl_stack, rl);
614 void free_range_list(struct range_list **rlist)
616 __free_ptr_list((struct ptr_list **)rlist);
619 static void free_single_dinfo(struct data_info *dinfo)
621 if (dinfo->type == DATA_RANGE)
622 free_range_list(&dinfo->value_ranges);
625 static void free_dinfos(struct allocation_blob *blob)
627 unsigned int size = sizeof(struct data_info);
628 unsigned int offset = 0;
630 while (offset < blob->offset) {
631 free_single_dinfo((struct data_info *)(blob->data + offset));
632 offset += size;
636 void free_data_info_allocs(void)
638 struct allocator_struct *desc = &data_info_allocator;
639 struct allocation_blob *blob = desc->blobs;
641 desc->blobs = NULL;
642 desc->allocations = 0;
643 desc->total_bytes = 0;
644 desc->useful_bytes = 0;
645 desc->freelist = NULL;
646 while (blob) {
647 struct allocation_blob *next = blob->next;
648 free_dinfos(blob);
649 blob_free(blob, desc->chunking);
650 blob = next;
652 clear_data_range_alloc();