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
;
74 case WHOLE_CONDITION_HOOK
:
75 container
->data_type
= EXPR_PTR
;
77 case FUNCTION_CALL_HOOK
:
78 container
->data_type
= EXPR_PTR
;
81 container
->data_type
= EXPR_PTR
;
87 container
->data_type
= SYMBOL_PTR
;
90 container
->data_type
= SYMBOL_PTR
;
93 container
->data_type
= SYMBOL_PTR
;
96 container
->data_type
= EXPR_PTR
;
99 /* nothing needed... */
102 add_ptr_list(&hook_funcs
, container
);
105 void add_merge_hook(int client_id
, merge_func_t
*func
)
107 struct hook_container
*container
= __alloc_hook_container(0);
108 container
->data_type
= client_id
;
109 container
->fn
= func
;
110 add_ptr_list(&merge_funcs
, container
);
113 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
)
115 struct hook_container
*container
= __alloc_hook_container(0);
116 container
->data_type
= client_id
;
117 container
->fn
= func
;
118 add_ptr_list(&unmatched_state_funcs
, container
);
121 static void pass_to_client(void * fn
)
123 typedef void (expr_func
)();
124 ((expr_func
*) fn
)();
127 static void pass_expr_to_client(void * fn
, void * data
)
129 typedef void (expr_func
)(struct expression
*expr
);
130 ((expr_func
*) fn
)((struct expression
*) data
);
133 static void pass_stmt_to_client(void * fn
, void * data
)
135 typedef void (stmt_func
)(struct statement
*stmt
);
136 ((stmt_func
*) fn
)((struct statement
*) data
);
139 static void pass_sym_to_client(void * fn
, void * data
)
141 typedef void (sym_func
)(struct symbol
*sym
);
142 ((sym_func
*) fn
)((struct symbol
*) data
);
145 void __pass_to_client(void *data
, enum hook_type type
)
147 struct hook_container
*container
;
152 FOR_EACH_PTR(hook_funcs
, container
) {
153 if (container
->hook_type
== type
) {
154 switch (container
->data_type
) {
156 pass_expr_to_client(container
->fn
, data
);
159 pass_stmt_to_client(container
->fn
, data
);
162 pass_sym_to_client(container
->fn
, data
);
166 } END_FOR_EACH_PTR(container
);
169 void __pass_to_client_no_data(enum hook_type type
)
171 struct hook_container
*container
;
173 FOR_EACH_PTR(hook_funcs
, container
) {
174 if (container
->hook_type
== type
)
175 pass_to_client(container
->fn
);
176 } END_FOR_EACH_PTR(container
);
179 void __pass_case_to_client(struct expression
*switch_expr
,
180 struct expression
*case_expr
)
182 typedef void (case_func
)(struct expression
*switch_expr
,
183 struct expression
*case_expr
);
184 struct hook_container
*container
;
186 FOR_EACH_PTR(hook_funcs
, container
) {
187 if (container
->hook_type
== CASE_HOOK
) {
188 ((case_func
*) container
->fn
)(switch_expr
, case_expr
);
190 } END_FOR_EACH_PTR(container
);
193 int __has_merge_function(int client_id
)
195 struct hook_container
*tmp
;
197 FOR_EACH_PTR(merge_funcs
, tmp
) {
198 if (tmp
->data_type
== client_id
)
200 } END_FOR_EACH_PTR(tmp
);
204 struct smatch_state
*__client_merge_function(int owner
, const char *name
,
206 struct smatch_state
*s1
,
207 struct smatch_state
*s2
)
209 struct smatch_state
*tmp_state
;
210 struct hook_container
*tmp
;
212 /* Pass NULL states first and the rest alphabetically by name */
213 if (!s2
|| (s1
&& strcmp(s2
->name
, s1
->name
) < 0)) {
219 FOR_EACH_PTR(merge_funcs
, tmp
) {
220 if (tmp
->data_type
== owner
)
221 return ((merge_func_t
*) tmp
->fn
)(name
, sym
, s1
, s2
);
222 } END_FOR_EACH_PTR(tmp
);
226 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
)
228 struct hook_container
*tmp
;
230 FOR_EACH_PTR(unmatched_state_funcs
, tmp
) {
231 if (tmp
->data_type
== sm
->owner
)
232 return ((unmatched_func_t
*) tmp
->fn
)(sm
);
233 } END_FOR_EACH_PTR(tmp
);
237 static struct scope_hook_list
*pop_scope_hook_list(struct scope_hook_stack
**stack
)
239 struct scope_hook_list
*hook_list
;
241 hook_list
= last_ptr_list((struct ptr_list
*)*stack
);
242 delete_ptr_list_last((struct ptr_list
**)stack
);
246 static void push_scope_hook_list(struct scope_hook_stack
**stack
, struct scope_hook_list
*l
)
248 add_ptr_list(stack
, l
);
251 void add_scope_hook(scope_hook
*fn
, void *data
)
253 struct scope_hook_list
*hook_list
;
254 struct scope_container
*new;
258 hook_list
= pop_scope_hook_list(&scope_hooks
);
259 new = __alloc_scope_container(0);
262 add_ptr_list(&hook_list
, new);
263 push_scope_hook_list(&scope_hooks
, hook_list
);
266 void __push_scope_hooks(void)
268 push_scope_hook_list(&scope_hooks
, NULL
);
271 void __call_scope_hooks(void)
273 struct scope_hook_list
*hook_list
;
274 struct scope_container
*tmp
;
279 hook_list
= pop_scope_hook_list(&scope_hooks
);
280 FOR_EACH_PTR(hook_list
, tmp
) {
281 ((scope_hook
*) tmp
->fn
)(tmp
->data
);
282 __free_scope_container(tmp
);
283 } END_FOR_EACH_PTR(tmp
);