4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 #include "smatch_slist.h"
12 #include "smatch_extra.h"
14 static long long _get_implied_value(struct expression
*expr
, int *undefined
, int implied
);
15 static long long _get_value(struct expression
*expr
, int *undefined
, int implied
);
26 static long long cast_to_type(struct expression
*expr
, long long val
)
28 struct symbol
*type
= get_type(expr
);
33 switch (type
->bit_size
) {
35 if (type
->ctype
.modifiers
& MOD_UNSIGNED
)
36 val
= (long long)(unsigned char) val
;
38 val
= (long long)(char) val
;
41 if (type
->ctype
.modifiers
& MOD_UNSIGNED
)
42 val
= (long long)(unsigned short) val
;
44 val
= (long long)(short) val
;
47 if (type
->ctype
.modifiers
& MOD_UNSIGNED
)
48 val
= (long long)(unsigned int) val
;
50 val
= (long long)(int) val
;
56 static int opposite_implied(int implied
)
58 if (implied
== IMPLIED_MIN
)
60 if (implied
== IMPLIED_MAX
)
65 static int last_stmt_val(struct statement
*stmt
, long long *val
)
67 struct expression
*expr
;
72 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
73 if (stmt
->type
!= STMT_EXPRESSION
)
75 expr
= stmt
->expression
;
76 return get_value(expr
, val
);
79 static long long handle_expression_statement(struct expression
*expr
, int *undefined
, int implied
)
81 struct statement
*stmt
;
84 stmt
= get_expression_statement(expr
);
85 if (last_stmt_val(stmt
, &tmp
))
92 static long long handle_preop(struct expression
*expr
, int *undefined
, int implied
)
94 long long ret
= BOGUS
;
98 ret
= !_get_value(expr
->unop
, undefined
, implied
);
101 ret
= ~_get_value(expr
->unop
, undefined
, implied
);
102 ret
= cast_to_type(expr
->unop
, ret
);
105 ret
= -_get_value(expr
->unop
, undefined
, implied
);
108 ret
= _get_implied_value(expr
, undefined
, implied
);
111 ret
= handle_expression_statement(expr
, undefined
, implied
);
119 static long long handle_divide(struct expression
*expr
, int *undefined
, int implied
)
123 long long ret
= BOGUS
;
125 left
= _get_value(expr
->left
, undefined
, implied
);
126 right
= _get_value(expr
->right
, undefined
, opposite_implied(implied
));
136 static long long handle_subtract(struct expression
*expr
, int *undefined
, int implied
)
141 left
= _get_value(expr
->left
, undefined
, implied
);
142 right
= _get_value(expr
->right
, undefined
, opposite_implied(implied
));
147 static long long handle_binop(struct expression
*expr
, int *undefined
, int implied
)
151 long long ret
= BOGUS
;
153 if (expr
->type
!= EXPR_BINOP
) {
158 left
= _get_value(expr
->left
, undefined
, implied
);
159 right
= _get_value(expr
->right
, undefined
, implied
);
166 ret
= handle_divide(expr
, undefined
, implied
);
172 ret
= handle_subtract(expr
, undefined
, implied
);
186 case SPECIAL_RIGHTSHIFT
:
189 case SPECIAL_LEFTSHIFT
:
201 static int do_comparison(struct expression
*expr
)
203 struct range_list
*left_ranges
= NULL
;
204 struct range_list
*right_ranges
= NULL
;
205 int poss_true
, poss_false
;
207 get_implied_range_list(expr
->left
, &left_ranges
);
208 get_implied_range_list(expr
->right
, &right_ranges
);
210 poss_true
= possibly_true_range_lists(left_ranges
, expr
->op
, right_ranges
);
211 poss_false
= possibly_false_range_lists(left_ranges
, expr
->op
, right_ranges
);
213 free_range_list(&left_ranges
);
214 free_range_list(&right_ranges
);
216 if (!poss_true
&& !poss_false
)
218 if (poss_true
&& !poss_false
)
220 if (!poss_true
&& poss_false
)
225 static long long handle_comparison(struct expression
*expr
, int *undefined
, int implied
)
229 /* TODO: we should be able to handle this... */
230 if (implied
== NOTIMPLIED
) {
235 res
= do_comparison(expr
);
241 if (implied
== IMPLIED_MIN
|| implied
== FUZZYMIN
)
243 if (implied
== IMPLIED_MAX
|| implied
== FUZZYMAX
)
250 static long long handle_logical(struct expression
*expr
, int *undefined
, int implied
)
252 long long left_val
, right_val
;
254 /* TODO: we should be able to handle this... */
255 if (implied
== NOTIMPLIED
) {
260 if (get_implied_value(expr
->left
, &left_val
) &&
261 get_implied_value(expr
->right
, &right_val
)) {
263 case SPECIAL_LOGICAL_OR
:
264 return left_val
|| right_val
;
265 case SPECIAL_LOGICAL_AND
:
266 return left_val
&& right_val
;
273 if (implied
== IMPLIED_MIN
|| implied
== FUZZYMIN
)
275 if (implied
== IMPLIED_MAX
|| implied
== FUZZYMAX
)
282 static int get_implied_value_helper(struct expression
*expr
, long long *val
, int what
)
284 struct smatch_state
*state
;
288 if (get_value(expr
, val
))
291 name
= get_variable_from_expr(expr
, &sym
);
294 state
= get_state(SMATCH_EXTRA
, name
, sym
);
296 if (!state
|| !state
->data
)
299 return estate_get_single_value(state
, val
);
300 if (what
== IMPLIED_MAX
) {
301 *val
= estate_max(state
);
302 if (*val
== whole_range
.max
) /* this means just guessing */
306 *val
= estate_min(state
);
307 if (*val
== whole_range
.min
)
312 static int get_fuzzy_max_helper(struct expression
*expr
, long long *max
)
315 struct sm_state
*tmp
;
317 if (get_implied_max(expr
, max
))
320 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
324 *max
= whole_range
.min
;
325 FOR_EACH_PTR(sm
->possible
, tmp
) {
328 new_min
= estate_min(tmp
->state
);
331 } END_FOR_EACH_PTR(tmp
);
333 if (*max
> whole_range
.min
)
338 static int get_fuzzy_min_helper(struct expression
*expr
, long long *min
)
341 struct sm_state
*tmp
;
343 if (get_implied_min(expr
, min
))
346 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
350 *min
= whole_range
.max
;
351 FOR_EACH_PTR(sm
->possible
, tmp
) {
354 new_max
= estate_max(tmp
->state
);
357 } END_FOR_EACH_PTR(tmp
);
359 if (*min
< whole_range
.max
)
364 static long long _get_implied_value(struct expression
*expr
, int *undefined
, int implied
)
366 long long ret
= BOGUS
;
372 if (!get_implied_value_helper(expr
, &ret
, implied
))
376 if (!get_fuzzy_max_helper(expr
, &ret
))
380 if (!get_fuzzy_min_helper(expr
, &ret
))
389 static int get_const_value(struct expression
*expr
, long long *val
)
396 if (!(sym
->ctype
.modifiers
& MOD_CONST
))
398 if (get_value(sym
->initializer
, val
))
403 static long long _get_value(struct expression
*expr
, int *undefined
, int implied
)
405 long long ret
= BOGUS
;
414 expr
= strip_parens(expr
);
416 switch (expr
->type
) {
419 ret
= cast_to_type(expr
, ret
);
422 ret
= handle_preop(expr
, undefined
, implied
);
425 ret
= _get_value(expr
->unop
, undefined
, implied
);
428 case EXPR_FORCE_CAST
:
429 case EXPR_IMPLIED_CAST
:
430 ret
= _get_value(expr
->cast_expression
, undefined
, implied
);
431 return cast_to_type(expr
, ret
);
433 ret
= handle_binop(expr
, undefined
, implied
);
436 ret
= handle_comparison(expr
, undefined
, implied
);
439 ret
= handle_logical(expr
, undefined
, implied
);
443 ret
= get_expression_value(expr
);
446 if (get_const_value(expr
, &ret
))
449 ret
= _get_implied_value(expr
, undefined
, implied
);
456 /* returns 1 if it can get a value literal or else returns 0 */
457 int get_value(struct expression
*expr
, long long *val
)
461 *val
= _get_value(expr
, &undefined
, NOTIMPLIED
);
467 int get_implied_value(struct expression
*expr
, long long *val
)
471 *val
= _get_value(expr
, &undefined
, IMPLIED
);
475 int get_implied_min(struct expression
*expr
, long long *val
)
479 *val
= _get_value(expr
, &undefined
, IMPLIED_MIN
);
483 int get_implied_max(struct expression
*expr
, long long *val
)
487 *val
= _get_value(expr
, &undefined
, IMPLIED_MAX
);
491 int get_fuzzy_min(struct expression
*expr
, long long *val
)
495 *val
= _get_value(expr
, &undefined
, FUZZYMIN
);
499 int get_fuzzy_max(struct expression
*expr
, long long *val
)
503 *val
= _get_value(expr
, &undefined
, FUZZYMAX
);
507 int get_absolute_min(struct expression
*expr
, long long *val
)
512 type
= get_type(expr
);
514 if (get_value(expr
, val
))
518 min
= type_min(type
);
519 if (!get_implied_min(expr
, val
) || *val
< min
)
524 int get_absolute_max(struct expression
*expr
, long long *val
)
529 type
= get_type(expr
);
531 if (get_value(expr
, val
))
535 max
= type_max(type
);
536 if (!get_implied_max(expr
, val
) || *val
> max
)
538 if (*val
< type_min(type
))
543 int known_condition_true(struct expression
*expr
)
550 if (get_value(expr
, &tmp
) && tmp
)
556 int known_condition_false(struct expression
*expr
)
564 if (expr
->type
== EXPR_CALL
) {
565 if (sym_name_is("__builtin_constant_p", expr
->fn
))
571 int implied_condition_true(struct expression
*expr
)
578 if (known_condition_true(expr
))
580 if (get_implied_value(expr
, &tmp
) && tmp
)
583 if (expr
->type
== EXPR_POSTOP
)
584 return implied_condition_true(expr
->unop
);
586 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_DECREMENT
)
587 return implied_not_equal(expr
->unop
, 1);
588 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_INCREMENT
)
589 return implied_not_equal(expr
->unop
, -1);
591 expr
= strip_expr(expr
);
592 switch (expr
->type
) {
594 if (do_comparison(expr
) == 1)
598 if (expr
->op
== '!') {
599 if (implied_condition_false(expr
->unop
))
605 if (implied_not_equal(expr
, 0) == 1)
612 int implied_condition_false(struct expression
*expr
)
614 struct expression
*tmp
;
620 if (known_condition_false(expr
))
623 switch (expr
->type
) {
625 if (do_comparison(expr
) == 2)
628 if (expr
->op
== '!') {
629 if (implied_condition_true(expr
->unop
))
633 tmp
= strip_expr(expr
);
635 return implied_condition_false(tmp
);
638 if (get_implied_value(expr
, &val
) && val
== 0)