struct_assignment: introduce get_faked_expression()
[smatch.git] / smatch_hooks.c
blob5782be98dbb40d066e70e3ca90339a94ecbdd577
1 /*
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
18 #include "smatch.h"
20 enum data_type {
21 EXPR_PTR,
22 STMT_PTR,
23 SYMBOL_PTR,
24 SYM_LIST_PTR,
27 struct hook_container {
28 int hook_type;
29 enum data_type data_type;
30 void *fn;
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 {
39 void *fn;
40 void *data;
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;
51 container->fn = func;
52 switch (type) {
53 case EXPR_HOOK:
54 container->data_type = EXPR_PTR;
55 break;
56 case STMT_HOOK:
57 container->data_type = STMT_PTR;
58 break;
59 case STMT_HOOK_AFTER:
60 container->data_type = STMT_PTR;
61 break;
62 case SYM_HOOK:
63 container->data_type = EXPR_PTR;
64 break;
65 case STRING_HOOK:
66 container->data_type = EXPR_PTR;
67 break;
68 case DECLARATION_HOOK:
69 container->data_type = SYMBOL_PTR;
70 break;
71 case ASSIGNMENT_HOOK:
72 container->data_type = EXPR_PTR;
73 break;
74 case RAW_ASSIGNMENT_HOOK:
75 container->data_type = EXPR_PTR;
76 break;
77 case GLOBAL_ASSIGNMENT_HOOK:
78 container->data_type = EXPR_PTR;
79 break;
80 case CALL_ASSIGNMENT_HOOK:
81 container->data_type = EXPR_PTR;
82 break;
83 case MACRO_ASSIGNMENT_HOOK:
84 container->data_type = EXPR_PTR;
85 break;
86 case BINOP_HOOK:
87 container->data_type = EXPR_PTR;
88 break;
89 case OP_HOOK:
90 container->data_type = EXPR_PTR;
91 break;
92 case LOGIC_HOOK:
93 container->data_type = EXPR_PTR;
94 break;
95 case PRELOOP_HOOK:
96 container->data_type = STMT_PTR;
97 break;
98 case CONDITION_HOOK:
99 container->data_type = EXPR_PTR;
100 break;
101 case SELECT_HOOK:
102 container->data_type = EXPR_PTR;
103 break;
104 case WHOLE_CONDITION_HOOK:
105 container->data_type = EXPR_PTR;
106 break;
107 case FUNCTION_CALL_HOOK:
108 container->data_type = EXPR_PTR;
109 break;
110 case CALL_HOOK_AFTER_INLINE:
111 container->data_type = EXPR_PTR;
112 break;
113 case DEREF_HOOK:
114 container->data_type = EXPR_PTR;
115 break;
116 case CASE_HOOK:
117 /* nothing needed */
118 break;
119 case ASM_HOOK:
120 container->data_type = STMT_PTR;
121 break;
122 case CAST_HOOK:
123 container->data_type = EXPR_PTR;
124 break;
125 case SIZEOF_HOOK:
126 container->data_type = EXPR_PTR;
127 break;
128 case BASE_HOOK:
129 container->data_type = SYMBOL_PTR;
130 break;
131 case FUNC_DEF_HOOK:
132 container->data_type = SYMBOL_PTR;
133 break;
134 case AFTER_DEF_HOOK:
135 container->data_type = SYMBOL_PTR;
136 break;
137 case END_FUNC_HOOK:
138 container->data_type = SYMBOL_PTR;
139 break;
140 case AFTER_FUNC_HOOK:
141 container->data_type = SYMBOL_PTR;
142 break;
143 case RETURN_HOOK:
144 container->data_type = EXPR_PTR;
145 break;
146 case INLINE_FN_START:
147 container->data_type = EXPR_PTR;
148 break;
149 case INLINE_FN_END:
150 container->data_type = EXPR_PTR;
151 break;
152 case END_FILE_HOOK:
153 container->data_type = SYM_LIST_PTR;
154 break;
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) {
212 case EXPR_PTR:
213 pass_expr_to_client(container->fn, data);
214 break;
215 case STMT_PTR:
216 pass_stmt_to_client(container->fn, data);
217 break;
218 case SYMBOL_PTR:
219 pass_sym_to_client(container->fn, data);
220 break;
221 case SYM_LIST_PTR:
222 pass_sym_list_to_client(container->fn, data);
223 break;
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)
258 return 1;
259 } END_FOR_EACH_PTR(tmp);
260 return 0;
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)) {
272 tmp_state = s1;
273 s1 = s2;
274 s2 = tmp_state;
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);
281 return &undefined;
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);
292 return &undefined;
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);
301 return hook_list;
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;
314 if (!scope_hooks)
315 return;
316 hook_list = pop_scope_hook_list(&scope_hooks);
317 new = __alloc_scope_container(0);
318 new->fn = fn;
319 new->data = data;
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;
334 if (!scope_hooks)
335 return;
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);