2 * Copyright (C) 2017 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 * This is for smatch_extra.c to use. It sort of like check_assigned_expr.c but
20 * more limited. Say a function returns "64min-s64max[$0->data]" and the caller
21 * does "struct whatever *p = get_data(dev);" then we want to record that p is
22 * now the same as "dev->data". Then if we update "p->foo" it means we can
23 * update "dev->data->foo" as well.
28 #include "smatch_slist.h"
29 #include "smatch_extra.h"
34 static struct smatch_state
*alloc_my_state(const char *name
, struct symbol
*sym
)
36 struct smatch_state
*state
;
38 state
= __alloc_smatch_state(0);
39 state
->name
= alloc_sname(name
);
44 static void undef(struct sm_state
*sm
, struct expression
*mod_expr
)
46 set_state(my_id
, sm
->name
, sm
->sym
, &undefined
);
49 char *map_call_to_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
51 struct smatch_state
*state
;
55 /* skip 'foo->'. This was checked in the caller. */
56 skip
= strlen(sym
->ident
->name
) + 2;
58 state
= get_state(my_id
, sym
->ident
->name
, sym
);
59 if (!state
|| !state
->data
)
62 snprintf(buf
, sizeof(buf
), "%s->%s", state
->name
, name
+ skip
);
63 *new_sym
= state
->data
;
64 return alloc_string(buf
);
68 * Normally, we expect people to consistently refer to variables by the shortest
69 * name. So they use "b->a" instead of "foo->bar.a" when both point to the
70 * same memory location. However, when we're dealing across function boundaries
71 * then sometimes we pass frob(foo) which sets foo->bar.a. In that case, we
72 * translate it to the shorter name. Smatch extra updates the shorter name,
73 * which in turn updates the longer name.
76 char *map_long_to_short_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
84 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
85 if (sm
->state
->data
!= sym
)
87 len
= strlen(sm
->state
->name
);
88 if (strncmp(name
, sm
->state
->name
, len
) == 0) {
92 snprintf(buf
, sizeof(buf
), "%s%s", sm
->name
, name
+ len
);
94 return alloc_string(buf
);
96 } END_FOR_EACH_SM(sm
);
101 char *map_call_to_param_name_sym(struct expression
*expr
, struct symbol
**sym
)
104 struct smatch_state
*state
;
108 name
= expr_to_str(expr
);
111 state
= get_state(my_id
, name
, (struct symbol
*)expr
);
112 if (!state
|| !state
->data
)
115 return alloc_string(state
->name
);
118 static void store_mapping_helper(char *left_name
, struct symbol
*left_sym
, struct expression
*call
, const char *return_string
)
120 const char *p
= return_string
;
123 struct expression
*arg
;
125 struct symbol
*right_sym
;
128 while (*p
&& *p
!= '[')
138 if (*p
!= '-' || *(p
+ 1) != '>')
140 end
= strchr(p
, ']');
144 arg
= get_argument_from_call_expr(call
->args
, param
);
147 name
= expr_to_var_sym(arg
, &right_sym
);
148 if (!name
|| !right_sym
)
150 snprintf(buf
, sizeof(buf
), "%s", name
);
152 if (end
- p
+ strlen(buf
) >= sizeof(buf
))
154 strncat(buf
, p
, end
- p
);
156 set_state(my_id
, left_name
, left_sym
, alloc_my_state(buf
, right_sym
));
157 //set_state(my_id, buf, right_sym, alloc_my_state(left_name, left_sym));
159 store_link(link_id
, buf
, right_sym
, left_name
, left_sym
);
165 void __add_return_to_param_mapping(struct expression
*expr
, const char *return_string
)
167 struct expression
*call
;
168 char *left_name
= NULL
;
169 struct symbol
*left_sym
;
171 if (expr
->type
== EXPR_ASSIGNMENT
) {
172 left_name
= expr_to_var_sym(expr
->left
, &left_sym
);
173 if (!left_name
|| !left_sym
)
176 call
= strip_expr(expr
->right
);
177 if (call
->type
!= EXPR_CALL
)
180 store_mapping_helper(left_name
, left_sym
, call
, return_string
);
184 if (expr
->type
== EXPR_CALL
&&
185 expr_get_parent_stmt(expr
) &&
186 expr_get_parent_stmt(expr
)->type
== STMT_RETURN
) {
187 call
= strip_expr(expr
);
188 left_name
= expr_to_str(call
);
193 * HACK ALERT: The symbol pointer is basically used as a cookie
194 * and not used as a pointer so we can pass expr here without
198 store_mapping_helper(left_name
, (struct symbol
*)expr
, call
, return_string
);
204 free_string(left_name
);
207 void register_return_to_param(int id
)
210 add_modification_hook(my_id
, &undef
);
213 void register_return_to_param_links(int id
)
216 set_up_link_functions(my_id
, link_id
);