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
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
;
39 if (estates_equiv(s1
, s2
))
42 value_ranges
= rl_union(estate_rl(s1
), estate_rl(s2
));
43 tmp
= alloc_estate_rl(value_ranges
);
44 rlist
= get_shared_relations(estate_related(s1
), estate_related(s2
));
45 set_related(tmp
, rlist
);
47 if ((estate_has_hard_max(s1
) && (!estate_rl(s2
) || estate_has_hard_max(s2
))) ||
48 (estate_has_hard_max(s2
) && (!estate_rl(s1
) || estate_has_hard_max(s1
))))
49 estate_set_hard_max(tmp
);
51 estate_set_fuzzy_max(tmp
, sval_max(estate_get_fuzzy_max(s1
), estate_get_fuzzy_max(s2
)));
56 struct data_info
*get_dinfo(struct smatch_state
*state
)
60 return (struct data_info
*)state
->data
;
63 struct range_list
*estate_rl(struct smatch_state
*state
)
67 return get_dinfo(state
)->value_ranges
;
70 struct related_list
*estate_related(struct smatch_state
*state
)
74 return get_dinfo(state
)->related
;
77 sval_t
estate_get_fuzzy_max(struct smatch_state
*state
)
81 if (!state
|| !get_dinfo(state
))
83 return get_dinfo(state
)->fuzzy_max
;
86 int estate_has_fuzzy_max(struct smatch_state
*state
)
88 if (estate_get_fuzzy_max(state
).type
)
93 void estate_set_fuzzy_max(struct smatch_state
*state
, sval_t fuzzy_max
)
95 if (!rl_has_sval(estate_rl(state
), fuzzy_max
))
97 get_dinfo(state
)->fuzzy_max
= fuzzy_max
;
100 void estate_copy_fuzzy_max(struct smatch_state
*new, struct smatch_state
*old
)
102 if (!estate_has_fuzzy_max(old
))
104 estate_set_fuzzy_max(new, estate_get_fuzzy_max(old
));
107 void estate_clear_fuzzy_max(struct smatch_state
*state
)
111 get_dinfo(state
)->fuzzy_max
= empty
;
114 int estate_has_hard_max(struct smatch_state
*state
)
118 return get_dinfo(state
)->hard_max
;
121 void estate_set_hard_max(struct smatch_state
*state
)
123 get_dinfo(state
)->hard_max
= 1;
126 void estate_clear_hard_max(struct smatch_state
*state
)
128 get_dinfo(state
)->hard_max
= 0;
131 int estate_get_hard_max(struct smatch_state
*state
, sval_t
*sval
)
133 if (!state
|| !get_dinfo(state
)->hard_max
|| !estate_rl(state
))
135 *sval
= rl_max(estate_rl(state
));
139 sval_t
estate_min(struct smatch_state
*state
)
141 return rl_min(estate_rl(state
));
144 sval_t
estate_max(struct smatch_state
*state
)
146 return rl_max(estate_rl(state
));
149 struct symbol
*estate_type(struct smatch_state
*state
)
151 return rl_max(estate_rl(state
)).type
;
154 static int rlists_equiv(struct related_list
*one
, struct related_list
*two
)
156 struct relation
*one_rel
;
157 struct relation
*two_rel
;
159 PREPARE_PTR_LIST(one
, one_rel
);
160 PREPARE_PTR_LIST(two
, two_rel
);
162 if (!one_rel
&& !two_rel
)
164 if (!one_rel
|| !two_rel
)
166 if (one_rel
->sym
!= two_rel
->sym
)
168 if (strcmp(one_rel
->name
, two_rel
->name
))
170 NEXT_PTR_LIST(one_rel
);
171 NEXT_PTR_LIST(two_rel
);
173 FINISH_PTR_LIST(two_rel
);
174 FINISH_PTR_LIST(one_rel
);
179 int estates_equiv(struct smatch_state
*one
, struct smatch_state
*two
)
185 if (!rlists_equiv(estate_related(one
), estate_related(two
)))
187 if (strcmp(one
->name
, two
->name
) == 0)
192 int estate_is_whole(struct smatch_state
*state
)
194 return is_whole_rl(estate_rl(state
));
197 int estate_is_empty(struct smatch_state
*state
)
199 return state
&& !estate_rl(state
);
202 int estate_is_unknown(struct smatch_state
*state
)
204 if (!estate_is_whole(state
))
206 if (estate_related(state
))
208 if (estate_has_fuzzy_max(state
))
213 int estate_get_single_value(struct smatch_state
*state
, sval_t
*sval
)
217 min
= rl_min(estate_rl(state
));
218 max
= rl_max(estate_rl(state
));
219 if (sval_cmp(min
, max
) != 0)
225 static struct data_info
*alloc_dinfo(void)
227 struct data_info
*ret
;
229 ret
= __alloc_data_info(0);
230 memset(ret
, 0, sizeof(*ret
));
234 static struct data_info
*alloc_dinfo_range(sval_t min
, sval_t max
)
236 struct data_info
*ret
;
239 add_range(&ret
->value_ranges
, min
, max
);
243 static struct data_info
*alloc_dinfo_range_list(struct range_list
*rl
)
245 struct data_info
*ret
;
248 ret
->value_ranges
= rl
;
252 static struct data_info
*clone_dinfo(struct data_info
*dinfo
)
254 struct data_info
*ret
;
257 ret
->related
= clone_related_list(dinfo
->related
);
258 ret
->value_ranges
= clone_rl(dinfo
->value_ranges
);
259 ret
->hard_max
= dinfo
->hard_max
;
260 ret
->fuzzy_max
= dinfo
->fuzzy_max
;
264 struct smatch_state
*clone_estate(struct smatch_state
*state
)
266 struct smatch_state
*ret
;
271 ret
= __alloc_smatch_state(0);
272 ret
->name
= state
->name
;
273 ret
->data
= clone_dinfo(get_dinfo(state
));
277 struct smatch_state
*alloc_estate_empty(void)
279 struct smatch_state
*state
;
280 struct data_info
*dinfo
;
282 dinfo
= alloc_dinfo();
283 state
= __alloc_smatch_state(0);
289 struct smatch_state
*alloc_estate_whole(struct symbol
*type
)
291 return alloc_estate_rl(alloc_whole_rl(type
));
294 struct smatch_state
*extra_empty(void)
296 struct smatch_state
*ret
;
298 ret
= __alloc_smatch_state(0);
300 ret
->data
= alloc_dinfo();
304 struct smatch_state
*alloc_estate_sval(sval_t sval
)
306 struct smatch_state
*state
;
308 state
= __alloc_smatch_state(0);
309 state
->data
= alloc_dinfo_range(sval
, sval
);
310 state
->name
= show_rl(get_dinfo(state
)->value_ranges
);
311 estate_set_hard_max(state
);
312 estate_set_fuzzy_max(state
, sval
);
316 struct smatch_state
*alloc_estate_range(sval_t min
, sval_t max
)
318 struct smatch_state
*state
;
320 state
= __alloc_smatch_state(0);
321 state
->data
= alloc_dinfo_range(min
, max
);
322 state
->name
= show_rl(get_dinfo(state
)->value_ranges
);
326 struct smatch_state
*alloc_estate_rl(struct range_list
*rl
)
328 struct smatch_state
*state
;
331 return extra_empty();
333 state
= __alloc_smatch_state(0);
334 state
->data
= alloc_dinfo_range_list(rl
);
335 state
->name
= show_rl(rl
);
339 struct smatch_state
*clone_estate_cast(struct symbol
*type
, struct smatch_state
*state
)
341 struct smatch_state
*ret
;
342 struct data_info
*dinfo
;
347 dinfo
= alloc_dinfo();
348 dinfo
->value_ranges
= clone_rl(cast_rl(type
, estate_rl(state
)));
350 ret
= __alloc_smatch_state(0);
351 ret
->name
= show_rl(dinfo
->value_ranges
);
357 struct smatch_state
*get_implied_estate(struct expression
*expr
)
359 struct smatch_state
*state
;
360 struct range_list
*rl
;
362 state
= get_state_expr(SMATCH_EXTRA
, expr
);
365 if (!get_implied_rl(expr
, &rl
))
366 rl
= alloc_whole_rl(get_type(expr
));
367 return alloc_estate_rl(rl
);
370 struct smatch_state
*estate_filter_range(struct smatch_state
*orig
,
371 sval_t filter_min
, sval_t filter_max
)
373 struct range_list
*rl
;
374 struct smatch_state
*state
;
377 orig
= alloc_estate_whole(filter_min
.type
);
379 rl
= remove_range(estate_rl(orig
), filter_min
, filter_max
);
380 state
= alloc_estate_rl(rl
);
381 if (estate_has_hard_max(orig
))
382 estate_set_hard_max(state
);
383 if (estate_has_fuzzy_max(orig
))
384 estate_set_fuzzy_max(state
, estate_get_fuzzy_max(orig
));
388 struct smatch_state
*estate_filter_sval(struct smatch_state
*orig
, sval_t sval
)
390 return estate_filter_range(orig
, sval
, sval
);
394 * One of the complications is that smatch tries to free a bunch of data at the
395 * end of every function.
397 struct data_info
*clone_dinfo_perm(struct data_info
*dinfo
)
399 struct data_info
*ret
;
401 ret
= malloc(sizeof(*ret
));
402 memset(ret
, 0, sizeof(*ret
));
404 ret
->value_ranges
= clone_rl_permanent(dinfo
->value_ranges
);
406 ret
->fuzzy_max
= dinfo
->fuzzy_max
;
410 struct smatch_state
*clone_estate_perm(struct smatch_state
*state
)
412 struct smatch_state
*ret
;
414 ret
= malloc(sizeof(*ret
));
415 ret
->name
= alloc_string(state
->name
);
416 ret
->data
= clone_dinfo_perm(get_dinfo(state
));