free: improve cross function tracking
[smatch.git] / smatch_stored_conditions.c
blob0b01dbb1fd7f8e6cd20444f221319a4cb78a2c13
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_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 int is_local_variable(struct expression *expr)
126 struct symbol *sym;
127 char *name;
129 name = expr_to_var_sym(expr, &sym);
130 free_string(name);
131 if (!sym || !sym->scope || !sym->scope->token)
132 return 0;
133 if (cmp_pos(sym->scope->token->pos, cur_func_sym->pos) < 0)
134 return 0;
135 if (is_static(expr))
136 return 0;
137 return 1;
140 static void store_all_links(struct expression *expr, const char *condition)
142 char *var;
143 struct symbol *sym;
145 expr = strip_expr(expr);
147 switch (expr->type) {
148 case EXPR_COMPARE:
149 case EXPR_BINOP:
150 store_all_links(expr->left, condition);
151 store_all_links(expr->right, condition);
152 return;
153 case EXPR_VALUE:
154 return;
157 var = expr_to_var_sym(expr, &sym);
158 if (!var || !sym)
159 goto free;
160 save_link_var_sym(var, sym, condition);
161 free:
162 free_string(var);
165 static int get_complication_score(struct expression *expr)
167 int score = 0;
169 expr = strip_expr(expr);
172 * Don't forget to keep get_complication_score() and store_all_links()
173 * in sync.
177 switch (expr->type) {
178 case EXPR_CALL:
179 return 999;
180 case EXPR_COMPARE:
181 case EXPR_BINOP:
182 score += get_complication_score(expr->left);
183 score += get_complication_score(expr->right);
184 return score;
185 case EXPR_SYMBOL:
186 if (is_local_variable(expr))
187 return 1;
188 return 999;
189 case EXPR_VALUE:
190 return 0;
191 #if 0
192 case EXPR_PREOP:
193 if (expr->op == SPECIAL_INCREMENT ||
194 expr->op == SPECIAL_DECREMENT)
195 return 999;
196 return get_complication_score(expr->unop);
197 #endif
198 default:
199 return 999;
203 static int condition_too_complicated(struct expression *expr)
205 if (get_complication_score(expr) > 2)
206 return 1;
207 return 0;
211 static void match_condition(struct expression *expr)
213 struct smatch_state *true_state, *false_state;
214 char *name;
215 sval_t val;
217 if (get_value(expr, &val))
218 return;
220 if (condition_too_complicated(expr))
221 return;
223 name = expr_to_str(expr);
224 if (!name)
225 return;
226 true_state = alloc_state(expr, TRUE);
227 false_state = alloc_state(expr, FALSE);
228 set_true_false_states(my_id, name, NULL, true_state, false_state);
229 store_all_links(expr, alloc_sname(name));
230 free_string(name);
233 struct smatch_state *get_stored_condition(struct expression *expr)
235 struct smatch_state *state;
236 char *name;
238 name = expr_to_str(expr);
239 if (!name)
240 return NULL;
242 state = get_state(my_id, name, NULL);
243 free_string(name);
244 return state;
247 void register_stored_conditions(int id)
249 my_id = id;
251 add_hook(&match_condition, CONDITION_HOOK);
254 void register_stored_conditions_links(int id)
256 link_id = id;
257 add_merge_hook(link_id, &merge_links);
258 add_modification_hook(link_id, &match_modify);