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
28 struct hook_container
{
33 ALLOCATOR(hook_container
, "hook functions");
34 DECLARE_PTR_LIST(hook_func_list
, struct hook_container
);
36 typedef void (sym_list_func
)(struct symbol_list
*sym_list
);
37 typedef void (array_init_hook
)(struct expression
*array
, int nr
);
39 static struct hook_func_list
*merge_funcs
;
40 static struct hook_func_list
*unmatched_state_funcs
;
41 static struct hook_func_list
*array_init_hooks
;
42 static struct hook_func_list
*hook_array
[NUM_HOOKS
] = {};
43 static const enum data_type data_types
[NUM_HOOKS
] = {
44 [EXPR_HOOK
] = EXPR_PTR
,
45 [EXPR_HOOK_AFTER
] = EXPR_PTR
,
46 [STMT_HOOK
] = STMT_PTR
,
47 [STMT_HOOK_AFTER
] = STMT_PTR
,
48 [SYM_HOOK
] = EXPR_PTR
,
49 [STRING_HOOK
] = EXPR_PTR
,
50 [DECLARATION_HOOK
] = SYMBOL_PTR
,
51 [DECLARATION_HOOK_AFTER
] = SYMBOL_PTR
,
52 [ASSIGNMENT_HOOK
] = EXPR_PTR
,
53 [ASSIGNMENT_HOOK_AFTER
] = EXPR_PTR
,
54 [RAW_ASSIGNMENT_HOOK
] = EXPR_PTR
,
55 [GLOBAL_ASSIGNMENT_HOOK
] = EXPR_PTR
,
56 [CALL_ASSIGNMENT_HOOK
] = EXPR_PTR
,
57 [MACRO_ASSIGNMENT_HOOK
] = EXPR_PTR
,
58 [BINOP_HOOK
] = EXPR_PTR
,
60 [LOGIC_HOOK
] = EXPR_PTR
,
61 [PRELOOP_HOOK
] = STMT_PTR
,
62 [AFTER_LOOP_NO_BREAKS
] = STMT_PTR
,
63 [CONDITION_HOOK
] = EXPR_PTR
,
64 [SELECT_HOOK
] = EXPR_PTR
,
65 [WHOLE_CONDITION_HOOK
] = EXPR_PTR
,
66 [FUNCTION_CALL_HOOK
] = EXPR_PTR
,
67 [CALL_HOOK_AFTER_INLINE
] = EXPR_PTR
,
68 [FUNCTION_CALL_HOOK_AFTER_DB
] = EXPR_PTR
,
69 [DEREF_HOOK
] = EXPR_PTR
,
70 [CASE_HOOK
] = NO_DATA
,
71 [ASM_HOOK
] = STMT_PTR
,
72 [CAST_HOOK
] = EXPR_PTR
,
73 [SIZEOF_HOOK
] = EXPR_PTR
,
74 [BASE_HOOK
] = SYMBOL_PTR
,
75 [FUNC_DEF_HOOK
] = SYMBOL_PTR
,
76 [AFTER_DEF_HOOK
] = SYMBOL_PTR
,
77 [END_FUNC_HOOK
] = SYMBOL_PTR
,
78 [AFTER_FUNC_HOOK
] = SYMBOL_PTR
,
79 [RETURN_HOOK
] = EXPR_PTR
,
80 [INLINE_FN_START
] = EXPR_PTR
,
81 [INLINE_FN_END
] = EXPR_PTR
,
82 [END_FILE_HOOK
] = SYM_LIST_PTR
,
85 void (**pre_merge_hooks
)(struct sm_state
*cur
, struct sm_state
*other
);
87 struct scope_container
{
91 ALLOCATOR(scope_container
, "scope hook functions");
92 DECLARE_PTR_LIST(scope_hook_list
, struct scope_container
);
93 DECLARE_PTR_LIST(scope_hook_stack
, struct scope_hook_list
);
94 static struct scope_hook_stack
*scope_hooks
;
96 void add_hook(void *func
, enum hook_type type
)
98 struct hook_container
*container
= __alloc_hook_container(0);
100 container
->hook_type
= type
;
101 container
->fn
= func
;
103 add_ptr_list(&hook_array
[type
], container
);
106 void add_merge_hook(int client_id
, merge_func_t
*func
)
108 struct hook_container
*container
= __alloc_hook_container(0);
109 container
->owner
= client_id
;
110 container
->fn
= func
;
111 add_ptr_list(&merge_funcs
, container
);
114 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
)
116 struct hook_container
*container
= __alloc_hook_container(0);
117 container
->owner
= client_id
;
118 container
->fn
= func
;
119 add_ptr_list(&unmatched_state_funcs
, container
);
122 void add_pre_merge_hook(int client_id
, void (*hook
)(struct sm_state
*cur
, struct sm_state
*other
))
124 pre_merge_hooks
[client_id
] = hook
;
127 static void pass_expr_to_client(void *fn
, void *data
)
129 ((expr_func
*)fn
)((struct expression
*)data
);
132 static void pass_stmt_to_client(void *fn
, void *data
)
134 ((stmt_func
*)fn
)((struct statement
*)data
);
137 static void pass_sym_to_client(void *fn
, void *data
)
139 ((sym_func
*)fn
)((struct symbol
*)data
);
142 static void pass_sym_list_to_client(void *fn
, void *data
)
144 ((sym_list_func
*)fn
)((struct symbol_list
*)data
);
147 void __pass_to_client(void *data
, enum hook_type type
)
149 struct hook_container
*container
;
151 FOR_EACH_PTR(hook_array
[type
], container
) {
152 switch (data_types
[type
]) {
154 pass_expr_to_client(container
->fn
, data
);
157 pass_stmt_to_client(container
->fn
, data
);
160 pass_sym_to_client(container
->fn
, data
);
163 pass_sym_list_to_client(container
->fn
, data
);
166 sm_warning("internal error. Unhandled hook type: %d", type
);
168 } END_FOR_EACH_PTR(container
);
171 void __pass_case_to_client(struct expression
*switch_expr
,
172 struct range_list
*rl
)
174 typedef void (case_func
)(struct expression
*switch_expr
,
175 struct range_list
*rl
);
176 struct hook_container
*container
;
178 FOR_EACH_PTR(hook_array
[CASE_HOOK
], container
) {
179 ((case_func
*)container
->fn
)(switch_expr
, rl
);
180 } END_FOR_EACH_PTR(container
);
183 int __has_merge_function(int client_id
)
185 struct hook_container
*tmp
;
187 FOR_EACH_PTR(merge_funcs
, tmp
) {
188 if (tmp
->owner
== client_id
)
190 } END_FOR_EACH_PTR(tmp
);
194 struct smatch_state
*__client_merge_function(int owner
,
195 struct smatch_state
*s1
,
196 struct smatch_state
*s2
)
198 struct smatch_state
*tmp_state
;
199 struct hook_container
*tmp
;
201 /* Pass NULL states first and the rest alphabetically by name */
202 if (!s2
|| (s1
&& strcmp(s2
->name
, s1
->name
) < 0)) {
208 FOR_EACH_PTR(merge_funcs
, tmp
) {
209 if (tmp
->owner
== owner
)
210 return ((merge_func_t
*)tmp
->fn
)(s1
, s2
);
211 } END_FOR_EACH_PTR(tmp
);
215 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
)
217 struct hook_container
*tmp
;
219 FOR_EACH_PTR(unmatched_state_funcs
, tmp
) {
220 if (tmp
->owner
== sm
->owner
)
221 return ((unmatched_func_t
*)tmp
->fn
)(sm
);
222 } END_FOR_EACH_PTR(tmp
);
226 void call_pre_merge_hook(struct sm_state
*cur
, struct sm_state
*other
)
228 if (cur
->owner
>= num_checks
)
231 if (pre_merge_hooks
[cur
->owner
])
232 pre_merge_hooks
[cur
->owner
](cur
, other
);
235 static struct scope_hook_list
*pop_scope_hook_list(struct scope_hook_stack
**stack
)
237 struct scope_hook_list
*hook_list
;
239 hook_list
= last_ptr_list((struct ptr_list
*)*stack
);
240 delete_ptr_list_last((struct ptr_list
**)stack
);
244 static void push_scope_hook_list(struct scope_hook_stack
**stack
, struct scope_hook_list
*l
)
246 add_ptr_list(stack
, l
);
249 void add_scope_hook(scope_hook
*fn
, void *data
)
251 struct scope_hook_list
*hook_list
;
252 struct scope_container
*new;
256 hook_list
= pop_scope_hook_list(&scope_hooks
);
257 new = __alloc_scope_container(0);
260 add_ptr_list(&hook_list
, new);
261 push_scope_hook_list(&scope_hooks
, hook_list
);
264 void __push_scope_hooks(void)
266 push_scope_hook_list(&scope_hooks
, NULL
);
269 void __call_scope_hooks(void)
271 struct scope_hook_list
*hook_list
;
272 struct scope_container
*tmp
;
277 hook_list
= pop_scope_hook_list(&scope_hooks
);
278 FOR_EACH_PTR(hook_list
, tmp
) {
279 ((scope_hook
*)tmp
->fn
)(tmp
->data
);
280 __free_scope_container(tmp
);
281 } END_FOR_EACH_PTR(tmp
);
284 void add_array_initialized_hook(void (*hook
)(struct expression
*array
, int nr
))
286 struct hook_container
*container
= __alloc_hook_container(0);
288 container
->fn
= hook
;
290 add_ptr_list(&array_init_hooks
, container
);
293 void __call_array_initialized_hooks(struct expression
*array
, int nr
)
295 struct hook_container
*tmp
;
297 FOR_EACH_PTR(array_init_hooks
, tmp
) {
298 ((array_init_hook
*)tmp
->fn
)(array
, nr
);
299 } END_FOR_EACH_PTR(tmp
);
302 void allocate_hook_memory(void)
304 pre_merge_hooks
= malloc(num_checks
* sizeof(*pre_merge_hooks
));
305 memset(pre_merge_hooks
, 0, num_checks
* sizeof(*pre_merge_hooks
));
308 void call_void_fns(struct void_fn_list
*list
)
312 FOR_EACH_PTR(list
, fn
) {
314 } END_FOR_EACH_PTR(fn
);
317 void call_expr_fns(struct expr_fn_list
*list
, struct expression
*expr
)
321 FOR_EACH_PTR(list
, fn
) {
323 } END_FOR_EACH_PTR(fn
);
326 void call_stmt_fns(struct stmt_fn_list
*list
, struct statement
*stmt
)
330 FOR_EACH_PTR(list
, fn
) {
332 } END_FOR_EACH_PTR(fn
);
335 void call_sym_fns(struct sym_fn_list
*list
, struct symbol
*sym
)
339 FOR_EACH_PTR(list
, fn
) {
341 } END_FOR_EACH_PTR(fn
);
344 void call_name_sym_fns(struct name_sym_fn_list
*list
, struct expression
*expr
, const char *name
, struct symbol
*sym
)
348 FOR_EACH_PTR(list
, fn
) {
349 (fn
)(expr
, name
, sym
);
350 } END_FOR_EACH_PTR(fn
);
353 void call_string_hooks(struct string_hook_list
*list
, struct expression
*expr
, const char *str
)
357 FOR_EACH_PTR(list
, fn
) {
359 } END_FOR_EACH_PTR(fn
);