kernel_atomic_dec_test_path: move this into a separate module
[smatch.git] / smatch_hooks.c
blobbd0e04a4dedf3445dac4756f6636c1c6c3299458
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 NO_DATA,
22 EXPR_PTR,
23 STMT_PTR,
24 SYMBOL_PTR,
25 SYM_LIST_PTR,
28 struct hook_container {
29 int hook_type;
30 int owner;
31 void *fn;
33 ALLOCATOR(hook_container, "hook functions");
34 DECLARE_PTR_LIST(hook_func_list, struct hook_container);
36 typedef void (sym_list_func)(struct symbol_list *sym_list);
37 typedef void (array_init_hook)(struct expression *array, int nr);
39 static struct hook_func_list *merge_funcs;
40 static struct hook_func_list *unmatched_state_funcs;
41 static struct hook_func_list *array_init_hooks;
42 static struct hook_func_list *hook_array[NUM_HOOKS] = {};
43 static const enum data_type data_types[NUM_HOOKS] = {
44 [EXPR_HOOK] = EXPR_PTR,
45 [EXPR_HOOK_AFTER] = EXPR_PTR,
46 [STMT_HOOK] = STMT_PTR,
47 [STMT_HOOK_AFTER] = STMT_PTR,
48 [SYM_HOOK] = EXPR_PTR,
49 [STRING_HOOK] = EXPR_PTR,
50 [DECLARATION_HOOK] = SYMBOL_PTR,
51 [DECLARATION_HOOK_AFTER] = SYMBOL_PTR,
52 [ASSIGNMENT_HOOK] = EXPR_PTR,
53 [ASSIGNMENT_HOOK_AFTER] = EXPR_PTR,
54 [RAW_ASSIGNMENT_HOOK] = EXPR_PTR,
55 [GLOBAL_ASSIGNMENT_HOOK] = EXPR_PTR,
56 [CALL_ASSIGNMENT_HOOK] = EXPR_PTR,
57 [MACRO_ASSIGNMENT_HOOK] = EXPR_PTR,
58 [BINOP_HOOK] = EXPR_PTR,
59 [OP_HOOK] = EXPR_PTR,
60 [LOGIC_HOOK] = EXPR_PTR,
61 [PRELOOP_HOOK] = STMT_PTR,
62 [POSTLOOP_HOOK] = STMT_PTR,
63 [AFTER_LOOP_NO_BREAKS] = STMT_PTR,
64 [CONDITION_HOOK] = EXPR_PTR,
65 [SELECT_HOOK] = EXPR_PTR,
66 [WHOLE_CONDITION_HOOK] = EXPR_PTR,
67 [FUNCTION_CALL_HOOK_BEFORE] = EXPR_PTR,
68 [FUNCTION_CALL_HOOK] = EXPR_PTR,
69 [CALL_HOOK_AFTER_INLINE] = EXPR_PTR,
70 [FUNCTION_CALL_HOOK_AFTER_DB] = EXPR_PTR,
71 [DEREF_HOOK] = EXPR_PTR,
72 [CASE_HOOK] = NO_DATA,
73 [ASM_HOOK] = STMT_PTR,
74 [CAST_HOOK] = EXPR_PTR,
75 [SIZEOF_HOOK] = EXPR_PTR,
76 [BASE_HOOK] = SYMBOL_PTR,
77 [FUNC_DEF_HOOK] = SYMBOL_PTR,
78 [AFTER_DEF_HOOK] = SYMBOL_PTR,
79 [END_FUNC_HOOK] = SYMBOL_PTR,
80 [AFTER_FUNC_HOOK] = SYMBOL_PTR,
81 [RETURN_HOOK] = EXPR_PTR,
82 [INLINE_FN_START] = EXPR_PTR,
83 [INLINE_FN_END] = EXPR_PTR,
84 [END_FILE_HOOK] = SYM_LIST_PTR,
87 void (**pre_merge_hooks)(struct sm_state *cur, struct sm_state *other);
89 struct scope_container {
90 void *fn;
91 void *data;
93 ALLOCATOR(scope_container, "scope hook functions");
94 DECLARE_PTR_LIST(scope_hook_list, struct scope_container);
95 DECLARE_PTR_LIST(scope_hook_stack, struct scope_hook_list);
96 static struct scope_hook_stack *scope_hooks;
98 extern int __cur_check_id;
99 void add_hook(void *func, enum hook_type type)
101 struct hook_container *container = __alloc_hook_container(0);
103 container->owner = __cur_check_id;
104 container->hook_type = type;
105 container->fn = func;
107 add_ptr_list(&hook_array[type], container);
110 void add_merge_hook(int client_id, merge_func_t *func)
112 struct hook_container *container = __alloc_hook_container(0);
113 container->owner = client_id;
114 container->fn = func;
115 add_ptr_list(&merge_funcs, container);
118 void add_unmatched_state_hook(int client_id, unmatched_func_t *func)
120 struct hook_container *container = __alloc_hook_container(0);
121 container->owner = client_id;
122 container->fn = func;
123 add_ptr_list(&unmatched_state_funcs, container);
126 void add_pre_merge_hook(int client_id, void (*hook)(struct sm_state *cur, struct sm_state *other))
128 pre_merge_hooks[client_id] = hook;
131 static void pass_expr_to_client(void *fn, void *data)
133 ((expr_func *)fn)((struct expression *)data);
136 static void pass_stmt_to_client(void *fn, void *data)
138 ((stmt_func *)fn)((struct statement *)data);
141 static void pass_sym_to_client(void *fn, void *data)
143 ((sym_func *)fn)((struct symbol *)data);
146 static void pass_sym_list_to_client(void *fn, void *data)
148 ((sym_list_func *)fn)((struct symbol_list *)data);
151 void __pass_to_client(void *data, enum hook_type type)
153 struct hook_container *container;
155 if (__debug_skip)
156 return;
158 FOR_EACH_PTR(hook_array[type], container) {
159 switch (data_types[type]) {
160 case EXPR_PTR:
161 pass_expr_to_client(container->fn, data);
162 break;
163 case STMT_PTR:
164 pass_stmt_to_client(container->fn, data);
165 break;
166 case SYMBOL_PTR:
167 pass_sym_to_client(container->fn, data);
168 break;
169 case SYM_LIST_PTR:
170 pass_sym_list_to_client(container->fn, data);
171 break;
172 default:
173 sm_warning("internal error. Unhandled hook type: %d", type);
175 } END_FOR_EACH_PTR(container);
178 void __pass_case_to_client(struct expression *switch_expr,
179 struct range_list *rl)
181 typedef void (case_func)(struct expression *switch_expr,
182 struct range_list *rl);
183 struct hook_container *container;
185 FOR_EACH_PTR(hook_array[CASE_HOOK], container) {
186 ((case_func *)container->fn)(switch_expr, rl);
187 } END_FOR_EACH_PTR(container);
190 int __has_merge_function(int client_id)
192 struct hook_container *tmp;
194 FOR_EACH_PTR(merge_funcs, tmp) {
195 if (tmp->owner == client_id)
196 return 1;
197 } END_FOR_EACH_PTR(tmp);
198 return 0;
201 struct smatch_state *__client_merge_function(int owner,
202 struct smatch_state *s1,
203 struct smatch_state *s2)
205 struct smatch_state *tmp_state;
206 struct hook_container *tmp;
208 /* Pass NULL states first and the rest alphabetically by name */
209 if (!s2 || (s1 && strcmp(s2->name, s1->name) < 0)) {
210 tmp_state = s1;
211 s1 = s2;
212 s2 = tmp_state;
215 FOR_EACH_PTR(merge_funcs, tmp) {
216 if (tmp->owner == owner)
217 return ((merge_func_t *)tmp->fn)(s1, s2);
218 } END_FOR_EACH_PTR(tmp);
219 return &undefined;
222 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm)
224 struct hook_container *tmp;
226 FOR_EACH_PTR(unmatched_state_funcs, tmp) {
227 if (tmp->owner == sm->owner)
228 return ((unmatched_func_t *)tmp->fn)(sm);
229 } END_FOR_EACH_PTR(tmp);
230 return &undefined;
233 void call_pre_merge_hook(struct sm_state *cur, struct sm_state *other)
235 if (cur->owner >= num_checks)
236 return;
238 if (pre_merge_hooks[cur->owner])
239 pre_merge_hooks[cur->owner](cur, other);
242 static struct scope_hook_list *pop_scope_hook_list(struct scope_hook_stack **stack)
244 struct scope_hook_list *hook_list;
246 hook_list = last_ptr_list((struct ptr_list *)*stack);
247 delete_ptr_list_last((struct ptr_list **)stack);
248 return hook_list;
251 static void push_scope_hook_list(struct scope_hook_stack **stack, struct scope_hook_list *l)
253 add_ptr_list(stack, l);
256 void add_scope_hook(scope_hook *fn, void *data)
258 struct scope_hook_list *hook_list;
259 struct scope_container *new;
261 if (!scope_hooks)
262 return;
263 hook_list = pop_scope_hook_list(&scope_hooks);
264 new = __alloc_scope_container(0);
265 new->fn = fn;
266 new->data = data;
267 add_ptr_list(&hook_list, new);
268 push_scope_hook_list(&scope_hooks, hook_list);
271 void __push_scope_hooks(void)
273 push_scope_hook_list(&scope_hooks, NULL);
276 void __call_scope_hooks(void)
278 struct scope_hook_list *hook_list;
279 struct scope_container *tmp;
281 if (!scope_hooks)
282 return;
284 hook_list = pop_scope_hook_list(&scope_hooks);
285 FOR_EACH_PTR_REVERSE(hook_list, tmp) {
286 ((scope_hook *)tmp->fn)(tmp->data);
287 __free_scope_container(tmp);
288 } END_FOR_EACH_PTR_REVERSE(tmp);
291 void add_array_initialized_hook(void (*hook)(struct expression *array, int nr))
293 struct hook_container *container = __alloc_hook_container(0);
295 container->fn = hook;
297 add_ptr_list(&array_init_hooks, container);
300 void __call_array_initialized_hooks(struct expression *array, int nr)
302 struct hook_container *tmp;
304 FOR_EACH_PTR(array_init_hooks, tmp) {
305 ((array_init_hook *)tmp->fn)(array, nr);
306 } END_FOR_EACH_PTR(tmp);
309 void allocate_hook_memory(void)
311 pre_merge_hooks = malloc(num_checks * sizeof(*pre_merge_hooks));
312 memset(pre_merge_hooks, 0, num_checks * sizeof(*pre_merge_hooks));
315 void call_void_fns(struct void_fn_list *list)
317 void_fn *fn;
319 FOR_EACH_PTR(list, fn) {
320 (fn)();
321 } END_FOR_EACH_PTR(fn);
324 void call_expr_fns(struct expr_fn_list *list, struct expression *expr)
326 expr_func *fn;
328 FOR_EACH_PTR(list, fn) {
329 (fn)(expr);
330 } END_FOR_EACH_PTR(fn);
333 void call_stmt_fns(struct stmt_fn_list *list, struct statement *stmt)
335 stmt_func *fn;
337 FOR_EACH_PTR(list, fn) {
338 (fn)(stmt);
339 } END_FOR_EACH_PTR(fn);
342 void call_sym_fns(struct sym_fn_list *list, struct symbol *sym)
344 sym_func *fn;
346 FOR_EACH_PTR(list, fn) {
347 (fn)(sym);
348 } END_FOR_EACH_PTR(fn);
351 void call_name_sym_fns(struct name_sym_fn_list *list, struct expression *expr, const char *name, struct symbol *sym)
353 name_sym_hook *fn;
355 FOR_EACH_PTR(list, fn) {
356 (fn)(expr, name, sym);
357 } END_FOR_EACH_PTR(fn);
360 void call_string_hooks(struct string_hook_list *list, struct expression *expr, const char *str)
362 string_hook *fn;
364 FOR_EACH_PTR(list, fn) {
365 (fn)(expr, str);
366 } END_FOR_EACH_PTR(fn);