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"
31 extern int check_assigned_expr_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
);
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
;
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
)
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
)
73 if (sm
->state
->data
!= sym
)
75 len
= strlen(sm
->state
->name
);
76 if (strncmp(name
, sm
->state
->name
, len
) != 0)
81 snprintf(buf
, sizeof(buf
), "%s%s", sm
->name
, name
+ len
);
83 return alloc_string(buf
);
86 static char *map_assignment_long_to_short(struct sm_state
*sm
, const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
88 struct symbol
*orig_sym
;
95 orig_sym
= expr_to_sym(sm
->state
->data
);
101 len
= strlen(sm
->state
->name
);
102 if (strncmp(name
, sm
->state
->name
, len
) != 0)
105 if (name
[len
] == '.')
107 snprintf(buf
, sizeof(buf
), "%s%s", sm
->name
, name
+ len
);
109 return alloc_string(buf
);
113 * Normally, we expect people to consistently refer to variables by the shortest
114 * name. So they use "b->a" instead of "foo->bar.a" when both point to the
115 * same memory location. However, when we're dealing across function boundaries
116 * then sometimes we pass frob(foo) which sets foo->bar.a. In that case, we
117 * translate it to the shorter name. Smatch extra updates the shorter name,
118 * which in turn updates the longer name.
121 char *map_long_to_short_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
)
128 FOR_EACH_SM(__get_cur_stree(), sm
) {
129 if (sm
->owner
== my_id
) {
130 ret
= map_my_state_long_to_short(sm
, name
, sym
, new_sym
);
135 if (sm
->owner
== check_assigned_expr_id
) {
136 ret
= map_assignment_long_to_short(sm
, name
, sym
, new_sym
);
142 } END_FOR_EACH_SM(sm
);
147 char *map_call_to_param_name_sym(struct expression
*expr
, struct symbol
**sym
)
150 struct smatch_state
*state
;
154 name
= expr_to_str(expr
);
157 state
= get_state(my_id
, name
, (struct symbol
*)expr
);
158 if (!state
|| !state
->data
)
161 return alloc_string(state
->name
);
164 static void store_mapping_helper(char *left_name
, struct symbol
*left_sym
, struct expression
*call
, const char *return_string
)
166 const char *p
= return_string
;
169 struct expression
*arg
;
171 struct symbol
*right_sym
;
174 while (*p
&& *p
!= '[')
184 if (*p
!= '-' || *(p
+ 1) != '>')
186 end
= strchr(p
, ']');
190 arg
= get_argument_from_call_expr(call
->args
, param
);
193 name
= expr_to_var_sym(arg
, &right_sym
);
194 if (!name
|| !right_sym
)
196 snprintf(buf
, sizeof(buf
), "%s", name
);
198 if (end
- p
+ strlen(buf
) >= sizeof(buf
))
200 strncat(buf
, p
, end
- p
);
202 set_state(my_id
, left_name
, left_sym
, alloc_my_state(buf
, right_sym
));
203 //set_state(my_id, buf, right_sym, alloc_my_state(left_name, left_sym));
205 store_link(link_id
, buf
, right_sym
, left_name
, left_sym
);
211 void __add_return_to_param_mapping(struct expression
*expr
, const char *return_string
)
213 struct expression
*call
;
214 char *left_name
= NULL
;
215 struct symbol
*left_sym
;
217 if (expr
->type
== EXPR_ASSIGNMENT
) {
218 left_name
= expr_to_var_sym(expr
->left
, &left_sym
);
219 if (!left_name
|| !left_sym
)
222 call
= strip_expr(expr
->right
);
223 if (call
->type
!= EXPR_CALL
)
226 store_mapping_helper(left_name
, left_sym
, call
, return_string
);
230 if (expr
->type
== EXPR_CALL
&&
231 expr_get_parent_stmt(expr
) &&
232 expr_get_parent_stmt(expr
)->type
== STMT_RETURN
) {
233 call
= strip_expr(expr
);
234 left_name
= expr_to_str(call
);
239 * HACK ALERT: The symbol pointer is basically used as a cookie
240 * and not used as a pointer so we can pass expr here without
244 store_mapping_helper(left_name
, (struct symbol
*)expr
, call
, return_string
);
250 free_string(left_name
);
253 void register_return_to_param(int id
)
256 add_modification_hook(my_id
, &undef
);
259 void register_return_to_param_links(int id
)
262 set_up_link_functions(my_id
, link_id
);