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:
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.
37 #include "smatch_slist.h"
38 #include "smatch_extra.h"
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
;
54 if (!__in_fake_assign
)
56 if (!is_whole_rl(estate_rl(state
)))
58 if (get_state(my_id
, name
, sym
))
61 faked
= get_faked_expression();
62 if (!faked
|| faked
->type
!= EXPR_ASSIGNMENT
)
65 left_name
= expr_to_var(faked
->left
);
69 len
= strlen(left_name
);
70 if (strncmp(name
, left_name
, len
) == 0 && name
[len
] == '-')
72 free_string(left_name
);
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
))
81 if (get_param_num_from_sym(sym
) < 0)
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
;
95 struct range_list
*rl
;
102 if (!is_array(expr
->left
))
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
))
110 name
= expr_to_var_sym(array
, &sym
);
113 if (get_param_num_from_sym(sym
) < 0)
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
));
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
)
134 FOR_EACH_PTR(list
, tmp
) {
136 ret
= strncmp(tmp
, name
, len
);
141 if (name
[len
] == '-')
143 } END_FOR_EACH_PTR(tmp
);
148 static void print_return_value_param(int return_id
, char *return_ranges
, struct expression
*expr
)
151 struct smatch_state
*extra
;
153 struct range_list
*rl
;
154 const char *param_name
;
155 struct string_list
*set_list
= NULL
;
160 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
161 if (!estate_rl(sm
->state
))
163 extra
= get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
165 rl
= rl_intersection(estate_rl(sm
->state
), estate_rl(extra
));
169 rl
= estate_rl(sm
->state
);
172 param
= get_param_num_from_sym(sm
->sym
);
175 param_name
= get_param_name(sm
);
178 if (strcmp(param_name
, "$") == 0)
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
));
189 math_str
= get_value_in_terms_of_parameter_math_var_sym(sm
->name
, sm
->sym
);
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
);
199 /* no useful information here. */
200 if (is_whole_rl(rl
) && parent_set(set_list
, sm
->name
))
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
)
218 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
221 len
= strlen(sm
->name
);
222 if (strncmp(sm
->name
, name
, len
) != 0)
224 if (name
[len
] == '\0' ||
227 } END_FOR_EACH_SM(sm
);
232 int param_was_set(struct expression
*expr
)
238 name
= expr_to_var_sym(expr
, &sym
);
242 ret
= param_was_set_var_sym(name
, sym
);
248 void register_param_set(int 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
);