2 * sparse/smatch_param_limit.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
11 * This is for functions like this:
15 * if (a >= 0 && a < 10) {
22 * If we pass in 5, it returns 1.
24 * It's a bit complicated because we can't just consider the final value, we
25 * have to always consider the passed in value.
31 #include "smatch_extra.h"
32 #include "smatch_slist.h"
38 static struct state_list
*start_states
;
39 static void save_start_states(struct statement
*stmt
)
41 start_states
= get_all_states(SMATCH_EXTRA
);
44 static void match_end_func(void)
46 free_slist(&start_states
);
49 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
54 static struct smatch_state
*filter_my_sm(struct sm_state
*sm
)
56 struct range_list
*ret
= NULL
;
58 struct smatch_state
*estate
;
60 FOR_EACH_PTR(sm
->possible
, tmp
) {
61 if (tmp
->state
== &merged
)
63 if (tmp
->state
== &original
) {
64 estate
= get_state_slist(tmp
->pool
, SMATCH_EXTRA
, tmp
->name
, tmp
->sym
);
66 sm_msg("debug: no value found in pool %p", tmp
->pool
);
72 ret
= rl_union(ret
, estate_rl(estate
));
73 } END_FOR_EACH_PTR(tmp
);
75 return alloc_estate_rl(ret
);
78 struct smatch_state
*get_orig_estate(const char *name
, struct symbol
*sym
)
81 struct smatch_state
*state
;
83 sm
= get_sm_state(my_id
, name
, sym
);
85 return filter_my_sm(sm
);
87 state
= get_state(SMATCH_EXTRA
, name
, sym
);
90 return alloc_estate_rl(alloc_whole_rl(get_real_base_type(sym
)));
93 static void print_return_value_param(int return_id
, char *return_ranges
, struct expression
*expr
, struct state_list
*slist
)
95 struct state_list
*my_slist
;
97 struct smatch_state
*state
;
100 my_slist
= get_all_states_slist(my_id
, slist
);
102 FOR_EACH_PTR(my_slist
, tmp
) {
103 param
= get_param_num_from_sym(tmp
->sym
);
106 state
= filter_my_sm(tmp
);
109 /* This represents an impossible state. I screwd up. Bail. */
110 if (!estate_rl(state
))
112 sm_msg("info: return_param_limit %d %d '%s' '$$' '%s' %s",
113 return_id
, param
, return_ranges
,
114 state
->name
, global_static());
115 } END_FOR_EACH_PTR(tmp
);
117 free_slist(&my_slist
);
122 static void extra_mod_hook(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
124 struct smatch_state
*orig_vals
;
127 param
= get_param_num_from_sym(sym
);
131 /* we are only saving params for now */
132 if (!sym
->ident
|| strcmp(name
, sym
->ident
->name
) != 0)
135 orig_vals
= get_orig_estate(name
, sym
);
136 set_state(my_id
, name
, sym
, orig_vals
);
139 void register_param_limit(int id
)
146 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
147 add_extra_mod_hook(&extra_mod_hook
);
148 add_unmatched_state_hook(my_id
, &unmatched_state
);
149 add_returned_state_callback(&print_return_value_param
);
150 add_hook(&match_end_func
, END_FUNC_HOOK
);