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
;
138 type
= get_type(expr
);
140 left_rl
= _get_rl(expr
->left
, implied
);
141 left_rl
= cast_rl(type
, left_rl
);
142 right_rl
= _get_rl(expr
->right
, implied
);
143 right_rl
= cast_rl(type
, right_rl
);
145 if (!left_rl
|| !right_rl
)
147 if (is_whole_rl(left_rl
) || is_whole_rl(right_rl
))
150 return rl_binop(left_rl
, '/', right_rl
);
153 static struct range_list
*handle_subtract_rl(struct expression
*expr
, int implied
)
156 struct range_list
*left_rl
, *right_rl
;
157 sval_t max
, min
, tmp
;
160 type
= get_type(expr
);
161 comparison
= get_comparison(expr
->left
, expr
->right
);
163 left_rl
= _get_rl(expr
->left
, implied
);
164 left_rl
= cast_rl(type
, left_rl
);
165 right_rl
= _get_rl(expr
->right
, implied
);
166 right_rl
= cast_rl(type
, right_rl
);
168 if ((!left_rl
|| !right_rl
) &&
169 (implied
== RL_EXACT
|| implied
== RL_HARD
|| implied
== RL_FUZZY
))
173 left_rl
= alloc_whole_rl(type
);
175 right_rl
= alloc_whole_rl(type
);
177 /* negative values complicate everything fix this later */
178 if (sval_is_negative(rl_min(right_rl
)))
180 max
= rl_max(left_rl
);
182 switch (comparison
) {
184 case SPECIAL_UNSIGNED_GT
:
185 min
= sval_type_val(type
, 1);
186 max
= rl_max(left_rl
);
189 case SPECIAL_UNSIGNED_GTE
:
190 min
= sval_type_val(type
, 0);
191 max
= rl_max(left_rl
);
194 if (sval_binop_overflows(rl_min(left_rl
), '-', rl_max(right_rl
)))
196 min
= sval_type_min(type
);
199 if (!sval_binop_overflows(rl_min(left_rl
), '-', rl_max(right_rl
))) {
200 tmp
= sval_binop(rl_min(left_rl
), '-', rl_max(right_rl
));
201 if (sval_cmp(tmp
, min
) > 0)
205 if (!sval_is_max(rl_max(left_rl
))) {
206 tmp
= sval_binop(rl_max(left_rl
), '-', rl_min(right_rl
));
207 if (sval_cmp(tmp
, max
) < 0)
211 if (sval_is_min(min
) && sval_is_max(max
))
214 return cast_rl(type
, alloc_rl(min
, max
));
217 static struct range_list
*handle_mod_rl(struct expression
*expr
, int implied
)
219 struct range_list
*rl
;
220 sval_t left
, right
, sval
;
222 if (implied
== RL_EXACT
) {
223 if (!get_value(expr
->right
, &right
))
225 if (!get_value(expr
->left
, &left
))
227 sval
= sval_binop(left
, '%', right
);
228 return alloc_rl(sval
, sval
);
230 /* if we can't figure out the right side it's probably hopeless */
231 if (!get_implied_value(expr
->right
, &right
))
234 right
= sval_cast(get_type(expr
), right
);
237 rl
= _get_rl(expr
->left
, implied
);
238 if (rl
&& rl_max(rl
).uvalue
< right
.uvalue
)
239 right
.uvalue
= rl_max(rl
).uvalue
;
241 return alloc_rl(zero
, right
);
244 static sval_t
sval_lowest_set_bit(sval_t sval
)
249 for (i
= 0; i
< 64; i
++) {
250 if (sval
.uvalue
& 1ULL << i
) {
253 sval
.uvalue
&= ~(1ULL << i
);
259 static struct range_list
*handle_bitwise_AND(struct expression
*expr
, int implied
)
262 struct range_list
*left_rl
, *right_rl
;
265 if (implied
!= RL_IMPLIED
&& implied
!= RL_ABSOLUTE
)
268 type
= get_type(expr
);
270 if (get_implied_value(expr
->left
, &known
)) {
273 min
= sval_lowest_set_bit(known
);
274 left_rl
= alloc_rl(min
, known
);
275 left_rl
= cast_rl(type
, left_rl
);
276 add_range(&left_rl
, sval_type_val(type
, 0), sval_type_val(type
, 0));
278 left_rl
= _get_rl(expr
->left
, implied
);
280 left_rl
= cast_rl(type
, left_rl
);
281 left_rl
= alloc_rl(sval_type_val(type
, 0), rl_max(left_rl
));
283 if (implied
== RL_HARD
)
285 left_rl
= alloc_whole_rl(type
);
289 if (get_implied_value(expr
->right
, &known
)) {
290 sval_t min
, left_max
, mod
;
292 min
= sval_lowest_set_bit(known
);
293 right_rl
= alloc_rl(min
, known
);
294 right_rl
= cast_rl(type
, right_rl
);
295 add_range(&right_rl
, sval_type_val(type
, 0), sval_type_val(type
, 0));
297 if (min
.value
!= 0) {
298 left_max
= rl_max(left_rl
);
299 mod
= sval_binop(left_max
, '%', min
);
301 left_max
= sval_binop(left_max
, '-', mod
);
303 if (left_max
.value
> 0 && sval_cmp(left_max
, rl_max(left_rl
)) < 0)
304 left_rl
= remove_range(left_rl
, left_max
, rl_max(left_rl
));
308 right_rl
= _get_rl(expr
->right
, implied
);
310 right_rl
= cast_rl(type
, right_rl
);
311 right_rl
= alloc_rl(sval_type_val(type
, 0), rl_max(right_rl
));
313 if (implied
== RL_HARD
)
315 right_rl
= alloc_whole_rl(type
);
319 return rl_intersection(left_rl
, right_rl
);
322 static struct range_list
*handle_bitwise_OR(struct expression
*expr
, int implied
)
325 struct range_list
*left_rl
, *right_rl
;
327 if (implied
!= RL_IMPLIED
&& implied
!= RL_ABSOLUTE
)
330 type
= get_type(expr
);
332 get_absolute_rl(expr
->left
, &left_rl
);
333 get_absolute_rl(expr
->right
, &right_rl
);
334 left_rl
= cast_rl(type
, left_rl
);
335 right_rl
= cast_rl(type
, right_rl
);
336 if (!left_rl
|| !right_rl
)
339 return rl_binop(left_rl
, '|', right_rl
);
342 static struct range_list
*handle_right_shift(struct expression
*expr
, int implied
)
344 struct range_list
*left_rl
;
348 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
351 left_rl
= _get_rl(expr
->left
, implied
);
353 max
= rl_max(left_rl
);
354 min
= rl_min(left_rl
);
356 if (implied
== RL_FUZZY
)
358 max
= sval_type_max(get_type(expr
->left
));
359 min
= sval_type_val(get_type(expr
->left
), 0);
362 if (get_implied_value(expr
->right
, &right
)) {
363 min
= sval_binop(min
, SPECIAL_RIGHTSHIFT
, right
);
364 max
= sval_binop(max
, SPECIAL_RIGHTSHIFT
, right
);
365 } else if (!sval_is_negative(min
)) {
367 max
= sval_type_max(max
.type
);
372 return alloc_rl(min
, max
);
375 static struct range_list
*handle_left_shift(struct expression
*expr
, int implied
)
377 struct range_list
*left_rl
, *res
;
382 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
384 /* this is hopeless without the right side */
385 if (!get_implied_value(expr
->right
, &right
))
387 left_rl
= _get_rl(expr
->left
, implied
);
389 max
= rl_max(left_rl
);
390 min
= rl_min(left_rl
);
391 if (min
.value
== 0) {
396 if (implied
== RL_FUZZY
)
398 max
= sval_type_max(get_type(expr
->left
));
399 min
= sval_type_val(get_type(expr
->left
), 1);
403 max
= sval_binop(max
, SPECIAL_LEFTSHIFT
, right
);
404 min
= sval_binop(min
, SPECIAL_LEFTSHIFT
, right
);
405 res
= alloc_rl(min
, max
);
407 res
= rl_union(res
, rl_zero());
411 static struct range_list
*handle_known_binop(struct expression
*expr
)
415 if (!get_value(expr
->left
, &left
))
417 if (!get_value(expr
->right
, &right
))
419 left
= sval_binop(left
, expr
->op
, right
);
420 return alloc_rl(left
, left
);
423 static int has_actual_ranges(struct range_list
*rl
)
425 struct data_range
*tmp
;
427 FOR_EACH_PTR(rl
, tmp
) {
428 if (sval_cmp(tmp
->min
, tmp
->max
) != 0)
430 } END_FOR_EACH_PTR(tmp
);
434 static struct range_list
*handle_implied_binop(struct range_list
*left_rl
, int op
, struct range_list
*right_rl
)
436 struct range_list
*res_rl
;
437 struct data_range
*left_drange
, *right_drange
;
440 if (!left_rl
|| !right_rl
)
442 if (has_actual_ranges(left_rl
))
444 if (has_actual_ranges(right_rl
))
447 if (ptr_list_size((struct ptr_list
*)left_rl
) * ptr_list_size((struct ptr_list
*)right_rl
) > 20)
452 FOR_EACH_PTR(left_rl
, left_drange
) {
453 FOR_EACH_PTR(right_rl
, right_drange
) {
454 if ((op
== '%' || op
== '/') &&
455 right_drange
->min
.value
== 0)
457 res
= sval_binop(left_drange
->min
, op
, right_drange
->min
);
458 add_range(&res_rl
, res
, res
);
459 } END_FOR_EACH_PTR(right_drange
);
460 } END_FOR_EACH_PTR(left_drange
);
465 static struct range_list
*handle_binop_rl(struct expression
*expr
, int implied
)
468 struct range_list
*left_rl
, *right_rl
, *rl
;
471 rl
= handle_known_binop(expr
);
474 if (implied
== RL_EXACT
)
477 type
= get_type(expr
);
478 left_rl
= _get_rl(expr
->left
, implied
);
479 left_rl
= cast_rl(type
, left_rl
);
480 right_rl
= _get_rl(expr
->right
, implied
);
481 right_rl
= cast_rl(type
, right_rl
);
483 rl
= handle_implied_binop(left_rl
, expr
->op
, right_rl
);
489 return handle_mod_rl(expr
, implied
);
491 return handle_bitwise_AND(expr
, implied
);
493 return handle_bitwise_OR(expr
, implied
);
494 case SPECIAL_RIGHTSHIFT
:
495 return handle_right_shift(expr
, implied
);
496 case SPECIAL_LEFTSHIFT
:
497 return handle_left_shift(expr
, implied
);
499 return handle_subtract_rl(expr
, implied
);
501 return handle_divide_rl(expr
, implied
);
504 if (!left_rl
|| !right_rl
)
507 if (sval_binop_overflows(rl_min(left_rl
), expr
->op
, rl_min(right_rl
)))
509 min
= sval_binop(rl_min(left_rl
), expr
->op
, rl_min(right_rl
));
511 if (sval_binop_overflows(rl_max(left_rl
), expr
->op
, rl_max(right_rl
))) {
517 max
= sval_type_max(get_type(expr
));
520 max
= sval_binop(rl_max(left_rl
), expr
->op
, rl_max(right_rl
));
523 return alloc_rl(min
, max
);
526 static int do_comparison(struct expression
*expr
)
528 struct range_list
*left_ranges
= NULL
;
529 struct range_list
*right_ranges
= NULL
;
530 int poss_true
, poss_false
;
533 type
= get_type(expr
);
534 get_absolute_rl(expr
->left
, &left_ranges
);
535 get_absolute_rl(expr
->right
, &right_ranges
);
537 left_ranges
= cast_rl(type
, left_ranges
);
538 right_ranges
= cast_rl(type
, right_ranges
);
540 poss_true
= possibly_true_rl(left_ranges
, expr
->op
, right_ranges
);
541 poss_false
= possibly_false_rl(left_ranges
, expr
->op
, right_ranges
);
543 free_rl(&left_ranges
);
544 free_rl(&right_ranges
);
546 if (!poss_true
&& !poss_false
)
548 if (poss_true
&& !poss_false
)
550 if (!poss_true
&& poss_false
)
555 static struct range_list
*handle_comparison_rl(struct expression
*expr
, int implied
)
560 if (get_value(expr
->left
, &left
) && get_value(expr
->right
, &right
)) {
561 struct data_range tmp_left
, tmp_right
;
565 tmp_right
.min
= right
;
566 tmp_right
.max
= right
;
567 if (true_comparison_range(&tmp_left
, expr
->op
, &tmp_right
))
572 if (implied
== RL_EXACT
)
575 res
= do_comparison(expr
);
581 return alloc_rl(zero
, one
);
584 static struct range_list
*handle_logical_rl(struct expression
*expr
, int implied
)
590 if (implied
== RL_EXACT
) {
591 if (get_value(expr
->left
, &left
))
593 if (get_value(expr
->right
, &right
))
596 if (get_implied_value(expr
->left
, &left
))
598 if (get_implied_value(expr
->right
, &right
))
603 case SPECIAL_LOGICAL_OR
:
604 if (left_known
&& left
.value
)
606 if (right_known
&& right
.value
)
608 if (left_known
&& right_known
)
611 case SPECIAL_LOGICAL_AND
:
612 if (left_known
&& right_known
) {
613 if (left
.value
&& right
.value
)
622 if (implied
== RL_EXACT
)
625 return alloc_rl(zero
, one
);
628 static struct range_list
*handle_conditional_rl(struct expression
*expr
, int implied
)
630 struct range_list
*true_rl
, *false_rl
;
632 int final_pass_orig
= final_pass
;
634 if (known_condition_true(expr
->conditional
))
635 return _get_rl(expr
->cond_true
, implied
);
636 if (known_condition_false(expr
->conditional
))
637 return _get_rl(expr
->cond_false
, implied
);
639 if (implied
== RL_EXACT
)
642 if (implied_condition_true(expr
->conditional
))
643 return _get_rl(expr
->cond_true
, implied
);
644 if (implied_condition_false(expr
->conditional
))
645 return _get_rl(expr
->cond_false
, implied
);
648 /* this becomes a problem with deeply nested conditional statements */
652 type
= get_type(expr
);
654 __push_fake_cur_stree();
656 __split_whole_condition(expr
->conditional
);
657 true_rl
= _get_rl(expr
->cond_true
, implied
);
658 __push_true_states();
659 __use_false_states();
660 false_rl
= _get_rl(expr
->cond_false
, implied
);
661 __merge_true_states();
662 __free_fake_cur_stree();
663 final_pass
= final_pass_orig
;
665 if (!true_rl
|| !false_rl
)
667 true_rl
= cast_rl(type
, true_rl
);
668 false_rl
= cast_rl(type
, false_rl
);
670 return rl_union(true_rl
, false_rl
);
673 static int get_fuzzy_max_helper(struct expression
*expr
, sval_t
*max
)
675 struct smatch_state
*state
;
678 if (get_hard_max(expr
, &sval
)) {
683 state
= get_state_expr(SMATCH_EXTRA
, expr
);
684 if (!state
|| !estate_has_fuzzy_max(state
))
686 *max
= sval_cast(get_type(expr
), estate_get_fuzzy_max(state
));
690 static int get_fuzzy_min_helper(struct expression
*expr
, sval_t
*min
)
692 struct smatch_state
*state
;
695 state
= get_state_expr(SMATCH_EXTRA
, expr
);
696 if (!state
|| !estate_rl(state
))
699 sval
= estate_min(state
);
700 if (sval_is_negative(sval
) && sval_is_min(sval
))
703 if (sval_is_max(sval
))
706 *min
= sval_cast(get_type(expr
), sval
);
710 int get_const_value(struct expression
*expr
, sval_t
*sval
)
715 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
718 if (!(sym
->ctype
.modifiers
& MOD_CONST
))
720 if (get_value(sym
->initializer
, &right
)) {
721 *sval
= sval_cast(get_type(expr
), right
);
727 struct range_list
*var_to_absolute_rl(struct expression
*expr
)
729 struct smatch_state
*state
;
730 struct range_list
*rl
;
732 state
= get_state_expr(SMATCH_EXTRA
, expr
);
734 if (get_local_rl(expr
, &rl
))
736 if (get_db_type_rl(expr
, &rl
))
738 return alloc_whole_rl(get_type(expr
));
740 /* err on the side of saying things are possible */
741 if (!estate_rl(state
))
742 return alloc_whole_rl(get_type(expr
));
743 return clone_rl(estate_rl(state
));
746 static struct range_list
*handle_variable(struct expression
*expr
, int implied
)
748 struct smatch_state
*state
;
749 struct range_list
*rl
;
750 sval_t sval
, min
, max
;
752 if (get_const_value(expr
, &sval
))
753 return alloc_rl(sval
, sval
);
755 if (custom_handle_variable
) {
756 rl
= custom_handle_variable(expr
);
758 return var_to_absolute_rl(expr
);
768 state
= get_state_expr(SMATCH_EXTRA
, expr
);
769 if (!state
|| !state
->data
) {
770 if (implied
== RL_HARD
)
772 if (get_local_rl(expr
, &rl
))
774 if (get_db_type_rl(expr
, &rl
))
778 if (implied
== RL_HARD
&& !estate_has_hard_max(state
))
780 return clone_rl(estate_rl(state
));
782 if (!get_fuzzy_min_helper(expr
, &min
))
783 min
= sval_type_min(get_type(expr
));
784 if (!get_fuzzy_max_helper(expr
, &max
))
786 /* fuzzy ranges are often inverted */
787 if (sval_cmp(min
, max
) > 0) {
792 return alloc_rl(min
, max
);
797 static sval_t
handle_sizeof(struct expression
*expr
)
802 ret
= sval_blank(expr
);
803 sym
= expr
->cast_type
;
805 sym
= evaluate_expression(expr
->cast_expression
);
807 * Expressions of restricted types will possibly get
808 * promoted - check that here
810 if (is_restricted_type(sym
)) {
811 if (type_bits(sym
) < bits_in_int
)
813 } else if (is_fouled_type(sym
)) {
817 examine_symbol_type(sym
);
819 ret
.type
= size_t_ctype
;
820 if (type_bits(sym
) <= 0) /* sizeof(void) */ {
821 if (get_real_base_type(sym
) == &void_ctype
)
826 ret
.value
= type_bytes(sym
);
831 static struct range_list
*handle_call_rl(struct expression
*expr
, int implied
)
833 struct range_list
*rl
;
835 if (sym_name_is("__builtin_expect", expr
->fn
)) {
836 struct expression
*arg
;
838 arg
= get_argument_from_call_expr(expr
->args
, 0);
839 return _get_rl(arg
, implied
);
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)