2 * sparse/smatch_hooks.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
12 struct hook_container
{
17 ALLOCATOR(hook_container
, "hook functions");
18 DECLARE_PTR_LIST(hook_func_list
, struct hook_container
);
19 static struct hook_func_list
*hook_funcs
;
20 static struct hook_func_list
*merge_funcs
;
21 static struct hook_func_list
*unmatched_state_funcs
;
23 void add_hook(void *func
, enum hook_type type
)
25 struct hook_container
*container
= __alloc_hook_container(0);
26 container
->hook_type
= type
;
30 container
->data_type
= EXPR_HOOK
;
33 container
->data_type
= STMT_HOOK
;
36 container
->data_type
= SYM_HOOK
;
38 case DECLARATION_HOOK
:
39 container
->data_type
= SYM_HOOK
;
42 container
->data_type
= EXPR_HOOK
;
44 case CALL_ASSIGNMENT_HOOK
:
45 container
->data_type
= EXPR_HOOK
;
48 container
->data_type
= EXPR_HOOK
;
51 container
->data_type
= EXPR_HOOK
;
53 case WHOLE_CONDITION_HOOK
:
54 container
->data_type
= EXPR_HOOK
;
56 case FUNCTION_CALL_HOOK
:
57 container
->data_type
= EXPR_HOOK
;
60 container
->data_type
= EXPR_HOOK
;
63 container
->data_type
= SYM_HOOK
;
66 container
->data_type
= SYM_HOOK
;
69 container
->data_type
= SYM_HOOK
;
72 container
->data_type
= STMT_HOOK
;
75 /* nothing needed... */
78 add_ptr_list(&hook_funcs
, container
);
81 void add_merge_hook(int client_id
, merge_func_t
*func
)
83 struct hook_container
*container
= __alloc_hook_container(0);
84 container
->data_type
= client_id
;
86 add_ptr_list(&merge_funcs
, container
);
89 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
)
91 struct hook_container
*container
= __alloc_hook_container(0);
92 container
->data_type
= client_id
;
94 add_ptr_list(&unmatched_state_funcs
, container
);
97 static void pass_to_client(void * fn
)
99 typedef void (expr_func
)();
100 ((expr_func
*) fn
)();
103 static void pass_expr_to_client(void * fn
, void * data
)
105 typedef void (expr_func
)(struct expression
*expr
);
106 ((expr_func
*) fn
)((struct expression
*) data
);
109 static void pass_stmt_to_client(void * fn
, void * data
)
111 typedef void (stmt_func
)(struct statement
*stmt
);
112 ((stmt_func
*) fn
)((struct statement
*) data
);
115 static void pass_sym_to_client(void * fn
, void * data
)
117 typedef void (sym_func
)(struct symbol
*sym
);
118 ((sym_func
*) fn
)((struct symbol
*) data
);
121 void __pass_to_client(void *data
, enum hook_type type
)
123 struct hook_container
*container
;
128 FOR_EACH_PTR(hook_funcs
, container
) {
129 if (container
->hook_type
== type
) {
130 switch(container
->data_type
) {
132 pass_expr_to_client(container
->fn
, data
);
135 pass_stmt_to_client(container
->fn
, data
);
138 pass_sym_to_client(container
->fn
, data
);
142 } END_FOR_EACH_PTR(container
);
145 void __pass_to_client_no_data(enum hook_type type
)
147 struct hook_container
*container
;
149 FOR_EACH_PTR(hook_funcs
, container
) {
150 if (container
->hook_type
== type
)
151 pass_to_client(container
->fn
);
152 } END_FOR_EACH_PTR(container
);
155 int __has_merge_function(int client_id
)
157 struct hook_container
*tmp
;
159 FOR_EACH_PTR(merge_funcs
, tmp
) {
160 if (tmp
->data_type
== client_id
)
162 } END_FOR_EACH_PTR(tmp
);
166 struct smatch_state
*__client_merge_function(int owner
, const char *name
,
168 struct smatch_state
*s1
,
169 struct smatch_state
*s2
)
171 struct smatch_state
*tmp_state
;
172 struct hook_container
*tmp
;
174 /* Pass NULL states first and the rest alphabetically by name */
175 if (!s2
|| (s1
&& strcmp(s2
->name
, s1
->name
) < 0)) {
181 FOR_EACH_PTR(merge_funcs
, tmp
) {
182 if (tmp
->data_type
== owner
)
183 return ((merge_func_t
*) tmp
->fn
)(name
, sym
, s1
, s2
);
184 } END_FOR_EACH_PTR(tmp
);
188 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
)
190 struct hook_container
*tmp
;
192 FOR_EACH_PTR(unmatched_state_funcs
, tmp
) {
193 if (tmp
->data_type
== sm
->owner
)
194 return ((unmatched_func_t
*) tmp
->fn
)(sm
);
195 } END_FOR_EACH_PTR(tmp
);