2 * Copyright (C) 2010 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
20 #include "smatch_slist.h"
21 #include "smatch_extra.h"
23 static struct range_list
*_get_rl(struct expression
*expr
, int implied
);
24 static struct range_list
*handle_variable(struct expression
*expr
, int implied
);
25 static struct range_list
*(*custom_handle_variable
)(struct expression
*expr
);
27 static sval_t zero
= {.type
= &int_ctype
, {.value
= 0} };
28 static sval_t one
= {.type
= &int_ctype
, {.value
= 1} };
30 struct range_list
*rl_zero(void)
32 return alloc_rl(zero
, zero
);
35 struct range_list
*rl_one(void)
37 return alloc_rl(one
, one
);
48 static struct range_list
*last_stmt_rl(struct statement
*stmt
, int implied
)
53 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
54 if (stmt
->type
!= STMT_EXPRESSION
)
56 return _get_rl(stmt
->expression
, implied
);
59 static struct range_list
*handle_expression_statement_rl(struct expression
*expr
, int implied
)
61 return last_stmt_rl(get_expression_statement(expr
), implied
);
64 static struct range_list
*handle_ampersand_rl(int implied
)
66 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
68 return alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
71 static struct range_list
*handle_negate_rl(struct expression
*expr
, int implied
)
73 if (known_condition_true(expr
->unop
))
75 if (known_condition_false(expr
->unop
))
78 if (implied
== RL_EXACT
)
81 if (implied_condition_true(expr
->unop
))
83 if (implied_condition_false(expr
->unop
))
85 return alloc_rl(zero
, one
);
88 static struct range_list
*handle_bitwise_negate(struct expression
*expr
, int implied
)
90 struct range_list
*rl
;
93 rl
= _get_rl(expr
->unop
, implied
);
94 if (!rl_to_sval(rl
, &sval
))
96 sval
= sval_preop(sval
, '~');
97 sval_cast(get_type(expr
->unop
), sval
);
98 return alloc_rl(sval
, sval
);
101 static struct range_list
*handle_minus_preop(struct expression
*expr
, int implied
)
103 struct range_list
*rl
;
106 rl
= _get_rl(expr
->unop
, implied
);
107 if (!rl_to_sval(rl
, &sval
))
109 sval
= sval_preop(sval
, '-');
110 return alloc_rl(sval
, sval
);
113 static struct range_list
*handle_preop_rl(struct expression
*expr
, int implied
)
117 return handle_ampersand_rl(implied
);
119 return handle_negate_rl(expr
, implied
);
121 return handle_bitwise_negate(expr
, implied
);
123 return handle_minus_preop(expr
, implied
);
125 return handle_variable(expr
, implied
);
127 return handle_expression_statement_rl(expr
, implied
);
133 static struct range_list
*handle_divide_rl(struct expression
*expr
, int implied
)
135 struct range_list
*left_rl
, *right_rl
;
139 type
= get_type(expr
);
141 left_rl
= _get_rl(expr
->left
, implied
);
142 left_rl
= cast_rl(type
, left_rl
);
143 right_rl
= _get_rl(expr
->right
, implied
);
144 right_rl
= cast_rl(type
, right_rl
);
146 if (!left_rl
|| !right_rl
)
148 if (is_whole_rl(left_rl
) || is_whole_rl(right_rl
))
150 if (sval_is_negative(rl_min(left_rl
)) || sval_cmp_val(rl_min(right_rl
), 0) <= 0)
153 max
= rl_max(left_rl
);
154 if (!sval_is_max(max
))
155 max
= sval_binop(max
, '/', rl_min(right_rl
));
156 min
= sval_binop(rl_min(left_rl
), '/', rl_max(right_rl
));
157 return alloc_rl(min
, max
);
160 static struct range_list
*handle_subtract_rl(struct expression
*expr
, int implied
)
163 struct range_list
*left_rl
, *right_rl
;
164 sval_t max
, min
, tmp
;
167 type
= get_type(expr
);
168 comparison
= get_comparison(expr
->left
, expr
->right
);
170 left_rl
= _get_rl(expr
->left
, implied
);
171 left_rl
= cast_rl(type
, left_rl
);
172 right_rl
= _get_rl(expr
->right
, implied
);
173 right_rl
= cast_rl(type
, right_rl
);
175 if ((!left_rl
|| !right_rl
) &&
176 (implied
== RL_EXACT
|| implied
== RL_HARD
|| implied
== RL_FUZZY
))
180 left_rl
= alloc_whole_rl(type
);
182 right_rl
= alloc_whole_rl(type
);
184 /* negative values complicate everything fix this later */
185 if (sval_is_negative(rl_min(right_rl
)))
187 max
= rl_max(left_rl
);
189 switch (comparison
) {
191 case SPECIAL_UNSIGNED_GT
:
192 min
= sval_type_val(type
, 1);
193 max
= rl_max(left_rl
);
196 case SPECIAL_UNSIGNED_GTE
:
197 min
= sval_type_val(type
, 0);
198 max
= rl_max(left_rl
);
201 if (sval_binop_overflows(rl_min(left_rl
), '-', rl_max(right_rl
)))
203 min
= sval_type_min(type
);
206 if (!sval_binop_overflows(rl_min(left_rl
), '-', rl_max(right_rl
))) {
207 tmp
= sval_binop(rl_min(left_rl
), '-', rl_max(right_rl
));
208 if (sval_cmp(tmp
, min
) > 0)
212 if (!sval_is_max(rl_max(left_rl
))) {
213 tmp
= sval_binop(rl_max(left_rl
), '-', rl_min(right_rl
));
214 if (sval_cmp(tmp
, max
) < 0)
218 if (sval_is_min(min
) && sval_is_max(max
))
221 return cast_rl(type
, alloc_rl(min
, max
));
224 static struct range_list
*handle_mod_rl(struct expression
*expr
, int implied
)
226 struct range_list
*rl
;
227 sval_t left
, right
, sval
;
229 if (implied
== RL_EXACT
) {
230 if (!get_value(expr
->right
, &right
))
232 if (!get_value(expr
->left
, &left
))
234 sval
= sval_binop(left
, '%', right
);
235 return alloc_rl(sval
, sval
);
237 /* if we can't figure out the right side it's probably hopeless */
238 if (!get_implied_value(expr
->right
, &right
))
241 right
= sval_cast(get_type(expr
), right
);
244 rl
= _get_rl(expr
->left
, implied
);
245 if (rl
&& rl_max(rl
).uvalue
< right
.uvalue
)
246 right
.uvalue
= rl_max(rl
).uvalue
;
248 return alloc_rl(zero
, right
);
251 static sval_t
sval_lowest_set_bit(sval_t sval
)
256 for (i
= 0; i
< 64; i
++) {
257 if (sval
.uvalue
& 1ULL << i
) {
260 sval
.uvalue
&= ~(1ULL << i
);
266 static struct range_list
*handle_bitwise_AND(struct expression
*expr
, int implied
)
269 struct range_list
*left_rl
, *right_rl
;
272 if (implied
!= RL_IMPLIED
&& implied
!= RL_ABSOLUTE
)
275 type
= get_type(expr
);
277 if (get_implied_value(expr
->left
, &known
)) {
280 min
= sval_lowest_set_bit(known
);
281 left_rl
= alloc_rl(min
, known
);
282 left_rl
= cast_rl(type
, left_rl
);
283 add_range(&left_rl
, sval_type_val(type
, 0), sval_type_val(type
, 0));
285 left_rl
= _get_rl(expr
->left
, implied
);
287 left_rl
= cast_rl(type
, left_rl
);
288 left_rl
= alloc_rl(sval_type_val(type
, 0), rl_max(left_rl
));
290 if (implied
== RL_HARD
)
292 left_rl
= alloc_whole_rl(type
);
296 if (get_implied_value(expr
->right
, &known
)) {
297 sval_t min
, left_max
, mod
;
299 min
= sval_lowest_set_bit(known
);
300 right_rl
= alloc_rl(min
, known
);
301 right_rl
= cast_rl(type
, right_rl
);
302 add_range(&right_rl
, sval_type_val(type
, 0), sval_type_val(type
, 0));
304 if (min
.value
!= 0) {
305 left_max
= rl_max(left_rl
);
306 mod
= sval_binop(left_max
, '%', min
);
308 left_max
= sval_binop(left_max
, '-', mod
);
310 if (left_max
.value
> 0 && sval_cmp(left_max
, rl_max(left_rl
)) < 0)
311 left_rl
= remove_range(left_rl
, left_max
, rl_max(left_rl
));
315 right_rl
= _get_rl(expr
->right
, implied
);
317 right_rl
= cast_rl(type
, right_rl
);
318 right_rl
= alloc_rl(sval_type_val(type
, 0), rl_max(right_rl
));
320 if (implied
== RL_HARD
)
322 right_rl
= alloc_whole_rl(type
);
326 return rl_intersection(left_rl
, right_rl
);
329 static struct range_list
*handle_bitwise_OR(struct expression
*expr
, int implied
)
332 struct range_list
*left_rl
, *right_rl
;
334 if (implied
!= RL_IMPLIED
&& implied
!= RL_ABSOLUTE
)
337 type
= get_type(expr
);
339 get_absolute_rl(expr
->left
, &left_rl
);
340 get_absolute_rl(expr
->right
, &right_rl
);
341 left_rl
= cast_rl(type
, left_rl
);
342 right_rl
= cast_rl(type
, right_rl
);
343 if (!left_rl
|| !right_rl
)
346 return rl_binop(left_rl
, '|', right_rl
);
349 static struct range_list
*handle_right_shift(struct expression
*expr
, int implied
)
351 struct range_list
*left_rl
;
355 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
358 left_rl
= _get_rl(expr
->left
, implied
);
360 max
= rl_max(left_rl
);
361 min
= rl_min(left_rl
);
363 if (implied
== RL_FUZZY
)
365 max
= sval_type_max(get_type(expr
->left
));
366 min
= sval_type_val(get_type(expr
->left
), 0);
369 if (get_implied_value(expr
->right
, &right
)) {
370 min
= sval_binop(min
, SPECIAL_RIGHTSHIFT
, right
);
371 max
= sval_binop(max
, SPECIAL_RIGHTSHIFT
, right
);
372 } else if (!sval_is_negative(min
)) {
374 max
= sval_type_max(max
.type
);
379 return alloc_rl(min
, max
);
382 static struct range_list
*handle_left_shift(struct expression
*expr
, int implied
)
384 struct range_list
*left_rl
, *res
;
389 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
391 /* this is hopeless without the right side */
392 if (!get_implied_value(expr
->right
, &right
))
394 left_rl
= _get_rl(expr
->left
, implied
);
396 max
= rl_max(left_rl
);
397 min
= rl_min(left_rl
);
398 if (min
.value
== 0) {
403 if (implied
== RL_FUZZY
)
405 max
= sval_type_max(get_type(expr
->left
));
406 min
= sval_type_val(get_type(expr
->left
), 1);
410 max
= sval_binop(max
, SPECIAL_LEFTSHIFT
, right
);
411 min
= sval_binop(min
, SPECIAL_LEFTSHIFT
, right
);
412 res
= alloc_rl(min
, max
);
414 res
= rl_union(res
, rl_zero());
418 static struct range_list
*handle_known_binop(struct expression
*expr
)
422 if (!get_value(expr
->left
, &left
))
424 if (!get_value(expr
->right
, &right
))
426 left
= sval_binop(left
, expr
->op
, right
);
427 return alloc_rl(left
, left
);
430 static int has_actual_ranges(struct range_list
*rl
)
432 struct data_range
*tmp
;
434 FOR_EACH_PTR(rl
, tmp
) {
435 if (sval_cmp(tmp
->min
, tmp
->max
) != 0)
437 } END_FOR_EACH_PTR(tmp
);
441 static struct range_list
*handle_implied_binop(struct range_list
*left_rl
, int op
, struct range_list
*right_rl
)
443 struct range_list
*res_rl
;
444 struct data_range
*left_drange
, *right_drange
;
447 if (!left_rl
|| !right_rl
)
449 if (has_actual_ranges(left_rl
))
451 if (has_actual_ranges(right_rl
))
454 if (ptr_list_size((struct ptr_list
*)left_rl
) * ptr_list_size((struct ptr_list
*)right_rl
) > 20)
459 FOR_EACH_PTR(left_rl
, left_drange
) {
460 FOR_EACH_PTR(right_rl
, right_drange
) {
461 if ((op
== '%' || op
== '/') &&
462 right_drange
->min
.value
== 0)
464 res
= sval_binop(left_drange
->min
, op
, right_drange
->min
);
465 add_range(&res_rl
, res
, res
);
466 } END_FOR_EACH_PTR(right_drange
);
467 } END_FOR_EACH_PTR(left_drange
);
472 static struct range_list
*handle_binop_rl(struct expression
*expr
, int implied
)
475 struct range_list
*left_rl
, *right_rl
, *rl
;
478 rl
= handle_known_binop(expr
);
481 if (implied
== RL_EXACT
)
484 type
= get_type(expr
);
485 left_rl
= _get_rl(expr
->left
, implied
);
486 left_rl
= cast_rl(type
, left_rl
);
487 right_rl
= _get_rl(expr
->right
, implied
);
488 right_rl
= cast_rl(type
, right_rl
);
490 rl
= handle_implied_binop(left_rl
, expr
->op
, right_rl
);
496 return handle_mod_rl(expr
, implied
);
498 return handle_bitwise_AND(expr
, implied
);
500 return handle_bitwise_OR(expr
, implied
);
501 case SPECIAL_RIGHTSHIFT
:
502 return handle_right_shift(expr
, implied
);
503 case SPECIAL_LEFTSHIFT
:
504 return handle_left_shift(expr
, implied
);
506 return handle_subtract_rl(expr
, implied
);
508 return handle_divide_rl(expr
, implied
);
511 if (!left_rl
|| !right_rl
)
514 if (sval_binop_overflows(rl_min(left_rl
), expr
->op
, rl_min(right_rl
)))
516 min
= sval_binop(rl_min(left_rl
), expr
->op
, rl_min(right_rl
));
518 if (sval_binop_overflows(rl_max(left_rl
), expr
->op
, rl_max(right_rl
))) {
524 max
= sval_type_max(get_type(expr
));
527 max
= sval_binop(rl_max(left_rl
), expr
->op
, rl_max(right_rl
));
530 return alloc_rl(min
, max
);
533 static int do_comparison(struct expression
*expr
)
535 struct range_list
*left_ranges
= NULL
;
536 struct range_list
*right_ranges
= NULL
;
537 int poss_true
, poss_false
;
540 type
= get_type(expr
);
542 get_implied_rl(expr
->left
, &left_ranges
);
543 get_implied_rl(expr
->right
, &right_ranges
);
544 left_ranges
= cast_rl(type
, left_ranges
);
545 right_ranges
= cast_rl(type
, right_ranges
);
547 poss_true
= possibly_true_rl(left_ranges
, expr
->op
, right_ranges
);
548 poss_false
= possibly_false_rl(left_ranges
, expr
->op
, right_ranges
);
550 free_rl(&left_ranges
);
551 free_rl(&right_ranges
);
553 if (!poss_true
&& !poss_false
)
555 if (poss_true
&& !poss_false
)
557 if (!poss_true
&& poss_false
)
562 static struct range_list
*handle_comparison_rl(struct expression
*expr
, int implied
)
567 if (get_value(expr
->left
, &left
) && get_value(expr
->right
, &right
)) {
568 struct data_range tmp_left
, tmp_right
;
572 tmp_right
.min
= right
;
573 tmp_right
.max
= right
;
574 if (true_comparison_range(&tmp_left
, expr
->op
, &tmp_right
))
579 if (implied
== RL_EXACT
)
582 res
= do_comparison(expr
);
588 return alloc_rl(zero
, one
);
591 static struct range_list
*handle_logical_rl(struct expression
*expr
, int implied
)
597 if (implied
== RL_EXACT
) {
598 if (get_value(expr
->left
, &left
))
600 if (get_value(expr
->right
, &right
))
603 if (get_implied_value(expr
->left
, &left
))
605 if (get_implied_value(expr
->right
, &right
))
610 case SPECIAL_LOGICAL_OR
:
611 if (left_known
&& left
.value
)
613 if (right_known
&& right
.value
)
615 if (left_known
&& right_known
)
618 case SPECIAL_LOGICAL_AND
:
619 if (left_known
&& right_known
) {
620 if (left
.value
&& right
.value
)
629 if (implied
== RL_EXACT
)
632 return alloc_rl(zero
, one
);
635 static struct range_list
*handle_conditional_rl(struct expression
*expr
, int implied
)
637 struct range_list
*true_rl
, *false_rl
;
639 int final_pass_orig
= final_pass
;
641 if (known_condition_true(expr
->conditional
))
642 return _get_rl(expr
->cond_true
, implied
);
643 if (known_condition_false(expr
->conditional
))
644 return _get_rl(expr
->cond_false
, implied
);
646 if (implied
== RL_EXACT
)
649 if (implied_condition_true(expr
->conditional
))
650 return _get_rl(expr
->cond_true
, implied
);
651 if (implied_condition_false(expr
->conditional
))
652 return _get_rl(expr
->cond_false
, implied
);
655 /* this becomes a problem with deeply nested conditional statements */
659 type
= get_type(expr
);
661 __push_fake_cur_stree();
663 __split_whole_condition(expr
->conditional
);
664 true_rl
= _get_rl(expr
->cond_true
, implied
);
665 __push_true_states();
666 __use_false_states();
667 false_rl
= _get_rl(expr
->cond_false
, implied
);
668 __merge_true_states();
669 __free_fake_cur_stree();
670 final_pass
= final_pass_orig
;
672 if (!true_rl
|| !false_rl
)
674 true_rl
= cast_rl(type
, true_rl
);
675 false_rl
= cast_rl(type
, false_rl
);
677 return rl_union(true_rl
, false_rl
);
680 static int get_fuzzy_max_helper(struct expression
*expr
, sval_t
*max
)
682 struct smatch_state
*state
;
685 if (get_hard_max(expr
, &sval
)) {
690 state
= get_state_expr(SMATCH_EXTRA
, expr
);
691 if (!state
|| !estate_has_fuzzy_max(state
))
693 *max
= sval_cast(get_type(expr
), estate_get_fuzzy_max(state
));
697 static int get_fuzzy_min_helper(struct expression
*expr
, sval_t
*min
)
699 struct smatch_state
*state
;
702 state
= get_state_expr(SMATCH_EXTRA
, expr
);
703 if (!state
|| !estate_rl(state
))
706 sval
= estate_min(state
);
707 if (sval_is_negative(sval
) && sval_is_min(sval
))
710 if (sval_is_max(sval
))
713 *min
= sval_cast(get_type(expr
), sval
);
717 int get_const_value(struct expression
*expr
, sval_t
*sval
)
722 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
725 if (!(sym
->ctype
.modifiers
& MOD_CONST
))
727 if (get_value(sym
->initializer
, &right
)) {
728 *sval
= sval_cast(get_type(expr
), right
);
734 struct range_list
*var_to_absolute_rl(struct expression
*expr
)
736 struct smatch_state
*state
;
737 struct range_list
*rl
;
739 state
= get_state_expr(SMATCH_EXTRA
, expr
);
741 if (get_local_rl(expr
, &rl
))
743 if (get_db_type_rl(expr
, &rl
))
745 return alloc_whole_rl(get_type(expr
));
747 /* err on the side of saying things are possible */
748 if (!estate_rl(state
))
749 return alloc_whole_rl(get_type(expr
));
750 return clone_rl(estate_rl(state
));
753 static struct range_list
*handle_variable(struct expression
*expr
, int implied
)
755 struct smatch_state
*state
;
756 struct range_list
*rl
;
757 sval_t sval
, min
, max
;
759 if (get_const_value(expr
, &sval
))
760 return alloc_rl(sval
, sval
);
762 if (custom_handle_variable
) {
763 rl
= custom_handle_variable(expr
);
765 return var_to_absolute_rl(expr
);
775 state
= get_state_expr(SMATCH_EXTRA
, expr
);
776 if (!state
|| !state
->data
) {
777 if (implied
== RL_HARD
)
779 if (get_local_rl(expr
, &rl
))
781 if (get_db_type_rl(expr
, &rl
))
785 if (implied
== RL_HARD
&& !estate_has_hard_max(state
))
787 return clone_rl(estate_rl(state
));
789 if (!get_fuzzy_min_helper(expr
, &min
))
790 min
= sval_type_min(get_type(expr
));
791 if (!get_fuzzy_max_helper(expr
, &max
))
793 /* fuzzy ranges are often inverted */
794 if (sval_cmp(min
, max
) > 0) {
799 return alloc_rl(min
, max
);
804 static sval_t
handle_sizeof(struct expression
*expr
)
809 ret
= sval_blank(expr
);
810 sym
= expr
->cast_type
;
812 sym
= evaluate_expression(expr
->cast_expression
);
814 * Expressions of restricted types will possibly get
815 * promoted - check that here
817 if (is_restricted_type(sym
)) {
818 if (type_bits(sym
) < bits_in_int
)
820 } else if (is_fouled_type(sym
)) {
824 examine_symbol_type(sym
);
826 ret
.type
= size_t_ctype
;
827 if (type_bits(sym
) <= 0) /* sizeof(void) */ {
828 if (get_real_base_type(sym
) == &void_ctype
)
833 ret
.value
= type_bytes(sym
);
838 static struct range_list
*handle_call_rl(struct expression
*expr
, int implied
)
840 struct range_list
*rl
;
842 if (implied
== RL_EXACT
|| implied
== RL_HARD
|| implied
== RL_FUZZY
)
845 if (get_implied_return(expr
, &rl
))
847 return db_return_vals(expr
);
850 static struct range_list
*handle_cast(struct expression
*expr
, int implied
)
852 struct range_list
*rl
;
855 type
= get_type(expr
);
856 rl
= _get_rl(expr
->cast_expression
, implied
);
858 return cast_rl(type
, rl
);
859 if (implied
== RL_ABSOLUTE
)
860 return alloc_whole_rl(type
);
861 if (implied
== RL_IMPLIED
&& type
&&
862 type_bits(type
) > 0 && type_bits(type
) < 32)
863 return alloc_whole_rl(type
);
867 static struct range_list
*_get_rl(struct expression
*expr
, int implied
)
869 struct range_list
*rl
;
873 type
= get_type(expr
);
874 expr
= strip_parens(expr
);
880 case EXPR_FORCE_CAST
:
881 case EXPR_IMPLIED_CAST
:
882 rl
= handle_cast(expr
, implied
);
886 expr
= strip_expr(expr
);
890 switch (expr
->type
) {
892 sval
= sval_from_val(expr
, expr
->value
);
893 rl
= alloc_rl(sval
, sval
);
896 rl
= handle_preop_rl(expr
, implied
);
899 rl
= _get_rl(expr
->unop
, implied
);
902 rl
= handle_binop_rl(expr
, implied
);
905 rl
= handle_comparison_rl(expr
, implied
);
908 rl
= handle_logical_rl(expr
, implied
);
912 sval
= handle_sizeof(expr
);
913 rl
= alloc_rl(sval
, sval
);
916 case EXPR_CONDITIONAL
:
917 rl
= handle_conditional_rl(expr
, implied
);
920 rl
= handle_call_rl(expr
, implied
);
923 rl
= handle_variable(expr
, implied
);
929 if (type
&& implied
== RL_ABSOLUTE
)
930 return alloc_whole_rl(type
);
934 /* returns 1 if it can get a value literal or else returns 0 */
935 int get_value(struct expression
*expr
, sval_t
*sval
)
937 struct range_list
*rl
;
939 rl
= _get_rl(expr
, RL_EXACT
);
940 if (!rl_to_sval(rl
, sval
))
945 int get_implied_value(struct expression
*expr
, sval_t
*sval
)
947 struct range_list
*rl
;
949 rl
= _get_rl(expr
, RL_IMPLIED
);
950 if (!rl_to_sval(rl
, sval
))
955 int get_implied_min(struct expression
*expr
, sval_t
*sval
)
957 struct range_list
*rl
;
959 rl
= _get_rl(expr
, RL_IMPLIED
);
966 int get_implied_max(struct expression
*expr
, sval_t
*sval
)
968 struct range_list
*rl
;
970 rl
= _get_rl(expr
, RL_IMPLIED
);
977 int get_implied_rl(struct expression
*expr
, struct range_list
**rl
)
979 *rl
= _get_rl(expr
, RL_IMPLIED
);
985 int get_absolute_rl(struct expression
*expr
, struct range_list
**rl
)
987 *rl
= _get_rl(expr
, RL_ABSOLUTE
);
989 *rl
= alloc_whole_rl(get_type(expr
));
993 int custom_get_absolute_rl(struct expression
*expr
,
994 struct range_list
*(*fn
)(struct expression
*expr
),
995 struct range_list
**rl
)
998 custom_handle_variable
= fn
;
999 *rl
= _get_rl(expr
, RL_ABSOLUTE
);
1000 custom_handle_variable
= NULL
;
1004 int get_implied_rl_var_sym(const char *var
, struct symbol
*sym
, struct range_list
**rl
)
1006 struct smatch_state
*state
;
1008 state
= get_state(SMATCH_EXTRA
, var
, sym
);
1009 *rl
= estate_rl(state
);
1015 int get_hard_max(struct expression
*expr
, sval_t
*sval
)
1017 struct range_list
*rl
;
1019 rl
= _get_rl(expr
, RL_HARD
);
1026 int get_fuzzy_min(struct expression
*expr
, sval_t
*sval
)
1028 struct range_list
*rl
;
1031 rl
= _get_rl(expr
, RL_FUZZY
);
1035 if (sval_is_negative(tmp
) && sval_is_min(tmp
))
1041 int get_fuzzy_max(struct expression
*expr
, sval_t
*sval
)
1043 struct range_list
*rl
;
1046 rl
= _get_rl(expr
, RL_FUZZY
);
1050 if (max
.uvalue
> INT_MAX
- 10000)
1056 int get_absolute_min(struct expression
*expr
, sval_t
*sval
)
1058 struct range_list
*rl
;
1059 struct symbol
*type
;
1061 type
= get_type(expr
);
1063 type
= &llong_ctype
; // FIXME: this is wrong but places assume get type can't fail.
1064 rl
= _get_rl(expr
, RL_ABSOLUTE
);
1068 *sval
= sval_type_min(type
);
1070 if (sval_cmp(*sval
, sval_type_min(type
)) < 0)
1071 *sval
= sval_type_min(type
);
1075 int get_absolute_max(struct expression
*expr
, sval_t
*sval
)
1077 struct range_list
*rl
;
1078 struct symbol
*type
;
1080 type
= get_type(expr
);
1082 type
= &llong_ctype
;
1083 rl
= _get_rl(expr
, RL_ABSOLUTE
);
1087 *sval
= sval_type_max(type
);
1089 if (sval_cmp(sval_type_max(type
), *sval
) < 0)
1090 *sval
= sval_type_max(type
);
1094 int known_condition_true(struct expression
*expr
)
1101 if (get_value(expr
, &tmp
) && tmp
.value
)
1107 int known_condition_false(struct expression
*expr
)
1115 if (expr
->type
== EXPR_CALL
) {
1116 if (sym_name_is("__builtin_constant_p", expr
->fn
))
1122 int implied_condition_true(struct expression
*expr
)
1129 if (known_condition_true(expr
))
1131 if (get_implied_value(expr
, &tmp
) && tmp
.value
)
1134 if (expr
->type
== EXPR_POSTOP
)
1135 return implied_condition_true(expr
->unop
);
1137 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_DECREMENT
)
1138 return implied_not_equal(expr
->unop
, 1);
1139 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_INCREMENT
)
1140 return implied_not_equal(expr
->unop
, -1);
1142 expr
= strip_expr(expr
);
1143 switch (expr
->type
) {
1145 if (do_comparison(expr
) == 1)
1149 if (expr
->op
== '!') {
1150 if (implied_condition_false(expr
->unop
))
1156 if (implied_not_equal(expr
, 0) == 1)
1163 int implied_condition_false(struct expression
*expr
)
1165 struct expression
*tmp
;
1171 if (known_condition_false(expr
))
1174 switch (expr
->type
) {
1176 if (do_comparison(expr
) == 2)
1179 if (expr
->op
== '!') {
1180 if (implied_condition_true(expr
->unop
))
1184 tmp
= strip_expr(expr
);
1186 return implied_condition_false(tmp
);
1189 if (get_implied_value(expr
, &sval
) && sval
.value
== 0)
1196 int can_integer_overflow(struct symbol
*type
, struct expression
*expr
)
1199 sval_t lmax
, rmax
, res
;
1204 expr
= strip_expr(expr
);
1206 if (expr
->type
== EXPR_ASSIGNMENT
) {
1208 case SPECIAL_MUL_ASSIGN
:
1211 case SPECIAL_ADD_ASSIGN
:
1214 case SPECIAL_SHL_ASSIGN
:
1215 op
= SPECIAL_LEFTSHIFT
;
1220 } else if (expr
->type
== EXPR_BINOP
) {
1221 if (expr
->op
!= '*' && expr
->op
!= '+' && expr
->op
!= SPECIAL_LEFTSHIFT
)
1228 get_absolute_max(expr
->left
, &lmax
);
1229 get_absolute_max(expr
->right
, &rmax
);
1231 if (sval_binop_overflows(lmax
, op
, rmax
))
1234 res
= sval_binop(lmax
, op
, rmax
);
1235 if (sval_cmp(res
, sval_type_max(type
)) > 0)