Silence the mutually exclusive output again.
[smatch.git] / smatch_hooks.c
blobfd0af76fb4ca1ea3e28badff43f99c064261aa26
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 void add_hook(void *func, enum hook_type type)
25 struct hook_container *container = __alloc_hook_container(0);
26 container->hook_type = type;
27 container->fn = func;
28 switch(type) {
29 case EXPR_HOOK:
30 container->data_type = EXPR_HOOK;
31 break;
32 case STMT_HOOK:
33 container->data_type = STMT_HOOK;
34 break;
35 case SYM_HOOK:
36 container->data_type = SYM_HOOK;
37 break;
38 case DECLARATION_HOOK:
39 container->data_type = SYM_HOOK;
40 break;
41 case ASSIGNMENT_HOOK:
42 container->data_type = EXPR_HOOK;
43 break;
44 case CALL_ASSIGNMENT_HOOK:
45 container->data_type = EXPR_HOOK;
46 break;
47 case OP_HOOK:
48 container->data_type = EXPR_HOOK;
49 break;
50 case CONDITION_HOOK:
51 container->data_type = EXPR_HOOK;
52 break;
53 case WHOLE_CONDITION_HOOK:
54 container->data_type = EXPR_HOOK;
55 break;
56 case FUNCTION_CALL_HOOK:
57 container->data_type = EXPR_HOOK;
58 break;
59 case DEREF_HOOK:
60 container->data_type = EXPR_HOOK;
61 break;
62 case BASE_HOOK:
63 container->data_type = SYM_HOOK;
64 break;
65 case FUNC_DEF_HOOK:
66 container->data_type = SYM_HOOK;
67 break;
68 case END_FUNC_HOOK:
69 container->data_type = SYM_HOOK;
70 break;
71 case RETURN_HOOK:
72 container->data_type = STMT_HOOK;
73 break;
74 case END_FILE_HOOK:
75 /* nothing needed... */
76 break;
78 add_ptr_list(&hook_funcs, container);
81 void add_merge_hook(int client_id, merge_func_t *func)
83 struct hook_container *container = __alloc_hook_container(0);
84 container->data_type = client_id;
85 container->fn = func;
86 add_ptr_list(&merge_funcs, container);
89 void add_unmatched_state_hook(int client_id, unmatched_func_t *func)
91 struct hook_container *container = __alloc_hook_container(0);
92 container->data_type = client_id;
93 container->fn = func;
94 add_ptr_list(&unmatched_state_funcs, container);
97 static void pass_to_client(void * fn)
99 typedef void (expr_func)();
100 ((expr_func *) fn)();
103 static void pass_expr_to_client(void * fn, void * data)
105 typedef void (expr_func)(struct expression *expr);
106 ((expr_func *) fn)((struct expression *) data);
109 static void pass_stmt_to_client(void * fn, void * data)
111 typedef void (stmt_func)(struct statement *stmt);
112 ((stmt_func *) fn)((struct statement *) data);
115 static void pass_sym_to_client(void * fn, void * data)
117 typedef void (sym_func)(struct symbol *sym);
118 ((sym_func *) fn)((struct symbol *) data);
121 void __pass_to_client(void *data, enum hook_type type)
123 struct hook_container *container;
125 if (!data)
126 return;
128 FOR_EACH_PTR(hook_funcs, container) {
129 if (container->hook_type == type) {
130 switch(container->data_type) {
131 case EXPR_HOOK:
132 pass_expr_to_client(container->fn, data);
133 break;
134 case STMT_HOOK:
135 pass_stmt_to_client(container->fn, data);
136 break;
137 case SYM_HOOK:
138 pass_sym_to_client(container->fn, data);
139 break;
142 } END_FOR_EACH_PTR(container);
145 void __pass_to_client_no_data(enum hook_type type)
147 struct hook_container *container;
149 FOR_EACH_PTR(hook_funcs, container) {
150 if (container->hook_type == type)
151 pass_to_client(container->fn);
152 } END_FOR_EACH_PTR(container);
155 int __has_merge_function(int client_id)
157 struct hook_container *tmp;
159 FOR_EACH_PTR(merge_funcs, tmp) {
160 if (tmp->data_type == client_id)
161 return 1;
162 } END_FOR_EACH_PTR(tmp);
163 return 0;
166 struct smatch_state *__client_merge_function(int owner, const char *name,
167 struct symbol *sym,
168 struct smatch_state *s1,
169 struct smatch_state *s2)
171 struct smatch_state *tmp_state;
172 struct hook_container *tmp;
174 /* Pass NULL states first and the rest alphabetically by name */
175 if (!s2 || (s1 && strcmp(s2->name, s1->name) < 0)) {
176 tmp_state = s1;
177 s1 = s2;
178 s2 = tmp_state;
181 FOR_EACH_PTR(merge_funcs, tmp) {
182 if (tmp->data_type == owner)
183 return ((merge_func_t *) tmp->fn)(name, sym, s1, s2);
184 } END_FOR_EACH_PTR(tmp);
185 return &undefined;
188 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm)
190 struct hook_container *tmp;
192 FOR_EACH_PTR(unmatched_state_funcs, tmp) {
193 if (tmp->data_type == sm->owner)
194 return ((unmatched_func_t *) tmp->fn)(sm);
195 } END_FOR_EACH_PTR(tmp);
196 return &undefined;