comparison: improve "foo = min(...);" assignment handling
[smatch.git] / smatch_return_to_param.c
blob3773904ca3a4ab143702a0bcbab643a7787671a7
1 /*
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.
27 #include "smatch.h"
28 #include "smatch_slist.h"
29 #include "smatch_extra.h"
31 extern int check_assigned_expr_id;
32 static int my_id;
33 static int link_id;
35 static struct smatch_state *alloc_my_state(const char *name, struct symbol *sym)
37 struct smatch_state *state;
39 state = __alloc_smatch_state(0);
40 state->name = alloc_sname(name);
41 state->data = sym;
42 return state;
45 static void undef(struct sm_state *sm, struct expression *mod_expr)
47 set_state(my_id, sm->name, sm->sym, &undefined);
50 char *map_call_to_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym)
52 struct smatch_state *state;
53 int skip;
54 char buf[256];
56 /* skip 'foo->'. This was checked in the caller. */
57 skip = strlen(sym->ident->name) + 2;
59 state = get_state(my_id, sym->ident->name, sym);
60 if (!state || !state->data)
61 return NULL;
63 snprintf(buf, sizeof(buf), "%s->%s", state->name, name + skip);
64 *new_sym = state->data;
65 return alloc_string(buf);
68 static char *map_my_state_long_to_short(struct sm_state *sm, const char *name, struct symbol *sym, struct symbol **new_sym, bool stack)
70 int len;
71 char buf[256];
73 if (sm->state->data != sym)
74 return NULL;
75 len = strlen(sm->state->name);
76 if (strncmp(name, sm->state->name, len) != 0)
77 return NULL;
79 if (name[len] == '.')
80 return NULL;
81 if (!stack && name[len] != '-')
82 return NULL;
83 snprintf(buf, sizeof(buf), "%s%s", sm->name, name + len);
84 *new_sym = sm->sym;
85 return alloc_string(buf);
88 static char *map_assignment_long_to_short(struct sm_state *sm, const char *name, struct symbol *sym, struct symbol **new_sym, bool stack)
90 struct expression *orig_expr;
91 struct symbol *orig_sym;
92 int len;
93 char buf[256];
95 orig_expr = sm->state->data;
96 if (!orig_expr)
97 return NULL;
100 * Say we have an assignment like:
101 * foo->bar->my_ptr = my_ptr;
102 * We still expect the function to carry on using "my_ptr" as the
103 * shorter name. That's not a long to short mapping.
106 if (orig_expr->type == EXPR_SYMBOL)
107 return NULL;
109 orig_sym = expr_to_sym(orig_expr);
110 if (!orig_sym)
111 return NULL;
112 if (sym != orig_sym)
113 return NULL;
115 len = strlen(sm->state->name);
116 if (strncmp(name, sm->state->name, len) != 0)
117 return NULL;
119 if (name[len] == '.')
120 return NULL;
121 if (!stack && name[len] != '-')
122 return NULL;
123 snprintf(buf, sizeof(buf), "%s%s", sm->name, name + len);
124 *new_sym = sm->sym;
125 return alloc_string(buf);
129 * Normally, we expect people to consistently refer to variables by the shortest
130 * name. So they use "b->a" instead of "foo->bar.a" when both point to the
131 * same memory location. However, when we're dealing across function boundaries
132 * then sometimes we pass frob(foo) which sets foo->bar.a. In that case, we
133 * translate it to the shorter name. Smatch extra updates the shorter name,
134 * which in turn updates the longer name.
137 static char *map_long_to_short_name_sym_helper(const char *name, struct symbol *sym, struct symbol **new_sym, bool stack)
139 char *ret;
140 struct sm_state *sm;
142 *new_sym = NULL;
144 FOR_EACH_SM(__get_cur_stree(), sm) {
145 if (sm->owner == my_id) {
146 ret = map_my_state_long_to_short(sm, name, sym, new_sym, stack);
147 if (ret)
148 return ret;
149 continue;
151 if (sm->owner == check_assigned_expr_id) {
152 ret = map_assignment_long_to_short(sm, name, sym, new_sym, stack);
153 if (ret)
154 return ret;
155 continue;
157 } END_FOR_EACH_SM(sm);
159 return NULL;
162 char *map_long_to_short_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym)
164 return map_long_to_short_name_sym_helper(name, sym, new_sym, 1);
167 char *map_long_to_short_name_sym_nostack(const char *name, struct symbol *sym, struct symbol **new_sym)
169 return map_long_to_short_name_sym_helper(name, sym, new_sym, 0);
172 char *map_call_to_param_name_sym(struct expression *expr, struct symbol **sym)
174 char *name;
175 struct symbol *start_sym;
176 struct smatch_state *state;
178 *sym = NULL;
180 name = expr_to_str_sym(expr, &start_sym);
181 if (!name)
182 return NULL;
183 if (expr->type == EXPR_CALL)
184 start_sym = expr_to_sym(expr->fn);
186 state = get_state(my_id, name, start_sym);
187 free_string(name);
188 if (!state || !state->data)
189 return NULL;
191 *sym = state->data;
192 return alloc_string(state->name);
195 static void store_mapping_helper(char *left_name, struct symbol *left_sym, struct expression *call, const char *return_string)
197 const char *p = return_string;
198 char *close;
199 int param;
200 struct expression *arg, *new;
201 char *right_name;
202 struct symbol *right_sym;
203 char buf[256];
205 while (*p && *p != '[')
206 p++;
207 if (!*p)
208 return;
209 p++;
210 if (*p != '$')
211 return;
213 snprintf(buf, sizeof(buf), "%s", p);
214 close = strchr(buf, ']');
215 if (!close)
216 return;
217 *close = '\0';
219 param = atoi(buf + 1);
220 arg = get_argument_from_call_expr(call->args, param);
221 if (!arg)
222 return;
224 new = gen_expression_from_key(arg, buf);
225 if (!new)
226 return;
228 right_name = expr_to_var_sym(new, &right_sym);
229 if (!right_name || !right_sym)
230 goto free;
232 set_state(my_id, left_name, left_sym, alloc_my_state(right_name, right_sym));
233 store_link(link_id, right_name, right_sym, left_name, left_sym);
235 free:
236 free_string(right_name);
239 void __add_return_to_param_mapping(struct expression *expr, const char *return_string)
241 struct expression *call;
242 char *left_name = NULL;
243 struct symbol *left_sym;
245 if (expr->type == EXPR_ASSIGNMENT) {
246 left_name = expr_to_var_sym(expr->left, &left_sym);
247 if (!left_name || !left_sym)
248 goto free;
250 call = strip_expr(expr->right);
251 if (call->type != EXPR_CALL)
252 goto free;
254 store_mapping_helper(left_name, left_sym, call, return_string);
255 goto free;
258 if (expr->type == EXPR_CALL &&
259 expr_get_parent_stmt(expr) &&
260 expr_get_parent_stmt(expr)->type == STMT_RETURN) {
261 call = strip_expr(expr);
262 left_sym = expr_to_sym(call->fn);
263 if (!left_sym)
264 return;
265 left_name = expr_to_str(call);
266 if (!left_name)
267 return;
269 store_mapping_helper(left_name, left_sym, call, return_string);
270 goto free;
274 free:
275 free_string(left_name);
278 void register_return_to_param(int id)
280 my_id = id;
281 add_modification_hook(my_id, &undef);
284 void register_return_to_param_links(int id)
286 link_id = id;
287 set_up_link_functions(my_id, link_id);