2 * sparse/smatch_helper.c
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 long long handle_preop(struct expression
*expr
, int *undefined
, int implied
)
58 long long ret
= BOGUS
;
62 ret
= ~ _get_value(expr
->unop
, undefined
, implied
);
63 ret
= cast_to_type(expr
->unop
, ret
);
66 ret
= - _get_value(expr
->unop
, undefined
, implied
);
69 ret
= _get_implied_value(expr
, undefined
, implied
);
77 static long long handle_binop(struct expression
*expr
, int *undefined
, int implied
)
81 long long ret
= BOGUS
;
83 if (expr
->type
!= EXPR_BINOP
) {
88 left
= _get_value(expr
->left
, undefined
, implied
);
89 right
= _get_value(expr
->right
, undefined
, implied
);
119 case SPECIAL_RIGHTSHIFT
:
122 case SPECIAL_LEFTSHIFT
:
134 static int get_implied_value_helper(struct expression
*expr
, long long *val
, int what
)
136 struct smatch_state
*state
;
140 if (get_value(expr
, val
))
143 name
= get_variable_from_expr(expr
, &sym
);
146 state
= get_state(SMATCH_EXTRA
, name
, sym
);
148 if (!state
|| !state
->data
)
151 return get_single_value_from_dinfo(get_dinfo(state
), val
);
152 if (what
== VAL_MAX
) {
153 *val
= get_dinfo_max(get_dinfo(state
));
154 if (*val
== whole_range
.max
) /* this means just guessing */
158 *val
= get_dinfo_min(get_dinfo(state
));
159 if (*val
== whole_range
.min
)
164 static int get_fuzzy_max_helper(struct expression
*expr
, long long *max
)
167 struct sm_state
*tmp
;
169 if (get_implied_max(expr
, max
))
172 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
176 *max
= whole_range
.min
;
177 FOR_EACH_PTR(sm
->possible
, tmp
) {
180 new_min
= get_dinfo_min(get_dinfo(tmp
->state
));
183 } END_FOR_EACH_PTR(tmp
);
185 if (*max
> whole_range
.min
)
190 static int get_fuzzy_min_helper(struct expression
*expr
, long long *min
)
193 struct sm_state
*tmp
;
195 if (get_implied_min(expr
, min
))
198 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
202 *min
= whole_range
.max
;
203 FOR_EACH_PTR(sm
->possible
, tmp
) {
206 new_max
= get_dinfo_max(get_dinfo(tmp
->state
));
209 } END_FOR_EACH_PTR(tmp
);
211 if (*min
< whole_range
.max
)
216 static long long _get_implied_value(struct expression
*expr
, int *undefined
, int implied
)
218 long long ret
= BOGUS
;
222 if (!get_implied_value_helper(expr
, &ret
, IMPLIED
))
226 if (!get_fuzzy_max_helper(expr
, &ret
))
230 if (!get_fuzzy_min_helper(expr
, &ret
))
239 static int get_const_value(struct expression
*expr
, long long *val
)
246 if (!(sym
->ctype
.modifiers
& MOD_CONST
))
248 if (get_value(sym
->initializer
, val
))
253 static long long _get_value(struct expression
*expr
, int *undefined
, int implied
)
255 long long ret
= BOGUS
;
264 expr
= strip_parens(expr
);
269 ret
= cast_to_type(expr
, ret
);
272 ret
= handle_preop(expr
, undefined
, implied
);
275 ret
= _get_value(expr
->unop
, undefined
, implied
);
278 case EXPR_FORCE_CAST
:
279 case EXPR_IMPLIED_CAST
:
280 ret
= _get_value(expr
->cast_expression
, undefined
, implied
);
281 return cast_to_type(expr
, ret
);
283 ret
= handle_binop(expr
, undefined
, implied
);
287 ret
= get_expression_value(expr
);
290 if (get_const_value(expr
, &ret
))
293 ret
= _get_implied_value(expr
, undefined
, implied
);
300 /* returns 1 if it can get a value literal or else returns 0 */
301 int get_value(struct expression
*expr
, long long *val
)
305 *val
= _get_value(expr
, &undefined
, NOTIMPLIED
);
311 int get_implied_value(struct expression
*expr
, long long *val
)
315 *val
= _get_value(expr
, &undefined
, IMPLIED
);
319 int get_implied_min(struct expression
*expr
, long long *val
)
321 return get_implied_value_helper(expr
, val
, VAL_MIN
);
324 int get_implied_max(struct expression
*expr
, long long *val
)
326 return get_implied_value_helper(expr
, val
, VAL_MAX
);
329 int get_fuzzy_min(struct expression
*expr
, long long *val
)
333 *val
= _get_value(expr
, &undefined
, FUZZYMIN
);
337 int get_fuzzy_max(struct expression
*expr
, long long *val
)
341 *val
= _get_value(expr
, &undefined
, FUZZYMAX
);
345 int get_absolute_min(struct expression
*expr
, long long *val
)
350 type
= get_type(expr
);
352 if (get_value(expr
, val
))
356 min
= type_min(type
);
357 if (!get_implied_min(expr
, val
) || *val
< min
)
362 int get_absolute_max(struct expression
*expr
, long long *val
)
367 type
= get_type(expr
);
369 if (get_value(expr
, val
))
373 max
= type_max(type
);
374 if (!get_implied_max(expr
, val
) || *val
> max
)
376 if (*val
< type_min(type
))