Improve how the debug info for merging states gets printed out.
[smatch.git] / smatch_hooks.c
blob4c7ebe49fde1510e6629fb0149a688c9d8c5bede
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 ASSIGNMENT_AFTER_HOOK:
44 container->data_type = EXPR_HOOK;
45 break;
46 case CONDITION_HOOK:
47 container->data_type = EXPR_HOOK;
48 break;
49 case WHOLE_CONDITION_HOOK:
50 container->data_type = EXPR_HOOK;
51 break;
52 case FUNCTION_CALL_HOOK:
53 container->data_type = EXPR_HOOK;
54 break;
55 case FUNCTION_CALL_AFTER_HOOK:
56 container->data_type = EXPR_HOOK;
57 break;
58 case DEREF_HOOK:
59 container->data_type = EXPR_HOOK;
60 break;
61 case BASE_HOOK:
62 container->data_type = SYM_HOOK;
63 break;
64 case FUNC_DEF_HOOK:
65 container->data_type = SYM_HOOK;
66 break;
67 case END_FUNC_HOOK:
68 container->data_type = SYM_HOOK;
69 break;
70 case RETURN_HOOK:
71 container->data_type = STMT_HOOK;
72 break;
74 add_ptr_list(&hook_funcs, container);
77 void add_merge_hook(int client_id, merge_func_t *func)
79 struct hook_container *container = __alloc_hook_container(0);
80 container->data_type = client_id;
81 container->fn = func;
82 add_ptr_list(&merge_funcs, container);
86 static void pass_expr_to_client(void * fn, void * data)
88 typedef void (expr_func)(struct expression *expr);
89 ((expr_func *) fn)((struct expression *) data);
92 static void pass_stmt_to_client(void * fn, void * data)
94 typedef void (stmt_func)(struct statement *stmt);
95 ((stmt_func *) fn)((struct statement *) data);
98 static void pass_sym_to_client(void * fn, void * data)
100 typedef void (sym_func)(struct symbol *sym);
101 ((sym_func *) fn)((struct symbol *) data);
104 void __pass_to_client(void *data, enum hook_type type)
106 struct hook_container *container;
108 if (!data)
109 return;
111 FOR_EACH_PTR(hook_funcs, container) {
112 if (container->hook_type == type) {
113 switch(container->data_type) {
114 case EXPR_HOOK:
115 pass_expr_to_client(container->fn, data);
116 break;
117 case STMT_HOOK:
118 pass_stmt_to_client(container->fn, data);
119 break;
120 case SYM_HOOK:
121 pass_sym_to_client(container->fn, data);
122 break;
125 } END_FOR_EACH_PTR(container);
128 void __pass_declarations_to_client(struct symbol_list *sym_list)
130 struct symbol *sym;
131 FOR_EACH_PTR(sym_list, sym) {
132 __pass_to_client(sym, DECLARATION_HOOK);
133 } END_FOR_EACH_PTR(sym);
136 int __has_merge_function(int client_id)
138 struct hook_container *tmp;
140 FOR_EACH_PTR(merge_funcs, tmp) {
141 if (tmp->data_type == client_id)
142 return 1;
143 } END_FOR_EACH_PTR(tmp);
144 return 0;
147 int __client_merge_function(int owner, const char *name, struct symbol *sym,
148 int s1, int s2)
150 struct hook_container *tmp;
152 FOR_EACH_PTR(merge_funcs, tmp) {
153 if (tmp->data_type == owner)
154 return ((merge_func_t *) tmp->fn)(name, sym, s1, s2);
155 } END_FOR_EACH_PTR(tmp);
156 return UNDEFINED;