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
;
66 container
->data_type
= SYM_HOOK
;
69 container
->data_type
= SYM_HOOK
;
72 container
->data_type
= SYM_HOOK
;
75 container
->data_type
= STMT_HOOK
;
78 /* nothing needed... */
81 add_ptr_list(&hook_funcs
, container
);
84 void add_merge_hook(int client_id
, merge_func_t
*func
)
86 struct hook_container
*container
= __alloc_hook_container(0);
87 container
->data_type
= client_id
;
89 add_ptr_list(&merge_funcs
, container
);
92 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
)
94 struct hook_container
*container
= __alloc_hook_container(0);
95 container
->data_type
= client_id
;
97 add_ptr_list(&unmatched_state_funcs
, container
);
100 static void pass_to_client(void * fn
)
102 typedef void (expr_func
)();
103 ((expr_func
*) fn
)();
106 static void pass_expr_to_client(void * fn
, void * data
)
108 typedef void (expr_func
)(struct expression
*expr
);
109 ((expr_func
*) fn
)((struct expression
*) data
);
112 static void pass_stmt_to_client(void * fn
, void * data
)
114 typedef void (stmt_func
)(struct statement
*stmt
);
115 ((stmt_func
*) fn
)((struct statement
*) data
);
118 static void pass_sym_to_client(void * fn
, void * data
)
120 typedef void (sym_func
)(struct symbol
*sym
);
121 ((sym_func
*) fn
)((struct symbol
*) data
);
124 void __pass_to_client(void *data
, enum hook_type type
)
126 struct hook_container
*container
;
131 FOR_EACH_PTR(hook_funcs
, container
) {
132 if (container
->hook_type
== type
) {
133 switch(container
->data_type
) {
135 pass_expr_to_client(container
->fn
, data
);
138 pass_stmt_to_client(container
->fn
, data
);
141 pass_sym_to_client(container
->fn
, data
);
145 } END_FOR_EACH_PTR(container
);
148 void __pass_to_client_no_data(enum hook_type type
)
150 struct hook_container
*container
;
152 FOR_EACH_PTR(hook_funcs
, container
) {
153 if (container
->hook_type
== type
)
154 pass_to_client(container
->fn
);
155 } END_FOR_EACH_PTR(container
);
158 void __pass_case_to_client(struct expression
*switch_expr
,
159 struct expression
*case_expr
)
161 typedef void (case_func
)(struct expression
*switch_expr
,
162 struct expression
*case_expr
);
163 struct hook_container
*container
;
165 FOR_EACH_PTR(hook_funcs
, container
) {
166 if (container
->hook_type
== CASE_HOOK
) {
167 ((case_func
*) container
->fn
)(switch_expr
, case_expr
);
169 } END_FOR_EACH_PTR(container
);
172 int __has_merge_function(int client_id
)
174 struct hook_container
*tmp
;
176 FOR_EACH_PTR(merge_funcs
, tmp
) {
177 if (tmp
->data_type
== client_id
)
179 } END_FOR_EACH_PTR(tmp
);
183 struct smatch_state
*__client_merge_function(int owner
, const char *name
,
185 struct smatch_state
*s1
,
186 struct smatch_state
*s2
)
188 struct smatch_state
*tmp_state
;
189 struct hook_container
*tmp
;
191 /* Pass NULL states first and the rest alphabetically by name */
192 if (!s2
|| (s1
&& strcmp(s2
->name
, s1
->name
) < 0)) {
198 FOR_EACH_PTR(merge_funcs
, tmp
) {
199 if (tmp
->data_type
== owner
)
200 return ((merge_func_t
*) tmp
->fn
)(name
, sym
, s1
, s2
);
201 } END_FOR_EACH_PTR(tmp
);
205 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
)
207 struct hook_container
*tmp
;
209 FOR_EACH_PTR(unmatched_state_funcs
, tmp
) {
210 if (tmp
->data_type
== sm
->owner
)
211 return ((unmatched_func_t
*) tmp
->fn
)(sm
);
212 } END_FOR_EACH_PTR(tmp
);