rosenberg: handle bit fields better
[smatch.git] / smatch_modification_hooks.c
blobddcc5938b54c7c1c7e11dd6f7224fbedf54e5d47
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 EARLY = 0,
39 LATE = 1,
40 BOTH = 2
43 static modification_hook **hooks;
44 static modification_hook **hooks_late;
46 ALLOCATOR(modification_data, "modification data");
48 static int my_id;
49 static struct smatch_state *alloc_my_state(struct expression *expr, struct smatch_state *prev)
51 struct smatch_state *state;
52 struct modification_data *data;
53 char *name;
55 expr = strip_expr(expr);
56 name = expr_to_str(expr);
57 if (!name)
58 return NULL;
60 state = __alloc_smatch_state(0);
61 state->name = alloc_sname(name);
62 free_string(name);
64 data = __alloc_modification_data(0);
65 data->prev = prev;
66 data->cur = expr;
67 state->data = data;
69 return state;
72 void add_modification_hook(int owner, modification_hook *call_back)
74 if (hooks[owner])
75 sm_fatal("multiple modification hooks for %s", check_name(owner));
76 hooks[owner] = call_back;
79 void add_modification_hook_late(int owner, modification_hook *call_back)
81 if (hooks_late[owner])
82 sm_fatal("multiple late modification hooks for %s", check_name(owner));
83 hooks_late[owner] = call_back;
86 static int shared_cnt(const char *one, const char *two)
88 int c = 0;
90 while (one[c] && two[c] && one[c] == two[c])
91 c++;
92 return c;
95 bool is_sub_member(const char *name, struct symbol *sym, struct sm_state *sm)
97 const char *sm_name;
98 int len;
100 if (sym != sm->sym)
101 return false;
103 sm_name = sm->name;
104 if (sm_name[0] == '&')
105 sm_name++;
107 len = shared_cnt(sm_name, name);
108 if (name[len] == '\0') {
109 if (sm_name[len] == '\0')
110 return true;
111 if (sm_name[len] == '-' || sm_name[len] == '.')
112 return true;
114 if (sm_name[0] != '*')
115 return false;
116 if (strncmp(sm_name + 1, name, len) == 0) {
117 if (sm_name[len + 1] == '\0')
118 return true;
119 if (sm_name[len + 1] == '-' || sm_name[len + 1] == '.')
120 return true;
122 return false;
125 static void call_modification_hooks_name_sym(char *name, struct symbol *sym, struct expression *mod_expr, int late)
127 struct sm_state *sm;
128 struct smatch_state *prev;
129 int match;
131 prev = get_state(my_id, name, sym);
133 if (cur_func_sym && !__in_fake_assign)
134 set_state(my_id, name, sym, alloc_my_state(mod_expr, prev));
136 FOR_EACH_SM(__get_cur_stree(), sm) {
137 if (sm->owner > num_checks)
138 continue;
139 if (!hooks[sm->owner] && !hooks_late[sm->owner])
140 continue;
141 match = is_sub_member(name, sym, sm);
142 if (!match)
143 continue;
145 if (late == EARLY || late == BOTH) {
146 if (hooks[sm->owner])
147 (hooks[sm->owner])(sm, mod_expr);
149 if (late == LATE || late == BOTH) {
150 if (hooks_late[sm->owner])
151 (hooks_late[sm->owner])(sm, mod_expr);
154 } END_FOR_EACH_SM(sm);
157 static void call_modification_hooks(struct expression *expr, struct expression *mod_expr, int late)
159 char *name;
160 struct symbol *sym;
162 name = expr_to_known_chunk_sym(expr, &sym);
163 if (!name)
164 goto free;
165 call_modification_hooks_name_sym(name, sym, mod_expr, late);
166 free:
167 free_string(name);
170 static void db_param_add(struct expression *expr, int param, char *key, char *value)
172 struct expression *arg;
173 char *name, *other_name;
174 struct symbol *sym, *other_sym;
176 while (expr->type == EXPR_ASSIGNMENT)
177 expr = strip_expr(expr->right);
178 if (expr->type != EXPR_CALL)
179 return;
181 arg = get_argument_from_call_expr(expr->args, param);
182 if (!arg)
183 return;
185 name = get_variable_from_key(arg, key, &sym);
186 if (!name || !sym)
187 goto free;
189 __in_fake_assign++;
190 call_modification_hooks_name_sym(name, sym, expr, BOTH);
191 __in_fake_assign--;
193 other_name = get_other_name_sym(name, sym, &other_sym);
194 if (other_name) {
195 __in_fake_assign++;
196 call_modification_hooks_name_sym(other_name, other_sym, expr, BOTH);
197 __in_fake_assign--;
198 free_string(other_name);
201 free:
202 free_string(name);
205 static void match_assign(struct expression *expr, int late)
207 if (__in_fake_parameter_assign)
208 return;
209 if (expr->left->smatch_flags & Fake)
210 return;
211 call_modification_hooks(expr->left, expr, late);
214 static void unop_expr(struct expression *expr, int late)
216 if (expr->op != SPECIAL_DECREMENT && expr->op != SPECIAL_INCREMENT)
217 return;
219 call_modification_hooks(expr->unop, expr, late);
222 static void match_call(struct expression *expr)
224 struct expression *arg, *tmp;
226 /* If we have the DB then trust the DB */
227 if (!option_no_db)
228 return;
230 FOR_EACH_PTR(expr->args, arg) {
231 tmp = strip_expr(arg);
232 if (tmp->type == EXPR_PREOP && tmp->op == '&')
233 call_modification_hooks(tmp->unop, expr, BOTH);
234 else
235 call_modification_hooks(deref_expression(tmp), expr, BOTH);
236 } END_FOR_EACH_PTR(arg);
239 static void asm_expr(struct statement *stmt, int late)
241 struct asm_operand *op;
243 FOR_EACH_PTR(stmt->asm_outputs, op) {
244 call_modification_hooks(op->expr, NULL, late);
245 } END_FOR_EACH_PTR(op);
248 static void match_assign_early(struct expression *expr)
250 match_assign(expr, EARLY);
253 static void unop_expr_early(struct expression *expr)
255 unop_expr(expr, EARLY);
258 static void asm_expr_early(struct statement *stmt)
260 asm_expr(stmt, EARLY);
263 static void match_assign_late(struct expression *expr)
265 match_assign(expr, LATE);
268 static void unop_expr_late(struct expression *expr)
270 unop_expr(expr, LATE);
273 static void asm_expr_late(struct statement *stmt)
275 asm_expr(stmt, LATE);
278 struct smatch_state *get_modification_state(struct expression *expr)
280 return get_state_expr(my_id, expr);
283 void register_modification_hooks(int id)
285 my_id = id;
287 set_dynamic_states(my_id);
289 hooks = malloc((num_checks + 1) * sizeof(*hooks));
290 memset(hooks, 0, (num_checks + 1) * sizeof(*hooks));
291 hooks_late = malloc((num_checks + 1) * sizeof(*hooks));
292 memset(hooks_late, 0, (num_checks + 1) * sizeof(*hooks));
294 add_hook(&match_assign_early, ASSIGNMENT_HOOK);
295 add_hook(&unop_expr_early, OP_HOOK);
296 add_hook(&asm_expr_early, ASM_HOOK);
299 void register_modification_hooks_late(int id)
301 add_hook(&match_call, FUNCTION_CALL_HOOK);
303 select_return_states_hook(PARAM_ADD, &db_param_add);
304 select_return_states_hook(PARAM_SET, &db_param_add);
306 add_hook(&match_assign_late, ASSIGNMENT_HOOK_AFTER);
307 add_hook(&unop_expr_late, OP_HOOK);
308 add_hook(&asm_expr_late, ASM_HOOK);