2 * smatch/smatch_dinfo.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * smatch_dinfo.c has helper functions for handling data_info structs
22 #include "smatch_slist.h"
23 #include "smatch_extra.h"
25 struct smatch_state
*merge_estates(struct smatch_state
*s1
, struct smatch_state
*s2
)
27 struct smatch_state
*tmp
;
28 struct range_list
*value_ranges
;
29 struct related_list
*rlist
;
31 if (estates_equiv(s1
, s2
))
34 value_ranges
= rl_union(estate_rl(s1
), estate_rl(s2
));
35 tmp
= alloc_estate_rl(value_ranges
);
36 rlist
= get_shared_relations(estate_related(s1
), estate_related(s2
));
37 set_related(tmp
, rlist
);
38 if (estate_has_hard_max(s1
) && estate_has_hard_max(s2
))
39 estate_set_hard_max(tmp
);
41 estate_set_fuzzy_max(tmp
, sval_max(estate_get_fuzzy_max(s1
), estate_get_fuzzy_max(s2
)));
46 struct data_info
*get_dinfo(struct smatch_state
*state
)
50 return (struct data_info
*)state
->data
;
53 struct range_list
*estate_rl(struct smatch_state
*state
)
57 return get_dinfo(state
)->value_ranges
;
60 struct related_list
*estate_related(struct smatch_state
*state
)
64 return get_dinfo(state
)->related
;
67 sval_t
estate_get_fuzzy_max(struct smatch_state
*state
)
71 if (!state
|| !get_dinfo(state
))
73 return get_dinfo(state
)->fuzzy_max
;
76 int estate_has_fuzzy_max(struct smatch_state
*state
)
78 if (estate_get_fuzzy_max(state
).type
)
83 void estate_set_fuzzy_max(struct smatch_state
*state
, sval_t fuzzy_max
)
85 if (!rl_has_sval(estate_rl(state
), fuzzy_max
))
87 get_dinfo(state
)->fuzzy_max
= fuzzy_max
;
90 void estate_copy_fuzzy_max(struct smatch_state
*new, struct smatch_state
*old
)
92 if (!estate_has_fuzzy_max(old
))
94 estate_set_fuzzy_max(new, estate_get_fuzzy_max(old
));
97 void estate_clear_fuzzy_max(struct smatch_state
*state
)
101 get_dinfo(state
)->fuzzy_max
= empty
;
104 int estate_has_hard_max(struct smatch_state
*state
)
108 return get_dinfo(state
)->hard_max
;
111 void estate_set_hard_max(struct smatch_state
*state
)
113 get_dinfo(state
)->hard_max
= 1;
116 void estate_clear_hard_max(struct smatch_state
*state
)
118 get_dinfo(state
)->hard_max
= 0;
121 int estate_get_hard_max(struct smatch_state
*state
, sval_t
*sval
)
123 if (!state
|| !get_dinfo(state
)->hard_max
|| !estate_rl(state
))
125 *sval
= rl_max(estate_rl(state
));
129 sval_t
estate_min(struct smatch_state
*state
)
131 return rl_min(estate_rl(state
));
134 sval_t
estate_max(struct smatch_state
*state
)
136 return rl_max(estate_rl(state
));
139 struct symbol
*estate_type(struct smatch_state
*state
)
141 return rl_max(estate_rl(state
)).type
;
144 static int rlists_equiv(struct related_list
*one
, struct related_list
*two
)
146 struct relation
*one_rel
;
147 struct relation
*two_rel
;
149 PREPARE_PTR_LIST(one
, one_rel
);
150 PREPARE_PTR_LIST(two
, two_rel
);
152 if (!one_rel
&& !two_rel
)
154 if (!one_rel
|| !two_rel
)
156 if (one_rel
->sym
!= two_rel
->sym
)
158 if (strcmp(one_rel
->name
, two_rel
->name
))
160 NEXT_PTR_LIST(one_rel
);
161 NEXT_PTR_LIST(two_rel
);
163 FINISH_PTR_LIST(two_rel
);
164 FINISH_PTR_LIST(one_rel
);
169 int estates_equiv(struct smatch_state
*one
, struct smatch_state
*two
)
173 if (!rlists_equiv(estate_related(one
), estate_related(two
)))
175 if (strcmp(one
->name
, two
->name
) == 0)
180 int estate_is_whole(struct smatch_state
*state
)
182 return is_whole_rl(estate_rl(state
));
185 int estate_get_single_value(struct smatch_state
*state
, sval_t
*sval
)
189 min
= rl_min(estate_rl(state
));
190 max
= rl_max(estate_rl(state
));
191 if (sval_cmp(min
, max
) != 0)
197 static struct data_info
*alloc_dinfo(void)
199 struct data_info
*ret
;
201 ret
= __alloc_data_info(0);
202 memset(ret
, 0, sizeof(*ret
));
206 static struct data_info
*alloc_dinfo_range(sval_t min
, sval_t max
)
208 struct data_info
*ret
;
211 add_range(&ret
->value_ranges
, min
, max
);
215 static struct data_info
*alloc_dinfo_range_list(struct range_list
*rl
)
217 struct data_info
*ret
;
220 ret
->value_ranges
= rl
;
224 static struct data_info
*clone_dinfo(struct data_info
*dinfo
)
226 struct data_info
*ret
;
229 ret
->related
= clone_related_list(dinfo
->related
);
230 ret
->value_ranges
= clone_rl(dinfo
->value_ranges
);
231 ret
->hard_max
= dinfo
->hard_max
;
232 ret
->fuzzy_max
= dinfo
->fuzzy_max
;
236 struct smatch_state
*clone_estate(struct smatch_state
*state
)
238 struct smatch_state
*ret
;
240 ret
= __alloc_smatch_state(0);
241 ret
->name
= state
->name
;
242 ret
->data
= clone_dinfo(get_dinfo(state
));
246 struct smatch_state
*alloc_estate_empty(void)
248 struct smatch_state
*state
;
249 struct data_info
*dinfo
;
251 dinfo
= alloc_dinfo();
252 state
= __alloc_smatch_state(0);
258 struct smatch_state
*alloc_estate_whole(struct symbol
*type
)
260 return alloc_estate_rl(alloc_whole_rl(type
));
263 struct smatch_state
*extra_empty(void)
265 struct smatch_state
*ret
;
267 ret
= __alloc_smatch_state(0);
269 ret
->data
= alloc_dinfo();
273 struct smatch_state
*alloc_estate_sval(sval_t sval
)
275 struct smatch_state
*state
;
277 state
= __alloc_smatch_state(0);
278 state
->data
= alloc_dinfo_range(sval
, sval
);
279 state
->name
= show_rl(get_dinfo(state
)->value_ranges
);
280 estate_set_hard_max(state
);
281 estate_set_fuzzy_max(state
, sval
);
285 struct smatch_state
*alloc_estate_range(sval_t min
, sval_t max
)
287 struct smatch_state
*state
;
289 state
= __alloc_smatch_state(0);
290 state
->data
= alloc_dinfo_range(min
, max
);
291 state
->name
= show_rl(get_dinfo(state
)->value_ranges
);
295 struct smatch_state
*alloc_estate_rl(struct range_list
*rl
)
297 struct smatch_state
*state
;
300 return extra_empty();
302 state
= __alloc_smatch_state(0);
303 state
->data
= alloc_dinfo_range_list(rl
);
304 state
->name
= show_rl(rl
);
308 struct smatch_state
*get_implied_estate(struct expression
*expr
)
310 struct smatch_state
*state
;
311 struct range_list
*rl
;
313 state
= get_state_expr(SMATCH_EXTRA
, expr
);
316 if (!get_implied_rl(expr
, &rl
))
317 rl
= alloc_whole_rl(get_type(expr
));
318 return alloc_estate_rl(rl
);
321 struct smatch_state
*estate_filter_range(struct smatch_state
*orig
,
322 sval_t filter_min
, sval_t filter_max
)
324 struct range_list
*rl
;
325 struct smatch_state
*state
;
328 orig
= alloc_estate_whole(filter_min
.type
);
330 rl
= remove_range(estate_rl(orig
), filter_min
, filter_max
);
331 state
= alloc_estate_rl(rl
);
332 if (estate_has_hard_max(orig
))
333 estate_set_hard_max(state
);
334 if (estate_has_fuzzy_max(orig
))
335 estate_set_fuzzy_max(state
, estate_get_fuzzy_max(orig
));
339 struct smatch_state
*estate_filter_sval(struct smatch_state
*orig
, sval_t sval
)
341 return estate_filter_range(orig
, sval
, sval
);