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 RAW_ASSIGNMENT_HOOK
:
63 container
->data_type
= EXPR_PTR
;
65 case CALL_ASSIGNMENT_HOOK
:
66 container
->data_type
= EXPR_PTR
;
68 case MACRO_ASSIGNMENT_HOOK
:
69 container
->data_type
= EXPR_PTR
;
72 container
->data_type
= EXPR_PTR
;
75 container
->data_type
= EXPR_PTR
;
78 container
->data_type
= EXPR_PTR
;
81 container
->data_type
= EXPR_PTR
;
84 container
->data_type
= EXPR_PTR
;
86 case WHOLE_CONDITION_HOOK
:
87 container
->data_type
= EXPR_PTR
;
89 case FUNCTION_CALL_HOOK
:
90 container
->data_type
= EXPR_PTR
;
93 container
->data_type
= EXPR_PTR
;
99 container
->data_type
= STMT_PTR
;
102 container
->data_type
= SYMBOL_PTR
;
105 container
->data_type
= SYMBOL_PTR
;
108 container
->data_type
= SYMBOL_PTR
;
111 container
->data_type
= EXPR_PTR
;
114 /* nothing needed... */
117 add_ptr_list(&hook_funcs
, container
);
120 void add_merge_hook(int client_id
, merge_func_t
*func
)
122 struct hook_container
*container
= __alloc_hook_container(0);
123 container
->data_type
= client_id
;
124 container
->fn
= func
;
125 add_ptr_list(&merge_funcs
, container
);
128 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
)
130 struct hook_container
*container
= __alloc_hook_container(0);
131 container
->data_type
= client_id
;
132 container
->fn
= func
;
133 add_ptr_list(&unmatched_state_funcs
, container
);
136 static void pass_to_client(void * fn
)
138 typedef void (expr_func
)();
139 ((expr_func
*) fn
)();
142 static void pass_expr_to_client(void * fn
, void * data
)
144 typedef void (expr_func
)(struct expression
*expr
);
145 ((expr_func
*) fn
)((struct expression
*) data
);
148 static void pass_stmt_to_client(void * fn
, void * data
)
150 typedef void (stmt_func
)(struct statement
*stmt
);
151 ((stmt_func
*) fn
)((struct statement
*) data
);
154 static void pass_sym_to_client(void * fn
, void * data
)
156 typedef void (sym_func
)(struct symbol
*sym
);
157 ((sym_func
*) fn
)((struct symbol
*) data
);
160 void __pass_to_client(void *data
, enum hook_type type
)
162 struct hook_container
*container
;
167 FOR_EACH_PTR(hook_funcs
, container
) {
168 if (container
->hook_type
== type
) {
169 switch (container
->data_type
) {
171 pass_expr_to_client(container
->fn
, data
);
174 pass_stmt_to_client(container
->fn
, data
);
177 pass_sym_to_client(container
->fn
, data
);
181 } END_FOR_EACH_PTR(container
);
184 void __pass_to_client_no_data(enum hook_type type
)
186 struct hook_container
*container
;
188 FOR_EACH_PTR(hook_funcs
, container
) {
189 if (container
->hook_type
== type
)
190 pass_to_client(container
->fn
);
191 } END_FOR_EACH_PTR(container
);
194 void __pass_case_to_client(struct expression
*switch_expr
,
195 struct expression
*case_expr
)
197 typedef void (case_func
)(struct expression
*switch_expr
,
198 struct expression
*case_expr
);
199 struct hook_container
*container
;
201 FOR_EACH_PTR(hook_funcs
, container
) {
202 if (container
->hook_type
== CASE_HOOK
) {
203 ((case_func
*) container
->fn
)(switch_expr
, case_expr
);
205 } END_FOR_EACH_PTR(container
);
208 int __has_merge_function(int client_id
)
210 struct hook_container
*tmp
;
212 FOR_EACH_PTR(merge_funcs
, tmp
) {
213 if (tmp
->data_type
== client_id
)
215 } END_FOR_EACH_PTR(tmp
);
219 struct smatch_state
*__client_merge_function(int owner
, const char *name
,
221 struct smatch_state
*s1
,
222 struct smatch_state
*s2
)
224 struct smatch_state
*tmp_state
;
225 struct hook_container
*tmp
;
227 /* Pass NULL states first and the rest alphabetically by name */
228 if (!s2
|| (s1
&& strcmp(s2
->name
, s1
->name
) < 0)) {
234 FOR_EACH_PTR(merge_funcs
, tmp
) {
235 if (tmp
->data_type
== owner
)
236 return ((merge_func_t
*) tmp
->fn
)(name
, sym
, s1
, s2
);
237 } END_FOR_EACH_PTR(tmp
);
241 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
)
243 struct hook_container
*tmp
;
245 FOR_EACH_PTR(unmatched_state_funcs
, tmp
) {
246 if (tmp
->data_type
== sm
->owner
)
247 return ((unmatched_func_t
*) tmp
->fn
)(sm
);
248 } END_FOR_EACH_PTR(tmp
);
252 static struct scope_hook_list
*pop_scope_hook_list(struct scope_hook_stack
**stack
)
254 struct scope_hook_list
*hook_list
;
256 hook_list
= last_ptr_list((struct ptr_list
*)*stack
);
257 delete_ptr_list_last((struct ptr_list
**)stack
);
261 static void push_scope_hook_list(struct scope_hook_stack
**stack
, struct scope_hook_list
*l
)
263 add_ptr_list(stack
, l
);
266 void add_scope_hook(scope_hook
*fn
, void *data
)
268 struct scope_hook_list
*hook_list
;
269 struct scope_container
*new;
273 hook_list
= pop_scope_hook_list(&scope_hooks
);
274 new = __alloc_scope_container(0);
277 add_ptr_list(&hook_list
, new);
278 push_scope_hook_list(&scope_hooks
, hook_list
);
281 void __push_scope_hooks(void)
283 push_scope_hook_list(&scope_hooks
, NULL
);
286 void __call_scope_hooks(void)
288 struct scope_hook_list
*hook_list
;
289 struct scope_container
*tmp
;
294 hook_list
= pop_scope_hook_list(&scope_hooks
);
295 FOR_EACH_PTR(hook_list
, tmp
) {
296 ((scope_hook
*) tmp
->fn
)(tmp
->data
);
297 __free_scope_container(tmp
);
298 } END_FOR_EACH_PTR(tmp
);