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
);
44 struct data_info
*get_dinfo(struct smatch_state
*state
)
48 return (struct data_info
*)state
->data
;
51 struct range_list
*estate_rl(struct smatch_state
*state
)
55 return get_dinfo(state
)->value_ranges
;
58 struct related_list
*estate_related(struct smatch_state
*state
)
62 return get_dinfo(state
)->related
;
65 int estate_has_hard_max(struct smatch_state
*state
)
69 return get_dinfo(state
)->hard_max
;
72 void estate_set_hard_max(struct smatch_state
*state
)
74 get_dinfo(state
)->hard_max
= 1;
77 void estate_clear_hard_max(struct smatch_state
*state
)
79 get_dinfo(state
)->hard_max
= 0;
82 int estate_get_hard_max(struct smatch_state
*state
, sval_t
*sval
)
84 if (!state
|| !get_dinfo(state
)->hard_max
|| !estate_rl(state
))
86 *sval
= rl_max(estate_rl(state
));
90 sval_t
estate_min(struct smatch_state
*state
)
92 return rl_min(estate_rl(state
));
95 sval_t
estate_max(struct smatch_state
*state
)
97 return rl_max(estate_rl(state
));
100 struct symbol
*estate_type(struct smatch_state
*state
)
102 return rl_max(estate_rl(state
)).type
;
105 static int rlists_equiv(struct related_list
*one
, struct related_list
*two
)
107 struct relation
*one_rel
;
108 struct relation
*two_rel
;
110 PREPARE_PTR_LIST(one
, one_rel
);
111 PREPARE_PTR_LIST(two
, two_rel
);
113 if (!one_rel
&& !two_rel
)
115 if (!one_rel
|| !two_rel
)
117 if (one_rel
->sym
!= two_rel
->sym
)
119 if (strcmp(one_rel
->name
, two_rel
->name
))
121 NEXT_PTR_LIST(one_rel
);
122 NEXT_PTR_LIST(two_rel
);
124 FINISH_PTR_LIST(two_rel
);
125 FINISH_PTR_LIST(one_rel
);
130 int estates_equiv(struct smatch_state
*one
, struct smatch_state
*two
)
134 if (!rlists_equiv(estate_related(one
), estate_related(two
)))
136 if (strcmp(one
->name
, two
->name
) == 0)
141 int estate_is_whole(struct smatch_state
*state
)
143 return is_whole_rl(estate_rl(state
));
146 int estate_get_single_value(struct smatch_state
*state
, sval_t
*sval
)
150 min
= rl_min(estate_rl(state
));
151 max
= rl_max(estate_rl(state
));
152 if (sval_cmp(min
, max
) != 0)
158 static struct data_info
*alloc_dinfo(void)
160 struct data_info
*ret
;
162 ret
= __alloc_data_info(0);
164 ret
->value_ranges
= NULL
;
169 static struct data_info
*alloc_dinfo_range(sval_t min
, sval_t max
)
171 struct data_info
*ret
;
174 add_range(&ret
->value_ranges
, min
, max
);
178 static struct data_info
*alloc_dinfo_range_list(struct range_list
*rl
)
180 struct data_info
*ret
;
183 ret
->value_ranges
= rl
;
187 static struct data_info
*clone_dinfo(struct data_info
*dinfo
)
189 struct data_info
*ret
;
192 ret
->related
= clone_related_list(dinfo
->related
);
193 ret
->value_ranges
= clone_rl(dinfo
->value_ranges
);
194 ret
->hard_max
= dinfo
->hard_max
;
198 struct smatch_state
*clone_estate(struct smatch_state
*state
)
200 struct smatch_state
*ret
;
202 ret
= __alloc_smatch_state(0);
203 ret
->name
= state
->name
;
204 ret
->data
= clone_dinfo(get_dinfo(state
));
208 struct smatch_state
*alloc_estate_empty(void)
210 struct smatch_state
*state
;
211 struct data_info
*dinfo
;
213 dinfo
= alloc_dinfo();
214 state
= __alloc_smatch_state(0);
220 struct smatch_state
*alloc_estate_whole(struct symbol
*type
)
222 return alloc_estate_rl(alloc_whole_rl(type
));
225 struct smatch_state
*extra_empty(void)
227 struct smatch_state
*ret
;
229 ret
= __alloc_smatch_state(0);
231 ret
->data
= alloc_dinfo();
235 struct smatch_state
*alloc_estate_sval(sval_t sval
)
237 struct smatch_state
*state
;
239 state
= __alloc_smatch_state(0);
240 state
->data
= alloc_dinfo_range(sval
, sval
);
241 state
->name
= show_rl(get_dinfo(state
)->value_ranges
);
242 estate_set_hard_max(state
);
246 struct smatch_state
*alloc_estate_range(sval_t min
, sval_t max
)
248 struct smatch_state
*state
;
250 state
= __alloc_smatch_state(0);
251 state
->data
= alloc_dinfo_range(min
, max
);
252 state
->name
= show_rl(get_dinfo(state
)->value_ranges
);
256 struct smatch_state
*alloc_estate_rl(struct range_list
*rl
)
258 struct smatch_state
*state
;
261 return extra_empty();
263 state
= __alloc_smatch_state(0);
264 state
->data
= alloc_dinfo_range_list(rl
);
265 state
->name
= show_rl(rl
);
269 struct smatch_state
*get_implied_estate(struct expression
*expr
)
271 struct smatch_state
*state
;
272 struct range_list
*rl
;
274 state
= get_state_expr(SMATCH_EXTRA
, expr
);
277 if (!get_implied_rl(expr
, &rl
))
278 rl
= alloc_whole_rl(get_type(expr
));
279 return alloc_estate_rl(rl
);
282 struct smatch_state
*estate_filter_range(struct smatch_state
*orig
,
283 sval_t filter_min
, sval_t filter_max
)
285 struct range_list
*rl
;
288 orig
= alloc_estate_whole(filter_min
.type
);
290 rl
= remove_range(estate_rl(orig
), filter_min
, filter_max
);
291 return alloc_estate_rl(rl
);
294 struct smatch_state
*estate_filter_sval(struct smatch_state
*orig
, sval_t sval
)
296 return estate_filter_range(orig
, sval
, sval
);