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 int is_whole_range_rl(struct range_list
*rl
)
71 struct data_range
*drange
;
73 if (ptr_list_empty(rl
))
75 drange
= first_ptr_list((struct ptr_list
*)rl
);
76 if (drange
->min
== whole_range
.min
&& drange
->max
== whole_range
.max
)
81 struct data_range
*alloc_range(long long min
, long long max
)
83 struct data_range
*ret
;
86 printf("Error invalid range %lld to %lld\n", min
, max
);
88 if (min
== whole_range
.min
&& max
== whole_range
.max
)
90 if (min
== 0 && max
== 0)
92 if (min
== 1 && max
== 1)
94 ret
= __alloc_data_range(0);
100 struct data_range
*alloc_range_perm(long long min
, long long max
)
102 struct data_range
*ret
;
105 printf("Error invalid range %lld to %lld\n", min
, max
);
107 if (min
== whole_range
.min
&& max
== whole_range
.max
)
109 if (min
== 0 && max
== 0)
111 if (min
== 1 && max
== 1)
113 ret
= __alloc_perm_data_range(0);
119 void add_range(struct range_list
**list
, long long min
, long long max
)
121 struct data_range
*tmp
= NULL
;
122 struct data_range
*new = NULL
;
125 FOR_EACH_PTR(*list
, tmp
) {
127 /* Sometimes we overlap with more than one range
128 so we have to delete or modify the next range. */
129 if (max
+ 1 == tmp
->min
) {
130 /* join 2 ranges here */
132 DELETE_CURRENT_PTR(tmp
);
136 /* Doesn't overlap with the next one. */
139 /* Partially overlaps with the next one. */
140 if (max
< tmp
->max
) {
144 /* Completely overlaps with the next one. */
145 if (max
>= tmp
->max
) {
146 DELETE_CURRENT_PTR(tmp
);
147 /* there could be more ranges to delete */
151 if (max
!= whole_range
.max
&& max
+ 1 == tmp
->min
) {
152 /* join 2 ranges into a big range */
153 new = alloc_range(min
, tmp
->max
);
154 REPLACE_CURRENT_PTR(tmp
, new);
157 if (max
< tmp
->min
) { /* new range entirely below */
158 new = alloc_range(min
, max
);
159 INSERT_CURRENT(new, tmp
);
162 if (min
< tmp
->min
) { /* new range partially below */
167 new = alloc_range(min
, max
);
168 REPLACE_CURRENT_PTR(tmp
, new);
173 if (max
<= tmp
->max
) /* new range already included */
175 if (min
<= tmp
->max
) { /* new range partially above */
177 new = alloc_range(min
, max
);
178 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);
189 /* the new range is entirely above the existing ranges */
190 } END_FOR_EACH_PTR(tmp
);
193 new = alloc_range(min
, max
);
194 add_ptr_list(list
, new);
197 struct range_list
*clone_range_list(struct range_list
*list
)
199 struct data_range
*tmp
;
200 struct range_list
*ret
= NULL
;
202 FOR_EACH_PTR(list
, tmp
) {
203 add_ptr_list(&ret
, tmp
);
204 } END_FOR_EACH_PTR(tmp
);
208 struct range_list
*range_list_union(struct range_list
*one
, struct range_list
*two
)
210 struct data_range
*tmp
;
211 struct range_list
*ret
= NULL
;
213 FOR_EACH_PTR(one
, tmp
) {
214 add_range(&ret
, tmp
->min
, tmp
->max
);
215 } END_FOR_EACH_PTR(tmp
);
216 FOR_EACH_PTR(two
, tmp
) {
217 add_range(&ret
, tmp
->min
, tmp
->max
);
218 } END_FOR_EACH_PTR(tmp
);
222 struct range_list
*remove_range(struct range_list
*list
, long long min
, long long max
)
224 struct data_range
*tmp
;
225 struct range_list
*ret
= NULL
;
227 FOR_EACH_PTR(list
, tmp
) {
228 if (tmp
->max
< min
) {
229 add_range(&ret
, tmp
->min
, tmp
->max
);
232 if (tmp
->min
> max
) {
233 add_range(&ret
, tmp
->min
, tmp
->max
);
236 if (tmp
->min
>= min
&& tmp
->max
<= max
)
238 if (tmp
->min
>= min
) {
239 add_range(&ret
, max
+ 1, tmp
->max
);
240 } else if (tmp
->max
<= max
) {
241 add_range(&ret
, tmp
->min
, min
- 1);
243 add_range(&ret
, tmp
->min
, min
- 1);
244 add_range(&ret
, max
+ 1, tmp
->max
);
246 } END_FOR_EACH_PTR(tmp
);
250 long long get_dinfo_min(struct data_info
*dinfo
)
252 struct data_range
*drange
;
254 if (!dinfo
|| !dinfo
->value_ranges
)
255 return whole_range
.min
;
256 drange
= first_ptr_list((struct ptr_list
*)dinfo
->value_ranges
);
260 long long get_dinfo_max(struct data_info
*dinfo
)
262 struct data_range
*drange
;
264 if (!dinfo
|| !dinfo
->value_ranges
)
265 return whole_range
.max
;
266 drange
= last_ptr_list((struct ptr_list
*)dinfo
->value_ranges
);
271 * if it can be only one and only value return 1, else return 0
273 int get_single_value_from_dinfo(struct data_info
*dinfo
, long long *val
)
275 struct data_range
*tmp
;
278 if (dinfo
->type
!= DATA_RANGE
)
281 FOR_EACH_PTR(dinfo
->value_ranges
, tmp
) {
283 if (tmp
->min
!= tmp
->max
)
289 } END_FOR_EACH_PTR(tmp
);
293 int true_comparison_range(struct data_range
*left
, int comparison
, struct data_range
*right
)
295 switch (comparison
) {
297 case SPECIAL_UNSIGNED_LT
:
298 if (left
->min
< right
->max
)
301 case SPECIAL_UNSIGNED_LTE
:
303 if (left
->min
<= right
->max
)
307 if (left
->max
< right
->min
)
309 if (left
->min
> right
->max
)
312 case SPECIAL_UNSIGNED_GTE
:
314 if (left
->max
>= right
->min
)
318 case SPECIAL_UNSIGNED_GT
:
319 if (left
->max
> right
->min
)
322 case SPECIAL_NOTEQUAL
:
323 if (left
->min
!= left
->max
)
325 if (right
->min
!= right
->max
)
327 if (left
->min
!= right
->min
)
331 sm_msg("unhandled comparison %d\n", comparison
);
337 int true_comparison_range_lr(int comparison
, struct data_range
*var
, struct data_range
*val
, int left
)
340 return true_comparison_range(var
, comparison
, val
);
342 return true_comparison_range(val
, comparison
, var
);
345 static int false_comparison_range(struct data_range
*left
, int comparison
, struct data_range
*right
)
347 switch (comparison
) {
349 case SPECIAL_UNSIGNED_LT
:
350 if (left
->max
>= right
->min
)
353 case SPECIAL_UNSIGNED_LTE
:
355 if (left
->max
> right
->min
)
359 if (left
->min
!= left
->max
)
361 if (right
->min
!= right
->max
)
363 if (left
->min
!= right
->min
)
366 case SPECIAL_UNSIGNED_GTE
:
368 if (left
->min
< right
->max
)
372 case SPECIAL_UNSIGNED_GT
:
373 if (left
->min
<= right
->max
)
376 case SPECIAL_NOTEQUAL
:
377 if (left
->max
< right
->min
)
379 if (left
->min
> right
->max
)
383 sm_msg("unhandled comparison %d\n", comparison
);
389 int false_comparison_range_lr(int comparison
, struct data_range
*var
, struct data_range
*val
, int left
)
392 return false_comparison_range(var
, comparison
, val
);
394 return false_comparison_range(val
, comparison
, var
);
397 int possibly_true(int comparison
, struct data_info
*dinfo
, long long num
, int left
)
399 struct data_range
*tmp
;
400 struct data_range drange
;
408 FOR_EACH_PTR(dinfo
->value_ranges
, tmp
) {
409 if (true_comparison_range_lr(comparison
, tmp
, &drange
, left
))
411 } END_FOR_EACH_PTR(tmp
);
415 int possibly_false(int comparison
, struct data_info
*dinfo
, long long num
, int left
)
417 struct data_range
*tmp
;
418 struct data_range drange
;
426 FOR_EACH_PTR(dinfo
->value_ranges
, tmp
) {
427 if (false_comparison_range_lr(comparison
, tmp
, &drange
, left
))
429 } END_FOR_EACH_PTR(tmp
);
433 int possibly_true_range_lists(struct range_list
*left_ranges
, int comparison
, struct range_list
*right_ranges
)
435 struct data_range
*left_tmp
, *right_tmp
;
437 if (!left_ranges
|| !right_ranges
)
440 FOR_EACH_PTR(left_ranges
, left_tmp
) {
441 FOR_EACH_PTR(right_ranges
, right_tmp
) {
442 if (true_comparison_range(left_tmp
, comparison
, right_tmp
))
444 } END_FOR_EACH_PTR(right_tmp
);
445 } END_FOR_EACH_PTR(left_tmp
);
449 int possibly_true_range_list_lr(int comparison
, struct data_info
*dinfo
, struct range_list
*values
, int left
)
451 struct data_range
*tmp
, *tmp2
;
453 FOR_EACH_PTR(dinfo
->value_ranges
, tmp
) {
454 FOR_EACH_PTR(values
, tmp2
) {
455 if (true_comparison_range_lr(comparison
, tmp
, tmp2
, left
))
457 } END_FOR_EACH_PTR(tmp2
);
458 } END_FOR_EACH_PTR(tmp
);
462 int possibly_false_range_lists(struct range_list
*left_ranges
, int comparison
, struct range_list
*right_ranges
)
464 struct data_range
*left_tmp
, *right_tmp
;
466 if (!left_ranges
|| !right_ranges
)
469 FOR_EACH_PTR(left_ranges
, left_tmp
) {
470 FOR_EACH_PTR(right_ranges
, right_tmp
) {
471 if (false_comparison_range(left_tmp
, comparison
, right_tmp
))
473 } END_FOR_EACH_PTR(right_tmp
);
474 } END_FOR_EACH_PTR(left_tmp
);
478 int possibly_false_range_list_lr(int comparison
, struct data_info
*dinfo
, struct range_list
*values
, int left
)
480 struct data_range
*tmp
, *tmp2
;
482 FOR_EACH_PTR(dinfo
->value_ranges
, tmp
) {
483 FOR_EACH_PTR(values
, tmp2
) {
484 if (false_comparison_range_lr(comparison
, tmp
, tmp2
, left
))
486 } END_FOR_EACH_PTR(tmp2
);
487 } END_FOR_EACH_PTR(tmp
);
491 void tack_on(struct range_list
**list
, struct data_range
*drange
)
493 add_ptr_list(list
, drange
);
496 int in_list_exact(struct range_list
*list
, struct data_range
*drange
)
498 struct data_range
*tmp
;
500 FOR_EACH_PTR(list
, tmp
) {
501 if (tmp
->min
== drange
->min
&& tmp
->max
== drange
->max
)
503 } END_FOR_EACH_PTR(tmp
);
508 void push_range_list(struct range_list_stack
**rl_stack
, struct range_list
*rl
)
510 add_ptr_list(rl_stack
, rl
);
513 struct range_list
*pop_range_list(struct range_list_stack
**rl_stack
)
515 struct range_list
*rl
;
517 rl
= last_ptr_list((struct ptr_list
*)*rl_stack
);
518 delete_ptr_list_last((struct ptr_list
**)rl_stack
);
522 struct range_list
*top_range_list(struct range_list_stack
*rl_stack
)
524 struct range_list
*rl
;
526 rl
= last_ptr_list((struct ptr_list
*)rl_stack
);
530 void filter_top_range_list(struct range_list_stack
**rl_stack
, long long num
)
532 struct range_list
*rl
;
534 rl
= pop_range_list(rl_stack
);
535 rl
= remove_range(rl
, num
, num
);
536 push_range_list(rl_stack
, rl
);
539 void free_range_list(struct range_list
**rlist
)
541 __free_ptr_list((struct ptr_list
**)rlist
);
544 static void free_single_dinfo(struct data_info
*dinfo
)
546 if (dinfo
->type
== DATA_RANGE
)
547 free_range_list(&dinfo
->value_ranges
);
550 static void free_dinfos(struct allocation_blob
*blob
)
552 unsigned int size
= sizeof(struct data_info
);
553 unsigned int offset
= 0;
555 while (offset
< blob
->offset
) {
556 free_single_dinfo((struct data_info
*)(blob
->data
+ offset
));
561 void free_data_info_allocs(void)
563 struct allocator_struct
*desc
= &data_info_allocator
;
564 struct allocation_blob
*blob
= desc
->blobs
;
567 desc
->allocations
= 0;
568 desc
->total_bytes
= 0;
569 desc
->useful_bytes
= 0;
570 desc
->freelist
= NULL
;
572 struct allocation_blob
*next
= blob
->next
;
574 blob_free(blob
, desc
->chunking
);
577 clear_data_range_alloc();