slist: export the set_state_stree_perm() function to everyone
[smatch.git] / smatch_type_val.c
blob704aee4edf00cfd997fdba33a055414ca72e9017
1 /*
2 * Copyright (C) 2013 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 * The plan here is to save all the possible values store to a given struct
20 * member.
22 * We will load all the values in to the function_type_val table first then
23 * run a script on that and load all the resulting values into the type_val
24 * table.
26 * So in this file we want to take the union of everything assigned to the
27 * struct member and insert it into the function_type_val at the end.
29 * You would think that we could use smatch_modification_hooks.c or
30 * extra_modification_hook() here to get the information here but in the end we
31 * need to code everything again a third time.
35 #include "smatch.h"
36 #include "smatch_slist.h"
37 #include "smatch_extra.h"
39 static int my_id;
41 struct stree_stack *fn_type_val_stack;
42 struct stree *fn_type_val;
43 struct stree *global_type_val;
45 static char *db_vals;
46 static int get_vals(void *unused, int argc, char **argv, char **azColName)
48 db_vals = alloc_string(argv[0]);
49 return 0;
52 static void match_inline_start(struct expression *expr)
54 push_stree(&fn_type_val_stack, fn_type_val);
55 fn_type_val = NULL;
58 static void match_inline_end(struct expression *expr)
60 free_stree(&fn_type_val);
61 fn_type_val = pop_stree(&fn_type_val_stack);
64 int get_db_type_rl(struct expression *expr, struct range_list **rl)
66 char *member;
67 struct range_list *tmp;
69 member = get_member_name(expr);
70 if (!member)
71 return 0;
73 db_vals = NULL;
74 run_sql(get_vals,
75 "select value from type_value where type = '%s';", member);
76 free_string(member);
77 if (!db_vals)
78 return 0;
79 str_to_rl(&llong_ctype, db_vals, &tmp);
80 tmp = cast_rl(get_type(expr), tmp);
81 if (is_whole_rl(tmp))
82 return 0;
83 *rl = tmp;
84 free_string(db_vals);
86 return 1;
89 static void add_type_val(char *member, struct range_list *rl)
91 struct smatch_state *old, *add, *new;
93 member = alloc_string(member);
94 old = get_state_stree(fn_type_val, my_id, member, NULL);
95 add = alloc_estate_rl(rl);
96 if (old)
97 new = merge_estates(old, add);
98 else
99 new = add;
100 set_state_stree(&fn_type_val, my_id, member, NULL, new);
103 static void add_fake_type_val(char *member, struct range_list *rl)
105 struct smatch_state *old, *add, *new;
107 member = alloc_string(member);
108 old = get_state_stree(fn_type_val, my_id, member, NULL);
109 add = alloc_estate_rl(rl);
110 if (old) {
111 new = merge_estates(old, add);
112 } else {
113 new = add;
114 new->name = alloc_string("min-max");
116 set_state_stree(&fn_type_val, my_id, member, NULL, new);
119 static void add_global_type_val(char *member, struct range_list *rl)
121 struct smatch_state *old, *add, *new;
123 member = alloc_string(member);
124 old = get_state_stree(global_type_val, my_id, member, NULL);
125 add = alloc_estate_rl(rl);
126 if (old)
127 new = merge_estates(old, add);
128 else
129 new = add;
130 new = clone_estate_perm(new);
131 set_state_stree_perm(&global_type_val, my_id, member, NULL, new);
134 static void match_assign_value(struct expression *expr)
136 char *member, *right_member;
137 struct range_list *rl;
138 struct symbol *type;
140 type = get_type(expr->left);
141 if (type && type->type == SYM_STRUCT)
142 return;
144 member = get_member_name(expr->left);
145 if (!member)
146 return;
148 /* if we're saying foo->mtu = bar->mtu then that doesn't add information */
149 right_member = get_member_name(expr->right);
150 if (right_member && strcmp(right_member, member) == 0)
151 goto free;
153 if (is_fake_call(expr->right)) {
154 add_fake_type_val(member, alloc_whole_rl(get_type(expr->left)));
155 goto free;
158 if (expr->op != '=') {
159 add_type_val(member, alloc_whole_rl(get_type(expr->left)));
160 goto free;
162 get_absolute_rl(expr->right, &rl);
163 rl = cast_rl(type, rl);
164 add_type_val(member, rl);
165 free:
166 free_string(right_member);
167 free_string(member);
171 * If we too: int *p = &my_struct->member then abandon all hope of tracking
172 * my_struct->member.
174 static void match_assign_pointer(struct expression *expr)
176 struct expression *right;
177 char *member;
178 struct range_list *rl;
179 struct symbol *type;
181 right = strip_expr(expr->right);
182 if (right->type != EXPR_PREOP || right->op != '&')
183 return;
184 right = strip_expr(right->unop);
186 member = get_member_name(right);
187 if (!member)
188 return;
189 type = get_type(right);
190 rl = alloc_whole_rl(type);
191 add_type_val(member, rl);
192 free_string(member);
195 static void match_global_assign(struct expression *expr)
197 char *member;
198 struct range_list *rl;
200 member = get_member_name(expr->left);
201 if (!member)
202 return;
203 get_absolute_rl(expr->right, &rl);
204 add_global_type_val(member, rl);
205 free_string(member);
208 static void unop_expr(struct expression *expr)
210 struct range_list *rl;
211 char *member;
213 if (expr->op != SPECIAL_DECREMENT && expr->op != SPECIAL_INCREMENT)
214 return;
216 expr = strip_expr(expr->unop);
217 member = get_member_name(expr);
218 if (!member)
219 return;
220 rl = alloc_whole_rl(get_type(expr));
221 add_type_val(member, rl);
222 free_string(member);
225 static void asm_expr(struct statement *stmt)
227 struct expression *expr;
228 struct range_list *rl;
229 char *member;
230 int state = 0;
232 FOR_EACH_PTR(stmt->asm_outputs, expr) {
233 switch (state) {
234 case 0: /* identifier */
235 case 1: /* constraint */
236 state++;
237 continue;
238 case 2: /* expression */
239 state = 0;
240 member = get_member_name(expr);
241 if (!member)
242 continue;
243 rl = alloc_whole_rl(get_type(expr));
244 add_type_val(member, rl);
245 free_string(member);
246 continue;
248 } END_FOR_EACH_PTR(expr);
251 static void db_param_add(struct expression *expr, int param, char *key, char *value)
253 struct expression *arg;
254 struct symbol *type;
255 struct range_list *rl;
256 char *member;
258 if (strcmp(key, "*$$") != 0)
259 return;
261 while (expr->type == EXPR_ASSIGNMENT)
262 expr = strip_expr(expr->right);
263 if (expr->type != EXPR_CALL)
264 return;
266 arg = get_argument_from_call_expr(expr->args, param);
267 arg = strip_expr(arg);
268 if (!arg)
269 return;
270 type = get_member_type_from_key(arg, key);
271 if (arg->type != EXPR_PREOP || arg->op != '&')
272 return;
273 arg = strip_expr(arg->unop);
275 member = get_member_name(arg);
276 if (!member)
277 return;
278 call_results_to_rl(expr, type, value, &rl);
279 add_type_val(member, rl);
280 free_string(member);
283 static void match_end_func_info(struct symbol *sym)
285 struct sm_state *sm;
287 FOR_EACH_SM(fn_type_val, sm) {
288 sql_insert_function_type_value(sm->name, sm->state->name);
289 } END_FOR_EACH_SM(sm);
291 free_stree(&fn_type_val);
294 static void match_end_file(struct symbol_list *sym_list)
296 struct sm_state *sm;
298 FOR_EACH_SM(global_type_val, sm) {
299 sql_insert_function_type_value(sm->name, sm->state->name);
300 } END_FOR_EACH_SM(sm);
303 void register_type_val(int id)
305 if (!option_info)
306 return;
308 my_id = id;
310 add_hook(&match_assign_value, ASSIGNMENT_HOOK);
311 add_hook(&match_assign_pointer, ASSIGNMENT_HOOK);
312 add_hook(&unop_expr, OP_HOOK);
313 add_hook(&asm_expr, ASM_HOOK);
314 select_return_states_hook(ADDED_VALUE, &db_param_add);
316 add_hook(&match_inline_start, INLINE_FN_START);
317 add_hook(&match_inline_end, INLINE_FN_END);
319 add_hook(&match_end_func_info, END_FUNC_HOOK);
321 add_hook(&match_global_assign, GLOBAL_ASSIGNMENT_HOOK);
322 add_hook(&match_end_file, END_FILE_HOOK);