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
;
45 container
->data_type
= EXPR_HOOK
;
48 container
->data_type
= EXPR_HOOK
;
50 case WHOLE_CONDITION_HOOK
:
51 container
->data_type
= EXPR_HOOK
;
53 case FUNCTION_CALL_HOOK
:
54 container
->data_type
= EXPR_HOOK
;
57 container
->data_type
= EXPR_HOOK
;
60 container
->data_type
= SYM_HOOK
;
63 container
->data_type
= SYM_HOOK
;
66 container
->data_type
= SYM_HOOK
;
69 container
->data_type
= STMT_HOOK
;
72 /* nothing needed... */
75 add_ptr_list(&hook_funcs
, container
);
78 void add_merge_hook(int client_id
, merge_func_t
*func
)
80 struct hook_container
*container
= __alloc_hook_container(0);
81 container
->data_type
= client_id
;
83 add_ptr_list(&merge_funcs
, container
);
86 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
)
88 struct hook_container
*container
= __alloc_hook_container(0);
89 container
->data_type
= client_id
;
91 add_ptr_list(&unmatched_state_funcs
, container
);
94 static void pass_to_client(void * fn
)
96 typedef void (expr_func
)();
100 static void pass_expr_to_client(void * fn
, void * data
)
102 typedef void (expr_func
)(struct expression
*expr
);
103 ((expr_func
*) fn
)((struct expression
*) data
);
106 static void pass_stmt_to_client(void * fn
, void * data
)
108 typedef void (stmt_func
)(struct statement
*stmt
);
109 ((stmt_func
*) fn
)((struct statement
*) data
);
112 static void pass_sym_to_client(void * fn
, void * data
)
114 typedef void (sym_func
)(struct symbol
*sym
);
115 ((sym_func
*) fn
)((struct symbol
*) data
);
118 void __pass_to_client(void *data
, enum hook_type type
)
120 struct hook_container
*container
;
125 FOR_EACH_PTR(hook_funcs
, container
) {
126 if (container
->hook_type
== type
) {
127 switch(container
->data_type
) {
129 pass_expr_to_client(container
->fn
, data
);
132 pass_stmt_to_client(container
->fn
, data
);
135 pass_sym_to_client(container
->fn
, data
);
139 } END_FOR_EACH_PTR(container
);
142 void __pass_to_client_no_data(enum hook_type type
)
144 struct hook_container
*container
;
146 FOR_EACH_PTR(hook_funcs
, container
) {
147 if (container
->hook_type
== type
)
148 pass_to_client(container
->fn
);
149 } END_FOR_EACH_PTR(container
);
152 void __pass_declarations_to_client(struct symbol_list
*sym_list
)
155 FOR_EACH_PTR(sym_list
, sym
) {
156 __pass_to_client(sym
, DECLARATION_HOOK
);
157 } END_FOR_EACH_PTR(sym
);
160 int __has_merge_function(int client_id
)
162 struct hook_container
*tmp
;
164 FOR_EACH_PTR(merge_funcs
, tmp
) {
165 if (tmp
->data_type
== client_id
)
167 } END_FOR_EACH_PTR(tmp
);
171 struct smatch_state
*__client_merge_function(int owner
, const char *name
,
173 struct smatch_state
*s1
,
174 struct smatch_state
*s2
)
176 struct smatch_state
*tmp_state
;
177 struct hook_container
*tmp
;
179 /* Pass NULL states first and the rest alphabetically by name */
180 if (!s2
|| (s1
&& strcmp(s2
->name
, s1
->name
) < 0)) {
186 FOR_EACH_PTR(merge_funcs
, tmp
) {
187 if (tmp
->data_type
== owner
)
188 return ((merge_func_t
*) tmp
->fn
)(name
, sym
, s1
, s2
);
189 } END_FOR_EACH_PTR(tmp
);
193 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
)
195 struct hook_container
*tmp
;
197 FOR_EACH_PTR(unmatched_state_funcs
, tmp
) {
198 if (tmp
->data_type
== sm
->owner
)
199 return ((unmatched_func_t
*) tmp
->fn
)(sm
);
200 } END_FOR_EACH_PTR(tmp
);