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 [POSTLOOP_HOOK
] = STMT_PTR
,
63 [AFTER_LOOP_NO_BREAKS
] = STMT_PTR
,
64 [CONDITION_HOOK
] = EXPR_PTR
,
65 [SELECT_HOOK
] = EXPR_PTR
,
66 [WHOLE_CONDITION_HOOK
] = EXPR_PTR
,
67 [FUNCTION_CALL_HOOK_BEFORE
] = EXPR_PTR
,
68 [FUNCTION_CALL_HOOK
] = EXPR_PTR
,
69 [CALL_HOOK_AFTER_INLINE
] = EXPR_PTR
,
70 [FUNCTION_CALL_HOOK_AFTER_DB
] = EXPR_PTR
,
71 [DEREF_HOOK
] = EXPR_PTR
,
72 [CASE_HOOK
] = NO_DATA
,
73 [ASM_HOOK
] = STMT_PTR
,
74 [CAST_HOOK
] = EXPR_PTR
,
75 [SIZEOF_HOOK
] = EXPR_PTR
,
76 [BASE_HOOK
] = SYMBOL_PTR
,
77 [FUNC_DEF_HOOK
] = SYMBOL_PTR
,
78 [AFTER_DEF_HOOK
] = SYMBOL_PTR
,
79 [END_FUNC_HOOK
] = SYMBOL_PTR
,
80 [AFTER_FUNC_HOOK
] = SYMBOL_PTR
,
81 [RETURN_HOOK
] = EXPR_PTR
,
82 [INLINE_FN_START
] = EXPR_PTR
,
83 [INLINE_FN_END
] = EXPR_PTR
,
84 [END_FILE_HOOK
] = SYM_LIST_PTR
,
87 void (**pre_merge_hooks
)(struct sm_state
*cur
, struct sm_state
*other
);
89 struct scope_container
{
93 ALLOCATOR(scope_container
, "scope hook functions");
94 DECLARE_PTR_LIST(scope_hook_list
, struct scope_container
);
95 DECLARE_PTR_LIST(scope_hook_stack
, struct scope_hook_list
);
96 static struct scope_hook_stack
*scope_hooks
;
98 extern int __cur_check_id
;
99 void add_hook(void *func
, enum hook_type type
)
101 struct hook_container
*container
= __alloc_hook_container(0);
103 container
->owner
= __cur_check_id
;
104 container
->hook_type
= type
;
105 container
->fn
= func
;
107 add_ptr_list(&hook_array
[type
], container
);
110 void add_merge_hook(int client_id
, merge_func_t
*func
)
112 struct hook_container
*container
= __alloc_hook_container(0);
113 container
->owner
= client_id
;
114 container
->fn
= func
;
115 add_ptr_list(&merge_funcs
, container
);
118 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
)
120 struct hook_container
*container
= __alloc_hook_container(0);
121 container
->owner
= client_id
;
122 container
->fn
= func
;
123 add_ptr_list(&unmatched_state_funcs
, container
);
126 void add_pre_merge_hook(int client_id
, void (*hook
)(struct sm_state
*cur
, struct sm_state
*other
))
128 pre_merge_hooks
[client_id
] = hook
;
131 static void pass_expr_to_client(void *fn
, void *data
)
133 ((expr_func
*)fn
)((struct expression
*)data
);
136 static void pass_stmt_to_client(void *fn
, void *data
)
138 ((stmt_func
*)fn
)((struct statement
*)data
);
141 static void pass_sym_to_client(void *fn
, void *data
)
143 ((sym_func
*)fn
)((struct symbol
*)data
);
146 static void pass_sym_list_to_client(void *fn
, void *data
)
148 ((sym_list_func
*)fn
)((struct symbol_list
*)data
);
151 void __pass_to_client(void *data
, enum hook_type type
)
153 struct hook_container
*container
;
155 FOR_EACH_PTR(hook_array
[type
], container
) {
156 switch (data_types
[type
]) {
158 pass_expr_to_client(container
->fn
, data
);
161 pass_stmt_to_client(container
->fn
, data
);
164 pass_sym_to_client(container
->fn
, data
);
167 pass_sym_list_to_client(container
->fn
, data
);
170 sm_warning("internal error. Unhandled hook type: %d", type
);
172 } END_FOR_EACH_PTR(container
);
175 void __pass_case_to_client(struct expression
*switch_expr
,
176 struct range_list
*rl
)
178 typedef void (case_func
)(struct expression
*switch_expr
,
179 struct range_list
*rl
);
180 struct hook_container
*container
;
182 FOR_EACH_PTR(hook_array
[CASE_HOOK
], container
) {
183 ((case_func
*)container
->fn
)(switch_expr
, rl
);
184 } END_FOR_EACH_PTR(container
);
187 int __has_merge_function(int client_id
)
189 struct hook_container
*tmp
;
191 FOR_EACH_PTR(merge_funcs
, tmp
) {
192 if (tmp
->owner
== client_id
)
194 } END_FOR_EACH_PTR(tmp
);
198 struct smatch_state
*__client_merge_function(int owner
,
199 struct smatch_state
*s1
,
200 struct smatch_state
*s2
)
202 struct smatch_state
*tmp_state
;
203 struct hook_container
*tmp
;
205 /* Pass NULL states first and the rest alphabetically by name */
206 if (!s2
|| (s1
&& strcmp(s2
->name
, s1
->name
) < 0)) {
212 FOR_EACH_PTR(merge_funcs
, tmp
) {
213 if (tmp
->owner
== owner
)
214 return ((merge_func_t
*)tmp
->fn
)(s1
, s2
);
215 } END_FOR_EACH_PTR(tmp
);
219 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
)
221 struct hook_container
*tmp
;
223 FOR_EACH_PTR(unmatched_state_funcs
, tmp
) {
224 if (tmp
->owner
== sm
->owner
)
225 return ((unmatched_func_t
*)tmp
->fn
)(sm
);
226 } END_FOR_EACH_PTR(tmp
);
230 void call_pre_merge_hook(struct sm_state
*cur
, struct sm_state
*other
)
232 if (cur
->owner
>= num_checks
)
235 if (pre_merge_hooks
[cur
->owner
])
236 pre_merge_hooks
[cur
->owner
](cur
, other
);
239 static struct scope_hook_list
*pop_scope_hook_list(struct scope_hook_stack
**stack
)
241 struct scope_hook_list
*hook_list
;
243 hook_list
= last_ptr_list((struct ptr_list
*)*stack
);
244 delete_ptr_list_last((struct ptr_list
**)stack
);
248 static void push_scope_hook_list(struct scope_hook_stack
**stack
, struct scope_hook_list
*l
)
250 add_ptr_list(stack
, l
);
253 void add_scope_hook(scope_hook
*fn
, void *data
)
255 struct scope_hook_list
*hook_list
;
256 struct scope_container
*new;
260 hook_list
= pop_scope_hook_list(&scope_hooks
);
261 new = __alloc_scope_container(0);
264 add_ptr_list(&hook_list
, new);
265 push_scope_hook_list(&scope_hooks
, hook_list
);
268 void __push_scope_hooks(void)
270 push_scope_hook_list(&scope_hooks
, NULL
);
273 void __call_scope_hooks(void)
275 struct scope_hook_list
*hook_list
;
276 struct scope_container
*tmp
;
281 hook_list
= pop_scope_hook_list(&scope_hooks
);
282 FOR_EACH_PTR(hook_list
, tmp
) {
283 ((scope_hook
*)tmp
->fn
)(tmp
->data
);
284 __free_scope_container(tmp
);
285 } END_FOR_EACH_PTR(tmp
);
288 void add_array_initialized_hook(void (*hook
)(struct expression
*array
, int nr
))
290 struct hook_container
*container
= __alloc_hook_container(0);
292 container
->fn
= hook
;
294 add_ptr_list(&array_init_hooks
, container
);
297 void __call_array_initialized_hooks(struct expression
*array
, int nr
)
299 struct hook_container
*tmp
;
301 FOR_EACH_PTR(array_init_hooks
, tmp
) {
302 ((array_init_hook
*)tmp
->fn
)(array
, nr
);
303 } END_FOR_EACH_PTR(tmp
);
306 void allocate_hook_memory(void)
308 pre_merge_hooks
= malloc(num_checks
* sizeof(*pre_merge_hooks
));
309 memset(pre_merge_hooks
, 0, num_checks
* sizeof(*pre_merge_hooks
));
312 void call_void_fns(struct void_fn_list
*list
)
316 FOR_EACH_PTR(list
, fn
) {
318 } END_FOR_EACH_PTR(fn
);
321 void call_expr_fns(struct expr_fn_list
*list
, struct expression
*expr
)
325 FOR_EACH_PTR(list
, fn
) {
327 } END_FOR_EACH_PTR(fn
);
330 void call_stmt_fns(struct stmt_fn_list
*list
, struct statement
*stmt
)
334 FOR_EACH_PTR(list
, fn
) {
336 } END_FOR_EACH_PTR(fn
);
339 void call_sym_fns(struct sym_fn_list
*list
, struct symbol
*sym
)
343 FOR_EACH_PTR(list
, fn
) {
345 } END_FOR_EACH_PTR(fn
);
348 void call_name_sym_fns(struct name_sym_fn_list
*list
, struct expression
*expr
, const char *name
, struct symbol
*sym
)
352 FOR_EACH_PTR(list
, fn
) {
353 (fn
)(expr
, name
, sym
);
354 } END_FOR_EACH_PTR(fn
);
357 void call_string_hooks(struct string_hook_list
*list
, struct expression
*expr
, const char *str
)
361 FOR_EACH_PTR(list
, fn
) {
363 } END_FOR_EACH_PTR(fn
);