check_overflow: test copy_to/from_user as well.
[smatch.git] / smatch_hooks.c
blob9db32fcbe72945b6f43a342725b86fd110549ecf
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 OP_HOOK:
45 container->data_type = EXPR_HOOK;
46 break;
47 case CONDITION_HOOK:
48 container->data_type = EXPR_HOOK;
49 break;
50 case WHOLE_CONDITION_HOOK:
51 container->data_type = EXPR_HOOK;
52 break;
53 case FUNCTION_CALL_HOOK:
54 container->data_type = EXPR_HOOK;
55 break;
56 case DEREF_HOOK:
57 container->data_type = EXPR_HOOK;
58 break;
59 case BASE_HOOK:
60 container->data_type = SYM_HOOK;
61 break;
62 case FUNC_DEF_HOOK:
63 container->data_type = SYM_HOOK;
64 break;
65 case END_FUNC_HOOK:
66 container->data_type = SYM_HOOK;
67 break;
68 case RETURN_HOOK:
69 container->data_type = STMT_HOOK;
70 break;
71 case END_FILE_HOOK:
72 /* nothing needed... */
73 break;
75 add_ptr_list(&hook_funcs, container);
78 void add_merge_hook(int client_id, merge_func_t *func)
80 struct hook_container *container = __alloc_hook_container(0);
81 container->data_type = client_id;
82 container->fn = func;
83 add_ptr_list(&merge_funcs, container);
86 void add_unmatched_state_hook(int client_id, unmatched_func_t *func)
88 struct hook_container *container = __alloc_hook_container(0);
89 container->data_type = client_id;
90 container->fn = func;
91 add_ptr_list(&unmatched_state_funcs, container);
94 static void pass_to_client(void * fn)
96 typedef void (expr_func)();
97 ((expr_func *) fn)();
100 static void pass_expr_to_client(void * fn, void * data)
102 typedef void (expr_func)(struct expression *expr);
103 ((expr_func *) fn)((struct expression *) data);
106 static void pass_stmt_to_client(void * fn, void * data)
108 typedef void (stmt_func)(struct statement *stmt);
109 ((stmt_func *) fn)((struct statement *) data);
112 static void pass_sym_to_client(void * fn, void * data)
114 typedef void (sym_func)(struct symbol *sym);
115 ((sym_func *) fn)((struct symbol *) data);
118 void __pass_to_client(void *data, enum hook_type type)
120 struct hook_container *container;
122 if (!data)
123 return;
125 FOR_EACH_PTR(hook_funcs, container) {
126 if (container->hook_type == type) {
127 switch(container->data_type) {
128 case EXPR_HOOK:
129 pass_expr_to_client(container->fn, data);
130 break;
131 case STMT_HOOK:
132 pass_stmt_to_client(container->fn, data);
133 break;
134 case SYM_HOOK:
135 pass_sym_to_client(container->fn, data);
136 break;
139 } END_FOR_EACH_PTR(container);
142 void __pass_to_client_no_data(enum hook_type type)
144 struct hook_container *container;
146 FOR_EACH_PTR(hook_funcs, container) {
147 if (container->hook_type == type)
148 pass_to_client(container->fn);
149 } END_FOR_EACH_PTR(container);
152 void __pass_declarations_to_client(struct symbol_list *sym_list)
154 struct symbol *sym;
155 FOR_EACH_PTR(sym_list, sym) {
156 __pass_to_client(sym, DECLARATION_HOOK);
157 } END_FOR_EACH_PTR(sym);
160 int __has_merge_function(int client_id)
162 struct hook_container *tmp;
164 FOR_EACH_PTR(merge_funcs, tmp) {
165 if (tmp->data_type == client_id)
166 return 1;
167 } END_FOR_EACH_PTR(tmp);
168 return 0;
171 struct smatch_state *__client_merge_function(int owner, const char *name,
172 struct symbol *sym,
173 struct smatch_state *s1,
174 struct smatch_state *s2)
176 struct smatch_state *tmp_state;
177 struct hook_container *tmp;
179 /* Pass NULL states first and the rest alphabetically by name */
180 if (!s2 || (s1 && strcmp(s2->name, s1->name) < 0)) {
181 tmp_state = s1;
182 s1 = s2;
183 s2 = tmp_state;
186 FOR_EACH_PTR(merge_funcs, tmp) {
187 if (tmp->data_type == owner)
188 return ((merge_func_t *) tmp->fn)(name, sym, s1, s2);
189 } END_FOR_EACH_PTR(tmp);
190 return &undefined;
193 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm)
195 struct hook_container *tmp;
197 FOR_EACH_PTR(unmatched_state_funcs, tmp) {
198 if (tmp->data_type == sm->owner)
199 return ((unmatched_func_t *) tmp->fn)(sm);
200 } END_FOR_EACH_PTR(tmp);
201 return &undefined;