param_set: don't save if the parent is set
[smatch.git] / smatch_param_set.c
blob6a0e9abcfe6c02f4741a7501d9b5eee5f7341568
1 /*
2 * Copyright (C) 2012 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 functions like:
21 * int foo(int *x)
22 * {
23 * if (*x == 42) {
24 * *x = 0;
25 * return 1;
26 * }
27 * return 0;
28 * }
30 * If we return 1 that means the value of *x has been set to 0. If we return
31 * 0 then we have left *x alone.
35 #include "scope.h"
36 #include "smatch.h"
37 #include "smatch_slist.h"
38 #include "smatch_extra.h"
40 static int my_id;
42 static struct smatch_state *unmatched_state(struct sm_state *sm)
44 return alloc_estate_empty();
47 static int parent_is_set(const char *name, struct symbol *sym, struct smatch_state *state)
49 struct expression *faked;
50 char *left_name;
51 int ret = 0;
52 int len;
54 if (!__in_fake_assign)
55 return 0;
56 if (!is_whole_rl(estate_rl(state)))
57 return 0;
58 if (get_state(my_id, name, sym))
59 return 0;
61 faked = get_faked_expression();
62 if (!faked || faked->type != EXPR_ASSIGNMENT)
63 return 0;
65 left_name = expr_to_var(faked->left);
66 if (!left_name)
67 return 0;
69 len = strlen(left_name);
70 if (strncmp(name, left_name, len) == 0 && name[len] == '-')
71 ret = 1;
72 free_string(left_name);
74 return ret;
77 static void extra_mod_hook(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
79 if (parent_is_set(name, sym, state))
80 return;
81 if (get_param_num_from_sym(sym) < 0)
82 return;
83 set_state(my_id, name, sym, state);
87 * This function is is a dirty hack because extra_mod_hook is giving us a NULL
88 * sym instead of a vsl.
90 static void match_array_assignment(struct expression *expr)
92 struct expression *array, *offset;
93 char *name;
94 struct symbol *sym;
95 struct range_list *rl;
96 sval_t sval;
97 char buf[256];
99 if (__in_fake_assign)
100 return;
102 if (!is_array(expr->left))
103 return;
104 array = get_array_base(expr->left);
105 offset = get_array_offset(expr->left);
107 /* These are handled by extra_mod_hook() */
108 if (get_value(offset, &sval))
109 return;
110 name = expr_to_var_sym(array, &sym);
111 if (!name || !sym)
112 goto free;
113 if (get_param_num_from_sym(sym) < 0)
114 goto free;
115 get_absolute_rl(expr->right, &rl);
116 rl = cast_rl(get_type(expr->left), rl);
118 snprintf(buf, sizeof(buf), "*%s", name);
119 set_state(my_id, buf, sym, alloc_estate_rl(rl));
120 free:
121 free_string(name);
125 * This relies on the fact that these states are stored so that
126 * foo->bar is before foo->bar->baz.
128 static int parent_set(struct string_list *list, const char *name)
130 char *tmp;
131 int len;
132 int ret;
134 FOR_EACH_PTR(list, tmp) {
135 len = strlen(tmp);
136 ret = strncmp(tmp, name, len);
137 if (ret < 0)
138 continue;
139 if (ret > 0)
140 return 0;
141 if (name[len] == '-')
142 return 1;
143 } END_FOR_EACH_PTR(tmp);
145 return 0;
148 static void print_return_value_param(int return_id, char *return_ranges, struct expression *expr)
150 struct sm_state *sm;
151 struct smatch_state *extra;
152 int param;
153 struct range_list *rl;
154 const char *param_name;
155 struct string_list *set_list = NULL;
156 char *math_str;
157 char buf[256];
158 sval_t sval;
160 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
161 if (!estate_rl(sm->state))
162 continue;
163 extra = get_state(SMATCH_EXTRA, sm->name, sm->sym);
164 if (extra) {
165 rl = rl_intersection(estate_rl(sm->state), estate_rl(extra));
166 if (!rl)
167 continue;
168 } else {
169 rl = estate_rl(sm->state);
172 param = get_param_num_from_sym(sm->sym);
173 if (param < 0)
174 continue;
175 param_name = get_param_name(sm);
176 if (!param_name)
177 continue;
178 if (strcmp(param_name, "$") == 0)
179 continue;
181 if (rl_to_sval(rl, &sval)) {
182 insert_string(&set_list, (char *)sm->name);
183 sql_insert_return_states(return_id, return_ranges,
184 param_has_filter_data(sm) ? PARAM_ADD : PARAM_SET,
185 param, param_name, show_rl(rl));
186 continue;
189 math_str = get_value_in_terms_of_parameter_math_var_sym(sm->name, sm->sym);
190 if (math_str) {
191 snprintf(buf, sizeof(buf), "%s[%s]", show_rl(rl), math_str);
192 insert_string(&set_list, (char *)sm->name);
193 sql_insert_return_states(return_id, return_ranges,
194 param_has_filter_data(sm) ? PARAM_ADD : PARAM_SET,
195 param, param_name, buf);
196 continue;
199 /* no useful information here. */
200 if (is_whole_rl(rl) && parent_set(set_list, sm->name))
201 continue;
202 insert_string(&set_list, (char *)sm->name);
204 sql_insert_return_states(return_id, return_ranges,
205 param_has_filter_data(sm) ? PARAM_ADD : PARAM_SET,
206 param, param_name, show_rl(rl));
208 } END_FOR_EACH_SM(sm);
210 free_ptr_list((struct ptr_list **)&set_list);
213 int param_was_set_var_sym(const char *name, struct symbol *sym)
215 struct sm_state *sm;
216 int len;
218 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
219 if (sm->sym != sym)
220 continue;
221 len = strlen(sm->name);
222 if (strncmp(sm->name, name, len) != 0)
223 continue;
224 if (name[len] == '\0' ||
225 name[len] == '-')
226 return 1;
227 } END_FOR_EACH_SM(sm);
229 return 0;
232 int param_was_set(struct expression *expr)
234 char *name;
235 struct symbol *sym;
236 int ret = 0;
238 name = expr_to_var_sym(expr, &sym);
239 if (!name || !sym)
240 goto free;
242 ret = param_was_set_var_sym(name, sym);
243 free:
244 free_string(name);
245 return ret;
248 void register_param_set(int id)
250 my_id = id;
252 add_extra_mod_hook(&extra_mod_hook);
253 add_hook(match_array_assignment, ASSIGNMENT_HOOK);
254 add_unmatched_state_hook(my_id, &unmatched_state);
255 add_merge_hook(my_id, &merge_estates);
256 add_split_return_callback(&print_return_value_param);