expressions: make gen_expression_from_key() handle *$ better
[smatch.git] / smatch_estate.c
blob7a8c896ba86af55c74a0aae6cbe4966675271fa2
1 /*
2 * Copyright (C) 2010 Dan Carpenter.
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 * smatch_dinfo.c has helper functions for handling data_info structs
23 #include <stdlib.h>
24 #ifndef __USE_ISOC99
25 #define __USE_ISOC99
26 #endif
27 #include <limits.h>
28 #include "parse.h"
29 #include "smatch.h"
30 #include "smatch_slist.h"
31 #include "smatch_extra.h"
33 struct smatch_state *merge_estates(struct smatch_state *s1, struct smatch_state *s2)
35 struct smatch_state *tmp;
36 struct range_list *value_ranges;
37 struct related_list *rlist;
38 bool capped = false;
40 if (estates_equiv(s1, s2))
41 return s1;
43 value_ranges = rl_union(estate_rl(s1), estate_rl(s2));
44 tmp = alloc_estate_rl(value_ranges);
45 rlist = get_shared_relations(estate_related(s1), estate_related(s2));
46 set_related(tmp, rlist);
48 if ((estate_has_hard_max(s1) && (!estate_rl(s2) || estate_has_hard_max(s2))) ||
49 (estate_has_hard_max(s2) && (!estate_rl(s1) || estate_has_hard_max(s1))))
50 estate_set_hard_max(tmp);
52 estate_set_fuzzy_max(tmp, sval_max(estate_get_fuzzy_max(s1), estate_get_fuzzy_max(s2)));
54 if (estate_capped(s1) && estate_capped(s2))
55 capped = true;
56 if (estate_rl(s1) && estate_rl(s2)) {
57 if (estate_capped(s1) && estate_max(s2).uvalue < 100)
58 capped = true;
59 if (estate_capped(s2) && estate_max(s1).uvalue < 100)
60 capped = true;
62 if (capped)
63 estate_set_capped(tmp);
65 if (estate_treat_untagged(s1) && estate_treat_untagged(s2))
66 estate_set_treat_untagged(tmp);
68 if (estate_assigned(s1) || estate_assigned(s2))
69 estate_set_assigned(tmp);
71 if (estate_new(s1) || estate_new(s2))
72 estate_set_new(tmp);
74 return tmp;
77 struct data_info *get_dinfo(struct smatch_state *state)
79 if (!state)
80 return NULL;
81 return (struct data_info *)state->data;
84 struct range_list *estate_rl(struct smatch_state *state)
86 if (!state)
87 return NULL;
88 return get_dinfo(state)->value_ranges;
91 struct related_list *estate_related(struct smatch_state *state)
93 if (!state)
94 return NULL;
95 return get_dinfo(state)->related;
98 sval_t estate_get_fuzzy_max(struct smatch_state *state)
100 sval_t empty = {};
102 if (!state || !get_dinfo(state))
103 return empty;
104 return get_dinfo(state)->fuzzy_max;
107 int estate_has_fuzzy_max(struct smatch_state *state)
109 if (estate_get_fuzzy_max(state).type)
110 return 1;
111 return 0;
114 void estate_set_fuzzy_max(struct smatch_state *state, sval_t fuzzy_max)
116 if (!rl_has_sval(estate_rl(state), fuzzy_max))
117 return;
118 get_dinfo(state)->fuzzy_max = fuzzy_max;
121 void estate_copy_fuzzy_max(struct smatch_state *new, struct smatch_state *old)
123 if (!estate_has_fuzzy_max(old))
124 return;
125 estate_set_fuzzy_max(new, estate_get_fuzzy_max(old));
128 void estate_clear_fuzzy_max(struct smatch_state *state)
130 sval_t empty = {};
132 get_dinfo(state)->fuzzy_max = empty;
135 int estate_has_hard_max(struct smatch_state *state)
137 if (!state || !estate_rl(state))
138 return 0;
139 return get_dinfo(state)->hard_max;
142 void estate_set_hard_max(struct smatch_state *state)
144 /* pointers don't have a hard max */
145 if (is_ptr_type(estate_type(state)))
146 return;
147 get_dinfo(state)->hard_max = 1;
150 void estate_clear_hard_max(struct smatch_state *state)
152 get_dinfo(state)->hard_max = 0;
155 int estate_get_hard_max(struct smatch_state *state, sval_t *sval)
157 if (!state || !get_dinfo(state)->hard_max || !estate_rl(state))
158 return 0;
159 *sval = rl_max(estate_rl(state));
160 return 1;
163 bool estate_capped(struct smatch_state *state)
165 if (!state)
166 return false;
167 /* impossible states are capped */
168 if (!estate_rl(state))
169 return true;
170 return get_dinfo(state)->capped;
173 void estate_set_capped(struct smatch_state *state)
175 get_dinfo(state)->capped = true;
178 bool estate_treat_untagged(struct smatch_state *state)
180 if (!state)
181 return false;
183 /* impossible states are capped */
184 if (!estate_rl(state))
185 return true;
187 return get_dinfo(state)->treat_untagged;
190 void estate_set_treat_untagged(struct smatch_state *state)
192 get_dinfo(state)->treat_untagged = true;
195 bool estate_assigned(struct smatch_state *state)
197 if (!estate_rl(state))
198 return false;
199 return get_dinfo(state)->assigned;
202 void estate_set_assigned(struct smatch_state *state)
204 get_dinfo(state)->assigned = true;
207 bool estate_new(struct smatch_state *state)
209 if (!estate_rl(state))
210 return false;
211 return get_dinfo(state)->set;
214 void estate_set_new(struct smatch_state *state)
216 get_dinfo(state)->set = true;
219 sval_t estate_min(struct smatch_state *state)
221 return rl_min(estate_rl(state));
224 sval_t estate_max(struct smatch_state *state)
226 return rl_max(estate_rl(state));
229 struct symbol *estate_type(struct smatch_state *state)
231 return rl_max(estate_rl(state)).type;
234 static int rlists_equiv(struct related_list *one, struct related_list *two)
236 struct relation *one_rel;
237 struct relation *two_rel;
239 PREPARE_PTR_LIST(one, one_rel);
240 PREPARE_PTR_LIST(two, two_rel);
241 for (;;) {
242 if (!one_rel && !two_rel)
243 return 1;
244 if (!one_rel || !two_rel)
245 return 0;
246 if (one_rel->sym != two_rel->sym)
247 return 0;
248 if (strcmp(one_rel->name, two_rel->name))
249 return 0;
250 NEXT_PTR_LIST(one_rel);
251 NEXT_PTR_LIST(two_rel);
253 FINISH_PTR_LIST(two_rel);
254 FINISH_PTR_LIST(one_rel);
256 return 1;
259 int estates_equiv(struct smatch_state *one, struct smatch_state *two)
261 if (!one || !two)
262 return 0;
263 if (one == two)
264 return 1;
265 if (!rlists_equiv(estate_related(one), estate_related(two)))
266 return 0;
267 if (estate_capped(one) != estate_capped(two))
268 return 0;
269 if (estate_treat_untagged(one) != estate_treat_untagged(two))
270 return 0;
271 if (estate_has_hard_max(one) != estate_has_hard_max(two))
272 return 0;
273 if (estate_new(one) != estate_new(two))
274 return 0;
275 if (strcmp(one->name, two->name) == 0)
276 return 1;
277 return 0;
280 int estate_is_whole(struct smatch_state *state)
282 return is_whole_rl(estate_rl(state));
285 int estate_is_empty(struct smatch_state *state)
287 return state && !estate_rl(state);
290 int estate_is_unknown(struct smatch_state *state)
292 if (!estate_is_whole(state))
293 return 0;
294 if (estate_related(state))
295 return 0;
296 if (estate_has_fuzzy_max(state))
297 return 0;
298 return 1;
301 int estate_get_single_value(struct smatch_state *state, sval_t *sval)
303 sval_t min, max;
305 if (!estate_rl(state))
306 return 0;
307 min = rl_min(estate_rl(state));
308 max = rl_max(estate_rl(state));
309 if (sval_cmp(min, max) != 0)
310 return 0;
311 *sval = min;
312 return 1;
315 static struct data_info *alloc_dinfo(void)
317 struct data_info *ret;
319 ret = __alloc_data_info(0);
320 memset(ret, 0, sizeof(*ret));
321 return ret;
324 static struct data_info *alloc_dinfo_range(sval_t min, sval_t max)
326 struct data_info *ret;
328 ret = alloc_dinfo();
329 add_range(&ret->value_ranges, min, max);
330 return ret;
333 static struct data_info *alloc_dinfo_range_list(struct range_list *rl)
335 struct data_info *ret;
337 ret = alloc_dinfo();
338 ret->value_ranges = rl;
339 return ret;
342 static struct data_info *clone_dinfo(struct data_info *dinfo)
344 struct data_info *ret;
346 ret = alloc_dinfo();
347 ret->related = clone_related_list(dinfo->related);
348 ret->value_ranges = clone_rl(dinfo->value_ranges);
349 ret->hard_max = dinfo->hard_max;
350 ret->fuzzy_max = dinfo->fuzzy_max;
351 return ret;
354 struct smatch_state *clone_estate(struct smatch_state *state)
356 struct smatch_state *ret;
358 if (!state)
359 return NULL;
361 ret = __alloc_smatch_state(0);
362 ret->name = state->name;
363 ret->data = clone_dinfo(get_dinfo(state));
364 return ret;
367 struct smatch_state *clone_partial_estate(struct smatch_state *state, struct range_list *rl)
369 struct smatch_state *ret;
371 if (!state)
372 return NULL;
374 rl = cast_rl(estate_type(state), rl);
376 ret = alloc_estate_rl(rl);
377 set_related(ret, clone_related_list(estate_related(state)));
378 if (estate_has_hard_max(state))
379 estate_set_hard_max(ret);
380 if (estate_has_fuzzy_max(state))
381 estate_set_fuzzy_max(ret, estate_get_fuzzy_max(state));
383 return ret;
386 struct smatch_state *alloc_estate_empty(void)
388 struct smatch_state *state;
389 struct data_info *dinfo;
391 dinfo = alloc_dinfo();
392 state = __alloc_smatch_state(0);
393 state->data = dinfo;
394 state->name = "";
395 return state;
398 struct smatch_state *alloc_estate_whole(struct symbol *type)
400 return alloc_estate_rl(alloc_whole_rl(type));
403 struct smatch_state *extra_empty(void)
405 struct smatch_state *ret;
407 ret = __alloc_smatch_state(0);
408 ret->name = "empty";
409 ret->data = alloc_dinfo();
410 return ret;
413 struct smatch_state *alloc_estate_sval(sval_t sval)
415 struct smatch_state *state;
417 state = __alloc_smatch_state(0);
418 state->data = alloc_dinfo_range(sval, sval);
419 state->name = show_rl(get_dinfo(state)->value_ranges);
420 estate_set_hard_max(state);
421 estate_set_fuzzy_max(state, sval);
422 return state;
425 struct smatch_state *alloc_estate_range(sval_t min, sval_t max)
427 struct smatch_state *state;
429 state = __alloc_smatch_state(0);
430 state->data = alloc_dinfo_range(min, max);
431 state->name = show_rl(get_dinfo(state)->value_ranges);
432 return state;
435 struct smatch_state *alloc_estate_rl(struct range_list *rl)
437 struct smatch_state *state;
439 if (!rl)
440 return extra_empty();
442 state = __alloc_smatch_state(0);
443 state->data = alloc_dinfo_range_list(rl);
444 state->name = show_rl(rl);
445 return state;
448 struct smatch_state *clone_estate_cast(struct symbol *type, struct smatch_state *state)
450 struct smatch_state *ret;
451 struct data_info *dinfo;
453 if (!state)
454 return NULL;
456 dinfo = alloc_dinfo();
457 dinfo->value_ranges = clone_rl(cast_rl(type, estate_rl(state)));
459 ret = __alloc_smatch_state(0);
460 ret->name = show_rl(dinfo->value_ranges);
461 ret->data = dinfo;
463 return ret;
466 struct smatch_state *get_implied_estate(struct expression *expr)
468 struct smatch_state *state;
469 struct range_list *rl;
471 state = get_state_expr(SMATCH_EXTRA, expr);
472 if (state)
473 return state;
474 if (!get_implied_rl(expr, &rl))
475 rl = alloc_whole_rl(get_type(expr));
476 return alloc_estate_rl(rl);
480 * One of the complications is that smatch tries to free a bunch of data at the
481 * end of every function.
483 struct data_info *clone_dinfo_perm(struct data_info *dinfo)
485 struct data_info *ret;
487 ret = malloc(sizeof(*ret));
488 memset(ret, 0, sizeof(*ret));
489 ret->related = NULL;
490 ret->value_ranges = clone_rl_permanent(dinfo->value_ranges);
491 ret->hard_max = 0;
492 ret->fuzzy_max = dinfo->fuzzy_max;
493 return ret;
496 struct smatch_state *clone_estate_perm(struct smatch_state *state)
498 struct smatch_state *ret;
500 ret = malloc(sizeof(*ret));
501 ret->name = alloc_string(state->name);
502 ret->data = clone_dinfo_perm(get_dinfo(state));
503 return ret;