2 * sparse/smatch_ranges.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
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");
29 snprintf(buff
, 255, "(%lld)", num
);
31 snprintf(buff
, 255, "%lld", num
);
37 char *show_ranges(struct range_list
*list
)
39 struct data_range
*tmp
;
45 FOR_EACH_PTR(list
, tmp
) {
47 strncat(full
, ",", 254 - strlen(full
));
48 if (tmp
->min
== tmp
->max
) {
49 strncat(full
, show_num(tmp
->min
), 254 - strlen(full
));
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
= {
64 static struct data_range range_one
= {
69 struct data_range
*alloc_range(long long min
, long long max
)
71 struct data_range
*ret
;
74 printf("Error invalid range %lld to %lld\n", min
, max
);
76 if (min
== whole_range
.min
&& max
== whole_range
.max
)
78 if (min
== 0 && max
== 0)
80 if (min
== 1 && max
== 1)
82 ret
= __alloc_data_range(0);
88 struct data_range
*alloc_range_perm(long long min
, long long max
)
90 struct data_range
*ret
;
93 printf("Error invalid range %lld to %lld\n", min
, max
);
95 if (min
== whole_range
.min
&& max
== whole_range
.max
)
97 if (min
== 0 && max
== 0)
99 if (min
== 1 && max
== 1)
101 ret
= __alloc_perm_data_range(0);
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
);
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
;
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;
134 FOR_EACH_PTR(*list
, tmp
) {
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. */
142 /* Partially overlaps with the next one. */
143 if (max
< tmp
->max
) {
147 /* Completely overlaps with the next one. */
148 if (max
> tmp
->max
) {
149 DELETE_CURRENT_PTR(tmp
);
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);
159 if (max
< tmp
->min
) { /* new range entirely below */
160 new = alloc_range(min
, max
);
161 INSERT_CURRENT(new, tmp
);
164 if (min
< tmp
->min
) { /* new range partially below */
169 new = alloc_range(min
, max
);
170 REPLACE_CURRENT_PTR(tmp
, new);
174 if (max
<= tmp
->max
) /* new range already included */
176 if (min
<= tmp
->max
) { /* new range partially above */
178 new = alloc_range(min
, max
);
179 REPLACE_CURRENT_PTR(tmp
, new);
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);
188 } END_FOR_EACH_PTR(tmp
);
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
);
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 */
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
);
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
);
233 if (tmp
->min
> max
) {
234 add_range(&ret
, tmp
->min
, tmp
->max
);
237 if (tmp
->min
>= min
&& tmp
->max
<= max
)
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);
244 add_range(&ret
, tmp
->min
, min
- 1);
245 add_range(&ret
, max
+ 1, tmp
->max
);
247 } END_FOR_EACH_PTR(tmp
);
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
);
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
);
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
;
279 if (dinfo
->type
!= DATA_RANGE
)
282 FOR_EACH_PTR(dinfo
->value_ranges
, tmp
) {
284 if (tmp
->min
!= tmp
->max
)
290 } END_FOR_EACH_PTR(tmp
);
294 int true_comparison_range(struct data_range
*left
, int comparison
, struct data_range
*right
)
298 case SPECIAL_UNSIGNED_LT
:
299 if (left
->min
< right
->max
)
302 case SPECIAL_UNSIGNED_LTE
:
304 if (left
->min
<= right
->max
)
308 if (left
->max
< right
->min
)
310 if (left
->min
> right
->max
)
313 case SPECIAL_UNSIGNED_GTE
:
315 if (left
->max
>= right
->min
)
319 case SPECIAL_UNSIGNED_GT
:
320 if (left
->max
> right
->min
)
323 case SPECIAL_NOTEQUAL
:
324 if (left
->min
!= left
->max
)
326 if (right
->min
!= right
->max
)
328 if (left
->min
!= right
->min
)
332 sm_msg("unhandled comparison %d\n", comparison
);
338 int true_comparison_range_lr(int comparison
, struct data_range
*var
, struct data_range
*val
, int left
)
341 return true_comparison_range(var
, comparison
, val
);
343 return true_comparison_range(val
, comparison
, var
);
346 static int false_comparison_range(struct data_range
*left
, int comparison
, struct data_range
*right
)
350 case SPECIAL_UNSIGNED_LT
:
351 if (left
->max
>= right
->min
)
354 case SPECIAL_UNSIGNED_LTE
:
356 if (left
->max
> right
->min
)
360 if (left
->min
!= left
->max
)
362 if (right
->min
!= right
->max
)
364 if (left
->min
!= right
->min
)
367 case SPECIAL_UNSIGNED_GTE
:
369 if (left
->min
< right
->max
)
373 case SPECIAL_UNSIGNED_GT
:
374 if (left
->min
<= right
->max
)
377 case SPECIAL_NOTEQUAL
:
378 if (left
->max
< right
->min
)
380 if (left
->min
> right
->max
)
384 sm_msg("unhandled comparison %d\n", comparison
);
390 int false_comparison_range_lr(int comparison
, struct data_range
*var
, struct data_range
*val
, int left
)
393 return false_comparison_range(var
, comparison
, val
);
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
;
406 FOR_EACH_PTR(dinfo
->value_ranges
, tmp
) {
407 if (true_comparison_range_lr(comparison
, tmp
, &drange
, left
))
409 } END_FOR_EACH_PTR(tmp
);
413 int possibly_false(int comparison
, struct data_info
*dinfo
, int num
, int left
)
415 struct data_range
*tmp
;
416 struct data_range drange
;
421 FOR_EACH_PTR(dinfo
->value_ranges
, tmp
) {
422 if (false_comparison_range_lr(comparison
, tmp
, &drange
, left
))
424 } END_FOR_EACH_PTR(tmp
);
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
))
436 } END_FOR_EACH_PTR(tmp2
);
437 } END_FOR_EACH_PTR(tmp
);
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
))
449 } END_FOR_EACH_PTR(tmp2
);
450 } END_FOR_EACH_PTR(tmp
);
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
)
466 } END_FOR_EACH_PTR(tmp
);
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
);
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
);
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
));
519 void free_data_info_allocs(void)
521 struct allocator_struct
*desc
= &data_info_allocator
;
522 struct allocation_blob
*blob
= desc
->blobs
;
525 desc
->allocations
= 0;
526 desc
->total_bytes
= 0;
527 desc
->useful_bytes
= 0;
528 desc
->freelist
= NULL
;
530 struct allocation_blob
*next
= blob
->next
;
532 blob_free(blob
, desc
->chunking
);
535 clear_data_range_alloc();