flow: hooks: introduce GLOBAL_ASSIGNMENT_HOOK
[smatch.git] / smatch_hooks.c
blob6817f4307a8af669b529b518fa430d3e0c154ed5
1 /*
2 * sparse/smatch_hooks.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "smatch.h"
12 enum data_type {
13 EXPR_PTR,
14 STMT_PTR,
15 SYMBOL_PTR,
18 struct hook_container {
19 int hook_type;
20 enum data_type data_type;
21 void *fn;
23 ALLOCATOR(hook_container, "hook functions");
24 DECLARE_PTR_LIST(hook_func_list, struct hook_container);
25 static struct hook_func_list *hook_funcs;
26 static struct hook_func_list *merge_funcs;
27 static struct hook_func_list *unmatched_state_funcs;
29 struct scope_container {
30 void *fn;
31 void *data;
33 ALLOCATOR(scope_container, "scope hook functions");
34 DECLARE_PTR_LIST(scope_hook_list, struct scope_container);
35 DECLARE_PTR_LIST(scope_hook_stack, struct scope_hook_list);
36 static struct scope_hook_stack *scope_hooks;
38 void add_hook(void *func, enum hook_type type)
40 struct hook_container *container = __alloc_hook_container(0);
41 container->hook_type = type;
42 container->fn = func;
43 switch (type) {
44 case EXPR_HOOK:
45 container->data_type = EXPR_PTR;
46 break;
47 case STMT_HOOK:
48 container->data_type = STMT_PTR;
49 break;
50 case SYM_HOOK:
51 container->data_type = EXPR_PTR;
52 break;
53 case STRING_HOOK:
54 container->data_type = EXPR_PTR;
55 break;
56 case DECLARATION_HOOK:
57 container->data_type = SYMBOL_PTR;
58 break;
59 case ASSIGNMENT_HOOK:
60 container->data_type = EXPR_PTR;
61 break;
62 case RAW_ASSIGNMENT_HOOK:
63 container->data_type = EXPR_PTR;
64 break;
65 case GLOBAL_ASSIGNMENT_HOOK:
66 container->data_type = EXPR_PTR;
67 break;
68 case CALL_ASSIGNMENT_HOOK:
69 container->data_type = EXPR_PTR;
70 break;
71 case MACRO_ASSIGNMENT_HOOK:
72 container->data_type = EXPR_PTR;
73 break;
74 case BINOP_HOOK:
75 container->data_type = EXPR_PTR;
76 break;
77 case OP_HOOK:
78 container->data_type = EXPR_PTR;
79 break;
80 case LOGIC_HOOK:
81 container->data_type = EXPR_PTR;
82 break;
83 case PRELOOP_HOOK:
84 container->data_type = STMT_PTR;
85 break;
86 case CONDITION_HOOK:
87 container->data_type = EXPR_PTR;
88 break;
89 case SELECT_HOOK:
90 container->data_type = EXPR_PTR;
91 break;
92 case WHOLE_CONDITION_HOOK:
93 container->data_type = EXPR_PTR;
94 break;
95 case FUNCTION_CALL_HOOK:
96 container->data_type = EXPR_PTR;
97 break;
98 case DEREF_HOOK:
99 container->data_type = EXPR_PTR;
100 break;
101 case CASE_HOOK:
102 /* nothing needed */
103 break;
104 case ASM_HOOK:
105 container->data_type = STMT_PTR;
106 break;
107 case CAST_HOOK:
108 container->data_type = EXPR_PTR;
109 break;
110 case BASE_HOOK:
111 container->data_type = SYMBOL_PTR;
112 break;
113 case FUNC_DEF_HOOK:
114 container->data_type = SYMBOL_PTR;
115 break;
116 case END_FUNC_HOOK:
117 container->data_type = SYMBOL_PTR;
118 break;
119 case RETURN_HOOK:
120 container->data_type = EXPR_PTR;
121 break;
122 case END_FILE_HOOK:
123 /* nothing needed... */
124 break;
126 add_ptr_list(&hook_funcs, container);
129 void add_merge_hook(int client_id, merge_func_t *func)
131 struct hook_container *container = __alloc_hook_container(0);
132 container->data_type = client_id;
133 container->fn = func;
134 add_ptr_list(&merge_funcs, container);
137 void add_unmatched_state_hook(int client_id, unmatched_func_t *func)
139 struct hook_container *container = __alloc_hook_container(0);
140 container->data_type = client_id;
141 container->fn = func;
142 add_ptr_list(&unmatched_state_funcs, container);
145 static void pass_to_client(void *fn)
147 typedef void (expr_func)();
148 ((expr_func *) fn)();
151 static void pass_expr_to_client(void *fn, void *data)
153 typedef void (expr_func)(struct expression *expr);
154 ((expr_func *) fn)((struct expression *) data);
157 static void pass_stmt_to_client(void *fn, void *data)
159 typedef void (stmt_func)(struct statement *stmt);
160 ((stmt_func *) fn)((struct statement *) data);
163 static void pass_sym_to_client(void *fn, void *data)
165 typedef void (sym_func)(struct symbol *sym);
166 ((sym_func *) fn)((struct symbol *) data);
169 void __pass_to_client(void *data, enum hook_type type)
171 struct hook_container *container;
173 FOR_EACH_PTR(hook_funcs, container) {
174 if (container->hook_type == type) {
175 switch (container->data_type) {
176 case EXPR_PTR:
177 pass_expr_to_client(container->fn, data);
178 break;
179 case STMT_PTR:
180 pass_stmt_to_client(container->fn, data);
181 break;
182 case SYMBOL_PTR:
183 pass_sym_to_client(container->fn, data);
184 break;
187 } END_FOR_EACH_PTR(container);
190 void __pass_to_client_no_data(enum hook_type type)
192 struct hook_container *container;
194 FOR_EACH_PTR(hook_funcs, container) {
195 if (container->hook_type == type)
196 pass_to_client(container->fn);
197 } END_FOR_EACH_PTR(container);
200 void __pass_case_to_client(struct expression *switch_expr,
201 struct expression *case_expr)
203 typedef void (case_func)(struct expression *switch_expr,
204 struct expression *case_expr);
205 struct hook_container *container;
207 FOR_EACH_PTR(hook_funcs, container) {
208 if (container->hook_type == CASE_HOOK)
209 ((case_func *) container->fn)(switch_expr, case_expr);
210 } END_FOR_EACH_PTR(container);
213 int __has_merge_function(int client_id)
215 struct hook_container *tmp;
217 FOR_EACH_PTR(merge_funcs, tmp) {
218 if (tmp->data_type == client_id)
219 return 1;
220 } END_FOR_EACH_PTR(tmp);
221 return 0;
224 struct smatch_state *__client_merge_function(int owner,
225 struct smatch_state *s1,
226 struct smatch_state *s2)
228 struct smatch_state *tmp_state;
229 struct hook_container *tmp;
231 /* Pass NULL states first and the rest alphabetically by name */
232 if (!s2 || (s1 && strcmp(s2->name, s1->name) < 0)) {
233 tmp_state = s1;
234 s1 = s2;
235 s2 = tmp_state;
238 FOR_EACH_PTR(merge_funcs, tmp) {
239 if (tmp->data_type == owner)
240 return ((merge_func_t *) tmp->fn)(s1, s2);
241 } END_FOR_EACH_PTR(tmp);
242 return &undefined;
245 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm)
247 struct hook_container *tmp;
249 FOR_EACH_PTR(unmatched_state_funcs, tmp) {
250 if (tmp->data_type == sm->owner)
251 return ((unmatched_func_t *) tmp->fn)(sm);
252 } END_FOR_EACH_PTR(tmp);
253 return &undefined;
256 static struct scope_hook_list *pop_scope_hook_list(struct scope_hook_stack **stack)
258 struct scope_hook_list *hook_list;
260 hook_list = last_ptr_list((struct ptr_list *)*stack);
261 delete_ptr_list_last((struct ptr_list **)stack);
262 return hook_list;
265 static void push_scope_hook_list(struct scope_hook_stack **stack, struct scope_hook_list *l)
267 add_ptr_list(stack, l);
270 void add_scope_hook(scope_hook *fn, void *data)
272 struct scope_hook_list *hook_list;
273 struct scope_container *new;
275 if (!scope_hooks)
276 return;
277 hook_list = pop_scope_hook_list(&scope_hooks);
278 new = __alloc_scope_container(0);
279 new->fn = fn;
280 new->data = data;
281 add_ptr_list(&hook_list, new);
282 push_scope_hook_list(&scope_hooks, hook_list);
285 void __push_scope_hooks(void)
287 push_scope_hook_list(&scope_hooks, NULL);
290 void __call_scope_hooks(void)
292 struct scope_hook_list *hook_list;
293 struct scope_container *tmp;
295 if (!scope_hooks)
296 return;
298 hook_list = pop_scope_hook_list(&scope_hooks);
299 FOR_EACH_PTR(hook_list, tmp) {
300 ((scope_hook *) tmp->fn)(tmp->data);
301 __free_scope_container(tmp);
302 } END_FOR_EACH_PTR(tmp);