extra: handle "if (a & 0x1)" conditions
[smatch.git] / smatch_hooks.c
blobc471d75ee914453cae7ece16c5666804f8194344
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,
16 SYM_LIST_PTR,
19 struct hook_container {
20 int hook_type;
21 enum data_type data_type;
22 void *fn;
24 ALLOCATOR(hook_container, "hook functions");
25 DECLARE_PTR_LIST(hook_func_list, struct hook_container);
26 static struct hook_func_list *hook_funcs;
27 static struct hook_func_list *merge_funcs;
28 static struct hook_func_list *unmatched_state_funcs;
30 struct scope_container {
31 void *fn;
32 void *data;
34 ALLOCATOR(scope_container, "scope hook functions");
35 DECLARE_PTR_LIST(scope_hook_list, struct scope_container);
36 DECLARE_PTR_LIST(scope_hook_stack, struct scope_hook_list);
37 static struct scope_hook_stack *scope_hooks;
39 void add_hook(void *func, enum hook_type type)
41 struct hook_container *container = __alloc_hook_container(0);
42 container->hook_type = type;
43 container->fn = func;
44 switch (type) {
45 case EXPR_HOOK:
46 container->data_type = EXPR_PTR;
47 break;
48 case STMT_HOOK:
49 container->data_type = STMT_PTR;
50 break;
51 case SYM_HOOK:
52 container->data_type = EXPR_PTR;
53 break;
54 case STRING_HOOK:
55 container->data_type = EXPR_PTR;
56 break;
57 case DECLARATION_HOOK:
58 container->data_type = SYMBOL_PTR;
59 break;
60 case ASSIGNMENT_HOOK:
61 container->data_type = EXPR_PTR;
62 break;
63 case RAW_ASSIGNMENT_HOOK:
64 container->data_type = EXPR_PTR;
65 break;
66 case GLOBAL_ASSIGNMENT_HOOK:
67 container->data_type = EXPR_PTR;
68 break;
69 case CALL_ASSIGNMENT_HOOK:
70 container->data_type = EXPR_PTR;
71 break;
72 case MACRO_ASSIGNMENT_HOOK:
73 container->data_type = EXPR_PTR;
74 break;
75 case BINOP_HOOK:
76 container->data_type = EXPR_PTR;
77 break;
78 case OP_HOOK:
79 container->data_type = EXPR_PTR;
80 break;
81 case LOGIC_HOOK:
82 container->data_type = EXPR_PTR;
83 break;
84 case PRELOOP_HOOK:
85 container->data_type = STMT_PTR;
86 break;
87 case CONDITION_HOOK:
88 container->data_type = EXPR_PTR;
89 break;
90 case SELECT_HOOK:
91 container->data_type = EXPR_PTR;
92 break;
93 case WHOLE_CONDITION_HOOK:
94 container->data_type = EXPR_PTR;
95 break;
96 case FUNCTION_CALL_HOOK:
97 container->data_type = EXPR_PTR;
98 break;
99 case CALL_HOOK_AFTER_INLINE:
100 container->data_type = EXPR_PTR;
101 break;
102 case DEREF_HOOK:
103 container->data_type = EXPR_PTR;
104 break;
105 case CASE_HOOK:
106 /* nothing needed */
107 break;
108 case ASM_HOOK:
109 container->data_type = STMT_PTR;
110 break;
111 case CAST_HOOK:
112 container->data_type = EXPR_PTR;
113 break;
114 case SIZEOF_HOOK:
115 container->data_type = EXPR_PTR;
116 break;
117 case BASE_HOOK:
118 container->data_type = SYMBOL_PTR;
119 break;
120 case FUNC_DEF_HOOK:
121 container->data_type = SYMBOL_PTR;
122 break;
123 case AFTER_DEF_HOOK:
124 container->data_type = SYMBOL_PTR;
125 break;
126 case END_FUNC_HOOK:
127 container->data_type = SYMBOL_PTR;
128 break;
129 case AFTER_FUNC_HOOK:
130 container->data_type = SYMBOL_PTR;
131 break;
132 case RETURN_HOOK:
133 container->data_type = EXPR_PTR;
134 break;
135 case INLINE_FN_START:
136 container->data_type = EXPR_PTR;
137 break;
138 case INLINE_FN_END:
139 container->data_type = EXPR_PTR;
140 break;
141 case END_FILE_HOOK:
142 container->data_type = SYM_LIST_PTR;
143 break;
145 add_ptr_list(&hook_funcs, container);
148 void add_merge_hook(int client_id, merge_func_t *func)
150 struct hook_container *container = __alloc_hook_container(0);
151 container->data_type = client_id;
152 container->fn = func;
153 add_ptr_list(&merge_funcs, container);
156 void add_unmatched_state_hook(int client_id, unmatched_func_t *func)
158 struct hook_container *container = __alloc_hook_container(0);
159 container->data_type = client_id;
160 container->fn = func;
161 add_ptr_list(&unmatched_state_funcs, container);
164 static void pass_to_client(void *fn)
166 typedef void (expr_func)();
167 ((expr_func *) fn)();
170 static void pass_expr_to_client(void *fn, void *data)
172 typedef void (expr_func)(struct expression *expr);
173 ((expr_func *) fn)((struct expression *) data);
176 static void pass_stmt_to_client(void *fn, void *data)
178 typedef void (stmt_func)(struct statement *stmt);
179 ((stmt_func *) fn)((struct statement *) data);
182 static void pass_sym_to_client(void *fn, void *data)
184 typedef void (sym_func)(struct symbol *sym);
185 ((sym_func *) fn)((struct symbol *) data);
188 static void pass_sym_list_to_client(void *fn, void *data)
190 typedef void (sym_func)(struct symbol_list *sym_list);
191 ((sym_func *) fn)((struct symbol_list *) data);
194 void __pass_to_client(void *data, enum hook_type type)
196 struct hook_container *container;
198 FOR_EACH_PTR(hook_funcs, container) {
199 if (container->hook_type == type) {
200 switch (container->data_type) {
201 case EXPR_PTR:
202 pass_expr_to_client(container->fn, data);
203 break;
204 case STMT_PTR:
205 pass_stmt_to_client(container->fn, data);
206 break;
207 case SYMBOL_PTR:
208 pass_sym_to_client(container->fn, data);
209 break;
210 case SYM_LIST_PTR:
211 pass_sym_list_to_client(container->fn, data);
212 break;
215 } END_FOR_EACH_PTR(container);
218 void __pass_to_client_no_data(enum hook_type type)
220 struct hook_container *container;
222 FOR_EACH_PTR(hook_funcs, container) {
223 if (container->hook_type == type)
224 pass_to_client(container->fn);
225 } END_FOR_EACH_PTR(container);
228 void __pass_case_to_client(struct expression *switch_expr,
229 struct expression *case_expr)
231 typedef void (case_func)(struct expression *switch_expr,
232 struct expression *case_expr);
233 struct hook_container *container;
235 FOR_EACH_PTR(hook_funcs, container) {
236 if (container->hook_type == CASE_HOOK)
237 ((case_func *) container->fn)(switch_expr, case_expr);
238 } END_FOR_EACH_PTR(container);
241 int __has_merge_function(int client_id)
243 struct hook_container *tmp;
245 FOR_EACH_PTR(merge_funcs, tmp) {
246 if (tmp->data_type == client_id)
247 return 1;
248 } END_FOR_EACH_PTR(tmp);
249 return 0;
252 struct smatch_state *__client_merge_function(int owner,
253 struct smatch_state *s1,
254 struct smatch_state *s2)
256 struct smatch_state *tmp_state;
257 struct hook_container *tmp;
259 /* Pass NULL states first and the rest alphabetically by name */
260 if (!s2 || (s1 && strcmp(s2->name, s1->name) < 0)) {
261 tmp_state = s1;
262 s1 = s2;
263 s2 = tmp_state;
266 FOR_EACH_PTR(merge_funcs, tmp) {
267 if (tmp->data_type == owner)
268 return ((merge_func_t *) tmp->fn)(s1, s2);
269 } END_FOR_EACH_PTR(tmp);
270 return &undefined;
273 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm)
275 struct hook_container *tmp;
277 FOR_EACH_PTR(unmatched_state_funcs, tmp) {
278 if (tmp->data_type == sm->owner)
279 return ((unmatched_func_t *) tmp->fn)(sm);
280 } END_FOR_EACH_PTR(tmp);
281 return &undefined;
284 static struct scope_hook_list *pop_scope_hook_list(struct scope_hook_stack **stack)
286 struct scope_hook_list *hook_list;
288 hook_list = last_ptr_list((struct ptr_list *)*stack);
289 delete_ptr_list_last((struct ptr_list **)stack);
290 return hook_list;
293 static void push_scope_hook_list(struct scope_hook_stack **stack, struct scope_hook_list *l)
295 add_ptr_list(stack, l);
298 void add_scope_hook(scope_hook *fn, void *data)
300 struct scope_hook_list *hook_list;
301 struct scope_container *new;
303 if (!scope_hooks)
304 return;
305 hook_list = pop_scope_hook_list(&scope_hooks);
306 new = __alloc_scope_container(0);
307 new->fn = fn;
308 new->data = data;
309 add_ptr_list(&hook_list, new);
310 push_scope_hook_list(&scope_hooks, hook_list);
313 void __push_scope_hooks(void)
315 push_scope_hook_list(&scope_hooks, NULL);
318 void __call_scope_hooks(void)
320 struct scope_hook_list *hook_list;
321 struct scope_container *tmp;
323 if (!scope_hooks)
324 return;
326 hook_list = pop_scope_hook_list(&scope_hooks);
327 FOR_EACH_PTR(hook_list, tmp) {
328 ((scope_hook *) tmp->fn)(tmp->data);
329 __free_scope_container(tmp);
330 } END_FOR_EACH_PTR(tmp);