2 * Copyright (C) 2013 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * Track how functions are saved as various struct members or passed as
26 #include "smatch_slist.h"
30 static char *get_array_ptr(struct expression
*expr
)
32 struct expression
*array
;
37 array
= get_array_name(expr
);
38 /* FIXME: is_array() should probably be is_array_element() */
39 type
= get_type(expr
);
40 if (!array
&& type
&& type
->type
== SYM_ARRAY
)
43 name
= expr_to_var(array
);
46 snprintf(buf
, sizeof(buf
), "%s[]", name
);
47 return alloc_string(buf
);
50 expr
= get_assigned_expr(expr
);
51 array
= get_array_name(expr
);
54 name
= expr_to_var(array
);
57 snprintf(buf
, sizeof(buf
), "%s[]", name
);
59 return alloc_string(buf
);
62 static int is_local_symbol(struct symbol
*sym
)
64 if (!sym
|| !sym
->scope
|| !sym
->scope
->token
)
66 if (positions_eq(sym
->scope
->token
->pos
, cur_func_sym
->pos
))
71 static char *ptr_prefix(struct symbol
*sym
)
76 if (is_local_symbol(sym
))
77 snprintf(buf
, sizeof(buf
), "%s ptr", get_function());
78 else if (sym
&& toplevel(sym
->scope
))
79 snprintf(buf
, sizeof(buf
), "%s ptr", get_base_file());
81 snprintf(buf
, sizeof(buf
), "ptr");
86 char *get_fnptr_name(struct expression
*expr
)
90 expr
= strip_expr(expr
);
92 /* (*ptrs[0])(a, b, c) is the same as ptrs[0](a, b, c); */
93 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
94 struct expression
*unop
= strip_expr(expr
->unop
);
96 if (unop
->type
== EXPR_PREOP
&& unop
->op
== '*')
98 else if (unop
->type
== EXPR_SYMBOL
)
102 name
= get_array_ptr(expr
);
106 if (expr
->type
== EXPR_SYMBOL
) {
112 param
= get_param_num_from_sym(expr
->symbol
);
114 snprintf(buf
, sizeof(buf
), "%s param %d", get_function(), param
);
115 return alloc_string(buf
);
118 name
= expr_to_var_sym(expr
, &sym
);
121 type
= get_type(expr
);
122 if (type
&& type
->type
== SYM_PTR
) {
123 snprintf(buf
, sizeof(buf
), "%s %s", ptr_prefix(sym
), name
);
125 return alloc_string(buf
);
129 name
= get_member_name(expr
);
132 return expr_to_var(expr
);
135 static void match_passes_function_pointer(struct expression
*expr
)
137 struct expression
*arg
, *tmp
;
146 FOR_EACH_PTR(expr
->args
, arg
) {
149 tmp
= strip_expr(arg
);
150 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
151 tmp
= strip_expr(tmp
->unop
);
153 type
= get_type(tmp
);
154 if (type
&& type
->type
== SYM_PTR
)
155 type
= get_real_base_type(type
);
156 if (!type
|| type
->type
!= SYM_FN
)
159 called_name
= expr_to_var(expr
->fn
);
162 fn_name
= get_fnptr_name(tmp
);
166 snprintf(ptr_name
, sizeof(ptr_name
), "%s param %d", called_name
, i
);
167 sql_insert_function_ptr(fn_name
, ptr_name
);
169 free_string(fn_name
);
170 free_string(called_name
);
171 } END_FOR_EACH_PTR(arg
);
175 static void match_function_assign(struct expression
*expr
)
177 struct expression
*right
;
182 if (__in_fake_assign
)
185 right
= strip_expr(expr
->right
);
186 if (right
->type
== EXPR_PREOP
&& right
->op
== '&')
187 right
= strip_expr(right
->unop
);
188 if (right
->type
!= EXPR_SYMBOL
&& right
->type
!= EXPR_DEREF
)
190 sym
= get_type(right
);
193 if (sym
->type
!= SYM_FN
&& sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
)
195 if (sym
->type
== SYM_PTR
) {
196 sym
= get_real_base_type(sym
);
199 if (sym
->type
!= SYM_FN
&& sym
!= &void_ctype
)
203 fn_name
= get_fnptr_name(right
);
204 ptr_name
= get_fnptr_name(expr
->left
);
205 if (!fn_name
|| !ptr_name
)
207 if (strcmp(fn_name
, ptr_name
) == 0)
210 sql_insert_function_ptr(fn_name
, ptr_name
);
213 free_string(fn_name
);
214 free_string(ptr_name
);
217 void register_function_ptrs(int id
)
224 add_hook(&match_passes_function_pointer
, FUNCTION_CALL_HOOK
);
225 add_hook(&match_function_assign
, ASSIGNMENT_HOOK
);
226 add_hook(&match_function_assign
, GLOBAL_ASSIGNMENT_HOOK
);