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
27 struct hook_container
{
29 enum data_type data_type
;
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
{
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
;
56 container
->data_type
= EXPR_PTR
;
59 container
->data_type
= STMT_PTR
;
62 container
->data_type
= STMT_PTR
;
65 container
->data_type
= EXPR_PTR
;
68 container
->data_type
= EXPR_PTR
;
70 case DECLARATION_HOOK
:
71 container
->data_type
= SYMBOL_PTR
;
74 container
->data_type
= EXPR_PTR
;
76 case ASSIGNMENT_HOOK_AFTER
:
77 container
->data_type
= EXPR_PTR
;
79 case RAW_ASSIGNMENT_HOOK
:
80 container
->data_type
= EXPR_PTR
;
82 case GLOBAL_ASSIGNMENT_HOOK
:
83 container
->data_type
= EXPR_PTR
;
85 case CALL_ASSIGNMENT_HOOK
:
86 container
->data_type
= EXPR_PTR
;
88 case MACRO_ASSIGNMENT_HOOK
:
89 container
->data_type
= EXPR_PTR
;
92 container
->data_type
= EXPR_PTR
;
95 container
->data_type
= EXPR_PTR
;
98 container
->data_type
= EXPR_PTR
;
101 container
->data_type
= STMT_PTR
;
104 container
->data_type
= EXPR_PTR
;
107 container
->data_type
= EXPR_PTR
;
109 case WHOLE_CONDITION_HOOK
:
110 container
->data_type
= EXPR_PTR
;
112 case FUNCTION_CALL_HOOK
:
113 container
->data_type
= EXPR_PTR
;
115 case CALL_HOOK_AFTER_INLINE
:
116 container
->data_type
= EXPR_PTR
;
118 case FUNCTION_CALL_HOOK_AFTER_DB
:
119 container
->data_type
= EXPR_PTR
;
122 container
->data_type
= EXPR_PTR
;
128 container
->data_type
= STMT_PTR
;
131 container
->data_type
= EXPR_PTR
;
134 container
->data_type
= EXPR_PTR
;
137 container
->data_type
= SYMBOL_PTR
;
140 container
->data_type
= SYMBOL_PTR
;
143 container
->data_type
= SYMBOL_PTR
;
146 container
->data_type
= SYMBOL_PTR
;
148 case AFTER_FUNC_HOOK
:
149 container
->data_type
= SYMBOL_PTR
;
152 container
->data_type
= EXPR_PTR
;
154 case INLINE_FN_START
:
155 container
->data_type
= EXPR_PTR
;
158 container
->data_type
= EXPR_PTR
;
161 container
->data_type
= SYM_LIST_PTR
;
164 add_ptr_list(&hook_array
[type
], container
);
167 void add_merge_hook(int client_id
, merge_func_t
*func
)
169 struct hook_container
*container
= __alloc_hook_container(0);
170 container
->data_type
= client_id
;
171 container
->fn
= func
;
172 add_ptr_list(&merge_funcs
, container
);
175 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
)
177 struct hook_container
*container
= __alloc_hook_container(0);
178 container
->data_type
= client_id
;
179 container
->fn
= func
;
180 add_ptr_list(&unmatched_state_funcs
, container
);
183 void add_pre_merge_hook(int client_id
, void (*hook
)(struct sm_state
*sm
))
185 pre_merge_hooks
[client_id
] = hook
;
188 static void pass_to_client(void *fn
)
190 typedef void (expr_func
)();
191 ((expr_func
*) fn
)();
194 static void pass_expr_to_client(void *fn
, void *data
)
196 typedef void (expr_func
)(struct expression
*expr
);
197 ((expr_func
*) fn
)((struct expression
*) data
);
200 static void pass_stmt_to_client(void *fn
, void *data
)
202 typedef void (stmt_func
)(struct statement
*stmt
);
203 ((stmt_func
*) fn
)((struct statement
*) data
);
206 static void pass_sym_to_client(void *fn
, void *data
)
208 typedef void (sym_func
)(struct symbol
*sym
);
209 ((sym_func
*) fn
)((struct symbol
*) data
);
212 static void pass_sym_list_to_client(void *fn
, void *data
)
214 typedef void (sym_func
)(struct symbol_list
*sym_list
);
215 ((sym_func
*) fn
)((struct symbol_list
*) data
);
218 void __pass_to_client(void *data
, enum hook_type type
)
220 struct hook_container
*container
;
223 FOR_EACH_PTR(hook_array
[type
], container
) {
224 switch (container
->data_type
) {
226 pass_expr_to_client(container
->fn
, data
);
229 pass_stmt_to_client(container
->fn
, data
);
232 pass_sym_to_client(container
->fn
, data
);
235 pass_sym_list_to_client(container
->fn
, data
);
238 } END_FOR_EACH_PTR(container
);
241 void __pass_to_client_no_data(enum hook_type type
)
243 struct hook_container
*container
;
245 FOR_EACH_PTR(hook_array
[type
], container
) {
246 pass_to_client(container
->fn
);
247 } END_FOR_EACH_PTR(container
);
250 void __pass_case_to_client(struct expression
*switch_expr
,
251 struct range_list
*rl
)
253 typedef void (case_func
)(struct expression
*switch_expr
,
254 struct range_list
*rl
);
255 struct hook_container
*container
;
257 FOR_EACH_PTR(hook_array
[CASE_HOOK
], container
) {
258 ((case_func
*) container
->fn
)(switch_expr
, rl
);
259 } END_FOR_EACH_PTR(container
);
262 int __has_merge_function(int client_id
)
264 struct hook_container
*tmp
;
266 FOR_EACH_PTR(merge_funcs
, tmp
) {
267 if (tmp
->data_type
== client_id
)
269 } END_FOR_EACH_PTR(tmp
);
273 struct smatch_state
*__client_merge_function(int owner
,
274 struct smatch_state
*s1
,
275 struct smatch_state
*s2
)
277 struct smatch_state
*tmp_state
;
278 struct hook_container
*tmp
;
280 /* Pass NULL states first and the rest alphabetically by name */
281 if (!s2
|| (s1
&& strcmp(s2
->name
, s1
->name
) < 0)) {
287 FOR_EACH_PTR(merge_funcs
, tmp
) {
288 if (tmp
->data_type
== owner
)
289 return ((merge_func_t
*) tmp
->fn
)(s1
, s2
);
290 } END_FOR_EACH_PTR(tmp
);
294 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
)
296 struct hook_container
*tmp
;
298 FOR_EACH_PTR(unmatched_state_funcs
, tmp
) {
299 if (tmp
->data_type
== sm
->owner
)
300 return ((unmatched_func_t
*) tmp
->fn
)(sm
);
301 } END_FOR_EACH_PTR(tmp
);
305 void call_pre_merge_hook(struct sm_state
*sm
)
307 if (sm
->owner
>= num_checks
)
310 if (pre_merge_hooks
[sm
->owner
])
311 pre_merge_hooks
[sm
->owner
](sm
);
314 static struct scope_hook_list
*pop_scope_hook_list(struct scope_hook_stack
**stack
)
316 struct scope_hook_list
*hook_list
;
318 hook_list
= last_ptr_list((struct ptr_list
*)*stack
);
319 delete_ptr_list_last((struct ptr_list
**)stack
);
323 static void push_scope_hook_list(struct scope_hook_stack
**stack
, struct scope_hook_list
*l
)
325 add_ptr_list(stack
, l
);
328 void add_scope_hook(scope_hook
*fn
, void *data
)
330 struct scope_hook_list
*hook_list
;
331 struct scope_container
*new;
335 hook_list
= pop_scope_hook_list(&scope_hooks
);
336 new = __alloc_scope_container(0);
339 add_ptr_list(&hook_list
, new);
340 push_scope_hook_list(&scope_hooks
, hook_list
);
343 void __push_scope_hooks(void)
345 push_scope_hook_list(&scope_hooks
, NULL
);
348 void __call_scope_hooks(void)
350 struct scope_hook_list
*hook_list
;
351 struct scope_container
*tmp
;
356 hook_list
= pop_scope_hook_list(&scope_hooks
);
357 FOR_EACH_PTR(hook_list
, tmp
) {
358 ((scope_hook
*) tmp
->fn
)(tmp
->data
);
359 __free_scope_container(tmp
);
360 } END_FOR_EACH_PTR(tmp
);
363 void allocate_hook_memory(void)
365 pre_merge_hooks
= malloc(num_checks
* sizeof(*pre_merge_hooks
));
366 memset(pre_merge_hooks
, 0, num_checks
* sizeof(*pre_merge_hooks
));