precedence: cut down a few false positives
[smatch.git] / smatch_hooks.c
blob05d8f96945b0a27c1a7bee15fa36571a818ccf16
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 DECLARATION_HOOK:
54 container->data_type = SYMBOL_PTR;
55 break;
56 case ASSIGNMENT_HOOK:
57 container->data_type = EXPR_PTR;
58 break;
59 case CALL_ASSIGNMENT_HOOK:
60 container->data_type = EXPR_PTR;
61 break;
62 case BINOP_HOOK:
63 container->data_type = EXPR_PTR;
64 break;
65 case OP_HOOK:
66 container->data_type = EXPR_PTR;
67 break;
68 case CONDITION_HOOK:
69 container->data_type = EXPR_PTR;
70 break;
71 case WHOLE_CONDITION_HOOK:
72 container->data_type = EXPR_PTR;
73 break;
74 case FUNCTION_CALL_HOOK:
75 container->data_type = EXPR_PTR;
76 break;
77 case DEREF_HOOK:
78 container->data_type = EXPR_PTR;
79 break;
80 case CASE_HOOK:
81 /* nothing needed */
82 break;
83 case BASE_HOOK:
84 container->data_type = SYMBOL_PTR;
85 break;
86 case FUNC_DEF_HOOK:
87 container->data_type = SYMBOL_PTR;
88 break;
89 case END_FUNC_HOOK:
90 container->data_type = SYMBOL_PTR;
91 break;
92 case RETURN_HOOK:
93 container->data_type = EXPR_PTR;
94 break;
95 case END_FILE_HOOK:
96 /* nothing needed... */
97 break;
99 add_ptr_list(&hook_funcs, container);
102 void add_merge_hook(int client_id, merge_func_t *func)
104 struct hook_container *container = __alloc_hook_container(0);
105 container->data_type = client_id;
106 container->fn = func;
107 add_ptr_list(&merge_funcs, container);
110 void add_unmatched_state_hook(int client_id, unmatched_func_t *func)
112 struct hook_container *container = __alloc_hook_container(0);
113 container->data_type = client_id;
114 container->fn = func;
115 add_ptr_list(&unmatched_state_funcs, container);
118 static void pass_to_client(void * fn)
120 typedef void (expr_func)();
121 ((expr_func *) fn)();
124 static void pass_expr_to_client(void * fn, void * data)
126 typedef void (expr_func)(struct expression *expr);
127 ((expr_func *) fn)((struct expression *) data);
130 static void pass_stmt_to_client(void * fn, void * data)
132 typedef void (stmt_func)(struct statement *stmt);
133 ((stmt_func *) fn)((struct statement *) data);
136 static void pass_sym_to_client(void * fn, void * data)
138 typedef void (sym_func)(struct symbol *sym);
139 ((sym_func *) fn)((struct symbol *) data);
142 void __pass_to_client(void *data, enum hook_type type)
144 struct hook_container *container;
146 // if (!data)
147 // return;
149 FOR_EACH_PTR(hook_funcs, container) {
150 if (container->hook_type == type) {
151 switch (container->data_type) {
152 case EXPR_PTR:
153 pass_expr_to_client(container->fn, data);
154 break;
155 case STMT_PTR:
156 pass_stmt_to_client(container->fn, data);
157 break;
158 case SYMBOL_PTR:
159 pass_sym_to_client(container->fn, data);
160 break;
163 } END_FOR_EACH_PTR(container);
166 void __pass_to_client_no_data(enum hook_type type)
168 struct hook_container *container;
170 FOR_EACH_PTR(hook_funcs, container) {
171 if (container->hook_type == type)
172 pass_to_client(container->fn);
173 } END_FOR_EACH_PTR(container);
176 void __pass_case_to_client(struct expression *switch_expr,
177 struct expression *case_expr)
179 typedef void (case_func)(struct expression *switch_expr,
180 struct expression *case_expr);
181 struct hook_container *container;
183 FOR_EACH_PTR(hook_funcs, container) {
184 if (container->hook_type == CASE_HOOK) {
185 ((case_func *) container->fn)(switch_expr, case_expr);
187 } END_FOR_EACH_PTR(container);
190 int __has_merge_function(int client_id)
192 struct hook_container *tmp;
194 FOR_EACH_PTR(merge_funcs, tmp) {
195 if (tmp->data_type == client_id)
196 return 1;
197 } END_FOR_EACH_PTR(tmp);
198 return 0;
201 struct smatch_state *__client_merge_function(int owner, const char *name,
202 struct symbol *sym,
203 struct smatch_state *s1,
204 struct smatch_state *s2)
206 struct smatch_state *tmp_state;
207 struct hook_container *tmp;
209 /* Pass NULL states first and the rest alphabetically by name */
210 if (!s2 || (s1 && strcmp(s2->name, s1->name) < 0)) {
211 tmp_state = s1;
212 s1 = s2;
213 s2 = tmp_state;
216 FOR_EACH_PTR(merge_funcs, tmp) {
217 if (tmp->data_type == owner)
218 return ((merge_func_t *) tmp->fn)(name, sym, s1, s2);
219 } END_FOR_EACH_PTR(tmp);
220 return &undefined;
223 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm)
225 struct hook_container *tmp;
227 FOR_EACH_PTR(unmatched_state_funcs, tmp) {
228 if (tmp->data_type == sm->owner)
229 return ((unmatched_func_t *) tmp->fn)(sm);
230 } END_FOR_EACH_PTR(tmp);
231 return &undefined;
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);
240 return hook_list;
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;
253 if (!scope_hooks)
254 return;
255 hook_list = pop_scope_hook_list(&scope_hooks);
256 new = __alloc_scope_container(0);
257 new->fn = fn;
258 new->data = data;
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;
273 if (!scope_hooks)
274 return;
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);