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
;
114 container
->data_type
= EXPR_PTR
;
120 container
->data_type
= STMT_PTR
;
123 container
->data_type
= EXPR_PTR
;
126 container
->data_type
= EXPR_PTR
;
129 container
->data_type
= SYMBOL_PTR
;
132 container
->data_type
= SYMBOL_PTR
;
135 container
->data_type
= SYMBOL_PTR
;
138 container
->data_type
= SYMBOL_PTR
;
140 case AFTER_FUNC_HOOK
:
141 container
->data_type
= SYMBOL_PTR
;
144 container
->data_type
= EXPR_PTR
;
146 case INLINE_FN_START
:
147 container
->data_type
= EXPR_PTR
;
150 container
->data_type
= EXPR_PTR
;
153 container
->data_type
= SYM_LIST_PTR
;
156 add_ptr_list(&hook_funcs
, container
);
159 void add_merge_hook(int client_id
, merge_func_t
*func
)
161 struct hook_container
*container
= __alloc_hook_container(0);
162 container
->data_type
= client_id
;
163 container
->fn
= func
;
164 add_ptr_list(&merge_funcs
, container
);
167 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
)
169 struct hook_container
*container
= __alloc_hook_container(0);
170 container
->data_type
= client_id
;
171 container
->fn
= func
;
172 add_ptr_list(&unmatched_state_funcs
, container
);
175 static void pass_to_client(void *fn
)
177 typedef void (expr_func
)();
178 ((expr_func
*) fn
)();
181 static void pass_expr_to_client(void *fn
, void *data
)
183 typedef void (expr_func
)(struct expression
*expr
);
184 ((expr_func
*) fn
)((struct expression
*) data
);
187 static void pass_stmt_to_client(void *fn
, void *data
)
189 typedef void (stmt_func
)(struct statement
*stmt
);
190 ((stmt_func
*) fn
)((struct statement
*) data
);
193 static void pass_sym_to_client(void *fn
, void *data
)
195 typedef void (sym_func
)(struct symbol
*sym
);
196 ((sym_func
*) fn
)((struct symbol
*) data
);
199 static void pass_sym_list_to_client(void *fn
, void *data
)
201 typedef void (sym_func
)(struct symbol_list
*sym_list
);
202 ((sym_func
*) fn
)((struct symbol_list
*) data
);
205 void __pass_to_client(void *data
, enum hook_type type
)
207 struct hook_container
*container
;
209 FOR_EACH_PTR(hook_funcs
, container
) {
210 if (container
->hook_type
== type
) {
211 switch (container
->data_type
) {
213 pass_expr_to_client(container
->fn
, data
);
216 pass_stmt_to_client(container
->fn
, data
);
219 pass_sym_to_client(container
->fn
, data
);
222 pass_sym_list_to_client(container
->fn
, data
);
226 } END_FOR_EACH_PTR(container
);
229 void __pass_to_client_no_data(enum hook_type type
)
231 struct hook_container
*container
;
233 FOR_EACH_PTR(hook_funcs
, container
) {
234 if (container
->hook_type
== type
)
235 pass_to_client(container
->fn
);
236 } END_FOR_EACH_PTR(container
);
239 void __pass_case_to_client(struct expression
*switch_expr
,
240 struct expression
*case_expr
)
242 typedef void (case_func
)(struct expression
*switch_expr
,
243 struct expression
*case_expr
);
244 struct hook_container
*container
;
246 FOR_EACH_PTR(hook_funcs
, container
) {
247 if (container
->hook_type
== CASE_HOOK
)
248 ((case_func
*) container
->fn
)(switch_expr
, case_expr
);
249 } END_FOR_EACH_PTR(container
);
252 int __has_merge_function(int client_id
)
254 struct hook_container
*tmp
;
256 FOR_EACH_PTR(merge_funcs
, tmp
) {
257 if (tmp
->data_type
== client_id
)
259 } END_FOR_EACH_PTR(tmp
);
263 struct smatch_state
*__client_merge_function(int owner
,
264 struct smatch_state
*s1
,
265 struct smatch_state
*s2
)
267 struct smatch_state
*tmp_state
;
268 struct hook_container
*tmp
;
270 /* Pass NULL states first and the rest alphabetically by name */
271 if (!s2
|| (s1
&& strcmp(s2
->name
, s1
->name
) < 0)) {
277 FOR_EACH_PTR(merge_funcs
, tmp
) {
278 if (tmp
->data_type
== owner
)
279 return ((merge_func_t
*) tmp
->fn
)(s1
, s2
);
280 } END_FOR_EACH_PTR(tmp
);
284 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
)
286 struct hook_container
*tmp
;
288 FOR_EACH_PTR(unmatched_state_funcs
, tmp
) {
289 if (tmp
->data_type
== sm
->owner
)
290 return ((unmatched_func_t
*) tmp
->fn
)(sm
);
291 } END_FOR_EACH_PTR(tmp
);
295 static struct scope_hook_list
*pop_scope_hook_list(struct scope_hook_stack
**stack
)
297 struct scope_hook_list
*hook_list
;
299 hook_list
= last_ptr_list((struct ptr_list
*)*stack
);
300 delete_ptr_list_last((struct ptr_list
**)stack
);
304 static void push_scope_hook_list(struct scope_hook_stack
**stack
, struct scope_hook_list
*l
)
306 add_ptr_list(stack
, l
);
309 void add_scope_hook(scope_hook
*fn
, void *data
)
311 struct scope_hook_list
*hook_list
;
312 struct scope_container
*new;
316 hook_list
= pop_scope_hook_list(&scope_hooks
);
317 new = __alloc_scope_container(0);
320 add_ptr_list(&hook_list
, new);
321 push_scope_hook_list(&scope_hooks
, hook_list
);
324 void __push_scope_hooks(void)
326 push_scope_hook_list(&scope_hooks
, NULL
);
329 void __call_scope_hooks(void)
331 struct scope_hook_list
*hook_list
;
332 struct scope_container
*tmp
;
337 hook_list
= pop_scope_hook_list(&scope_hooks
);
338 FOR_EACH_PTR(hook_list
, tmp
) {
339 ((scope_hook
*) tmp
->fn
)(tmp
->data
);
340 __free_scope_container(tmp
);
341 } END_FOR_EACH_PTR(tmp
);