Save the possible states instead of just merging them into undefined. The
[smatch.git] / smatch_hooks.c
bloba4d907e2574e47f32b4bc27daa5be0715077a12f
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;
22 void add_hook(void *func, enum hook_type type)
24 struct hook_container *container = __alloc_hook_container(0);
25 container->hook_type = type;
26 container->fn = func;
27 switch(type) {
28 case EXPR_HOOK:
29 container->data_type = EXPR_HOOK;
30 break;
31 case STMT_HOOK:
32 container->data_type = STMT_HOOK;
33 break;
34 case SYM_HOOK:
35 container->data_type = SYM_HOOK;
36 break;
37 case DECLARATION_HOOK:
38 container->data_type = SYM_HOOK;
39 break;
40 case ASSIGNMENT_HOOK:
41 container->data_type = EXPR_HOOK;
42 break;
43 case OP_HOOK:
44 container->data_type = EXPR_HOOK;
45 break;
46 case ASSIGNMENT_AFTER_HOOK:
47 container->data_type = EXPR_HOOK;
48 break;
49 case CONDITION_HOOK:
50 container->data_type = EXPR_HOOK;
51 break;
52 case WHOLE_CONDITION_HOOK:
53 container->data_type = EXPR_HOOK;
54 break;
55 case FUNCTION_CALL_HOOK:
56 container->data_type = EXPR_HOOK;
57 break;
58 case FUNCTION_CALL_AFTER_HOOK:
59 container->data_type = EXPR_HOOK;
60 break;
61 case DEREF_HOOK:
62 container->data_type = EXPR_HOOK;
63 break;
64 case BASE_HOOK:
65 container->data_type = SYM_HOOK;
66 break;
67 case FUNC_DEF_HOOK:
68 container->data_type = SYM_HOOK;
69 break;
70 case END_FUNC_HOOK:
71 container->data_type = SYM_HOOK;
72 break;
73 case RETURN_HOOK:
74 container->data_type = STMT_HOOK;
75 break;
76 case END_FILE_HOOK:
77 /* nothing needed... */
78 break;
80 add_ptr_list(&hook_funcs, container);
83 void add_merge_hook(int client_id, merge_func_t *func)
85 struct hook_container *container = __alloc_hook_container(0);
86 container->data_type = client_id;
87 container->fn = func;
88 add_ptr_list(&merge_funcs, container);
92 static void pass_to_client(void * fn)
94 typedef void (expr_func)();
95 ((expr_func *) fn)();
98 static void pass_expr_to_client(void * fn, void * data)
100 typedef void (expr_func)(struct expression *expr);
101 ((expr_func *) fn)((struct expression *) data);
104 static void pass_stmt_to_client(void * fn, void * data)
106 typedef void (stmt_func)(struct statement *stmt);
107 ((stmt_func *) fn)((struct statement *) data);
110 static void pass_sym_to_client(void * fn, void * data)
112 typedef void (sym_func)(struct symbol *sym);
113 ((sym_func *) fn)((struct symbol *) data);
116 void __pass_to_client(void *data, enum hook_type type)
118 struct hook_container *container;
120 if (!data)
121 return;
123 FOR_EACH_PTR(hook_funcs, container) {
124 if (container->hook_type == type) {
125 switch(container->data_type) {
126 case EXPR_HOOK:
127 pass_expr_to_client(container->fn, data);
128 break;
129 case STMT_HOOK:
130 pass_stmt_to_client(container->fn, data);
131 break;
132 case SYM_HOOK:
133 pass_sym_to_client(container->fn, data);
134 break;
137 } END_FOR_EACH_PTR(container);
140 void __pass_to_client_no_data(enum hook_type type)
142 struct hook_container *container;
144 FOR_EACH_PTR(hook_funcs, container) {
145 if (container->hook_type == type)
146 pass_to_client(container->fn);
147 } END_FOR_EACH_PTR(container);
150 void __pass_declarations_to_client(struct symbol_list *sym_list)
152 struct symbol *sym;
153 FOR_EACH_PTR(sym_list, sym) {
154 __pass_to_client(sym, DECLARATION_HOOK);
155 } END_FOR_EACH_PTR(sym);
158 int __has_merge_function(int client_id)
160 struct hook_container *tmp;
162 FOR_EACH_PTR(merge_funcs, tmp) {
163 if (tmp->data_type == client_id)
164 return 1;
165 } END_FOR_EACH_PTR(tmp);
166 return 0;
169 struct smatch_state *__client_merge_function(int owner, const char *name,
170 struct symbol *sym,
171 struct smatch_state *s1,
172 struct smatch_state *s2)
174 struct smatch_state *tmp_state;
175 struct hook_container *tmp;
177 /* Pass NULL states first and the rest alphabetically by name */
178 if (!s2 || (s1 && strcmp(s2->name, s1->name) < 0)) {
179 tmp_state = s1;
180 s1 = s2;
181 s2 = tmp_state;
184 FOR_EACH_PTR(merge_funcs, tmp) {
185 if (tmp->data_type == owner)
186 return ((merge_func_t *) tmp->fn)(name, sym, s1, s2);
187 } END_FOR_EACH_PTR(tmp);
188 return &undefined;