atomic_dec_test_path: hack around assignments
[smatch.git] / smatch_hooks.c
blob8de9547f02073037ee39fdeab439f97991dd00bf
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;
97 int my_id;
99 extern int __cur_check_id;
100 void add_hook(void *func, enum hook_type type)
102 struct hook_container *container = __alloc_hook_container(0);
104 container->owner = __cur_check_id;
105 container->hook_type = type;
106 container->fn = func;
108 add_ptr_list(&hook_array[type], container);
111 void add_merge_hook(int client_id, merge_func_t *func)
113 struct hook_container *container = __alloc_hook_container(0);
114 container->owner = client_id;
115 container->fn = func;
116 add_ptr_list(&merge_funcs, container);
119 void add_unmatched_state_hook(int client_id, unmatched_func_t *func)
121 struct hook_container *container = __alloc_hook_container(0);
122 container->owner = client_id;
123 container->fn = func;
124 add_ptr_list(&unmatched_state_funcs, container);
127 void add_pre_merge_hook(int client_id, void (*hook)(struct sm_state *cur, struct sm_state *other))
129 pre_merge_hooks[client_id] = hook;
132 static void pass_expr_to_client(void *fn, void *data)
134 ((expr_func *)fn)((struct expression *)data);
137 static void pass_stmt_to_client(void *fn, void *data)
139 ((stmt_func *)fn)((struct statement *)data);
142 static void pass_sym_to_client(void *fn, void *data)
144 ((sym_func *)fn)((struct symbol *)data);
147 static void pass_sym_list_to_client(void *fn, void *data)
149 ((sym_list_func *)fn)((struct symbol_list *)data);
152 void __pass_to_client(void *data, enum hook_type type)
154 struct hook_container *container;
156 if (__debug_skip)
157 return;
159 FOR_EACH_PTR(hook_array[type], container) {
160 switch (data_types[type]) {
161 case EXPR_PTR:
162 pass_expr_to_client(container->fn, data);
163 break;
164 case STMT_PTR:
165 pass_stmt_to_client(container->fn, data);
166 break;
167 case SYMBOL_PTR:
168 pass_sym_to_client(container->fn, data);
169 break;
170 case SYM_LIST_PTR:
171 pass_sym_list_to_client(container->fn, data);
172 break;
173 default:
174 sm_warning("internal error. Unhandled hook type: %d", type);
176 } END_FOR_EACH_PTR(container);
179 void __pass_case_to_client(struct expression *switch_expr,
180 struct range_list *rl)
182 typedef void (case_func)(struct expression *switch_expr,
183 struct range_list *rl);
184 struct hook_container *container;
186 FOR_EACH_PTR(hook_array[CASE_HOOK], container) {
187 ((case_func *)container->fn)(switch_expr, rl);
188 } END_FOR_EACH_PTR(container);
191 int __has_merge_function(int client_id)
193 struct hook_container *tmp;
195 FOR_EACH_PTR(merge_funcs, tmp) {
196 if (tmp->owner == client_id)
197 return 1;
198 } END_FOR_EACH_PTR(tmp);
199 return 0;
202 struct smatch_state *__client_merge_function(int owner,
203 struct smatch_state *s1,
204 struct smatch_state *s2)
206 struct smatch_state *tmp_state;
207 struct hook_container *tmp;
209 /* Pass NULL states first and the rest alphabetically by name */
210 if (!s2 || (s1 && strcmp(s2->name, s1->name) < 0)) {
211 tmp_state = s1;
212 s1 = s2;
213 s2 = tmp_state;
216 FOR_EACH_PTR(merge_funcs, tmp) {
217 if (tmp->owner == owner)
218 return ((merge_func_t *)tmp->fn)(s1, s2);
219 } END_FOR_EACH_PTR(tmp);
220 return &undefined;
223 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm)
225 struct hook_container *tmp;
227 FOR_EACH_PTR(unmatched_state_funcs, tmp) {
228 if (tmp->owner == sm->owner)
229 return ((unmatched_func_t *)tmp->fn)(sm);
230 } END_FOR_EACH_PTR(tmp);
231 return &undefined;
234 void call_pre_merge_hook(struct sm_state *cur, struct sm_state *other)
236 if (cur->owner >= num_checks)
237 return;
239 if (pre_merge_hooks[cur->owner])
240 pre_merge_hooks[cur->owner](cur, other);
243 static struct scope_hook_list *pop_scope_hook_list(struct scope_hook_stack **stack)
245 struct scope_hook_list *hook_list;
247 hook_list = last_ptr_list((struct ptr_list *)*stack);
248 delete_ptr_list_last((struct ptr_list **)stack);
249 return hook_list;
252 static void push_scope_hook_list(struct scope_hook_stack **stack, struct scope_hook_list *list)
254 add_ptr_list(stack, list);
257 void add_scope_hook(scope_hook *fn, void *data)
259 struct scope_hook_list *hook_list;
260 struct scope_container *new;
262 if (!scope_hooks)
263 return;
264 hook_list = pop_scope_hook_list(&scope_hooks);
265 new = __alloc_scope_container(0);
266 new->fn = fn;
267 new->data = data;
268 add_ptr_list(&hook_list, new);
269 push_scope_hook_list(&scope_hooks, hook_list);
272 void __push_scope_hooks(void)
274 push_scope_hook_list(&scope_hooks, NULL);
277 void __call_scope_hooks(void)
279 struct scope_hook_list *hook_list;
280 struct scope_container *tmp;
282 if (!scope_hooks)
283 return;
285 hook_list = pop_scope_hook_list(&scope_hooks);
286 FOR_EACH_PTR_REVERSE(hook_list, tmp) {
287 ((scope_hook *)tmp->fn)(tmp->data);
288 __free_scope_container(tmp);
289 } END_FOR_EACH_PTR_REVERSE(tmp);
292 void __call_all_scope_hooks(void)
294 struct scope_hook_list *tmp_scope;
295 struct scope_container *tmp;
297 FOR_EACH_PTR_REVERSE(scope_hooks, tmp_scope) {
298 FOR_EACH_PTR_REVERSE(tmp_scope, tmp) {
299 ((scope_hook *)tmp->fn)(tmp->data);
300 } END_FOR_EACH_PTR_REVERSE(tmp);
301 } END_FOR_EACH_PTR_REVERSE(tmp_scope);
304 void add_array_initialized_hook(void (*hook)(struct expression *array, int nr))
306 struct hook_container *container = __alloc_hook_container(0);
308 container->fn = hook;
310 add_ptr_list(&array_init_hooks, container);
313 void __call_array_initialized_hooks(struct expression *array, int nr)
315 struct hook_container *tmp;
317 FOR_EACH_PTR(array_init_hooks, tmp) {
318 ((array_init_hook *)tmp->fn)(array, nr);
319 } END_FOR_EACH_PTR(tmp);
322 void allocate_hook_memory(void)
324 pre_merge_hooks = malloc(num_checks * sizeof(*pre_merge_hooks));
325 memset(pre_merge_hooks, 0, num_checks * sizeof(*pre_merge_hooks));
328 void register_hooks(int id)
330 add_function_data((unsigned long *)&scope_hooks);
331 my_id = id;
334 void call_void_fns(struct void_fn_list *list)
336 void_fn *fn;
338 FOR_EACH_PTR(list, fn) {
339 (fn)();
340 } END_FOR_EACH_PTR(fn);
343 void call_expr_fns(struct expr_fn_list *list, struct expression *expr)
345 expr_func *fn;
347 FOR_EACH_PTR(list, fn) {
348 (fn)(expr);
349 } END_FOR_EACH_PTR(fn);
352 void call_stmt_fns(struct stmt_fn_list *list, struct statement *stmt)
354 stmt_func *fn;
356 FOR_EACH_PTR(list, fn) {
357 (fn)(stmt);
358 } END_FOR_EACH_PTR(fn);
361 void call_sym_fns(struct sym_fn_list *list, struct symbol *sym)
363 sym_func *fn;
365 FOR_EACH_PTR(list, fn) {
366 (fn)(sym);
367 } END_FOR_EACH_PTR(fn);
370 void call_name_sym_fns(struct name_sym_fn_list *list, struct expression *expr, const char *name, struct symbol *sym)
372 name_sym_hook *fn;
374 FOR_EACH_PTR(list, fn) {
375 (fn)(expr, name, sym);
376 } END_FOR_EACH_PTR(fn);
379 void call_string_hooks(struct string_hook_list *list, struct expression *expr, const char *str)
381 string_hook *fn;
383 FOR_EACH_PTR(list, fn) {
384 (fn)(expr, str);
385 } END_FOR_EACH_PTR(fn);