slist, implied: preserve the entire cur_stree in the pool for fake_strees
[smatch.git] / smatch_hooks.c
blobd200291cd9dd81afc86c86efe435fb08accedff4
1 /*
2 * Copyright (C) 2006 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
18 #include "smatch.h"
20 enum data_type {
21 EXPR_PTR,
22 STMT_PTR,
23 SYMBOL_PTR,
24 SYM_LIST_PTR,
27 struct hook_container {
28 int hook_type;
29 enum data_type data_type;
30 void *fn;
32 ALLOCATOR(hook_container, "hook functions");
33 DECLARE_PTR_LIST(hook_func_list, struct hook_container);
34 static struct hook_func_list *merge_funcs;
35 static struct hook_func_list *unmatched_state_funcs;
36 static struct hook_func_list *hook_array[NUM_HOOKS] = {};
37 void (**pre_merge_hooks)(struct sm_state *sm);
39 struct scope_container {
40 void *fn;
41 void *data;
43 ALLOCATOR(scope_container, "scope hook functions");
44 DECLARE_PTR_LIST(scope_hook_list, struct scope_container);
45 DECLARE_PTR_LIST(scope_hook_stack, struct scope_hook_list);
46 static struct scope_hook_stack *scope_hooks;
48 void add_hook(void *func, enum hook_type type)
50 struct hook_container *container = __alloc_hook_container(0);
52 container->hook_type = type;
53 container->fn = func;
54 switch (type) {
55 case EXPR_HOOK:
56 container->data_type = EXPR_PTR;
57 break;
58 case STMT_HOOK:
59 container->data_type = STMT_PTR;
60 break;
61 case STMT_HOOK_AFTER:
62 container->data_type = STMT_PTR;
63 break;
64 case SYM_HOOK:
65 container->data_type = EXPR_PTR;
66 break;
67 case STRING_HOOK:
68 container->data_type = EXPR_PTR;
69 break;
70 case DECLARATION_HOOK:
71 container->data_type = SYMBOL_PTR;
72 break;
73 case ASSIGNMENT_HOOK:
74 container->data_type = EXPR_PTR;
75 break;
76 case RAW_ASSIGNMENT_HOOK:
77 container->data_type = EXPR_PTR;
78 break;
79 case GLOBAL_ASSIGNMENT_HOOK:
80 container->data_type = EXPR_PTR;
81 break;
82 case CALL_ASSIGNMENT_HOOK:
83 container->data_type = EXPR_PTR;
84 break;
85 case MACRO_ASSIGNMENT_HOOK:
86 container->data_type = EXPR_PTR;
87 break;
88 case BINOP_HOOK:
89 container->data_type = EXPR_PTR;
90 break;
91 case OP_HOOK:
92 container->data_type = EXPR_PTR;
93 break;
94 case LOGIC_HOOK:
95 container->data_type = EXPR_PTR;
96 break;
97 case PRELOOP_HOOK:
98 container->data_type = STMT_PTR;
99 break;
100 case CONDITION_HOOK:
101 container->data_type = EXPR_PTR;
102 break;
103 case SELECT_HOOK:
104 container->data_type = EXPR_PTR;
105 break;
106 case WHOLE_CONDITION_HOOK:
107 container->data_type = EXPR_PTR;
108 break;
109 case FUNCTION_CALL_HOOK:
110 container->data_type = EXPR_PTR;
111 break;
112 case CALL_HOOK_AFTER_INLINE:
113 container->data_type = EXPR_PTR;
114 break;
115 case FUNCTION_CALL_HOOK_AFTER_DB:
116 container->data_type = EXPR_PTR;
117 break;
118 case DEREF_HOOK:
119 container->data_type = EXPR_PTR;
120 break;
121 case CASE_HOOK:
122 /* nothing needed */
123 break;
124 case ASM_HOOK:
125 container->data_type = STMT_PTR;
126 break;
127 case CAST_HOOK:
128 container->data_type = EXPR_PTR;
129 break;
130 case SIZEOF_HOOK:
131 container->data_type = EXPR_PTR;
132 break;
133 case BASE_HOOK:
134 container->data_type = SYMBOL_PTR;
135 break;
136 case FUNC_DEF_HOOK:
137 container->data_type = SYMBOL_PTR;
138 break;
139 case AFTER_DEF_HOOK:
140 container->data_type = SYMBOL_PTR;
141 break;
142 case END_FUNC_HOOK:
143 container->data_type = SYMBOL_PTR;
144 break;
145 case AFTER_FUNC_HOOK:
146 container->data_type = SYMBOL_PTR;
147 break;
148 case RETURN_HOOK:
149 container->data_type = EXPR_PTR;
150 break;
151 case INLINE_FN_START:
152 container->data_type = EXPR_PTR;
153 break;
154 case INLINE_FN_END:
155 container->data_type = EXPR_PTR;
156 break;
157 case END_FILE_HOOK:
158 container->data_type = SYM_LIST_PTR;
159 break;
161 add_ptr_list(&hook_array[type], container);
164 void add_merge_hook(int client_id, merge_func_t *func)
166 struct hook_container *container = __alloc_hook_container(0);
167 container->data_type = client_id;
168 container->fn = func;
169 add_ptr_list(&merge_funcs, container);
172 void add_unmatched_state_hook(int client_id, unmatched_func_t *func)
174 struct hook_container *container = __alloc_hook_container(0);
175 container->data_type = client_id;
176 container->fn = func;
177 add_ptr_list(&unmatched_state_funcs, container);
180 void add_pre_merge_hook(int client_id, void (*hook)(struct sm_state *sm))
182 pre_merge_hooks[client_id] = hook;
185 static void pass_to_client(void *fn)
187 typedef void (expr_func)();
188 ((expr_func *) fn)();
191 static void pass_expr_to_client(void *fn, void *data)
193 typedef void (expr_func)(struct expression *expr);
194 ((expr_func *) fn)((struct expression *) data);
197 static void pass_stmt_to_client(void *fn, void *data)
199 typedef void (stmt_func)(struct statement *stmt);
200 ((stmt_func *) fn)((struct statement *) data);
203 static void pass_sym_to_client(void *fn, void *data)
205 typedef void (sym_func)(struct symbol *sym);
206 ((sym_func *) fn)((struct symbol *) data);
209 static void pass_sym_list_to_client(void *fn, void *data)
211 typedef void (sym_func)(struct symbol_list *sym_list);
212 ((sym_func *) fn)((struct symbol_list *) data);
215 void __pass_to_client(void *data, enum hook_type type)
217 struct hook_container *container;
220 FOR_EACH_PTR(hook_array[type], container) {
221 switch (container->data_type) {
222 case EXPR_PTR:
223 pass_expr_to_client(container->fn, data);
224 break;
225 case STMT_PTR:
226 pass_stmt_to_client(container->fn, data);
227 break;
228 case SYMBOL_PTR:
229 pass_sym_to_client(container->fn, data);
230 break;
231 case SYM_LIST_PTR:
232 pass_sym_list_to_client(container->fn, data);
233 break;
235 } END_FOR_EACH_PTR(container);
238 void __pass_to_client_no_data(enum hook_type type)
240 struct hook_container *container;
242 FOR_EACH_PTR(hook_array[type], container) {
243 pass_to_client(container->fn);
244 } END_FOR_EACH_PTR(container);
247 void __pass_case_to_client(struct expression *switch_expr,
248 struct range_list *rl)
250 typedef void (case_func)(struct expression *switch_expr,
251 struct range_list *rl);
252 struct hook_container *container;
254 FOR_EACH_PTR(hook_array[CASE_HOOK], container) {
255 ((case_func *) container->fn)(switch_expr, rl);
256 } END_FOR_EACH_PTR(container);
259 int __has_merge_function(int client_id)
261 struct hook_container *tmp;
263 FOR_EACH_PTR(merge_funcs, tmp) {
264 if (tmp->data_type == client_id)
265 return 1;
266 } END_FOR_EACH_PTR(tmp);
267 return 0;
270 struct smatch_state *__client_merge_function(int owner,
271 struct smatch_state *s1,
272 struct smatch_state *s2)
274 struct smatch_state *tmp_state;
275 struct hook_container *tmp;
277 /* Pass NULL states first and the rest alphabetically by name */
278 if (!s2 || (s1 && strcmp(s2->name, s1->name) < 0)) {
279 tmp_state = s1;
280 s1 = s2;
281 s2 = tmp_state;
284 FOR_EACH_PTR(merge_funcs, tmp) {
285 if (tmp->data_type == owner)
286 return ((merge_func_t *) tmp->fn)(s1, s2);
287 } END_FOR_EACH_PTR(tmp);
288 return &undefined;
291 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm)
293 struct hook_container *tmp;
295 FOR_EACH_PTR(unmatched_state_funcs, tmp) {
296 if (tmp->data_type == sm->owner)
297 return ((unmatched_func_t *) tmp->fn)(sm);
298 } END_FOR_EACH_PTR(tmp);
299 return &undefined;
302 void call_pre_merge_hook(struct sm_state *sm)
304 if (sm->owner >= num_checks)
305 return;
307 if (pre_merge_hooks[sm->owner])
308 pre_merge_hooks[sm->owner](sm);
311 static struct scope_hook_list *pop_scope_hook_list(struct scope_hook_stack **stack)
313 struct scope_hook_list *hook_list;
315 hook_list = last_ptr_list((struct ptr_list *)*stack);
316 delete_ptr_list_last((struct ptr_list **)stack);
317 return hook_list;
320 static void push_scope_hook_list(struct scope_hook_stack **stack, struct scope_hook_list *l)
322 add_ptr_list(stack, l);
325 void add_scope_hook(scope_hook *fn, void *data)
327 struct scope_hook_list *hook_list;
328 struct scope_container *new;
330 if (!scope_hooks)
331 return;
332 hook_list = pop_scope_hook_list(&scope_hooks);
333 new = __alloc_scope_container(0);
334 new->fn = fn;
335 new->data = data;
336 add_ptr_list(&hook_list, new);
337 push_scope_hook_list(&scope_hooks, hook_list);
340 void __push_scope_hooks(void)
342 push_scope_hook_list(&scope_hooks, NULL);
345 void __call_scope_hooks(void)
347 struct scope_hook_list *hook_list;
348 struct scope_container *tmp;
350 if (!scope_hooks)
351 return;
353 hook_list = pop_scope_hook_list(&scope_hooks);
354 FOR_EACH_PTR(hook_list, tmp) {
355 ((scope_hook *) tmp->fn)(tmp->data);
356 __free_scope_container(tmp);
357 } END_FOR_EACH_PTR(tmp);
360 void allocate_hook_memory(void)
362 pre_merge_hooks = malloc(num_checks * sizeof(*pre_merge_hooks));
363 memset(pre_merge_hooks, 0, num_checks * sizeof(*pre_merge_hooks));