2 * sparse/smatch_hooks.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
19 struct hook_container
{
21 enum data_type data_type
;
24 ALLOCATOR(hook_container
, "hook functions");
25 DECLARE_PTR_LIST(hook_func_list
, struct hook_container
);
26 static struct hook_func_list
*hook_funcs
;
27 static struct hook_func_list
*merge_funcs
;
28 static struct hook_func_list
*unmatched_state_funcs
;
30 struct scope_container
{
34 ALLOCATOR(scope_container
, "scope hook functions");
35 DECLARE_PTR_LIST(scope_hook_list
, struct scope_container
);
36 DECLARE_PTR_LIST(scope_hook_stack
, struct scope_hook_list
);
37 static struct scope_hook_stack
*scope_hooks
;
39 void add_hook(void *func
, enum hook_type type
)
41 struct hook_container
*container
= __alloc_hook_container(0);
42 container
->hook_type
= type
;
46 container
->data_type
= EXPR_PTR
;
49 container
->data_type
= STMT_PTR
;
52 container
->data_type
= EXPR_PTR
;
55 container
->data_type
= EXPR_PTR
;
57 case DECLARATION_HOOK
:
58 container
->data_type
= SYMBOL_PTR
;
61 container
->data_type
= EXPR_PTR
;
63 case RAW_ASSIGNMENT_HOOK
:
64 container
->data_type
= EXPR_PTR
;
66 case GLOBAL_ASSIGNMENT_HOOK
:
67 container
->data_type
= EXPR_PTR
;
69 case CALL_ASSIGNMENT_HOOK
:
70 container
->data_type
= EXPR_PTR
;
72 case MACRO_ASSIGNMENT_HOOK
:
73 container
->data_type
= EXPR_PTR
;
76 container
->data_type
= EXPR_PTR
;
79 container
->data_type
= EXPR_PTR
;
82 container
->data_type
= EXPR_PTR
;
85 container
->data_type
= STMT_PTR
;
88 container
->data_type
= EXPR_PTR
;
91 container
->data_type
= EXPR_PTR
;
93 case WHOLE_CONDITION_HOOK
:
94 container
->data_type
= EXPR_PTR
;
96 case FUNCTION_CALL_HOOK
:
97 container
->data_type
= EXPR_PTR
;
99 case CALL_HOOK_AFTER_INLINE
:
100 container
->data_type
= EXPR_PTR
;
103 container
->data_type
= EXPR_PTR
;
109 container
->data_type
= STMT_PTR
;
112 container
->data_type
= EXPR_PTR
;
115 container
->data_type
= EXPR_PTR
;
118 container
->data_type
= SYMBOL_PTR
;
121 container
->data_type
= SYMBOL_PTR
;
124 container
->data_type
= SYMBOL_PTR
;
127 container
->data_type
= SYMBOL_PTR
;
129 case AFTER_FUNC_HOOK
:
130 container
->data_type
= SYMBOL_PTR
;
133 container
->data_type
= EXPR_PTR
;
135 case INLINE_FN_START
:
136 container
->data_type
= EXPR_PTR
;
139 container
->data_type
= EXPR_PTR
;
142 container
->data_type
= SYM_LIST_PTR
;
145 add_ptr_list(&hook_funcs
, container
);
148 void add_merge_hook(int client_id
, merge_func_t
*func
)
150 struct hook_container
*container
= __alloc_hook_container(0);
151 container
->data_type
= client_id
;
152 container
->fn
= func
;
153 add_ptr_list(&merge_funcs
, container
);
156 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
)
158 struct hook_container
*container
= __alloc_hook_container(0);
159 container
->data_type
= client_id
;
160 container
->fn
= func
;
161 add_ptr_list(&unmatched_state_funcs
, container
);
164 static void pass_to_client(void *fn
)
166 typedef void (expr_func
)();
167 ((expr_func
*) fn
)();
170 static void pass_expr_to_client(void *fn
, void *data
)
172 typedef void (expr_func
)(struct expression
*expr
);
173 ((expr_func
*) fn
)((struct expression
*) data
);
176 static void pass_stmt_to_client(void *fn
, void *data
)
178 typedef void (stmt_func
)(struct statement
*stmt
);
179 ((stmt_func
*) fn
)((struct statement
*) data
);
182 static void pass_sym_to_client(void *fn
, void *data
)
184 typedef void (sym_func
)(struct symbol
*sym
);
185 ((sym_func
*) fn
)((struct symbol
*) data
);
188 static void pass_sym_list_to_client(void *fn
, void *data
)
190 typedef void (sym_func
)(struct symbol_list
*sym_list
);
191 ((sym_func
*) fn
)((struct symbol_list
*) data
);
194 void __pass_to_client(void *data
, enum hook_type type
)
196 struct hook_container
*container
;
198 FOR_EACH_PTR(hook_funcs
, container
) {
199 if (container
->hook_type
== type
) {
200 switch (container
->data_type
) {
202 pass_expr_to_client(container
->fn
, data
);
205 pass_stmt_to_client(container
->fn
, data
);
208 pass_sym_to_client(container
->fn
, data
);
211 pass_sym_list_to_client(container
->fn
, data
);
215 } END_FOR_EACH_PTR(container
);
218 void __pass_to_client_no_data(enum hook_type type
)
220 struct hook_container
*container
;
222 FOR_EACH_PTR(hook_funcs
, container
) {
223 if (container
->hook_type
== type
)
224 pass_to_client(container
->fn
);
225 } END_FOR_EACH_PTR(container
);
228 void __pass_case_to_client(struct expression
*switch_expr
,
229 struct expression
*case_expr
)
231 typedef void (case_func
)(struct expression
*switch_expr
,
232 struct expression
*case_expr
);
233 struct hook_container
*container
;
235 FOR_EACH_PTR(hook_funcs
, container
) {
236 if (container
->hook_type
== CASE_HOOK
)
237 ((case_func
*) container
->fn
)(switch_expr
, case_expr
);
238 } END_FOR_EACH_PTR(container
);
241 int __has_merge_function(int client_id
)
243 struct hook_container
*tmp
;
245 FOR_EACH_PTR(merge_funcs
, tmp
) {
246 if (tmp
->data_type
== client_id
)
248 } END_FOR_EACH_PTR(tmp
);
252 struct smatch_state
*__client_merge_function(int owner
,
253 struct smatch_state
*s1
,
254 struct smatch_state
*s2
)
256 struct smatch_state
*tmp_state
;
257 struct hook_container
*tmp
;
259 /* Pass NULL states first and the rest alphabetically by name */
260 if (!s2
|| (s1
&& strcmp(s2
->name
, s1
->name
) < 0)) {
266 FOR_EACH_PTR(merge_funcs
, tmp
) {
267 if (tmp
->data_type
== owner
)
268 return ((merge_func_t
*) tmp
->fn
)(s1
, s2
);
269 } END_FOR_EACH_PTR(tmp
);
273 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
)
275 struct hook_container
*tmp
;
277 FOR_EACH_PTR(unmatched_state_funcs
, tmp
) {
278 if (tmp
->data_type
== sm
->owner
)
279 return ((unmatched_func_t
*) tmp
->fn
)(sm
);
280 } END_FOR_EACH_PTR(tmp
);
284 static struct scope_hook_list
*pop_scope_hook_list(struct scope_hook_stack
**stack
)
286 struct scope_hook_list
*hook_list
;
288 hook_list
= last_ptr_list((struct ptr_list
*)*stack
);
289 delete_ptr_list_last((struct ptr_list
**)stack
);
293 static void push_scope_hook_list(struct scope_hook_stack
**stack
, struct scope_hook_list
*l
)
295 add_ptr_list(stack
, l
);
298 void add_scope_hook(scope_hook
*fn
, void *data
)
300 struct scope_hook_list
*hook_list
;
301 struct scope_container
*new;
305 hook_list
= pop_scope_hook_list(&scope_hooks
);
306 new = __alloc_scope_container(0);
309 add_ptr_list(&hook_list
, new);
310 push_scope_hook_list(&scope_hooks
, hook_list
);
313 void __push_scope_hooks(void)
315 push_scope_hook_list(&scope_hooks
, NULL
);
318 void __call_scope_hooks(void)
320 struct scope_hook_list
*hook_list
;
321 struct scope_container
*tmp
;
326 hook_list
= pop_scope_hook_list(&scope_hooks
);
327 FOR_EACH_PTR(hook_list
, tmp
) {
328 ((scope_hook
*) tmp
->fn
)(tmp
->data
);
329 __free_scope_container(tmp
);
330 } END_FOR_EACH_PTR(tmp
);