2 * sparse/smatch_hooks.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
12 struct hook_container
{
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
;
29 container
->data_type
= EXPR_HOOK
;
32 container
->data_type
= STMT_HOOK
;
35 container
->data_type
= SYM_HOOK
;
37 case DECLARATION_HOOK
:
38 container
->data_type
= SYM_HOOK
;
41 container
->data_type
= EXPR_HOOK
;
44 container
->data_type
= EXPR_HOOK
;
46 case ASSIGNMENT_AFTER_HOOK
:
47 container
->data_type
= EXPR_HOOK
;
50 container
->data_type
= EXPR_HOOK
;
52 case WHOLE_CONDITION_HOOK
:
53 container
->data_type
= EXPR_HOOK
;
55 case FUNCTION_CALL_HOOK
:
56 container
->data_type
= EXPR_HOOK
;
58 case FUNCTION_CALL_AFTER_HOOK
:
59 container
->data_type
= EXPR_HOOK
;
62 container
->data_type
= EXPR_HOOK
;
65 container
->data_type
= SYM_HOOK
;
68 container
->data_type
= SYM_HOOK
;
71 container
->data_type
= SYM_HOOK
;
74 container
->data_type
= STMT_HOOK
;
77 /* nothing needed... */
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
;
88 add_ptr_list(&merge_funcs
, container
);
92 static void pass_to_client(void * fn
)
94 typedef void (expr_func
)();
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
;
123 FOR_EACH_PTR(hook_funcs
, container
) {
124 if (container
->hook_type
== type
) {
125 switch(container
->data_type
) {
127 pass_expr_to_client(container
->fn
, data
);
130 pass_stmt_to_client(container
->fn
, data
);
133 pass_sym_to_client(container
->fn
, data
);
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
)
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
)
165 } END_FOR_EACH_PTR(tmp
);
169 struct smatch_state
*__client_merge_function(int owner
, const char *name
,
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)) {
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
);