sval: cast the result in sval_binop()
[smatch.git] / smatch_param_filter.c
blobe8b8ca255bcfc4756ed2c544799840cbde019021
1 /*
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:
21 * void foo(int *x)
22 * {
23 * if (*x == 42)
24 * *x = 0;
25 * }
27 * The final value of *x depends on the input to the function but with *x == 42
28 * filtered out.
32 #include "smatch.h"
33 #include "smatch_extra.h"
34 #include "smatch_slist.h"
36 static int my_id;
38 static struct stree *start_states;
39 static struct stree_stack *saved_stack;
40 static void save_start_states(struct statement *stmt)
42 start_states = get_all_states_stree(SMATCH_EXTRA);
45 static void free_start_states(void)
47 free_stree(&start_states);
50 static void match_save_states(struct expression *expr)
52 push_stree(&saved_stack, start_states);
53 start_states = NULL;
56 static void match_restore_states(struct expression *expr)
58 free_stree(&start_states);
59 start_states = pop_stree(&saved_stack);
62 static struct smatch_state *unmatched_state(struct sm_state *sm)
64 struct smatch_state *state;
66 if (parent_is_gone_var_sym(sm->name, sm->sym))
67 return alloc_estate_empty();
69 state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
70 if (state)
71 return state;
72 return alloc_estate_whole(get_real_base_type(sm->sym));
75 static void pre_merge_hook(struct sm_state *sm)
77 struct smatch_state *extra, *mine;
78 struct range_list *rl;
80 if (estate_rl(sm->state))
81 return;
83 extra = get_state(SMATCH_EXTRA, sm->name, sm->sym);
84 if (!extra)
85 return;
86 mine = get_state(my_id, sm->name, sm->sym);
88 rl = rl_intersection(estate_rl(extra), estate_rl(mine));
89 if (rl_equiv(rl, estate_rl(mine)))
90 return;
91 set_state(my_id, sm->name, sm->sym, alloc_estate_rl(clone_rl(rl)));
94 static void extra_mod_hook(const char *name, struct symbol *sym, struct smatch_state *state)
96 int param;
98 param = get_param_num_from_sym(sym);
99 if (param < 0)
100 return;
102 /* on stack parameters are handled in smatch_param_limit.c */
103 if (sym->ident && strcmp(sym->ident->name, name) == 0)
104 return;
106 set_state(my_id, name, sym, alloc_estate_empty());
110 * This relies on the fact that these states are stored so that
111 * foo->bar is before foo->bar->baz.
113 static int parent_set(struct string_list *list, const char *name)
115 char *tmp;
116 int len;
117 int ret;
119 FOR_EACH_PTR(list, tmp) {
120 len = strlen(tmp);
121 ret = strncmp(tmp, name, len);
122 if (ret < 0)
123 continue;
124 if (ret > 0)
125 return 0;
126 if (name[len] == '-')
127 return 1;
128 } END_FOR_EACH_PTR(tmp);
130 return 0;
133 static void print_one_mod_param(int return_id, char *return_ranges,
134 int param, struct sm_state *sm, struct string_list **totally_filtered)
136 const char *param_name;
138 param_name = get_param_name(sm);
139 if (!param_name)
140 return;
141 if (is_whole_rl(estate_rl(sm->state)))
142 return;
143 if (!estate_rl(sm->state)) {
144 insert_string(totally_filtered, (char *)sm->name);
145 return;
148 sql_insert_return_states(return_id, return_ranges, FILTER_VALUE, param,
149 param_name, show_rl(estate_rl(sm->state)));
152 static void print_one_extra_param(int return_id, char *return_ranges,
153 int param, struct sm_state *sm, struct string_list **totally_filtered)
155 struct smatch_state *old;
156 const char *param_name;
158 if (estate_is_whole(sm->state))
159 return;
160 old = get_state_stree(start_states, SMATCH_EXTRA, sm->name, sm->sym);
161 if (old && estates_equiv(old, sm->state))
162 return;
164 param_name = get_param_name(sm);
165 if (!param_name)
166 return;
168 if (strcmp(sm->state->name, "") == 0)
169 insert_string(totally_filtered, (char *)sm->name);
171 sql_insert_return_states(return_id, return_ranges, FILTER_VALUE, param,
172 param_name, sm->state->name);
175 static void print_return_value_param(int return_id, char *return_ranges, struct expression *expr)
177 struct sm_state *tmp;
178 struct sm_state *sm;
179 struct string_list *totally_filtered = NULL;
180 int param;
182 FOR_EACH_MY_SM(SMATCH_EXTRA, __get_cur_stree(), tmp) {
183 param = get_param_num_from_sym(tmp->sym);
184 if (param < 0)
185 continue;
187 /* on stack parameters are handled in smatch_param_limit.c */
188 if (tmp->sym->ident && strcmp(tmp->sym->ident->name, tmp->name) == 0)
189 continue;
191 if (parent_set(totally_filtered, tmp->name))
192 continue;
194 sm = get_sm_state(my_id, tmp->name, tmp->sym);
195 if (sm)
196 print_one_mod_param(return_id, return_ranges, param, sm, &totally_filtered);
197 else
198 print_one_extra_param(return_id, return_ranges, param, tmp, &totally_filtered);
199 } END_FOR_EACH_SM(tmp);
201 free_ptr_list((struct ptr_list **)&totally_filtered);
204 int param_has_filter_data(struct sm_state *sm)
206 struct smatch_state *state;
208 state = get_state(my_id, sm->name, sm->sym);
209 if (!state)
210 return 1;
211 if (estate_rl(state))
212 return 1;
213 return 0;
216 void register_param_filter(int id)
218 my_id = id;
220 add_hook(&save_start_states, AFTER_DEF_HOOK);
221 add_hook(&free_start_states, END_FUNC_HOOK);
223 add_extra_mod_hook(&extra_mod_hook);
224 add_unmatched_state_hook(my_id, &unmatched_state);
225 add_pre_merge_hook(my_id, &pre_merge_hook);
226 add_merge_hook(my_id, &merge_estates);
228 add_hook(&match_save_states, INLINE_FN_START);
229 add_hook(&match_restore_states, INLINE_FN_END);
231 add_split_return_callback(&print_return_value_param);