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_ampersand(int *undefined
, int implied
)
94 if (implied
== IMPLIED_MIN
|| implied
== FUZZYMIN
)
96 if (implied
== IMPLIED_MAX
|| implied
== FUZZYMAX
)
103 static long long handle_preop(struct expression
*expr
, int *undefined
, int implied
)
105 long long ret
= BOGUS
;
109 ret
= handle_ampersand(undefined
, implied
);
112 ret
= !_get_value(expr
->unop
, undefined
, implied
);
115 ret
= ~_get_value(expr
->unop
, undefined
, implied
);
116 ret
= cast_to_type(expr
->unop
, ret
);
119 ret
= -_get_value(expr
->unop
, undefined
, implied
);
122 ret
= _get_implied_value(expr
, undefined
, implied
);
125 ret
= handle_expression_statement(expr
, undefined
, implied
);
133 static long long handle_divide(struct expression
*expr
, int *undefined
, int implied
)
137 long long ret
= BOGUS
;
139 left
= _get_value(expr
->left
, undefined
, implied
);
140 right
= _get_value(expr
->right
, undefined
, opposite_implied(implied
));
150 static long long handle_subtract(struct expression
*expr
, int *undefined
, int implied
)
155 left
= _get_value(expr
->left
, undefined
, implied
);
156 right
= _get_value(expr
->right
, undefined
, opposite_implied(implied
));
161 static long long handle_binop(struct expression
*expr
, int *undefined
, int implied
)
165 long long ret
= BOGUS
;
167 if (expr
->type
!= EXPR_BINOP
) {
172 left
= _get_value(expr
->left
, undefined
, implied
);
173 right
= _get_value(expr
->right
, undefined
, implied
);
180 ret
= handle_divide(expr
, undefined
, implied
);
186 ret
= handle_subtract(expr
, undefined
, implied
);
200 case SPECIAL_RIGHTSHIFT
:
203 case SPECIAL_LEFTSHIFT
:
215 static int do_comparison(struct expression
*expr
)
217 struct range_list
*left_ranges
= NULL
;
218 struct range_list
*right_ranges
= NULL
;
219 int poss_true
, poss_false
;
221 get_implied_range_list(expr
->left
, &left_ranges
);
222 get_implied_range_list(expr
->right
, &right_ranges
);
224 poss_true
= possibly_true_range_lists(left_ranges
, expr
->op
, right_ranges
);
225 poss_false
= possibly_false_range_lists(left_ranges
, expr
->op
, right_ranges
);
227 free_range_list(&left_ranges
);
228 free_range_list(&right_ranges
);
230 if (!poss_true
&& !poss_false
)
232 if (poss_true
&& !poss_false
)
234 if (!poss_true
&& poss_false
)
239 static long long handle_comparison(struct expression
*expr
, int *undefined
, int implied
)
243 /* TODO: we should be able to handle this... */
244 if (implied
== NOTIMPLIED
) {
249 res
= do_comparison(expr
);
255 if (implied
== IMPLIED_MIN
|| implied
== FUZZYMIN
)
257 if (implied
== IMPLIED_MAX
|| implied
== FUZZYMAX
)
264 static long long handle_logical(struct expression
*expr
, int *undefined
, int implied
)
266 long long left_val
, right_val
;
268 /* TODO: we should be able to handle this... */
269 if (implied
== NOTIMPLIED
) {
274 if (get_implied_value(expr
->left
, &left_val
) &&
275 get_implied_value(expr
->right
, &right_val
)) {
277 case SPECIAL_LOGICAL_OR
:
278 return left_val
|| right_val
;
279 case SPECIAL_LOGICAL_AND
:
280 return left_val
&& right_val
;
287 if (implied
== IMPLIED_MIN
|| implied
== FUZZYMIN
)
289 if (implied
== IMPLIED_MAX
|| implied
== FUZZYMAX
)
296 static int get_implied_value_helper(struct expression
*expr
, long long *val
, int what
)
298 struct smatch_state
*state
;
302 expr
= strip_expr(expr
);
304 if (get_value(expr
, val
))
307 name
= get_variable_from_expr(expr
, &sym
);
310 state
= get_state(SMATCH_EXTRA
, name
, sym
);
312 if (!state
|| !state
->data
)
315 return estate_get_single_value(state
, val
);
316 if (what
== IMPLIED_MAX
) {
317 *val
= estate_max(state
);
318 if (*val
== whole_range
.max
) /* this means just guessing */
322 *val
= estate_min(state
);
323 if (*val
== whole_range
.min
)
328 static int get_fuzzy_max_helper(struct expression
*expr
, long long *max
)
331 struct sm_state
*tmp
;
333 if (get_implied_max(expr
, max
))
336 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
340 *max
= whole_range
.min
;
341 FOR_EACH_PTR(sm
->possible
, tmp
) {
344 new_min
= estate_min(tmp
->state
);
347 } END_FOR_EACH_PTR(tmp
);
349 if (*max
> whole_range
.min
)
354 static int get_fuzzy_min_helper(struct expression
*expr
, long long *min
)
357 struct sm_state
*tmp
;
359 if (get_implied_min(expr
, min
))
362 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
366 *min
= whole_range
.max
;
367 FOR_EACH_PTR(sm
->possible
, tmp
) {
370 new_max
= estate_max(tmp
->state
);
373 } END_FOR_EACH_PTR(tmp
);
375 if (*min
< whole_range
.max
)
380 static long long _get_implied_value(struct expression
*expr
, int *undefined
, int implied
)
382 long long ret
= BOGUS
;
388 if (!get_implied_value_helper(expr
, &ret
, implied
))
392 if (!get_fuzzy_max_helper(expr
, &ret
))
396 if (!get_fuzzy_min_helper(expr
, &ret
))
405 static int get_const_value(struct expression
*expr
, long long *val
)
412 if (!(sym
->ctype
.modifiers
& MOD_CONST
))
414 if (get_value(sym
->initializer
, val
))
419 static long long _get_value(struct expression
*expr
, int *undefined
, int implied
)
421 long long ret
= BOGUS
;
430 expr
= strip_parens(expr
);
432 switch (expr
->type
) {
435 ret
= cast_to_type(expr
, ret
);
438 ret
= handle_preop(expr
, undefined
, implied
);
441 ret
= _get_value(expr
->unop
, undefined
, implied
);
444 case EXPR_FORCE_CAST
:
445 case EXPR_IMPLIED_CAST
:
446 ret
= _get_value(expr
->cast_expression
, undefined
, implied
);
447 return cast_to_type(expr
, ret
);
449 ret
= handle_binop(expr
, undefined
, implied
);
452 ret
= handle_comparison(expr
, undefined
, implied
);
455 ret
= handle_logical(expr
, undefined
, implied
);
459 ret
= get_expression_value_nomod(expr
);
462 if (get_const_value(expr
, &ret
))
465 ret
= _get_implied_value(expr
, undefined
, implied
);
472 /* returns 1 if it can get a value literal or else returns 0 */
473 int get_value(struct expression
*expr
, long long *val
)
477 *val
= _get_value(expr
, &undefined
, NOTIMPLIED
);
483 int get_implied_value(struct expression
*expr
, long long *val
)
487 *val
= _get_value(expr
, &undefined
, IMPLIED
);
491 int get_implied_min(struct expression
*expr
, long long *val
)
495 *val
= _get_value(expr
, &undefined
, IMPLIED_MIN
);
499 int get_implied_max(struct expression
*expr
, long long *val
)
503 *val
= _get_value(expr
, &undefined
, IMPLIED_MAX
);
507 int get_fuzzy_min(struct expression
*expr
, long long *val
)
511 *val
= _get_value(expr
, &undefined
, FUZZYMIN
);
515 int get_fuzzy_max(struct expression
*expr
, long long *val
)
519 *val
= _get_value(expr
, &undefined
, FUZZYMAX
);
523 int get_absolute_min(struct expression
*expr
, long long *val
)
528 type
= get_type(expr
);
530 if (get_value(expr
, val
))
534 min
= type_min(type
);
535 if (!get_implied_min(expr
, val
) || *val
< min
)
540 int get_absolute_max(struct expression
*expr
, long long *val
)
545 type
= get_type(expr
);
547 if (get_value(expr
, val
))
551 max
= type_max(type
);
552 if (!get_implied_max(expr
, val
) || *val
> max
)
554 if (*val
< type_min(type
))
559 int known_condition_true(struct expression
*expr
)
566 if (get_value(expr
, &tmp
) && tmp
)
572 int known_condition_false(struct expression
*expr
)
580 if (expr
->type
== EXPR_CALL
) {
581 if (sym_name_is("__builtin_constant_p", expr
->fn
))
587 int implied_condition_true(struct expression
*expr
)
594 if (known_condition_true(expr
))
596 if (get_implied_value(expr
, &tmp
) && tmp
)
599 if (expr
->type
== EXPR_POSTOP
)
600 return implied_condition_true(expr
->unop
);
602 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_DECREMENT
)
603 return implied_not_equal(expr
->unop
, 1);
604 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_INCREMENT
)
605 return implied_not_equal(expr
->unop
, -1);
607 expr
= strip_expr(expr
);
608 switch (expr
->type
) {
610 if (do_comparison(expr
) == 1)
614 if (expr
->op
== '!') {
615 if (implied_condition_false(expr
->unop
))
621 if (implied_not_equal(expr
, 0) == 1)
628 int implied_condition_false(struct expression
*expr
)
630 struct expression
*tmp
;
636 if (known_condition_false(expr
))
639 switch (expr
->type
) {
641 if (do_comparison(expr
) == 2)
644 if (expr
->op
== '!') {
645 if (implied_condition_true(expr
->unop
))
649 tmp
= strip_expr(expr
);
651 return implied_condition_false(tmp
);
654 if (get_implied_value(expr
, &val
) && val
== 0)