db: prevent slow downs splitting returns apart
[smatch.git] / smatch_modification_hooks.c
blobe0cd641fed5eb1be8b0fd1c79ddc8c323859ce49
1 /*
2 * Copyright (C) 2009 Dan Carpenter.
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 * There are a number of ways that variables are modified:
20 * 1) assignment
21 * 2) increment/decrement
22 * 3) assembly
23 * 4) inside functions.
25 * For setting stuff inside a function then, of course, it's more accurate if
26 * you have the cross function database built. Otherwise we are super
27 * aggressive about marking things as modified and if you have "frob(foo);" then
28 * we assume "foo->bar" is modified.
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include "smatch.h"
34 #include "smatch_extra.h"
35 #include "smatch_slist.h"
37 enum {
38 match_none = 0,
39 match_exact,
40 match_indirect
43 enum {
44 EARLY = 0,
45 LATE = 1,
46 BOTH = 2
49 static modification_hook **hooks;
50 static modification_hook **indirect_hooks; /* parent struct modified etc */
51 static modification_hook **hooks_late;
52 static modification_hook **indirect_hooks_late; /* parent struct modified etc */
54 ALLOCATOR(modification_data, "modification data");
56 static int my_id;
57 static struct smatch_state *alloc_my_state(struct expression *expr, struct smatch_state *prev)
59 struct smatch_state *state;
60 struct modification_data *data;
61 char *name;
63 state = __alloc_smatch_state(0);
64 expr = strip_expr(expr);
65 name = expr_to_str(expr);
66 state->name = alloc_sname(name);
67 free_string(name);
69 data = __alloc_modification_data(0);
70 data->prev = prev;
71 data->cur = expr;
72 state->data = data;
74 return state;
77 void add_modification_hook(int owner, modification_hook *call_back)
79 hooks[owner] = call_back;
82 void add_indirect_modification_hook(int owner, modification_hook *call_back)
84 indirect_hooks[owner] = call_back;
87 void add_modification_hook_late(int owner, modification_hook *call_back)
89 hooks_late[owner] = call_back;
92 void add_indirect_modification_hook_late(int owner, modification_hook *call_back)
94 indirect_hooks_late[owner] = call_back;
97 static int matches(char *name, struct symbol *sym, struct sm_state *sm)
99 int len;
101 if (sym != sm->sym)
102 return match_none;
104 len = strlen(name);
105 if (strncmp(sm->name, name, len) == 0) {
106 if (sm->name[len] == '\0')
107 return match_exact;
108 if (sm->name[len] == '-' || sm->name[len] == '.')
109 return match_indirect;
111 if (sm->name[0] != '*')
112 return match_none;
113 if (strncmp(sm->name + 1, name, len) == 0) {
114 if (sm->name[len + 1] == '\0')
115 return match_indirect;
116 if (sm->name[len + 1] == '-' || sm->name[len + 1] == '.')
117 return match_indirect;
119 return match_none;
122 static void call_modification_hooks_name_sym(char *name, struct symbol *sym, struct expression *mod_expr, int late)
124 struct sm_state *sm;
125 struct smatch_state *prev;
126 int match;
128 prev = get_state(my_id, name, sym);
129 set_state(my_id, name, sym, alloc_my_state(mod_expr, prev));
131 FOR_EACH_SM(__get_cur_stree(), sm) {
132 if (sm->owner > num_checks)
133 continue;
134 match = matches(name, sym, sm);
135 if (!match)
136 continue;
138 if (late == EARLY || late == BOTH) {
139 if (hooks[sm->owner])
140 (hooks[sm->owner])(sm, mod_expr);
141 if (match == match_indirect && indirect_hooks[sm->owner])
142 (indirect_hooks[sm->owner])(sm, mod_expr);
144 if (late == LATE || late == BOTH) {
145 if (hooks_late[sm->owner])
146 (hooks_late[sm->owner])(sm, mod_expr);
147 if (match == match_indirect && indirect_hooks_late[sm->owner])
148 (indirect_hooks_late[sm->owner])(sm, mod_expr);
151 } END_FOR_EACH_SM(sm);
154 static void call_modification_hooks(struct expression *expr, struct expression *mod_expr, int late)
156 char *name;
157 struct symbol *sym;
159 name = expr_to_known_chunk_sym(expr, &sym);
160 if (!name)
161 goto free;
162 call_modification_hooks_name_sym(name, sym, mod_expr, late);
163 free:
164 free_string(name);
167 static void db_param_add(struct expression *expr, int param, char *key, char *value)
169 struct expression *arg;
170 char *name;
171 struct symbol *sym;
173 while (expr->type == EXPR_ASSIGNMENT)
174 expr = strip_expr(expr->right);
175 if (expr->type != EXPR_CALL)
176 return;
178 arg = get_argument_from_call_expr(expr->args, param);
179 if (!arg)
180 return;
182 name = get_variable_from_key(arg, key, &sym);
183 if (!name || !sym)
184 goto free;
186 call_modification_hooks_name_sym(name, sym, expr, BOTH);
187 free:
188 free_string(name);
191 static void match_assign(struct expression *expr, int late)
193 call_modification_hooks(expr->left, expr, late);
196 static void unop_expr(struct expression *expr, int late)
198 if (expr->op != SPECIAL_DECREMENT && expr->op != SPECIAL_INCREMENT)
199 return;
201 call_modification_hooks(expr->unop, expr, late);
204 static void match_call(struct expression *expr)
206 struct expression *arg, *tmp;
208 FOR_EACH_PTR(expr->args, arg) {
209 tmp = strip_expr(arg);
210 if (tmp->type == EXPR_PREOP && tmp->op == '&')
211 call_modification_hooks(tmp->unop, expr, BOTH);
212 else if (option_no_db)
213 call_modification_hooks(deref_expression(tmp), expr, BOTH);
214 } END_FOR_EACH_PTR(arg);
217 static void asm_expr(struct statement *stmt, int late)
219 struct expression *expr;
220 int state = 0;
222 FOR_EACH_PTR(stmt->asm_outputs, expr) {
223 switch (state) {
224 case 0: /* identifier */
225 case 1: /* constraint */
226 state++;
227 continue;
228 case 2: /* expression */
229 state = 0;
230 call_modification_hooks(expr, NULL, late);
231 continue;
233 } END_FOR_EACH_PTR(expr);
237 static void match_assign_early(struct expression *expr)
239 match_assign(expr, EARLY);
242 static void unop_expr_early(struct expression *expr)
244 unop_expr(expr, EARLY);
247 static void asm_expr_early(struct statement *stmt)
249 asm_expr(stmt, EARLY);
252 static void match_assign_late(struct expression *expr)
254 match_assign(expr, LATE);
257 static void unop_expr_late(struct expression *expr)
259 unop_expr(expr, LATE);
262 static void asm_expr_late(struct statement *stmt)
264 asm_expr(stmt, LATE);
267 struct smatch_state *get_modification_state(struct expression *expr)
269 return get_state_expr(my_id, expr);
272 void register_modification_hooks(int id)
274 my_id = id;
276 hooks = malloc((num_checks + 1) * sizeof(*hooks));
277 memset(hooks, 0, (num_checks + 1) * sizeof(*hooks));
278 indirect_hooks = malloc((num_checks + 1) * sizeof(*hooks));
279 memset(indirect_hooks, 0, (num_checks + 1) * sizeof(*hooks));
280 hooks_late = malloc((num_checks + 1) * sizeof(*hooks));
281 memset(hooks_late, 0, (num_checks + 1) * sizeof(*hooks));
282 indirect_hooks_late = malloc((num_checks + 1) * sizeof(*hooks));
283 memset(indirect_hooks_late, 0, (num_checks + 1) * sizeof(*hooks));
285 add_hook(&match_assign_early, ASSIGNMENT_HOOK);
286 add_hook(&unop_expr_early, OP_HOOK);
287 add_hook(&asm_expr_early, ASM_HOOK);
290 void register_modification_hooks_late(int id)
292 add_hook(&match_call, FUNCTION_CALL_HOOK);
294 add_hook(&match_assign_late, ASSIGNMENT_HOOK_AFTER);
295 add_hook(&unop_expr_late, OP_HOOK);
296 add_hook(&asm_expr_late, ASM_HOOK);
298 select_return_states_hook(PARAM_ADD, &db_param_add);
299 select_return_states_hook(PARAM_SET, &db_param_add);