kernel-printf: add printf format %pra for struct range
[smatch.git] / smatch_estate.c
blob22af1fabe8a452b7c2c4fef2f9a1f5bb8eb7d160
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 (is_ptr_type(estate_type(state)))
117 return;
118 if (!rl_has_sval(estate_rl(state), fuzzy_max))
119 return;
120 get_dinfo(state)->fuzzy_max = fuzzy_max;
123 void estate_copy_fuzzy_max(struct smatch_state *new, struct smatch_state *old)
125 if (is_ptr_type(estate_type(new)))
126 return;
127 if (!estate_has_fuzzy_max(old))
128 return;
129 estate_set_fuzzy_max(new, estate_get_fuzzy_max(old));
132 void estate_clear_fuzzy_max(struct smatch_state *state)
134 sval_t empty = {};
136 get_dinfo(state)->fuzzy_max = empty;
139 int estate_has_hard_max(struct smatch_state *state)
141 if (!state || !estate_rl(state))
142 return 0;
143 return get_dinfo(state)->hard_max;
146 void estate_set_hard_max(struct smatch_state *state)
148 /* pointers don't have a hard max */
149 if (is_ptr_type(estate_type(state)))
150 return;
151 get_dinfo(state)->hard_max = 1;
154 void estate_clear_hard_max(struct smatch_state *state)
156 get_dinfo(state)->hard_max = 0;
159 int estate_get_hard_max(struct smatch_state *state, sval_t *sval)
161 if (!state || !get_dinfo(state)->hard_max || !estate_rl(state))
162 return 0;
163 *sval = rl_max(estate_rl(state));
164 return 1;
167 bool estate_capped(struct smatch_state *state)
169 if (!state)
170 return false;
171 /* impossible states are capped */
172 if (!estate_rl(state))
173 return true;
174 return get_dinfo(state)->capped;
177 void estate_set_capped(struct smatch_state *state)
179 get_dinfo(state)->capped = true;
182 bool estate_treat_untagged(struct smatch_state *state)
184 if (!state)
185 return false;
187 /* impossible states are capped */
188 if (!estate_rl(state))
189 return true;
191 return get_dinfo(state)->treat_untagged;
194 void estate_set_treat_untagged(struct smatch_state *state)
196 get_dinfo(state)->treat_untagged = true;
199 bool estate_assigned(struct smatch_state *state)
201 if (!estate_rl(state))
202 return false;
203 return get_dinfo(state)->assigned;
206 void estate_set_assigned(struct smatch_state *state)
208 get_dinfo(state)->assigned = true;
211 bool estate_new(struct smatch_state *state)
213 if (!estate_rl(state))
214 return false;
215 return get_dinfo(state)->set;
218 void estate_set_new(struct smatch_state *state)
220 get_dinfo(state)->set = true;
223 sval_t estate_min(struct smatch_state *state)
225 return rl_min(estate_rl(state));
228 sval_t estate_max(struct smatch_state *state)
230 return rl_max(estate_rl(state));
233 struct symbol *estate_type(struct smatch_state *state)
235 if (!state)
236 return NULL;
237 return rl_max(estate_rl(state)).type;
240 static int rlists_equiv(struct related_list *one, struct related_list *two)
242 struct relation *one_rel;
243 struct relation *two_rel;
245 PREPARE_PTR_LIST(one, one_rel);
246 PREPARE_PTR_LIST(two, two_rel);
247 for (;;) {
248 if (!one_rel && !two_rel)
249 return 1;
250 if (!one_rel || !two_rel)
251 return 0;
252 if (one_rel->sym != two_rel->sym)
253 return 0;
254 if (strcmp(one_rel->name, two_rel->name))
255 return 0;
256 NEXT_PTR_LIST(one_rel);
257 NEXT_PTR_LIST(two_rel);
259 FINISH_PTR_LIST(two_rel);
260 FINISH_PTR_LIST(one_rel);
262 return 1;
265 int estates_equiv(struct smatch_state *one, struct smatch_state *two)
267 if (!one || !two)
268 return 0;
269 if (one == two)
270 return 1;
271 if (!rlists_equiv(estate_related(one), estate_related(two)))
272 return 0;
273 if (estate_capped(one) != estate_capped(two))
274 return 0;
275 if (estate_treat_untagged(one) != estate_treat_untagged(two))
276 return 0;
277 if (estate_has_hard_max(one) != estate_has_hard_max(two))
278 return 0;
279 if (estate_new(one) != estate_new(two))
280 return 0;
281 if (strcmp(one->name, two->name) == 0)
282 return 1;
283 return 0;
286 int estate_is_whole(struct smatch_state *state)
288 return is_whole_rl(estate_rl(state));
291 int estate_is_empty(struct smatch_state *state)
293 return state && !estate_rl(state);
296 int estate_is_unknown(struct smatch_state *state)
298 if (!estate_is_whole(state))
299 return 0;
300 if (estate_related(state))
301 return 0;
302 if (estate_has_fuzzy_max(state))
303 return 0;
304 return 1;
307 int estate_get_single_value(struct smatch_state *state, sval_t *sval)
309 sval_t min, max;
311 if (!estate_rl(state))
312 return 0;
313 min = rl_min(estate_rl(state));
314 max = rl_max(estate_rl(state));
315 if (sval_cmp(min, max) != 0)
316 return 0;
317 *sval = min;
318 return 1;
321 static struct data_info *alloc_dinfo(void)
323 struct data_info *ret;
325 ret = __alloc_data_info(0);
326 memset(ret, 0, sizeof(*ret));
327 return ret;
330 static struct data_info *alloc_dinfo_range(sval_t min, sval_t max)
332 struct data_info *ret;
334 ret = alloc_dinfo();
335 add_range(&ret->value_ranges, min, max);
336 return ret;
339 static struct data_info *alloc_dinfo_range_list(struct range_list *rl)
341 struct data_info *ret;
343 ret = alloc_dinfo();
344 ret->value_ranges = rl;
345 return ret;
348 static struct data_info *clone_dinfo(struct data_info *dinfo)
350 struct data_info *ret;
352 ret = alloc_dinfo();
353 ret->related = clone_related_list(dinfo->related);
354 ret->value_ranges = clone_rl(dinfo->value_ranges);
355 ret->hard_max = dinfo->hard_max;
356 ret->fuzzy_max = dinfo->fuzzy_max;
357 return ret;
360 struct smatch_state *clone_estate(struct smatch_state *state)
362 struct smatch_state *ret;
364 if (!state)
365 return NULL;
367 ret = __alloc_smatch_state(0);
368 ret->name = state->name;
369 ret->data = clone_dinfo(get_dinfo(state));
370 return ret;
373 struct smatch_state *clone_partial_estate(struct smatch_state *state, struct range_list *rl)
375 struct smatch_state *ret;
377 if (!state)
378 return NULL;
380 rl = cast_rl(estate_type(state), rl);
382 ret = alloc_estate_rl(rl);
383 set_related(ret, clone_related_list(estate_related(state)));
384 if (estate_has_hard_max(state))
385 estate_set_hard_max(ret);
386 if (estate_has_fuzzy_max(state))
387 estate_set_fuzzy_max(ret, estate_get_fuzzy_max(state));
389 return ret;
392 struct smatch_state *alloc_estate_empty(void)
394 struct smatch_state *state;
395 struct data_info *dinfo;
397 dinfo = alloc_dinfo();
398 state = __alloc_smatch_state(0);
399 state->data = dinfo;
400 state->name = "";
401 return state;
404 struct smatch_state *alloc_estate_whole(struct symbol *type)
406 return alloc_estate_rl(alloc_whole_rl(type));
409 struct smatch_state *extra_empty(void)
411 struct smatch_state *ret;
413 ret = __alloc_smatch_state(0);
414 ret->name = "empty";
415 ret->data = alloc_dinfo();
416 return ret;
419 struct smatch_state *alloc_estate_sval(sval_t sval)
421 struct smatch_state *state;
423 state = __alloc_smatch_state(0);
424 state->data = alloc_dinfo_range(sval, sval);
425 state->name = show_rl(get_dinfo(state)->value_ranges);
426 estate_set_hard_max(state);
427 estate_set_fuzzy_max(state, sval);
428 return state;
431 struct smatch_state *alloc_estate_range(sval_t min, sval_t max)
433 struct smatch_state *state;
435 state = __alloc_smatch_state(0);
436 state->data = alloc_dinfo_range(min, max);
437 state->name = show_rl(get_dinfo(state)->value_ranges);
438 return state;
441 struct smatch_state *alloc_estate_rl(struct range_list *rl)
443 struct smatch_state *state;
445 if (!rl)
446 return extra_empty();
448 state = __alloc_smatch_state(0);
449 state->data = alloc_dinfo_range_list(rl);
450 state->name = show_rl(rl);
451 return state;
454 struct smatch_state *clone_estate_cast(struct symbol *type, struct smatch_state *state)
456 struct smatch_state *ret;
457 struct data_info *dinfo;
459 if (!state)
460 return NULL;
462 dinfo = alloc_dinfo();
463 dinfo->value_ranges = clone_rl(cast_rl(type, estate_rl(state)));
465 ret = __alloc_smatch_state(0);
466 ret->name = show_rl(dinfo->value_ranges);
467 ret->data = dinfo;
469 return ret;
472 struct smatch_state *get_implied_estate(struct expression *expr)
474 struct smatch_state *state;
475 struct range_list *rl;
477 state = get_state_expr(SMATCH_EXTRA, expr);
478 if (state)
479 return state;
480 if (!get_implied_rl(expr, &rl))
481 rl = alloc_whole_rl(get_type(expr));
482 return alloc_estate_rl(rl);
486 * One of the complications is that smatch tries to free a bunch of data at the
487 * end of every function.
489 struct data_info *clone_dinfo_perm(struct data_info *dinfo)
491 struct data_info *ret;
493 ret = malloc(sizeof(*ret));
494 memset(ret, 0, sizeof(*ret));
495 ret->related = NULL;
496 ret->value_ranges = clone_rl_permanent(dinfo->value_ranges);
497 ret->hard_max = 0;
498 ret->fuzzy_max = dinfo->fuzzy_max;
499 return ret;
502 struct smatch_state *clone_estate_perm(struct smatch_state *state)
504 struct smatch_state *ret;
506 ret = malloc(sizeof(*ret));
507 ret->name = alloc_string(state->name);
508 ret->data = clone_dinfo_perm(get_dinfo(state));
509 return ret;