2 * sparse/smatch_hooks.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
18 struct hook_container
{
20 enum data_type data_type
;
23 ALLOCATOR(hook_container
, "hook functions");
24 DECLARE_PTR_LIST(hook_func_list
, struct hook_container
);
25 static struct hook_func_list
*hook_funcs
;
26 static struct hook_func_list
*merge_funcs
;
27 static struct hook_func_list
*unmatched_state_funcs
;
29 struct scope_container
{
33 ALLOCATOR(scope_container
, "scope hook functions");
34 DECLARE_PTR_LIST(scope_hook_list
, struct scope_container
);
35 DECLARE_PTR_LIST(scope_hook_stack
, struct scope_hook_list
);
36 static struct scope_hook_stack
*scope_hooks
;
38 void add_hook(void *func
, enum hook_type type
)
40 struct hook_container
*container
= __alloc_hook_container(0);
41 container
->hook_type
= type
;
45 container
->data_type
= EXPR_PTR
;
48 container
->data_type
= STMT_PTR
;
51 container
->data_type
= EXPR_PTR
;
54 container
->data_type
= EXPR_PTR
;
56 case DECLARATION_HOOK
:
57 container
->data_type
= SYMBOL_PTR
;
60 container
->data_type
= EXPR_PTR
;
62 case CALL_ASSIGNMENT_HOOK
:
63 container
->data_type
= EXPR_PTR
;
66 container
->data_type
= EXPR_PTR
;
69 container
->data_type
= EXPR_PTR
;
72 container
->data_type
= EXPR_PTR
;
75 container
->data_type
= EXPR_PTR
;
77 case WHOLE_CONDITION_HOOK
:
78 container
->data_type
= EXPR_PTR
;
80 case FUNCTION_CALL_HOOK
:
81 container
->data_type
= EXPR_PTR
;
84 container
->data_type
= EXPR_PTR
;
90 container
->data_type
= SYMBOL_PTR
;
93 container
->data_type
= SYMBOL_PTR
;
96 container
->data_type
= SYMBOL_PTR
;
99 container
->data_type
= EXPR_PTR
;
102 /* nothing needed... */
105 add_ptr_list(&hook_funcs
, container
);
108 void add_merge_hook(int client_id
, merge_func_t
*func
)
110 struct hook_container
*container
= __alloc_hook_container(0);
111 container
->data_type
= client_id
;
112 container
->fn
= func
;
113 add_ptr_list(&merge_funcs
, container
);
116 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
)
118 struct hook_container
*container
= __alloc_hook_container(0);
119 container
->data_type
= client_id
;
120 container
->fn
= func
;
121 add_ptr_list(&unmatched_state_funcs
, container
);
124 static void pass_to_client(void * fn
)
126 typedef void (expr_func
)();
127 ((expr_func
*) fn
)();
130 static void pass_expr_to_client(void * fn
, void * data
)
132 typedef void (expr_func
)(struct expression
*expr
);
133 ((expr_func
*) fn
)((struct expression
*) data
);
136 static void pass_stmt_to_client(void * fn
, void * data
)
138 typedef void (stmt_func
)(struct statement
*stmt
);
139 ((stmt_func
*) fn
)((struct statement
*) data
);
142 static void pass_sym_to_client(void * fn
, void * data
)
144 typedef void (sym_func
)(struct symbol
*sym
);
145 ((sym_func
*) fn
)((struct symbol
*) data
);
148 void __pass_to_client(void *data
, enum hook_type type
)
150 struct hook_container
*container
;
155 FOR_EACH_PTR(hook_funcs
, container
) {
156 if (container
->hook_type
== type
) {
157 switch (container
->data_type
) {
159 pass_expr_to_client(container
->fn
, data
);
162 pass_stmt_to_client(container
->fn
, data
);
165 pass_sym_to_client(container
->fn
, data
);
169 } END_FOR_EACH_PTR(container
);
172 void __pass_to_client_no_data(enum hook_type type
)
174 struct hook_container
*container
;
176 FOR_EACH_PTR(hook_funcs
, container
) {
177 if (container
->hook_type
== type
)
178 pass_to_client(container
->fn
);
179 } END_FOR_EACH_PTR(container
);
182 void __pass_case_to_client(struct expression
*switch_expr
,
183 struct expression
*case_expr
)
185 typedef void (case_func
)(struct expression
*switch_expr
,
186 struct expression
*case_expr
);
187 struct hook_container
*container
;
189 FOR_EACH_PTR(hook_funcs
, container
) {
190 if (container
->hook_type
== CASE_HOOK
) {
191 ((case_func
*) container
->fn
)(switch_expr
, case_expr
);
193 } END_FOR_EACH_PTR(container
);
196 int __has_merge_function(int client_id
)
198 struct hook_container
*tmp
;
200 FOR_EACH_PTR(merge_funcs
, tmp
) {
201 if (tmp
->data_type
== client_id
)
203 } END_FOR_EACH_PTR(tmp
);
207 struct smatch_state
*__client_merge_function(int owner
, const char *name
,
209 struct smatch_state
*s1
,
210 struct smatch_state
*s2
)
212 struct smatch_state
*tmp_state
;
213 struct hook_container
*tmp
;
215 /* Pass NULL states first and the rest alphabetically by name */
216 if (!s2
|| (s1
&& strcmp(s2
->name
, s1
->name
) < 0)) {
222 FOR_EACH_PTR(merge_funcs
, tmp
) {
223 if (tmp
->data_type
== owner
)
224 return ((merge_func_t
*) tmp
->fn
)(name
, sym
, s1
, s2
);
225 } END_FOR_EACH_PTR(tmp
);
229 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
)
231 struct hook_container
*tmp
;
233 FOR_EACH_PTR(unmatched_state_funcs
, tmp
) {
234 if (tmp
->data_type
== sm
->owner
)
235 return ((unmatched_func_t
*) tmp
->fn
)(sm
);
236 } END_FOR_EACH_PTR(tmp
);
240 static struct scope_hook_list
*pop_scope_hook_list(struct scope_hook_stack
**stack
)
242 struct scope_hook_list
*hook_list
;
244 hook_list
= last_ptr_list((struct ptr_list
*)*stack
);
245 delete_ptr_list_last((struct ptr_list
**)stack
);
249 static void push_scope_hook_list(struct scope_hook_stack
**stack
, struct scope_hook_list
*l
)
251 add_ptr_list(stack
, l
);
254 void add_scope_hook(scope_hook
*fn
, void *data
)
256 struct scope_hook_list
*hook_list
;
257 struct scope_container
*new;
261 hook_list
= pop_scope_hook_list(&scope_hooks
);
262 new = __alloc_scope_container(0);
265 add_ptr_list(&hook_list
, new);
266 push_scope_hook_list(&scope_hooks
, hook_list
);
269 void __push_scope_hooks(void)
271 push_scope_hook_list(&scope_hooks
, NULL
);
274 void __call_scope_hooks(void)
276 struct scope_hook_list
*hook_list
;
277 struct scope_container
*tmp
;
282 hook_list
= pop_scope_hook_list(&scope_hooks
);
283 FOR_EACH_PTR(hook_list
, tmp
) {
284 ((scope_hook
*) tmp
->fn
)(tmp
->data
);
285 __free_scope_container(tmp
);
286 } END_FOR_EACH_PTR(tmp
);