2 * Copyright (C) 2012 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * This is for functions like this:
23 * if (a >= 0 && a < 10) {
30 * If we pass in 5, it returns 1.
32 * It's a bit complicated because we can't just consider the final value, we
33 * have to always consider the passed in value.
39 #include "smatch_extra.h"
40 #include "smatch_slist.h"
46 static struct stree
*start_states
;
47 static struct stree_stack
*saved_stack
;
49 static void save_start_states(struct statement
*stmt
)
51 start_states
= get_all_states_stree(SMATCH_EXTRA
);
54 static void match_end_func(void)
56 free_stree(&start_states
);
59 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
64 static struct smatch_state
*filter_my_sm(struct sm_state
*sm
)
66 struct range_list
*ret
= NULL
;
68 struct smatch_state
*estate
;
70 FOR_EACH_PTR(sm
->possible
, tmp
) {
71 if (tmp
->state
== &merged
)
73 if (tmp
->state
== &original
) {
74 estate
= get_state_stree(tmp
->pool
, SMATCH_EXTRA
, tmp
->name
, tmp
->sym
);
76 // sm_msg("debug: no value found in pool %p", tmp->pool);
82 ret
= rl_union(ret
, estate_rl(estate
));
83 } END_FOR_EACH_PTR(tmp
);
85 return alloc_estate_rl(ret
);
88 struct smatch_state
*get_orig_estate(const char *name
, struct symbol
*sym
)
91 struct smatch_state
*state
;
93 sm
= get_sm_state(my_id
, name
, sym
);
95 return filter_my_sm(sm
);
97 state
= get_state(SMATCH_EXTRA
, name
, sym
);
100 return alloc_estate_rl(alloc_whole_rl(get_real_base_type(sym
)));
103 static void print_return_value_param(int return_id
, char *return_ranges
, struct expression
*expr
)
106 struct sm_state
*tmp
;
107 struct sm_state
*my_sm
;
108 struct smatch_state
*state
;
111 const char *compare_str
;
114 stree
= __get_cur_stree();
116 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
117 if (!tmp
->sym
|| !tmp
->sym
->ident
|| strcmp(tmp
->name
, tmp
->sym
->ident
->name
) != 0)
120 param
= get_param_num_from_sym(tmp
->sym
);
124 compare
= expr_equal_to_param(symbol_expression(tmp
->sym
), param
);
126 compare
= expr_lte_to_param(symbol_expression(tmp
->sym
), param
);
127 compare_str
= compare
;
131 my_sm
= get_sm_state(my_id
, tmp
->name
, tmp
->sym
);
133 struct smatch_state
*old
;
135 if (estate_is_whole(tmp
->state
) && !compare
)
137 old
= get_state_stree(start_states
, SMATCH_EXTRA
, tmp
->name
, tmp
->sym
);
138 if (old
&& estates_equiv(old
, tmp
->state
) && !compare
)
141 snprintf(buf
, sizeof(buf
), "%s%s", tmp
->state
->name
, compare_str
);
142 sql_insert_return_states(return_id
, return_ranges
,
143 LIMITED_VALUE
, param
, "$$", buf
);
147 state
= filter_my_sm(my_sm
);
150 /* This represents an impossible state. I screwd up. Bail. */
151 if (!estate_rl(state
))
153 if (estate_is_whole(state
) && !compare
) {
157 snprintf(buf
, sizeof(buf
), "%s%s", state
->name
, compare_str
);
158 sql_insert_return_states(return_id
, return_ranges
,
159 LIMITED_VALUE
, param
, "$$", buf
);
160 } END_FOR_EACH_SM(tmp
);
163 static void extra_mod_hook(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
165 struct smatch_state
*orig_vals
;
168 param
= get_param_num_from_sym(sym
);
172 /* we are only saving params for now */
173 if (!sym
->ident
|| strcmp(name
, sym
->ident
->name
) != 0)
176 orig_vals
= get_orig_estate(name
, sym
);
177 set_state(my_id
, name
, sym
, orig_vals
);
180 static void match_save_states(struct expression
*expr
)
182 push_stree(&saved_stack
, start_states
);
186 static void match_restore_states(struct expression
*expr
)
188 free_stree(&start_states
);
189 start_states
= pop_stree(&saved_stack
);
192 void register_param_limit(int id
)
196 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
197 add_extra_mod_hook(&extra_mod_hook
);
198 add_unmatched_state_hook(my_id
, &unmatched_state
);
199 add_split_return_callback(&print_return_value_param
);
200 add_hook(&match_end_func
, END_FUNC_HOOK
);
201 add_hook(&match_save_states
, INLINE_FN_START
);
202 add_hook(&match_restore_states
, INLINE_FN_END
);