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
)
104 static long long handle_preop(struct expression
*expr
, int *undefined
, int implied
)
106 long long ret
= BOGUS
;
110 ret
= handle_ampersand(undefined
, implied
);
113 ret
= !_get_value(expr
->unop
, undefined
, implied
);
116 ret
= ~_get_value(expr
->unop
, undefined
, implied
);
117 ret
= cast_to_type(expr
->unop
, ret
);
120 ret
= -_get_value(expr
->unop
, undefined
, implied
);
123 ret
= _get_implied_value(expr
, undefined
, implied
);
126 ret
= handle_expression_statement(expr
, undefined
, implied
);
134 static long long handle_divide(struct expression
*expr
, int *undefined
, int implied
)
138 long long ret
= BOGUS
;
140 left
= _get_value(expr
->left
, undefined
, implied
);
141 right
= _get_value(expr
->right
, undefined
, opposite_implied(implied
));
151 static long long handle_subtract(struct expression
*expr
, int *undefined
, int implied
)
156 left
= _get_value(expr
->left
, undefined
, implied
);
157 right
= _get_value(expr
->right
, undefined
, opposite_implied(implied
));
162 static long long handle_binop(struct expression
*expr
, int *undefined
, int implied
)
166 long long ret
= BOGUS
;
168 if (expr
->type
!= EXPR_BINOP
) {
173 left
= _get_value(expr
->left
, undefined
, implied
);
174 right
= _get_value(expr
->right
, undefined
, implied
);
181 ret
= handle_divide(expr
, undefined
, implied
);
187 ret
= handle_subtract(expr
, undefined
, implied
);
201 case SPECIAL_RIGHTSHIFT
:
204 case SPECIAL_LEFTSHIFT
:
216 static int do_comparison(struct expression
*expr
)
218 struct range_list
*left_ranges
= NULL
;
219 struct range_list
*right_ranges
= NULL
;
220 int poss_true
, poss_false
;
222 get_implied_range_list(expr
->left
, &left_ranges
);
223 get_implied_range_list(expr
->right
, &right_ranges
);
225 poss_true
= possibly_true_range_lists(left_ranges
, expr
->op
, right_ranges
);
226 poss_false
= possibly_false_range_lists(left_ranges
, expr
->op
, right_ranges
);
228 free_range_list(&left_ranges
);
229 free_range_list(&right_ranges
);
231 if (!poss_true
&& !poss_false
)
233 if (poss_true
&& !poss_false
)
235 if (!poss_true
&& poss_false
)
240 static long long handle_comparison(struct expression
*expr
, int *undefined
, int implied
)
244 /* TODO: we should be able to handle this... */
245 if (implied
== NOTIMPLIED
) {
250 res
= do_comparison(expr
);
256 if (implied
== IMPLIED_MIN
|| implied
== FUZZYMIN
)
258 if (implied
== IMPLIED_MAX
|| implied
== FUZZYMAX
)
265 static long long handle_logical(struct expression
*expr
, int *undefined
, int implied
)
267 long long left_val
, right_val
;
269 /* TODO: we should be able to handle this... */
270 if (implied
== NOTIMPLIED
) {
275 if (get_implied_value(expr
->left
, &left_val
) &&
276 get_implied_value(expr
->right
, &right_val
)) {
278 case SPECIAL_LOGICAL_OR
:
279 return left_val
|| right_val
;
280 case SPECIAL_LOGICAL_AND
:
281 return left_val
&& right_val
;
288 if (implied
== IMPLIED_MIN
|| implied
== FUZZYMIN
)
290 if (implied
== IMPLIED_MAX
|| implied
== FUZZYMAX
)
297 static int get_implied_value_helper(struct expression
*expr
, long long *val
, int what
)
299 struct smatch_state
*state
;
303 if (get_value(expr
, val
))
306 name
= get_variable_from_expr(expr
, &sym
);
309 state
= get_state(SMATCH_EXTRA
, name
, sym
);
311 if (!state
|| !state
->data
)
314 return estate_get_single_value(state
, val
);
315 if (what
== IMPLIED_MAX
) {
316 *val
= estate_max(state
);
317 if (*val
== whole_range
.max
) /* this means just guessing */
321 *val
= estate_min(state
);
322 if (*val
== whole_range
.min
)
327 static int get_fuzzy_max_helper(struct expression
*expr
, long long *max
)
330 struct sm_state
*tmp
;
332 if (get_implied_max(expr
, max
))
335 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
339 *max
= whole_range
.min
;
340 FOR_EACH_PTR(sm
->possible
, tmp
) {
343 new_min
= estate_min(tmp
->state
);
346 } END_FOR_EACH_PTR(tmp
);
348 if (*max
> whole_range
.min
)
353 static int get_fuzzy_min_helper(struct expression
*expr
, long long *min
)
356 struct sm_state
*tmp
;
358 if (get_implied_min(expr
, min
))
361 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
365 *min
= whole_range
.max
;
366 FOR_EACH_PTR(sm
->possible
, tmp
) {
369 new_max
= estate_max(tmp
->state
);
372 } END_FOR_EACH_PTR(tmp
);
374 if (*min
< whole_range
.max
)
379 static long long _get_implied_value(struct expression
*expr
, int *undefined
, int implied
)
381 long long ret
= BOGUS
;
387 if (!get_implied_value_helper(expr
, &ret
, implied
))
391 if (!get_fuzzy_max_helper(expr
, &ret
))
395 if (!get_fuzzy_min_helper(expr
, &ret
))
404 static int get_const_value(struct expression
*expr
, long long *val
)
411 if (!(sym
->ctype
.modifiers
& MOD_CONST
))
413 if (get_value(sym
->initializer
, val
))
418 static long long _get_value(struct expression
*expr
, int *undefined
, int implied
)
420 long long ret
= BOGUS
;
429 expr
= strip_parens(expr
);
431 switch (expr
->type
) {
434 ret
= cast_to_type(expr
, ret
);
437 ret
= handle_preop(expr
, undefined
, implied
);
440 ret
= _get_value(expr
->unop
, undefined
, implied
);
443 case EXPR_FORCE_CAST
:
444 case EXPR_IMPLIED_CAST
:
445 ret
= _get_value(expr
->cast_expression
, undefined
, implied
);
446 return cast_to_type(expr
, ret
);
448 ret
= handle_binop(expr
, undefined
, implied
);
451 ret
= handle_comparison(expr
, undefined
, implied
);
454 ret
= handle_logical(expr
, undefined
, implied
);
458 ret
= get_expression_value_nomod(expr
);
461 if (get_const_value(expr
, &ret
))
464 ret
= _get_implied_value(expr
, undefined
, implied
);
471 /* returns 1 if it can get a value literal or else returns 0 */
472 int get_value(struct expression
*expr
, long long *val
)
476 *val
= _get_value(expr
, &undefined
, NOTIMPLIED
);
482 int get_implied_value(struct expression
*expr
, long long *val
)
486 *val
= _get_value(expr
, &undefined
, IMPLIED
);
490 int get_implied_min(struct expression
*expr
, long long *val
)
494 *val
= _get_value(expr
, &undefined
, IMPLIED_MIN
);
498 int get_implied_max(struct expression
*expr
, long long *val
)
502 *val
= _get_value(expr
, &undefined
, IMPLIED_MAX
);
506 int get_fuzzy_min(struct expression
*expr
, long long *val
)
510 *val
= _get_value(expr
, &undefined
, FUZZYMIN
);
514 int get_fuzzy_max(struct expression
*expr
, long long *val
)
518 *val
= _get_value(expr
, &undefined
, FUZZYMAX
);
522 int get_absolute_min(struct expression
*expr
, long long *val
)
527 type
= get_type(expr
);
529 if (get_value(expr
, val
))
533 min
= type_min(type
);
534 if (!get_implied_min(expr
, val
) || *val
< min
)
539 int get_absolute_max(struct expression
*expr
, long long *val
)
544 type
= get_type(expr
);
546 if (get_value(expr
, val
))
550 max
= type_max(type
);
551 if (!get_implied_max(expr
, val
) || *val
> max
)
553 if (*val
< type_min(type
))
558 int known_condition_true(struct expression
*expr
)
565 if (get_value(expr
, &tmp
) && tmp
)
571 int known_condition_false(struct expression
*expr
)
579 if (expr
->type
== EXPR_CALL
) {
580 if (sym_name_is("__builtin_constant_p", expr
->fn
))
586 int implied_condition_true(struct expression
*expr
)
593 if (known_condition_true(expr
))
595 if (get_implied_value(expr
, &tmp
) && tmp
)
598 if (expr
->type
== EXPR_POSTOP
)
599 return implied_condition_true(expr
->unop
);
601 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_DECREMENT
)
602 return implied_not_equal(expr
->unop
, 1);
603 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_INCREMENT
)
604 return implied_not_equal(expr
->unop
, -1);
606 expr
= strip_expr(expr
);
607 switch (expr
->type
) {
609 if (do_comparison(expr
) == 1)
613 if (expr
->op
== '!') {
614 if (implied_condition_false(expr
->unop
))
620 if (implied_not_equal(expr
, 0) == 1)
627 int implied_condition_false(struct expression
*expr
)
629 struct expression
*tmp
;
635 if (known_condition_false(expr
))
638 switch (expr
->type
) {
640 if (do_comparison(expr
) == 2)
643 if (expr
->op
== '!') {
644 if (implied_condition_true(expr
->unop
))
648 tmp
= strip_expr(expr
);
650 return implied_condition_false(tmp
);
653 if (get_implied_value(expr
, &val
) && val
== 0)