hooks: introduce a pre_merge hook (similar to a merge hook)
[smatch.git] / smatch_hooks.c
blobea88112bb9f6a770d46e025dd8f2c1732f93ce2a
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 *hook_funcs;
35 static struct hook_func_list *merge_funcs;
36 static struct hook_func_list *unmatched_state_funcs;
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);
51 container->hook_type = type;
52 container->fn = func;
53 switch (type) {
54 case EXPR_HOOK:
55 container->data_type = EXPR_PTR;
56 break;
57 case STMT_HOOK:
58 container->data_type = STMT_PTR;
59 break;
60 case STMT_HOOK_AFTER:
61 container->data_type = STMT_PTR;
62 break;
63 case SYM_HOOK:
64 container->data_type = EXPR_PTR;
65 break;
66 case STRING_HOOK:
67 container->data_type = EXPR_PTR;
68 break;
69 case DECLARATION_HOOK:
70 container->data_type = SYMBOL_PTR;
71 break;
72 case ASSIGNMENT_HOOK:
73 container->data_type = EXPR_PTR;
74 break;
75 case RAW_ASSIGNMENT_HOOK:
76 container->data_type = EXPR_PTR;
77 break;
78 case GLOBAL_ASSIGNMENT_HOOK:
79 container->data_type = EXPR_PTR;
80 break;
81 case CALL_ASSIGNMENT_HOOK:
82 container->data_type = EXPR_PTR;
83 break;
84 case MACRO_ASSIGNMENT_HOOK:
85 container->data_type = EXPR_PTR;
86 break;
87 case BINOP_HOOK:
88 container->data_type = EXPR_PTR;
89 break;
90 case OP_HOOK:
91 container->data_type = EXPR_PTR;
92 break;
93 case LOGIC_HOOK:
94 container->data_type = EXPR_PTR;
95 break;
96 case PRELOOP_HOOK:
97 container->data_type = STMT_PTR;
98 break;
99 case CONDITION_HOOK:
100 container->data_type = EXPR_PTR;
101 break;
102 case SELECT_HOOK:
103 container->data_type = EXPR_PTR;
104 break;
105 case WHOLE_CONDITION_HOOK:
106 container->data_type = EXPR_PTR;
107 break;
108 case FUNCTION_CALL_HOOK:
109 container->data_type = EXPR_PTR;
110 break;
111 case CALL_HOOK_AFTER_INLINE:
112 container->data_type = EXPR_PTR;
113 break;
114 case FUNCTION_CALL_HOOK_AFTER:
115 container->data_type = EXPR_PTR;
116 break;
117 case DEREF_HOOK:
118 container->data_type = EXPR_PTR;
119 break;
120 case CASE_HOOK:
121 /* nothing needed */
122 break;
123 case ASM_HOOK:
124 container->data_type = STMT_PTR;
125 break;
126 case CAST_HOOK:
127 container->data_type = EXPR_PTR;
128 break;
129 case SIZEOF_HOOK:
130 container->data_type = EXPR_PTR;
131 break;
132 case BASE_HOOK:
133 container->data_type = SYMBOL_PTR;
134 break;
135 case FUNC_DEF_HOOK:
136 container->data_type = SYMBOL_PTR;
137 break;
138 case AFTER_DEF_HOOK:
139 container->data_type = SYMBOL_PTR;
140 break;
141 case END_FUNC_HOOK:
142 container->data_type = SYMBOL_PTR;
143 break;
144 case AFTER_FUNC_HOOK:
145 container->data_type = SYMBOL_PTR;
146 break;
147 case RETURN_HOOK:
148 container->data_type = EXPR_PTR;
149 break;
150 case INLINE_FN_START:
151 container->data_type = EXPR_PTR;
152 break;
153 case INLINE_FN_END:
154 container->data_type = EXPR_PTR;
155 break;
156 case END_FILE_HOOK:
157 container->data_type = SYM_LIST_PTR;
158 break;
160 add_ptr_list(&hook_funcs, container);
163 void add_merge_hook(int client_id, merge_func_t *func)
165 struct hook_container *container = __alloc_hook_container(0);
166 container->data_type = client_id;
167 container->fn = func;
168 add_ptr_list(&merge_funcs, container);
171 void add_unmatched_state_hook(int client_id, unmatched_func_t *func)
173 struct hook_container *container = __alloc_hook_container(0);
174 container->data_type = client_id;
175 container->fn = func;
176 add_ptr_list(&unmatched_state_funcs, container);
179 void add_pre_merge_hook(int client_id, void (*hook)(struct sm_state *sm))
181 pre_merge_hooks[client_id] = hook;
184 static void pass_to_client(void *fn)
186 typedef void (expr_func)();
187 ((expr_func *) fn)();
190 static void pass_expr_to_client(void *fn, void *data)
192 typedef void (expr_func)(struct expression *expr);
193 ((expr_func *) fn)((struct expression *) data);
196 static void pass_stmt_to_client(void *fn, void *data)
198 typedef void (stmt_func)(struct statement *stmt);
199 ((stmt_func *) fn)((struct statement *) data);
202 static void pass_sym_to_client(void *fn, void *data)
204 typedef void (sym_func)(struct symbol *sym);
205 ((sym_func *) fn)((struct symbol *) data);
208 static void pass_sym_list_to_client(void *fn, void *data)
210 typedef void (sym_func)(struct symbol_list *sym_list);
211 ((sym_func *) fn)((struct symbol_list *) data);
214 void __pass_to_client(void *data, enum hook_type type)
216 struct hook_container *container;
218 FOR_EACH_PTR(hook_funcs, container) {
219 if (container->hook_type == type) {
220 switch (container->data_type) {
221 case EXPR_PTR:
222 pass_expr_to_client(container->fn, data);
223 break;
224 case STMT_PTR:
225 pass_stmt_to_client(container->fn, data);
226 break;
227 case SYMBOL_PTR:
228 pass_sym_to_client(container->fn, data);
229 break;
230 case SYM_LIST_PTR:
231 pass_sym_list_to_client(container->fn, data);
232 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_funcs, container) {
243 if (container->hook_type == type)
244 pass_to_client(container->fn);
245 } END_FOR_EACH_PTR(container);
248 void __pass_case_to_client(struct expression *switch_expr,
249 struct expression *case_expr)
251 typedef void (case_func)(struct expression *switch_expr,
252 struct expression *case_expr);
253 struct hook_container *container;
255 FOR_EACH_PTR(hook_funcs, container) {
256 if (container->hook_type == CASE_HOOK)
257 ((case_func *) container->fn)(switch_expr, case_expr);
258 } END_FOR_EACH_PTR(container);
261 int __has_merge_function(int client_id)
263 struct hook_container *tmp;
265 FOR_EACH_PTR(merge_funcs, tmp) {
266 if (tmp->data_type == client_id)
267 return 1;
268 } END_FOR_EACH_PTR(tmp);
269 return 0;
272 struct smatch_state *__client_merge_function(int owner,
273 struct smatch_state *s1,
274 struct smatch_state *s2)
276 struct smatch_state *tmp_state;
277 struct hook_container *tmp;
279 /* Pass NULL states first and the rest alphabetically by name */
280 if (!s2 || (s1 && strcmp(s2->name, s1->name) < 0)) {
281 tmp_state = s1;
282 s1 = s2;
283 s2 = tmp_state;
286 FOR_EACH_PTR(merge_funcs, tmp) {
287 if (tmp->data_type == owner)
288 return ((merge_func_t *) tmp->fn)(s1, s2);
289 } END_FOR_EACH_PTR(tmp);
290 return &undefined;
293 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm)
295 struct hook_container *tmp;
297 FOR_EACH_PTR(unmatched_state_funcs, tmp) {
298 if (tmp->data_type == sm->owner)
299 return ((unmatched_func_t *) tmp->fn)(sm);
300 } END_FOR_EACH_PTR(tmp);
301 return &undefined;
304 void call_pre_merge_hook(struct sm_state *sm)
306 if (sm->owner > num_checks)
307 return;
309 if (pre_merge_hooks[sm->owner])
310 pre_merge_hooks[sm->owner](sm);
313 static struct scope_hook_list *pop_scope_hook_list(struct scope_hook_stack **stack)
315 struct scope_hook_list *hook_list;
317 hook_list = last_ptr_list((struct ptr_list *)*stack);
318 delete_ptr_list_last((struct ptr_list **)stack);
319 return hook_list;
322 static void push_scope_hook_list(struct scope_hook_stack **stack, struct scope_hook_list *l)
324 add_ptr_list(stack, l);
327 void add_scope_hook(scope_hook *fn, void *data)
329 struct scope_hook_list *hook_list;
330 struct scope_container *new;
332 if (!scope_hooks)
333 return;
334 hook_list = pop_scope_hook_list(&scope_hooks);
335 new = __alloc_scope_container(0);
336 new->fn = fn;
337 new->data = data;
338 add_ptr_list(&hook_list, new);
339 push_scope_hook_list(&scope_hooks, hook_list);
342 void __push_scope_hooks(void)
344 push_scope_hook_list(&scope_hooks, NULL);
347 void __call_scope_hooks(void)
349 struct scope_hook_list *hook_list;
350 struct scope_container *tmp;
352 if (!scope_hooks)
353 return;
355 hook_list = pop_scope_hook_list(&scope_hooks);
356 FOR_EACH_PTR(hook_list, tmp) {
357 ((scope_hook *) tmp->fn)(tmp->data);
358 __free_scope_container(tmp);
359 } END_FOR_EACH_PTR(tmp);
362 void allocate_hook_memory(void)
364 pre_merge_hooks = malloc((num_checks + 1) * sizeof(*pre_merge_hooks));
365 memset(pre_merge_hooks, 0, (num_checks + 1) * sizeof(*pre_merge_hooks));