validation: silence some uninitialized variable warnings
[smatch.git] / smatch_param_filter.c
blob7e73bf2ddb01f260bfd17e6c7d79f79af7791ae7
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 STATE(modified);
39 STATE(original);
41 static struct stree *start_states;
42 static struct stree_stack *saved_stack;
43 static void save_start_states(struct statement *stmt)
45 start_states = get_all_states_stree(SMATCH_EXTRA);
48 static void match_end_func(void)
50 free_stree(&start_states);
53 static void match_save_states(struct expression *expr)
55 push_stree(&saved_stack, start_states);
56 start_states = NULL;
59 static void match_restore_states(struct expression *expr)
61 free_stree(&start_states);
62 start_states = pop_stree(&saved_stack);
65 static struct smatch_state *unmatched_state(struct sm_state *sm)
67 if (parent_is_gone_var_sym(sm->name, sm->sym))
68 return &modified;
69 return &original;
72 static void extra_mod_hook(const char *name, struct symbol *sym, struct smatch_state *state)
74 int param;
76 param = get_param_num_from_sym(sym);
77 if (param < 0)
78 return;
80 set_state(my_id, name, sym, &modified);
84 * This relies on the fact that these states are stored so that
85 * foo->bar is before foo->bar->baz.
87 static int parent_set(struct string_list *list, const char *name)
89 char *tmp;
90 int len;
91 int ret;
93 FOR_EACH_PTR(list, tmp) {
94 len = strlen(tmp);
95 ret = strncmp(tmp, name, len);
96 if (ret < 0)
97 continue;
98 if (ret > 0)
99 return 0;
100 if (name[len] == '-')
101 return 1;
102 } END_FOR_EACH_PTR(tmp);
104 return 0;
107 static struct range_list *get_orig_rl(struct sm_state *sm)
109 struct range_list *ret = NULL;
110 struct sm_state *tmp;
111 struct smatch_state *extra;
113 FOR_EACH_PTR(sm->possible, tmp) {
114 if (tmp->state != &original)
115 continue;
116 extra = get_state_stree(tmp->pool, SMATCH_EXTRA, tmp->name, tmp->sym);
117 if (!extra) {
118 // sm_msg("debug: no value found in pool %p", tmp->pool);
119 return NULL;
121 ret = rl_union(ret, estate_rl(extra));
122 } END_FOR_EACH_PTR(tmp);
123 return ret;
126 static void print_one_mod_param(int return_id, char *return_ranges,
127 int param, struct sm_state *sm, struct string_list **totally_filtered)
129 const char *param_name;
130 struct range_list *rl;
132 param_name = get_param_name(sm);
133 if (!param_name)
134 return;
135 rl = get_orig_rl(sm);
136 if (is_whole_rl(rl))
137 return;
139 if (!rl)
140 insert_string(totally_filtered, (char *)sm->name);
142 sql_insert_return_states(return_id, return_ranges, FILTER_VALUE, param,
143 param_name, show_rl(rl));
146 static void print_one_extra_param(int return_id, char *return_ranges,
147 int param, struct sm_state *sm, struct string_list **totally_filtered)
149 struct smatch_state *old;
150 const char *param_name;
152 if (estate_is_whole(sm->state))
153 return;
154 old = get_state_stree(start_states, SMATCH_EXTRA, sm->name, sm->sym);
155 if (old && estates_equiv(old, sm->state))
156 return;
158 param_name = get_param_name(sm);
159 if (!param_name)
160 return;
162 if (strcmp(sm->state->name, "") == 0)
163 insert_string(totally_filtered, (char *)sm->name);
165 sql_insert_return_states(return_id, return_ranges, FILTER_VALUE, param,
166 param_name, sm->state->name);
169 static void print_return_value_param(int return_id, char *return_ranges, struct expression *expr)
171 struct stree *stree;
172 struct sm_state *tmp;
173 struct sm_state *sm;
174 struct string_list *totally_filtered = NULL;
175 int param;
177 stree = __get_cur_stree();
179 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
180 param = get_param_num_from_sym(tmp->sym);
181 if (param < 0)
182 continue;
184 * skip the parameter itself because that's stored on the stack
185 * and thus handled by smatch_param_limit.c.
187 if (tmp->sym->ident && strcmp(tmp->sym->ident->name, tmp->name) == 0)
188 continue;
190 if (parent_set(totally_filtered, tmp->name))
191 continue;
193 sm = get_sm_state(my_id, tmp->name, tmp->sym);
194 if (sm)
195 print_one_mod_param(return_id, return_ranges, param, sm, &totally_filtered);
196 else
197 print_one_extra_param(return_id, return_ranges, param, tmp, &totally_filtered);
198 } END_FOR_EACH_SM(tmp);
200 free_ptr_list((struct ptr_list **)&totally_filtered);
203 void register_param_filter(int id)
205 my_id = id;
207 add_hook(&save_start_states, AFTER_DEF_HOOK);
208 add_extra_mod_hook(&extra_mod_hook);
209 add_unmatched_state_hook(my_id, &unmatched_state);
210 add_split_return_callback(&print_return_value_param);
211 add_hook(&match_end_func, END_FUNC_HOOK);
212 add_hook(&match_save_states, INLINE_FN_START);
213 add_hook(&match_restore_states, INLINE_FN_END);