db: remove some extra semi-colons
[smatch.git] / smatch_stored_conditions.c
blobd036621ddd5923c21d74971979507bc0409b367b
1 /*
2 * Copyright (C) 2014 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 * Keep a record of all the things we have tested for so that we know when we
20 * test for it again. For example, if we have code like this:
22 * if (foo & FLAG)
23 * lock();
25 * ...
27 * if (foo & FLAG)
28 * unlock();
30 * That's the end goal at least. But actually implementing the flow of that
31 * requires quite a bit of work because if "foo" changes the condition needs to
32 * be retested and smatch_implications.c needs to be updated.
34 * For now, I just record the conditions and use it to see if we test for NULL
35 * twice.
39 #include "smatch.h"
40 #include "smatch_slist.h"
42 static int my_id;
43 static int link_id;
45 static struct smatch_state *alloc_link_state(struct string_list *links)
47 struct smatch_state *state;
48 static char buf[256];
49 char *tmp;
50 int i;
52 state = __alloc_smatch_state(0);
54 i = 0;
55 FOR_EACH_PTR(links, tmp) {
56 if (!i++) {
57 snprintf(buf, sizeof(buf), "%s", tmp);
58 } else {
59 append(buf, ", ", sizeof(buf));
60 append(buf, tmp, sizeof(buf));
62 } END_FOR_EACH_PTR(tmp);
64 state->name = alloc_sname(buf);
65 state->data = links;
66 return state;
69 static struct smatch_state *merge_links(struct smatch_state *s1, struct smatch_state *s2)
71 struct smatch_state *ret;
72 struct string_list *links;
74 links = combine_string_lists(s1->data, s2->data);
75 ret = alloc_link_state(links);
76 return ret;
79 static void save_link_var_sym(const char *var, struct symbol *sym, const char *link)
81 struct smatch_state *old_state, *new_state;
82 struct string_list *links;
83 char *new;
85 old_state = get_state(link_id, var, sym);
86 if (old_state)
87 links = clone_str_list(old_state->data);
88 else
89 links = NULL;
91 new = alloc_sname(link);
92 insert_string(&links, new);
94 new_state = alloc_link_state(links);
95 set_state(link_id, var, sym, new_state);
98 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
100 struct string_list *links;
101 char *tmp;
103 links = sm->state->data;
105 FOR_EACH_PTR(links, tmp) {
106 set_state(my_id, tmp, NULL, &undefined);
107 } END_FOR_EACH_PTR(tmp);
108 set_state(link_id, sm->name, sm->sym, &undefined);
111 static struct smatch_state *alloc_state(struct expression *expr, int is_true)
113 struct smatch_state *state;
115 state = __alloc_smatch_state(0);
116 if (is_true)
117 state->name = alloc_sname("true");
118 else
119 state->name = alloc_sname("false");
120 state->data = expr;
121 return state;
124 static void store_all_links(struct expression *expr, const char *condition)
126 char *var;
127 struct symbol *sym;
129 expr = strip_expr(expr);
131 if (is_array(expr)) {
132 var = expr_to_known_chunk_sym(expr, &sym);
133 if (var)
134 save_link_var_sym(var, sym, condition);
137 switch (expr->type) {
138 case EXPR_COMPARE:
139 case EXPR_BINOP:
140 store_all_links(expr->left, condition);
141 store_all_links(expr->right, condition);
142 return;
143 case EXPR_VALUE:
144 return;
147 var = expr_to_var_sym(expr, &sym);
148 if (!var || !sym)
149 goto free;
150 save_link_var_sym(var, sym, condition);
151 free:
152 free_string(var);
155 static int condition_too_complicated(struct expression *expr)
157 if (get_complication_score(expr) > 2)
158 return 1;
159 return 0;
162 void __stored_condition(struct expression *expr)
164 struct smatch_state *true_state, *false_state;
165 char *name;
166 sval_t val;
168 if (get_implied_value(expr, &val))
169 return;
171 if (condition_too_complicated(expr))
172 return;
174 name = expr_to_str(expr);
175 if (!name)
176 return;
177 true_state = alloc_state(expr, TRUE);
178 false_state = alloc_state(expr, FALSE);
179 set_true_false_states(my_id, name, NULL, true_state, false_state);
180 store_all_links(expr, alloc_sname(name));
181 free_string(name);
184 struct smatch_state *get_stored_condition(struct expression *expr)
186 struct smatch_state *state;
187 char *name;
189 name = expr_to_str(expr);
190 if (!name)
191 return NULL;
193 state = get_state(my_id, name, NULL);
194 free_string(name);
195 return state;
198 void register_stored_conditions(int id)
200 my_id = id;
203 void register_stored_conditions_links(int id)
205 link_id = id;
206 add_merge_hook(link_id, &merge_links);
207 add_modification_hook(link_id, &match_link_modify);
210 #define RECURSE_LIMIT 50
212 static void filter_by_sm(struct sm_state *sm,
213 struct state_list **true_stack,
214 struct state_list **false_stack,
215 int *recurse_cnt)
217 if (!sm)
218 return;
220 if ((*recurse_cnt)++ > RECURSE_LIMIT)
221 return;
223 if (strcmp(sm->state->name, "true") == 0) {
224 add_ptr_list(true_stack, sm);
225 } else if (strcmp(sm->state->name, "false") == 0) {
226 add_ptr_list(false_stack, sm);
229 if (sm->merged) {
230 filter_by_sm(sm->left, true_stack, false_stack, recurse_cnt);
231 filter_by_sm(sm->right, true_stack, false_stack, recurse_cnt);
235 struct sm_state *stored_condition_implication_hook(struct expression *expr,
236 struct state_list **true_stack,
237 struct state_list **false_stack)
239 struct sm_state *sm;
240 char *name;
241 int recurse_cnt = 0;
242 struct state_list *tmp_true = NULL;
243 struct state_list *tmp_false = NULL;
244 struct sm_state *tmp;
246 expr = strip_expr(expr);
248 name = expr_to_str(expr);
249 if (!name)
250 return NULL;
252 sm = get_sm_state(my_id, name, NULL);
253 free_string(name);
254 if (!sm)
255 return NULL;
256 if (!sm->merged)
257 return NULL;
259 filter_by_sm(sm, &tmp_true, &tmp_false, &recurse_cnt);
260 if (!tmp_true && !tmp_false)
261 return NULL;
262 if (recurse_cnt > RECURSE_LIMIT) {
263 sm = NULL;
264 goto free;
267 FOR_EACH_PTR(tmp_true, tmp) {
268 add_ptr_list(true_stack, tmp);
269 } END_FOR_EACH_PTR(tmp);
271 FOR_EACH_PTR(tmp_false, tmp) {
272 add_ptr_list(false_stack, tmp);
273 } END_FOR_EACH_PTR(tmp);
275 free:
276 free_slist(&tmp_true);
277 free_slist(&tmp_false);
278 return sm;