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 } END_FOR_EACH_PTR(container
);
169 void __pass_case_to_client(struct expression
*switch_expr
,
170 struct range_list
*rl
)
172 typedef void (case_func
)(struct expression
*switch_expr
,
173 struct range_list
*rl
);
174 struct hook_container
*container
;
176 FOR_EACH_PTR(hook_array
[CASE_HOOK
], container
) {
177 ((case_func
*)container
->fn
)(switch_expr
, rl
);
178 } END_FOR_EACH_PTR(container
);
181 int __has_merge_function(int client_id
)
183 struct hook_container
*tmp
;
185 FOR_EACH_PTR(merge_funcs
, tmp
) {
186 if (tmp
->owner
== client_id
)
188 } END_FOR_EACH_PTR(tmp
);
192 struct smatch_state
*__client_merge_function(int owner
,
193 struct smatch_state
*s1
,
194 struct smatch_state
*s2
)
196 struct smatch_state
*tmp_state
;
197 struct hook_container
*tmp
;
199 /* Pass NULL states first and the rest alphabetically by name */
200 if (!s2
|| (s1
&& strcmp(s2
->name
, s1
->name
) < 0)) {
206 FOR_EACH_PTR(merge_funcs
, tmp
) {
207 if (tmp
->owner
== owner
)
208 return ((merge_func_t
*)tmp
->fn
)(s1
, s2
);
209 } END_FOR_EACH_PTR(tmp
);
213 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
)
215 struct hook_container
*tmp
;
217 FOR_EACH_PTR(unmatched_state_funcs
, tmp
) {
218 if (tmp
->owner
== sm
->owner
)
219 return ((unmatched_func_t
*)tmp
->fn
)(sm
);
220 } END_FOR_EACH_PTR(tmp
);
224 void call_pre_merge_hook(struct sm_state
*cur
, struct sm_state
*other
)
226 if (cur
->owner
>= num_checks
)
229 if (pre_merge_hooks
[cur
->owner
])
230 pre_merge_hooks
[cur
->owner
](cur
, other
);
233 static struct scope_hook_list
*pop_scope_hook_list(struct scope_hook_stack
**stack
)
235 struct scope_hook_list
*hook_list
;
237 hook_list
= last_ptr_list((struct ptr_list
*)*stack
);
238 delete_ptr_list_last((struct ptr_list
**)stack
);
242 static void push_scope_hook_list(struct scope_hook_stack
**stack
, struct scope_hook_list
*l
)
244 add_ptr_list(stack
, l
);
247 void add_scope_hook(scope_hook
*fn
, void *data
)
249 struct scope_hook_list
*hook_list
;
250 struct scope_container
*new;
254 hook_list
= pop_scope_hook_list(&scope_hooks
);
255 new = __alloc_scope_container(0);
258 add_ptr_list(&hook_list
, new);
259 push_scope_hook_list(&scope_hooks
, hook_list
);
262 void __push_scope_hooks(void)
264 push_scope_hook_list(&scope_hooks
, NULL
);
267 void __call_scope_hooks(void)
269 struct scope_hook_list
*hook_list
;
270 struct scope_container
*tmp
;
275 hook_list
= pop_scope_hook_list(&scope_hooks
);
276 FOR_EACH_PTR(hook_list
, tmp
) {
277 ((scope_hook
*)tmp
->fn
)(tmp
->data
);
278 __free_scope_container(tmp
);
279 } END_FOR_EACH_PTR(tmp
);
282 void add_array_initialized_hook(void (*hook
)(struct expression
*array
, int nr
))
284 struct hook_container
*container
= __alloc_hook_container(0);
286 container
->fn
= hook
;
288 add_ptr_list(&array_init_hooks
, container
);
291 void __call_array_initialized_hooks(struct expression
*array
, int nr
)
293 struct hook_container
*tmp
;
295 FOR_EACH_PTR(array_init_hooks
, tmp
) {
296 ((array_init_hook
*)tmp
->fn
)(array
, nr
);
297 } END_FOR_EACH_PTR(tmp
);
300 void allocate_hook_memory(void)
302 pre_merge_hooks
= malloc(num_checks
* sizeof(*pre_merge_hooks
));
303 memset(pre_merge_hooks
, 0, num_checks
* sizeof(*pre_merge_hooks
));
306 void call_void_fns(struct void_fn_list
*list
)
310 FOR_EACH_PTR(list
, fn
) {
312 } END_FOR_EACH_PTR(fn
);
315 void call_expr_fns(struct expr_fn_list
*list
, struct expression
*expr
)
319 FOR_EACH_PTR(list
, fn
) {
321 } END_FOR_EACH_PTR(fn
);
324 void call_stmt_fns(struct stmt_fn_list
*list
, struct statement
*stmt
)
328 FOR_EACH_PTR(list
, fn
) {
330 } END_FOR_EACH_PTR(fn
);
333 void call_sym_fns(struct sym_fn_list
*list
, struct symbol
*sym
)
337 FOR_EACH_PTR(list
, fn
) {
339 } END_FOR_EACH_PTR(fn
);
342 void call_name_sym_fns(struct name_sym_fn_list
*list
, struct expression
*expr
, const char *name
, struct symbol
*sym
)
346 FOR_EACH_PTR(list
, fn
) {
347 (fn
)(expr
, name
, sym
);
348 } END_FOR_EACH_PTR(fn
);
351 void call_string_hooks(struct string_hook_list
*list
, struct expression
*expr
, const char *str
)
355 FOR_EACH_PTR(list
, fn
) {
357 } END_FOR_EACH_PTR(fn
);