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 (expr_func
)(struct expression
*expr
);
37 typedef void (stmt_func
)(struct statement
*stmt
);
38 typedef void (sym_func
)(struct symbol
*sym
);
39 typedef void (sym_list_func
)(struct symbol_list
*sym_list
);
40 typedef void (array_init_hook
)(struct expression
*array
, int nr
);
42 static struct hook_func_list
*merge_funcs
;
43 static struct hook_func_list
*unmatched_state_funcs
;
44 static struct hook_func_list
*array_init_hooks
;
45 static struct hook_func_list
*hook_array
[NUM_HOOKS
] = {};
46 static const enum data_type data_types
[NUM_HOOKS
] = {
47 [EXPR_HOOK
] = EXPR_PTR
,
48 [EXPR_HOOK_AFTER
] = EXPR_PTR
,
49 [STMT_HOOK
] = STMT_PTR
,
50 [STMT_HOOK_AFTER
] = STMT_PTR
,
51 [SYM_HOOK
] = EXPR_PTR
,
52 [STRING_HOOK
] = EXPR_PTR
,
53 [DECLARATION_HOOK
] = SYMBOL_PTR
,
54 [ASSIGNMENT_HOOK
] = EXPR_PTR
,
55 [ASSIGNMENT_HOOK_AFTER
] = EXPR_PTR
,
56 [RAW_ASSIGNMENT_HOOK
] = EXPR_PTR
,
57 [GLOBAL_ASSIGNMENT_HOOK
] = EXPR_PTR
,
58 [CALL_ASSIGNMENT_HOOK
] = EXPR_PTR
,
59 [MACRO_ASSIGNMENT_HOOK
] = EXPR_PTR
,
60 [BINOP_HOOK
] = EXPR_PTR
,
62 [LOGIC_HOOK
] = EXPR_PTR
,
63 [PRELOOP_HOOK
] = STMT_PTR
,
64 [CONDITION_HOOK
] = EXPR_PTR
,
65 [SELECT_HOOK
] = EXPR_PTR
,
66 [WHOLE_CONDITION_HOOK
] = EXPR_PTR
,
67 [FUNCTION_CALL_HOOK
] = EXPR_PTR
,
68 [CALL_HOOK_AFTER_INLINE
] = EXPR_PTR
,
69 [FUNCTION_CALL_HOOK_AFTER_DB
] = EXPR_PTR
,
70 [DEREF_HOOK
] = EXPR_PTR
,
71 [CASE_HOOK
] = NO_DATA
,
72 [ASM_HOOK
] = STMT_PTR
,
73 [CAST_HOOK
] = EXPR_PTR
,
74 [SIZEOF_HOOK
] = EXPR_PTR
,
75 [BASE_HOOK
] = SYMBOL_PTR
,
76 [FUNC_DEF_HOOK
] = SYMBOL_PTR
,
77 [AFTER_DEF_HOOK
] = SYMBOL_PTR
,
78 [END_FUNC_HOOK
] = SYMBOL_PTR
,
79 [AFTER_FUNC_HOOK
] = SYMBOL_PTR
,
80 [RETURN_HOOK
] = EXPR_PTR
,
81 [INLINE_FN_START
] = EXPR_PTR
,
82 [INLINE_FN_END
] = EXPR_PTR
,
83 [END_FILE_HOOK
] = SYM_LIST_PTR
,
86 void (**pre_merge_hooks
)(struct sm_state
*cur
, struct sm_state
*other
);
88 struct scope_container
{
92 ALLOCATOR(scope_container
, "scope hook functions");
93 DECLARE_PTR_LIST(scope_hook_list
, struct scope_container
);
94 DECLARE_PTR_LIST(scope_hook_stack
, struct scope_hook_list
);
95 static struct scope_hook_stack
*scope_hooks
;
97 void add_hook(void *func
, enum hook_type type
)
99 struct hook_container
*container
= __alloc_hook_container(0);
101 container
->hook_type
= type
;
102 container
->fn
= func
;
104 add_ptr_list(&hook_array
[type
], container
);
107 void add_merge_hook(int client_id
, merge_func_t
*func
)
109 struct hook_container
*container
= __alloc_hook_container(0);
110 container
->owner
= client_id
;
111 container
->fn
= func
;
112 add_ptr_list(&merge_funcs
, container
);
115 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
)
117 struct hook_container
*container
= __alloc_hook_container(0);
118 container
->owner
= client_id
;
119 container
->fn
= func
;
120 add_ptr_list(&unmatched_state_funcs
, container
);
123 void add_pre_merge_hook(int client_id
, void (*hook
)(struct sm_state
*cur
, struct sm_state
*other
))
125 pre_merge_hooks
[client_id
] = hook
;
128 static void pass_expr_to_client(void *fn
, void *data
)
130 ((expr_func
*)fn
)((struct expression
*)data
);
133 static void pass_stmt_to_client(void *fn
, void *data
)
135 ((stmt_func
*)fn
)((struct statement
*)data
);
138 static void pass_sym_to_client(void *fn
, void *data
)
140 ((sym_func
*)fn
)((struct symbol
*)data
);
143 static void pass_sym_list_to_client(void *fn
, void *data
)
145 ((sym_list_func
*)fn
)((struct symbol_list
*)data
);
148 void __pass_to_client(void *data
, enum hook_type type
)
150 struct hook_container
*container
;
152 FOR_EACH_PTR(hook_array
[type
], container
) {
153 switch (data_types
[type
]) {
155 pass_expr_to_client(container
->fn
, data
);
158 pass_stmt_to_client(container
->fn
, data
);
161 pass_sym_to_client(container
->fn
, data
);
164 pass_sym_list_to_client(container
->fn
, data
);
167 } END_FOR_EACH_PTR(container
);
170 void __pass_case_to_client(struct expression
*switch_expr
,
171 struct range_list
*rl
)
173 typedef void (case_func
)(struct expression
*switch_expr
,
174 struct range_list
*rl
);
175 struct hook_container
*container
;
177 FOR_EACH_PTR(hook_array
[CASE_HOOK
], container
) {
178 ((case_func
*)container
->fn
)(switch_expr
, rl
);
179 } END_FOR_EACH_PTR(container
);
182 int __has_merge_function(int client_id
)
184 struct hook_container
*tmp
;
186 FOR_EACH_PTR(merge_funcs
, tmp
) {
187 if (tmp
->owner
== client_id
)
189 } END_FOR_EACH_PTR(tmp
);
193 struct smatch_state
*__client_merge_function(int owner
,
194 struct smatch_state
*s1
,
195 struct smatch_state
*s2
)
197 struct smatch_state
*tmp_state
;
198 struct hook_container
*tmp
;
200 /* Pass NULL states first and the rest alphabetically by name */
201 if (!s2
|| (s1
&& strcmp(s2
->name
, s1
->name
) < 0)) {
207 FOR_EACH_PTR(merge_funcs
, tmp
) {
208 if (tmp
->owner
== owner
)
209 return ((merge_func_t
*)tmp
->fn
)(s1
, s2
);
210 } END_FOR_EACH_PTR(tmp
);
214 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
)
216 struct hook_container
*tmp
;
218 FOR_EACH_PTR(unmatched_state_funcs
, tmp
) {
219 if (tmp
->owner
== sm
->owner
)
220 return ((unmatched_func_t
*)tmp
->fn
)(sm
);
221 } END_FOR_EACH_PTR(tmp
);
225 void call_pre_merge_hook(struct sm_state
*cur
, struct sm_state
*other
)
227 if (cur
->owner
>= num_checks
)
230 if (pre_merge_hooks
[cur
->owner
])
231 pre_merge_hooks
[cur
->owner
](cur
, other
);
234 static struct scope_hook_list
*pop_scope_hook_list(struct scope_hook_stack
**stack
)
236 struct scope_hook_list
*hook_list
;
238 hook_list
= last_ptr_list((struct ptr_list
*)*stack
);
239 delete_ptr_list_last((struct ptr_list
**)stack
);
243 static void push_scope_hook_list(struct scope_hook_stack
**stack
, struct scope_hook_list
*l
)
245 add_ptr_list(stack
, l
);
248 void add_scope_hook(scope_hook
*fn
, void *data
)
250 struct scope_hook_list
*hook_list
;
251 struct scope_container
*new;
255 hook_list
= pop_scope_hook_list(&scope_hooks
);
256 new = __alloc_scope_container(0);
259 add_ptr_list(&hook_list
, new);
260 push_scope_hook_list(&scope_hooks
, hook_list
);
263 void __push_scope_hooks(void)
265 push_scope_hook_list(&scope_hooks
, NULL
);
268 void __call_scope_hooks(void)
270 struct scope_hook_list
*hook_list
;
271 struct scope_container
*tmp
;
276 hook_list
= pop_scope_hook_list(&scope_hooks
);
277 FOR_EACH_PTR(hook_list
, tmp
) {
278 ((scope_hook
*)tmp
->fn
)(tmp
->data
);
279 __free_scope_container(tmp
);
280 } END_FOR_EACH_PTR(tmp
);
283 void add_array_initialized_hook(void (*hook
)(struct expression
*array
, int nr
))
285 struct hook_container
*container
= __alloc_hook_container(0);
287 container
->fn
= hook
;
289 add_ptr_list(&array_init_hooks
, container
);
292 void __call_array_initialized_hooks(struct expression
*array
, int nr
)
294 struct hook_container
*tmp
;
296 FOR_EACH_PTR(array_init_hooks
, tmp
) {
297 ((array_init_hook
*)tmp
->fn
)(array
, nr
);
298 } END_FOR_EACH_PTR(tmp
);
301 void allocate_hook_memory(void)
303 pre_merge_hooks
= malloc(num_checks
* sizeof(*pre_merge_hooks
));
304 memset(pre_merge_hooks
, 0, num_checks
* sizeof(*pre_merge_hooks
));