try handle do {} while (i-- > 0)
[smatch.git] / smatch_hooks.c
blobcc2ea249e16e3c9ae8fbc60464b5f786d943a2a3
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 struct hook_container {
13 int hook_type;
14 int data_type;
15 void *fn;
17 ALLOCATOR(hook_container, "hook functions");
18 DECLARE_PTR_LIST(hook_func_list, struct hook_container);
19 static struct hook_func_list *hook_funcs;
20 static struct hook_func_list *merge_funcs;
21 static struct hook_func_list *unmatched_state_funcs;
23 struct scope_container {
24 void *fn;
25 void *data;
27 ALLOCATOR(scope_container, "scope hook functions");
28 DECLARE_PTR_LIST(scope_hook_list, struct scope_container);
29 DECLARE_PTR_LIST(scope_hook_stack, struct scope_hook_list);
30 static struct scope_hook_stack *scope_hooks;
32 void add_hook(void *func, enum hook_type type)
34 struct hook_container *container = __alloc_hook_container(0);
35 container->hook_type = type;
36 container->fn = func;
37 switch(type) {
38 case EXPR_HOOK:
39 container->data_type = EXPR_HOOK;
40 break;
41 case STMT_HOOK:
42 container->data_type = STMT_HOOK;
43 break;
44 case SYM_HOOK:
45 container->data_type = SYM_HOOK;
46 break;
47 case DECLARATION_HOOK:
48 container->data_type = SYM_HOOK;
49 break;
50 case ASSIGNMENT_HOOK:
51 container->data_type = EXPR_HOOK;
52 break;
53 case CALL_ASSIGNMENT_HOOK:
54 container->data_type = EXPR_HOOK;
55 break;
56 case OP_HOOK:
57 container->data_type = EXPR_HOOK;
58 break;
59 case CONDITION_HOOK:
60 container->data_type = EXPR_HOOK;
61 break;
62 case WHOLE_CONDITION_HOOK:
63 container->data_type = EXPR_HOOK;
64 break;
65 case FUNCTION_CALL_HOOK:
66 container->data_type = EXPR_HOOK;
67 break;
68 case DEREF_HOOK:
69 container->data_type = EXPR_HOOK;
70 break;
71 case CASE_HOOK:
72 /* nothing needed */
73 break;
74 case BASE_HOOK:
75 container->data_type = SYM_HOOK;
76 break;
77 case FUNC_DEF_HOOK:
78 container->data_type = SYM_HOOK;
79 break;
80 case END_FUNC_HOOK:
81 container->data_type = SYM_HOOK;
82 break;
83 case RETURN_HOOK:
84 container->data_type = EXPR_HOOK;
85 break;
86 case END_FILE_HOOK:
87 /* nothing needed... */
88 break;
90 add_ptr_list(&hook_funcs, container);
93 void add_merge_hook(int client_id, merge_func_t *func)
95 struct hook_container *container = __alloc_hook_container(0);
96 container->data_type = client_id;
97 container->fn = func;
98 add_ptr_list(&merge_funcs, container);
101 void add_unmatched_state_hook(int client_id, unmatched_func_t *func)
103 struct hook_container *container = __alloc_hook_container(0);
104 container->data_type = client_id;
105 container->fn = func;
106 add_ptr_list(&unmatched_state_funcs, container);
109 static void pass_to_client(void * fn)
111 typedef void (expr_func)();
112 ((expr_func *) fn)();
115 static void pass_expr_to_client(void * fn, void * data)
117 typedef void (expr_func)(struct expression *expr);
118 ((expr_func *) fn)((struct expression *) data);
121 static void pass_stmt_to_client(void * fn, void * data)
123 typedef void (stmt_func)(struct statement *stmt);
124 ((stmt_func *) fn)((struct statement *) data);
127 static void pass_sym_to_client(void * fn, void * data)
129 typedef void (sym_func)(struct symbol *sym);
130 ((sym_func *) fn)((struct symbol *) data);
133 void __pass_to_client(void *data, enum hook_type type)
135 struct hook_container *container;
137 // if (!data)
138 // return;
140 FOR_EACH_PTR(hook_funcs, container) {
141 if (container->hook_type == type) {
142 switch(container->data_type) {
143 case EXPR_HOOK:
144 pass_expr_to_client(container->fn, data);
145 break;
146 case STMT_HOOK:
147 pass_stmt_to_client(container->fn, data);
148 break;
149 case SYM_HOOK:
150 pass_sym_to_client(container->fn, data);
151 break;
154 } END_FOR_EACH_PTR(container);
157 void __pass_to_client_no_data(enum hook_type type)
159 struct hook_container *container;
161 FOR_EACH_PTR(hook_funcs, container) {
162 if (container->hook_type == type)
163 pass_to_client(container->fn);
164 } END_FOR_EACH_PTR(container);
167 void __pass_case_to_client(struct expression *switch_expr,
168 struct expression *case_expr)
170 typedef void (case_func)(struct expression *switch_expr,
171 struct expression *case_expr);
172 struct hook_container *container;
174 FOR_EACH_PTR(hook_funcs, container) {
175 if (container->hook_type == CASE_HOOK) {
176 ((case_func *) container->fn)(switch_expr, case_expr);
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->data_type == client_id)
187 return 1;
188 } END_FOR_EACH_PTR(tmp);
189 return 0;
192 struct smatch_state *__client_merge_function(int owner, const char *name,
193 struct symbol *sym,
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)) {
202 tmp_state = s1;
203 s1 = s2;
204 s2 = tmp_state;
207 FOR_EACH_PTR(merge_funcs, tmp) {
208 if (tmp->data_type == owner)
209 return ((merge_func_t *) tmp->fn)(name, sym, s1, s2);
210 } END_FOR_EACH_PTR(tmp);
211 return &undefined;
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->data_type == sm->owner)
220 return ((unmatched_func_t *) tmp->fn)(sm);
221 } END_FOR_EACH_PTR(tmp);
222 return &undefined;
225 static struct scope_hook_list *pop_scope_hook_list(struct scope_hook_stack **stack)
227 struct scope_hook_list *hook_list;
229 hook_list = last_ptr_list((struct ptr_list *)*stack);
230 delete_ptr_list_last((struct ptr_list **)stack);
231 return hook_list;
234 static void push_scope_hook_list(struct scope_hook_stack **stack, struct scope_hook_list *l)
236 add_ptr_list(stack, l);
239 void add_scope_hook(scope_hook *fn, void *data)
241 struct scope_hook_list *hook_list;
242 struct scope_container *new;
244 if (!scope_hooks)
245 return;
246 hook_list = pop_scope_hook_list(&scope_hooks);
247 new = __alloc_scope_container(0);
248 new->fn = fn;
249 new->data = data;
250 add_ptr_list(&hook_list, new);
251 push_scope_hook_list(&scope_hooks, hook_list);
254 void __push_scope_hooks(void)
256 push_scope_hook_list(&scope_hooks, NULL);
259 void __call_scope_hooks(void)
261 struct scope_hook_list *hook_list;
262 struct scope_container *tmp;
264 if (!scope_hooks)
265 return;
267 hook_list = pop_scope_hook_list(&scope_hooks);
268 FOR_EACH_PTR(hook_list, tmp) {
269 ((scope_hook *) tmp->fn)(tmp->data);
270 __free_scope_container(tmp);
271 } END_FOR_EACH_PTR(tmp);