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 GLOBAL_ASSIGNMENT_HOOK
:
66 container
->data_type
= EXPR_PTR
;
68 case CALL_ASSIGNMENT_HOOK
:
69 container
->data_type
= EXPR_PTR
;
71 case MACRO_ASSIGNMENT_HOOK
:
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
= STMT_PTR
;
87 container
->data_type
= EXPR_PTR
;
90 container
->data_type
= EXPR_PTR
;
92 case WHOLE_CONDITION_HOOK
:
93 container
->data_type
= EXPR_PTR
;
95 case FUNCTION_CALL_HOOK
:
96 container
->data_type
= EXPR_PTR
;
98 case CALL_HOOK_AFTER_INLINE
:
99 container
->data_type
= EXPR_PTR
;
102 container
->data_type
= EXPR_PTR
;
108 container
->data_type
= STMT_PTR
;
111 container
->data_type
= EXPR_PTR
;
114 container
->data_type
= EXPR_PTR
;
117 container
->data_type
= SYMBOL_PTR
;
120 container
->data_type
= SYMBOL_PTR
;
123 container
->data_type
= SYMBOL_PTR
;
126 container
->data_type
= SYMBOL_PTR
;
129 container
->data_type
= EXPR_PTR
;
131 case INLINE_FN_START
:
132 container
->data_type
= EXPR_PTR
;
135 container
->data_type
= EXPR_PTR
;
138 /* nothing needed... */
141 add_ptr_list(&hook_funcs
, container
);
144 void add_merge_hook(int client_id
, merge_func_t
*func
)
146 struct hook_container
*container
= __alloc_hook_container(0);
147 container
->data_type
= client_id
;
148 container
->fn
= func
;
149 add_ptr_list(&merge_funcs
, container
);
152 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
)
154 struct hook_container
*container
= __alloc_hook_container(0);
155 container
->data_type
= client_id
;
156 container
->fn
= func
;
157 add_ptr_list(&unmatched_state_funcs
, container
);
160 static void pass_to_client(void *fn
)
162 typedef void (expr_func
)();
163 ((expr_func
*) fn
)();
166 static void pass_expr_to_client(void *fn
, void *data
)
168 typedef void (expr_func
)(struct expression
*expr
);
169 ((expr_func
*) fn
)((struct expression
*) data
);
172 static void pass_stmt_to_client(void *fn
, void *data
)
174 typedef void (stmt_func
)(struct statement
*stmt
);
175 ((stmt_func
*) fn
)((struct statement
*) data
);
178 static void pass_sym_to_client(void *fn
, void *data
)
180 typedef void (sym_func
)(struct symbol
*sym
);
181 ((sym_func
*) fn
)((struct symbol
*) data
);
184 void __pass_to_client(void *data
, enum hook_type type
)
186 struct hook_container
*container
;
188 FOR_EACH_PTR(hook_funcs
, container
) {
189 if (container
->hook_type
== type
) {
190 switch (container
->data_type
) {
192 pass_expr_to_client(container
->fn
, data
);
195 pass_stmt_to_client(container
->fn
, data
);
198 pass_sym_to_client(container
->fn
, data
);
202 } END_FOR_EACH_PTR(container
);
205 void __pass_to_client_no_data(enum hook_type type
)
207 struct hook_container
*container
;
209 FOR_EACH_PTR(hook_funcs
, container
) {
210 if (container
->hook_type
== type
)
211 pass_to_client(container
->fn
);
212 } END_FOR_EACH_PTR(container
);
215 void __pass_case_to_client(struct expression
*switch_expr
,
216 struct expression
*case_expr
)
218 typedef void (case_func
)(struct expression
*switch_expr
,
219 struct expression
*case_expr
);
220 struct hook_container
*container
;
222 FOR_EACH_PTR(hook_funcs
, container
) {
223 if (container
->hook_type
== CASE_HOOK
)
224 ((case_func
*) container
->fn
)(switch_expr
, case_expr
);
225 } END_FOR_EACH_PTR(container
);
228 int __has_merge_function(int client_id
)
230 struct hook_container
*tmp
;
232 FOR_EACH_PTR(merge_funcs
, tmp
) {
233 if (tmp
->data_type
== client_id
)
235 } END_FOR_EACH_PTR(tmp
);
239 struct smatch_state
*__client_merge_function(int owner
,
240 struct smatch_state
*s1
,
241 struct smatch_state
*s2
)
243 struct smatch_state
*tmp_state
;
244 struct hook_container
*tmp
;
246 /* Pass NULL states first and the rest alphabetically by name */
247 if (!s2
|| (s1
&& strcmp(s2
->name
, s1
->name
) < 0)) {
253 FOR_EACH_PTR(merge_funcs
, tmp
) {
254 if (tmp
->data_type
== owner
)
255 return ((merge_func_t
*) tmp
->fn
)(s1
, s2
);
256 } END_FOR_EACH_PTR(tmp
);
260 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
)
262 struct hook_container
*tmp
;
264 FOR_EACH_PTR(unmatched_state_funcs
, tmp
) {
265 if (tmp
->data_type
== sm
->owner
)
266 return ((unmatched_func_t
*) tmp
->fn
)(sm
);
267 } END_FOR_EACH_PTR(tmp
);
271 static struct scope_hook_list
*pop_scope_hook_list(struct scope_hook_stack
**stack
)
273 struct scope_hook_list
*hook_list
;
275 hook_list
= last_ptr_list((struct ptr_list
*)*stack
);
276 delete_ptr_list_last((struct ptr_list
**)stack
);
280 static void push_scope_hook_list(struct scope_hook_stack
**stack
, struct scope_hook_list
*l
)
282 add_ptr_list(stack
, l
);
285 void add_scope_hook(scope_hook
*fn
, void *data
)
287 struct scope_hook_list
*hook_list
;
288 struct scope_container
*new;
292 hook_list
= pop_scope_hook_list(&scope_hooks
);
293 new = __alloc_scope_container(0);
296 add_ptr_list(&hook_list
, new);
297 push_scope_hook_list(&scope_hooks
, hook_list
);
300 void __push_scope_hooks(void)
302 push_scope_hook_list(&scope_hooks
, NULL
);
305 void __call_scope_hooks(void)
307 struct scope_hook_list
*hook_list
;
308 struct scope_container
*tmp
;
313 hook_list
= pop_scope_hook_list(&scope_hooks
);
314 FOR_EACH_PTR(hook_list
, tmp
) {
315 ((scope_hook
*) tmp
->fn
)(tmp
->data
);
316 __free_scope_container(tmp
);
317 } END_FOR_EACH_PTR(tmp
);