extra, db: don't use PARAM_VALUE for return states
[smatch.git] / smatch_modification_hooks.c
blob82a8d04d7df378ed2cb426f130985a57cde2765a
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 static modification_hook **hooks;
44 static modification_hook **indirect_hooks; /* parent struct modified etc */
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 state = __alloc_smatch_state(0);
56 expr = strip_expr(expr);
57 name = expr_to_str(expr);
58 state->name = alloc_sname(name);
59 free_string(name);
61 data = __alloc_modification_data(0);
62 data->prev = prev;
63 data->cur = expr;
64 state->data = data;
66 return state;
69 void add_modification_hook(int owner, modification_hook *call_back)
71 hooks[owner] = call_back;
74 void add_indirect_modification_hook(int owner, modification_hook *call_back)
76 indirect_hooks[owner] = call_back;
79 static int matches(char *name, struct symbol *sym, struct sm_state *sm)
81 int len;
83 if (sym != sm->sym)
84 return match_none;
86 len = strlen(name);
87 if (strncmp(sm->name, name, len) == 0) {
88 if (sm->name[len] == '\0')
89 return match_exact;
90 if (sm->name[len] == '-' || sm->name[len] == '.')
91 return match_indirect;
93 if (sm->name[0] != '*')
94 return match_none;
95 if (strncmp(sm->name + 1, name, len) == 0) {
96 if (sm->name[len + 1] == '\0')
97 return match_indirect;
98 if (sm->name[len + 1] == '-' || sm->name[len + 1] == '.')
99 return match_indirect;
101 return match_none;
104 static void call_modification_hooks_name_sym(char *name, struct symbol *sym, struct expression *mod_expr)
106 struct stree *stree;
107 struct sm_state *sm;
108 struct smatch_state *prev;
109 int match;
111 prev = get_state(my_id, name, sym);
112 set_state(my_id, name, sym, alloc_my_state(mod_expr, prev));
114 stree = __get_cur_stree();
116 FOR_EACH_SM(stree, sm) {
117 if (sm->owner > num_checks)
118 continue;
119 match = matches(name, sym, sm);
121 if (match && hooks[sm->owner])
122 (hooks[sm->owner])(sm, mod_expr);
124 if (match == match_indirect && indirect_hooks[sm->owner])
125 (indirect_hooks[sm->owner])(sm, mod_expr);
126 } END_FOR_EACH_SM(sm);
129 static void call_modification_hooks(struct expression *expr, struct expression *mod_expr)
131 char *name;
132 struct symbol *sym;
134 name = expr_to_known_chunk_sym(expr, &sym);
135 if (!name)
136 goto free;
137 call_modification_hooks_name_sym(name, sym, mod_expr);
138 free:
139 free_string(name);
142 static void db_param_add(struct expression *expr, int param, char *key, char *value)
144 struct expression *arg;
145 char *name;
146 struct symbol *sym;
148 while (expr->type == EXPR_ASSIGNMENT)
149 expr = strip_expr(expr->right);
150 if (expr->type != EXPR_CALL)
151 return;
153 arg = get_argument_from_call_expr(expr->args, param);
154 if (!arg)
155 return;
157 name = get_variable_from_key(arg, key, &sym);
158 if (!name || !sym)
159 goto free;
161 call_modification_hooks_name_sym(name, sym, expr);
162 free:
163 free_string(name);
166 static void match_assign(struct expression *expr)
168 call_modification_hooks(expr->left, expr);
171 static void unop_expr(struct expression *expr)
173 if (expr->op != SPECIAL_DECREMENT && expr->op != SPECIAL_INCREMENT)
174 return;
176 call_modification_hooks(expr->unop, expr);
179 static void match_call(struct expression *expr)
181 struct expression *arg, *tmp;
183 FOR_EACH_PTR(expr->args, arg) {
184 tmp = strip_expr(arg);
185 if (tmp->type == EXPR_PREOP && tmp->op == '&')
186 call_modification_hooks(tmp->unop, expr);
187 else if (option_no_db)
188 call_modification_hooks(deref_expression(tmp), expr);
189 } END_FOR_EACH_PTR(arg);
192 static void asm_expr(struct statement *stmt)
194 struct expression *expr;
195 int state = 0;
197 FOR_EACH_PTR(stmt->asm_outputs, expr) {
198 switch (state) {
199 case 0: /* identifier */
200 case 1: /* constraint */
201 state++;
202 continue;
203 case 2: /* expression */
204 state = 0;
205 call_modification_hooks(expr, NULL);
206 continue;
208 } END_FOR_EACH_PTR(expr);
211 static void scope_end(void *_sym)
213 struct symbol *sym = _sym;
214 struct expression *expr = symbol_expression(sym);
216 call_modification_hooks(expr, NULL);
219 static void match_declaration(struct symbol *sym)
221 add_scope_hook(&scope_end, sym);
224 struct smatch_state *get_modification_state(struct expression *expr)
226 return get_state_expr(my_id, expr);
229 void register_modification_hooks(int id)
231 my_id = id;
233 hooks = malloc((num_checks + 1) * sizeof(*hooks));
234 memset(hooks, 0, (num_checks + 1) * sizeof(*hooks));
235 indirect_hooks = malloc((num_checks + 1) * sizeof(*hooks));
236 memset(indirect_hooks, 0, (num_checks + 1) * sizeof(*hooks));
238 add_hook(&match_assign, ASSIGNMENT_HOOK);
239 add_hook(&unop_expr, OP_HOOK);
240 add_hook(&asm_expr, ASM_HOOK);
243 void register_modification_hooks_late(int id)
245 add_hook(&match_call, FUNCTION_CALL_HOOK);
246 select_return_states_hook(PARAM_ADD, &db_param_add);
247 select_return_states_hook(PARAM_SET, &db_param_add);
248 add_hook(&match_declaration, DECLARATION_HOOK);