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
= STMT_PTR
;
55 container
->data_type
= EXPR_PTR
;
58 container
->data_type
= EXPR_PTR
;
60 case DECLARATION_HOOK
:
61 container
->data_type
= SYMBOL_PTR
;
64 container
->data_type
= EXPR_PTR
;
66 case RAW_ASSIGNMENT_HOOK
:
67 container
->data_type
= EXPR_PTR
;
69 case GLOBAL_ASSIGNMENT_HOOK
:
70 container
->data_type
= EXPR_PTR
;
72 case CALL_ASSIGNMENT_HOOK
:
73 container
->data_type
= EXPR_PTR
;
75 case MACRO_ASSIGNMENT_HOOK
:
76 container
->data_type
= EXPR_PTR
;
79 container
->data_type
= EXPR_PTR
;
82 container
->data_type
= EXPR_PTR
;
85 container
->data_type
= EXPR_PTR
;
88 container
->data_type
= STMT_PTR
;
91 container
->data_type
= EXPR_PTR
;
94 container
->data_type
= EXPR_PTR
;
96 case WHOLE_CONDITION_HOOK
:
97 container
->data_type
= EXPR_PTR
;
99 case FUNCTION_CALL_HOOK
:
100 container
->data_type
= EXPR_PTR
;
102 case CALL_HOOK_AFTER_INLINE
:
103 container
->data_type
= EXPR_PTR
;
106 container
->data_type
= EXPR_PTR
;
112 container
->data_type
= STMT_PTR
;
115 container
->data_type
= EXPR_PTR
;
118 container
->data_type
= EXPR_PTR
;
121 container
->data_type
= SYMBOL_PTR
;
124 container
->data_type
= SYMBOL_PTR
;
127 container
->data_type
= SYMBOL_PTR
;
130 container
->data_type
= SYMBOL_PTR
;
132 case AFTER_FUNC_HOOK
:
133 container
->data_type
= SYMBOL_PTR
;
136 container
->data_type
= EXPR_PTR
;
138 case INLINE_FN_START
:
139 container
->data_type
= EXPR_PTR
;
142 container
->data_type
= EXPR_PTR
;
145 container
->data_type
= SYM_LIST_PTR
;
148 add_ptr_list(&hook_funcs
, container
);
151 void add_merge_hook(int client_id
, merge_func_t
*func
)
153 struct hook_container
*container
= __alloc_hook_container(0);
154 container
->data_type
= client_id
;
155 container
->fn
= func
;
156 add_ptr_list(&merge_funcs
, container
);
159 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
)
161 struct hook_container
*container
= __alloc_hook_container(0);
162 container
->data_type
= client_id
;
163 container
->fn
= func
;
164 add_ptr_list(&unmatched_state_funcs
, container
);
167 static void pass_to_client(void *fn
)
169 typedef void (expr_func
)();
170 ((expr_func
*) fn
)();
173 static void pass_expr_to_client(void *fn
, void *data
)
175 typedef void (expr_func
)(struct expression
*expr
);
176 ((expr_func
*) fn
)((struct expression
*) data
);
179 static void pass_stmt_to_client(void *fn
, void *data
)
181 typedef void (stmt_func
)(struct statement
*stmt
);
182 ((stmt_func
*) fn
)((struct statement
*) data
);
185 static void pass_sym_to_client(void *fn
, void *data
)
187 typedef void (sym_func
)(struct symbol
*sym
);
188 ((sym_func
*) fn
)((struct symbol
*) data
);
191 static void pass_sym_list_to_client(void *fn
, void *data
)
193 typedef void (sym_func
)(struct symbol_list
*sym_list
);
194 ((sym_func
*) fn
)((struct symbol_list
*) data
);
197 void __pass_to_client(void *data
, enum hook_type type
)
199 struct hook_container
*container
;
201 FOR_EACH_PTR(hook_funcs
, container
) {
202 if (container
->hook_type
== type
) {
203 switch (container
->data_type
) {
205 pass_expr_to_client(container
->fn
, data
);
208 pass_stmt_to_client(container
->fn
, data
);
211 pass_sym_to_client(container
->fn
, data
);
214 pass_sym_list_to_client(container
->fn
, data
);
218 } END_FOR_EACH_PTR(container
);
221 void __pass_to_client_no_data(enum hook_type type
)
223 struct hook_container
*container
;
225 FOR_EACH_PTR(hook_funcs
, container
) {
226 if (container
->hook_type
== type
)
227 pass_to_client(container
->fn
);
228 } END_FOR_EACH_PTR(container
);
231 void __pass_case_to_client(struct expression
*switch_expr
,
232 struct expression
*case_expr
)
234 typedef void (case_func
)(struct expression
*switch_expr
,
235 struct expression
*case_expr
);
236 struct hook_container
*container
;
238 FOR_EACH_PTR(hook_funcs
, container
) {
239 if (container
->hook_type
== CASE_HOOK
)
240 ((case_func
*) container
->fn
)(switch_expr
, case_expr
);
241 } END_FOR_EACH_PTR(container
);
244 int __has_merge_function(int client_id
)
246 struct hook_container
*tmp
;
248 FOR_EACH_PTR(merge_funcs
, tmp
) {
249 if (tmp
->data_type
== client_id
)
251 } END_FOR_EACH_PTR(tmp
);
255 struct smatch_state
*__client_merge_function(int owner
,
256 struct smatch_state
*s1
,
257 struct smatch_state
*s2
)
259 struct smatch_state
*tmp_state
;
260 struct hook_container
*tmp
;
262 /* Pass NULL states first and the rest alphabetically by name */
263 if (!s2
|| (s1
&& strcmp(s2
->name
, s1
->name
) < 0)) {
269 FOR_EACH_PTR(merge_funcs
, tmp
) {
270 if (tmp
->data_type
== owner
)
271 return ((merge_func_t
*) tmp
->fn
)(s1
, s2
);
272 } END_FOR_EACH_PTR(tmp
);
276 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
)
278 struct hook_container
*tmp
;
280 FOR_EACH_PTR(unmatched_state_funcs
, tmp
) {
281 if (tmp
->data_type
== sm
->owner
)
282 return ((unmatched_func_t
*) tmp
->fn
)(sm
);
283 } END_FOR_EACH_PTR(tmp
);
287 static struct scope_hook_list
*pop_scope_hook_list(struct scope_hook_stack
**stack
)
289 struct scope_hook_list
*hook_list
;
291 hook_list
= last_ptr_list((struct ptr_list
*)*stack
);
292 delete_ptr_list_last((struct ptr_list
**)stack
);
296 static void push_scope_hook_list(struct scope_hook_stack
**stack
, struct scope_hook_list
*l
)
298 add_ptr_list(stack
, l
);
301 void add_scope_hook(scope_hook
*fn
, void *data
)
303 struct scope_hook_list
*hook_list
;
304 struct scope_container
*new;
308 hook_list
= pop_scope_hook_list(&scope_hooks
);
309 new = __alloc_scope_container(0);
312 add_ptr_list(&hook_list
, new);
313 push_scope_hook_list(&scope_hooks
, hook_list
);
316 void __push_scope_hooks(void)
318 push_scope_hook_list(&scope_hooks
, NULL
);
321 void __call_scope_hooks(void)
323 struct scope_hook_list
*hook_list
;
324 struct scope_container
*tmp
;
329 hook_list
= pop_scope_hook_list(&scope_hooks
);
330 FOR_EACH_PTR(hook_list
, tmp
) {
331 ((scope_hook
*) tmp
->fn
)(tmp
->data
);
332 __free_scope_container(tmp
);
333 } END_FOR_EACH_PTR(tmp
);