2 * Copyright (C) 2006 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
27 struct hook_container
{
29 enum data_type data_type
;
32 ALLOCATOR(hook_container
, "hook functions");
33 DECLARE_PTR_LIST(hook_func_list
, struct hook_container
);
34 static struct hook_func_list
*hook_funcs
;
35 static struct hook_func_list
*merge_funcs
;
36 static struct hook_func_list
*unmatched_state_funcs
;
38 struct scope_container
{
42 ALLOCATOR(scope_container
, "scope hook functions");
43 DECLARE_PTR_LIST(scope_hook_list
, struct scope_container
);
44 DECLARE_PTR_LIST(scope_hook_stack
, struct scope_hook_list
);
45 static struct scope_hook_stack
*scope_hooks
;
47 void add_hook(void *func
, enum hook_type type
)
49 struct hook_container
*container
= __alloc_hook_container(0);
50 container
->hook_type
= type
;
54 container
->data_type
= EXPR_PTR
;
57 container
->data_type
= STMT_PTR
;
60 container
->data_type
= STMT_PTR
;
63 container
->data_type
= EXPR_PTR
;
66 container
->data_type
= EXPR_PTR
;
68 case DECLARATION_HOOK
:
69 container
->data_type
= SYMBOL_PTR
;
72 container
->data_type
= EXPR_PTR
;
74 case RAW_ASSIGNMENT_HOOK
:
75 container
->data_type
= EXPR_PTR
;
77 case GLOBAL_ASSIGNMENT_HOOK
:
78 container
->data_type
= EXPR_PTR
;
80 case CALL_ASSIGNMENT_HOOK
:
81 container
->data_type
= EXPR_PTR
;
83 case MACRO_ASSIGNMENT_HOOK
:
84 container
->data_type
= EXPR_PTR
;
87 container
->data_type
= EXPR_PTR
;
90 container
->data_type
= EXPR_PTR
;
93 container
->data_type
= EXPR_PTR
;
96 container
->data_type
= STMT_PTR
;
99 container
->data_type
= EXPR_PTR
;
102 container
->data_type
= EXPR_PTR
;
104 case WHOLE_CONDITION_HOOK
:
105 container
->data_type
= EXPR_PTR
;
107 case FUNCTION_CALL_HOOK
:
108 container
->data_type
= EXPR_PTR
;
110 case CALL_HOOK_AFTER_INLINE
:
111 container
->data_type
= EXPR_PTR
;
113 case FUNCTION_CALL_HOOK_AFTER
:
114 container
->data_type
= EXPR_PTR
;
117 container
->data_type
= EXPR_PTR
;
123 container
->data_type
= STMT_PTR
;
126 container
->data_type
= EXPR_PTR
;
129 container
->data_type
= EXPR_PTR
;
132 container
->data_type
= SYMBOL_PTR
;
135 container
->data_type
= SYMBOL_PTR
;
138 container
->data_type
= SYMBOL_PTR
;
141 container
->data_type
= SYMBOL_PTR
;
143 case AFTER_FUNC_HOOK
:
144 container
->data_type
= SYMBOL_PTR
;
147 container
->data_type
= EXPR_PTR
;
149 case INLINE_FN_START
:
150 container
->data_type
= EXPR_PTR
;
153 container
->data_type
= EXPR_PTR
;
156 container
->data_type
= SYM_LIST_PTR
;
159 add_ptr_list(&hook_funcs
, container
);
162 void add_merge_hook(int client_id
, merge_func_t
*func
)
164 struct hook_container
*container
= __alloc_hook_container(0);
165 container
->data_type
= client_id
;
166 container
->fn
= func
;
167 add_ptr_list(&merge_funcs
, container
);
170 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
)
172 struct hook_container
*container
= __alloc_hook_container(0);
173 container
->data_type
= client_id
;
174 container
->fn
= func
;
175 add_ptr_list(&unmatched_state_funcs
, container
);
178 static void pass_to_client(void *fn
)
180 typedef void (expr_func
)();
181 ((expr_func
*) fn
)();
184 static void pass_expr_to_client(void *fn
, void *data
)
186 typedef void (expr_func
)(struct expression
*expr
);
187 ((expr_func
*) fn
)((struct expression
*) data
);
190 static void pass_stmt_to_client(void *fn
, void *data
)
192 typedef void (stmt_func
)(struct statement
*stmt
);
193 ((stmt_func
*) fn
)((struct statement
*) data
);
196 static void pass_sym_to_client(void *fn
, void *data
)
198 typedef void (sym_func
)(struct symbol
*sym
);
199 ((sym_func
*) fn
)((struct symbol
*) data
);
202 static void pass_sym_list_to_client(void *fn
, void *data
)
204 typedef void (sym_func
)(struct symbol_list
*sym_list
);
205 ((sym_func
*) fn
)((struct symbol_list
*) data
);
208 void __pass_to_client(void *data
, enum hook_type type
)
210 struct hook_container
*container
;
212 FOR_EACH_PTR(hook_funcs
, container
) {
213 if (container
->hook_type
== type
) {
214 switch (container
->data_type
) {
216 pass_expr_to_client(container
->fn
, data
);
219 pass_stmt_to_client(container
->fn
, data
);
222 pass_sym_to_client(container
->fn
, data
);
225 pass_sym_list_to_client(container
->fn
, data
);
229 } END_FOR_EACH_PTR(container
);
232 void __pass_to_client_no_data(enum hook_type type
)
234 struct hook_container
*container
;
236 FOR_EACH_PTR(hook_funcs
, container
) {
237 if (container
->hook_type
== type
)
238 pass_to_client(container
->fn
);
239 } END_FOR_EACH_PTR(container
);
242 void __pass_case_to_client(struct expression
*switch_expr
,
243 struct expression
*case_expr
)
245 typedef void (case_func
)(struct expression
*switch_expr
,
246 struct expression
*case_expr
);
247 struct hook_container
*container
;
249 FOR_EACH_PTR(hook_funcs
, container
) {
250 if (container
->hook_type
== CASE_HOOK
)
251 ((case_func
*) container
->fn
)(switch_expr
, case_expr
);
252 } END_FOR_EACH_PTR(container
);
255 int __has_merge_function(int client_id
)
257 struct hook_container
*tmp
;
259 FOR_EACH_PTR(merge_funcs
, tmp
) {
260 if (tmp
->data_type
== client_id
)
262 } END_FOR_EACH_PTR(tmp
);
266 struct smatch_state
*__client_merge_function(int owner
,
267 struct smatch_state
*s1
,
268 struct smatch_state
*s2
)
270 struct smatch_state
*tmp_state
;
271 struct hook_container
*tmp
;
273 /* Pass NULL states first and the rest alphabetically by name */
274 if (!s2
|| (s1
&& strcmp(s2
->name
, s1
->name
) < 0)) {
280 FOR_EACH_PTR(merge_funcs
, tmp
) {
281 if (tmp
->data_type
== owner
)
282 return ((merge_func_t
*) tmp
->fn
)(s1
, s2
);
283 } END_FOR_EACH_PTR(tmp
);
287 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
)
289 struct hook_container
*tmp
;
291 FOR_EACH_PTR(unmatched_state_funcs
, tmp
) {
292 if (tmp
->data_type
== sm
->owner
)
293 return ((unmatched_func_t
*) tmp
->fn
)(sm
);
294 } END_FOR_EACH_PTR(tmp
);
298 static struct scope_hook_list
*pop_scope_hook_list(struct scope_hook_stack
**stack
)
300 struct scope_hook_list
*hook_list
;
302 hook_list
= last_ptr_list((struct ptr_list
*)*stack
);
303 delete_ptr_list_last((struct ptr_list
**)stack
);
307 static void push_scope_hook_list(struct scope_hook_stack
**stack
, struct scope_hook_list
*l
)
309 add_ptr_list(stack
, l
);
312 void add_scope_hook(scope_hook
*fn
, void *data
)
314 struct scope_hook_list
*hook_list
;
315 struct scope_container
*new;
319 hook_list
= pop_scope_hook_list(&scope_hooks
);
320 new = __alloc_scope_container(0);
323 add_ptr_list(&hook_list
, new);
324 push_scope_hook_list(&scope_hooks
, hook_list
);
327 void __push_scope_hooks(void)
329 push_scope_hook_list(&scope_hooks
, NULL
);
332 void __call_scope_hooks(void)
334 struct scope_hook_list
*hook_list
;
335 struct scope_container
*tmp
;
340 hook_list
= pop_scope_hook_list(&scope_hooks
);
341 FOR_EACH_PTR(hook_list
, tmp
) {
342 ((scope_hook
*) tmp
->fn
)(tmp
->data
);
343 __free_scope_container(tmp
);
344 } END_FOR_EACH_PTR(tmp
);