2 * sparse/smatch_hooks.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
12 struct hook_container
{
17 ALLOCATOR(hook_container
, "hook functions");
18 DECLARE_PTR_LIST(hook_func_list
, struct hook_container
);
19 static struct hook_func_list
*hook_funcs
;
20 static struct hook_func_list
*merge_funcs
;
21 static struct hook_func_list
*unmatched_state_funcs
;
23 struct scope_container
{
27 ALLOCATOR(scope_container
, "scope hook functions");
28 DECLARE_PTR_LIST(scope_hook_list
, struct scope_container
);
29 DECLARE_PTR_LIST(scope_hook_stack
, struct scope_hook_list
);
30 static struct scope_hook_stack
*scope_hooks
;
32 void add_hook(void *func
, enum hook_type type
)
34 struct hook_container
*container
= __alloc_hook_container(0);
35 container
->hook_type
= type
;
39 container
->data_type
= EXPR_HOOK
;
42 container
->data_type
= STMT_HOOK
;
45 container
->data_type
= SYM_HOOK
;
47 case DECLARATION_HOOK
:
48 container
->data_type
= SYM_HOOK
;
51 container
->data_type
= EXPR_HOOK
;
53 case CALL_ASSIGNMENT_HOOK
:
54 container
->data_type
= EXPR_HOOK
;
57 container
->data_type
= EXPR_HOOK
;
60 container
->data_type
= EXPR_HOOK
;
62 case WHOLE_CONDITION_HOOK
:
63 container
->data_type
= EXPR_HOOK
;
65 case FUNCTION_CALL_HOOK
:
66 container
->data_type
= EXPR_HOOK
;
69 container
->data_type
= EXPR_HOOK
;
75 container
->data_type
= SYM_HOOK
;
78 container
->data_type
= SYM_HOOK
;
81 container
->data_type
= SYM_HOOK
;
84 container
->data_type
= EXPR_HOOK
;
87 /* nothing needed... */
90 add_ptr_list(&hook_funcs
, container
);
93 void add_merge_hook(int client_id
, merge_func_t
*func
)
95 struct hook_container
*container
= __alloc_hook_container(0);
96 container
->data_type
= client_id
;
98 add_ptr_list(&merge_funcs
, container
);
101 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
)
103 struct hook_container
*container
= __alloc_hook_container(0);
104 container
->data_type
= client_id
;
105 container
->fn
= func
;
106 add_ptr_list(&unmatched_state_funcs
, container
);
109 static void pass_to_client(void * fn
)
111 typedef void (expr_func
)();
112 ((expr_func
*) fn
)();
115 static void pass_expr_to_client(void * fn
, void * data
)
117 typedef void (expr_func
)(struct expression
*expr
);
118 ((expr_func
*) fn
)((struct expression
*) data
);
121 static void pass_stmt_to_client(void * fn
, void * data
)
123 typedef void (stmt_func
)(struct statement
*stmt
);
124 ((stmt_func
*) fn
)((struct statement
*) data
);
127 static void pass_sym_to_client(void * fn
, void * data
)
129 typedef void (sym_func
)(struct symbol
*sym
);
130 ((sym_func
*) fn
)((struct symbol
*) data
);
133 void __pass_to_client(void *data
, enum hook_type type
)
135 struct hook_container
*container
;
140 FOR_EACH_PTR(hook_funcs
, container
) {
141 if (container
->hook_type
== type
) {
142 switch(container
->data_type
) {
144 pass_expr_to_client(container
->fn
, data
);
147 pass_stmt_to_client(container
->fn
, data
);
150 pass_sym_to_client(container
->fn
, data
);
154 } END_FOR_EACH_PTR(container
);
157 void __pass_to_client_no_data(enum hook_type type
)
159 struct hook_container
*container
;
161 FOR_EACH_PTR(hook_funcs
, container
) {
162 if (container
->hook_type
== type
)
163 pass_to_client(container
->fn
);
164 } END_FOR_EACH_PTR(container
);
167 void __pass_case_to_client(struct expression
*switch_expr
,
168 struct expression
*case_expr
)
170 typedef void (case_func
)(struct expression
*switch_expr
,
171 struct expression
*case_expr
);
172 struct hook_container
*container
;
174 FOR_EACH_PTR(hook_funcs
, container
) {
175 if (container
->hook_type
== CASE_HOOK
) {
176 ((case_func
*) container
->fn
)(switch_expr
, case_expr
);
178 } END_FOR_EACH_PTR(container
);
181 int __has_merge_function(int client_id
)
183 struct hook_container
*tmp
;
185 FOR_EACH_PTR(merge_funcs
, tmp
) {
186 if (tmp
->data_type
== client_id
)
188 } END_FOR_EACH_PTR(tmp
);
192 struct smatch_state
*__client_merge_function(int owner
, const char *name
,
194 struct smatch_state
*s1
,
195 struct smatch_state
*s2
)
197 struct smatch_state
*tmp_state
;
198 struct hook_container
*tmp
;
200 /* Pass NULL states first and the rest alphabetically by name */
201 if (!s2
|| (s1
&& strcmp(s2
->name
, s1
->name
) < 0)) {
207 FOR_EACH_PTR(merge_funcs
, tmp
) {
208 if (tmp
->data_type
== owner
)
209 return ((merge_func_t
*) tmp
->fn
)(name
, sym
, s1
, s2
);
210 } END_FOR_EACH_PTR(tmp
);
214 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
)
216 struct hook_container
*tmp
;
218 FOR_EACH_PTR(unmatched_state_funcs
, tmp
) {
219 if (tmp
->data_type
== sm
->owner
)
220 return ((unmatched_func_t
*) tmp
->fn
)(sm
);
221 } END_FOR_EACH_PTR(tmp
);
225 static struct scope_hook_list
*pop_scope_hook_list(struct scope_hook_stack
**stack
)
227 struct scope_hook_list
*hook_list
;
229 hook_list
= last_ptr_list((struct ptr_list
*)*stack
);
230 delete_ptr_list_last((struct ptr_list
**)stack
);
234 static void push_scope_hook_list(struct scope_hook_stack
**stack
, struct scope_hook_list
*l
)
236 add_ptr_list(stack
, l
);
239 void add_scope_hook(scope_hook
*fn
, void *data
)
241 struct scope_hook_list
*hook_list
;
242 struct scope_container
*new;
246 hook_list
= pop_scope_hook_list(&scope_hooks
);
247 new = __alloc_scope_container(0);
250 add_ptr_list(&hook_list
, new);
251 push_scope_hook_list(&scope_hooks
, hook_list
);
254 void __push_scope_hooks(void)
256 push_scope_hook_list(&scope_hooks
, NULL
);
259 void __call_scope_hooks(void)
261 struct scope_hook_list
*hook_list
;
262 struct scope_container
*tmp
;
267 hook_list
= pop_scope_hook_list(&scope_hooks
);
268 FOR_EACH_PTR(hook_list
, tmp
) {
269 ((scope_hook
*) tmp
->fn
)(tmp
->data
);
270 __free_scope_container(tmp
);
271 } END_FOR_EACH_PTR(tmp
);