user_data2: handle inlines better
[smatch.git] / smatch_return_to_param.c
blob269d747aaf3e4c802dcf3c7760935fb646496712
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 static int my_id;
32 static int link_id;
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);
40 state->data = sym;
41 return state;
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;
52 int skip;
53 char buf[256];
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)
60 return NULL;
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)
78 struct sm_state *sm;
79 int len;
80 char buf[256];
82 *new_sym = NULL;
84 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
85 if (sm->state->data != sym)
86 continue;
87 len = strlen(sm->state->name);
88 if (strncmp(name, sm->state->name, len) == 0) {
89 if (name[len] == '.')
90 continue;
92 snprintf(buf, sizeof(buf), "%s%s", sm->name, name + len);
93 *new_sym = sm->sym;
94 return alloc_string(buf);
96 } END_FOR_EACH_SM(sm);
98 return NULL;
101 char *map_call_to_param_name_sym(struct expression *expr, struct symbol **sym)
103 char *name;
104 struct smatch_state *state;
106 *sym = NULL;
108 name = expr_to_str(expr);
109 if (!name)
110 return NULL;
111 state = get_state(my_id, name, (struct symbol *)expr);
112 if (!state || !state->data)
113 return NULL;
114 *sym = 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;
121 const char *end;
122 int param;
123 struct expression *arg;
124 char *name = NULL;
125 struct symbol *right_sym;
126 char buf[256];
128 while (*p && *p != '[')
129 p++;
130 if (!*p)
131 goto free;
132 p++;
133 if (*p != '$')
134 goto free;
135 p++;
136 param = atoi(p);
137 p++;
138 if (*p != '-' || *(p + 1) != '>')
139 goto free;
140 end = strchr(p, ']');
141 if (!end)
142 goto free;
144 arg = get_argument_from_call_expr(call->args, param);
145 if (!arg)
146 goto free;
147 name = expr_to_var_sym(arg, &right_sym);
148 if (!name || !right_sym)
149 goto free;
150 snprintf(buf, sizeof(buf), "%s", name);
152 if (end - p + strlen(buf) >= sizeof(buf))
153 goto free;
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);
161 free:
162 free_string(name);
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)
174 goto free;
176 call = strip_expr(expr->right);
177 if (call->type != EXPR_CALL)
178 goto free;
180 store_mapping_helper(left_name, left_sym, call, return_string);
181 goto free;
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);
189 if (!left_name)
190 goto free;
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
195 * causing an issue.
198 store_mapping_helper(left_name, (struct symbol *)expr, call, return_string);
199 goto free;
203 free:
204 free_string(left_name);
207 void register_return_to_param(int id)
209 my_id = id;
210 add_modification_hook(my_id, &undef);
213 void register_return_to_param_links(int id)
215 link_id = id;
216 set_up_link_functions(my_id, link_id);