*new* double_checking: warn no duplicative conditons
[smatch.git] / smatch_estate.c
blob8acab844c2c2aca94980a5f34624b7afc0de0165
1 /*
2 * smatch/smatch_dinfo.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * smatch_dinfo.c has helper functions for handling data_info structs
15 #include <stdlib.h>
16 #ifndef __USE_ISOC99
17 #define __USE_ISOC99
18 #endif
19 #include <limits.h>
20 #include "parse.h"
21 #include "smatch.h"
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))
32 return s1;
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)));
43 return tmp;
46 struct data_info *get_dinfo(struct smatch_state *state)
48 if (!state)
49 return NULL;
50 return (struct data_info *)state->data;
53 struct range_list *estate_rl(struct smatch_state *state)
55 if (!state)
56 return NULL;
57 return get_dinfo(state)->value_ranges;
60 struct related_list *estate_related(struct smatch_state *state)
62 if (!state)
63 return NULL;
64 return get_dinfo(state)->related;
67 sval_t estate_get_fuzzy_max(struct smatch_state *state)
69 sval_t empty = {};
71 if (!state || !get_dinfo(state))
72 return empty;
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)
79 return 1;
80 return 0;
83 void estate_set_fuzzy_max(struct smatch_state *state, sval_t fuzzy_max)
85 if (!rl_has_sval(estate_rl(state), fuzzy_max))
86 return;
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))
93 return;
94 estate_set_fuzzy_max(new, estate_get_fuzzy_max(old));
97 void estate_clear_fuzzy_max(struct smatch_state *state)
99 sval_t empty = {};
101 get_dinfo(state)->fuzzy_max = empty;
104 int estate_has_hard_max(struct smatch_state *state)
106 if (!state)
107 return 0;
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))
124 return 0;
125 *sval = rl_max(estate_rl(state));
126 return 1;
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);
151 for (;;) {
152 if (!one_rel && !two_rel)
153 return 1;
154 if (!one_rel || !two_rel)
155 return 0;
156 if (one_rel->sym != two_rel->sym)
157 return 0;
158 if (strcmp(one_rel->name, two_rel->name))
159 return 0;
160 NEXT_PTR_LIST(one_rel);
161 NEXT_PTR_LIST(two_rel);
163 FINISH_PTR_LIST(two_rel);
164 FINISH_PTR_LIST(one_rel);
166 return 1;
169 int estates_equiv(struct smatch_state *one, struct smatch_state *two)
171 if (one == two)
172 return 1;
173 if (!rlists_equiv(estate_related(one), estate_related(two)))
174 return 0;
175 if (strcmp(one->name, two->name) == 0)
176 return 1;
177 return 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)
187 sval_t min, max;
189 min = rl_min(estate_rl(state));
190 max = rl_max(estate_rl(state));
191 if (sval_cmp(min, max) != 0)
192 return 0;
193 *sval = min;
194 return 1;
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));
203 return ret;
206 static struct data_info *alloc_dinfo_range(sval_t min, sval_t max)
208 struct data_info *ret;
210 ret = alloc_dinfo();
211 add_range(&ret->value_ranges, min, max);
212 return ret;
215 static struct data_info *alloc_dinfo_range_list(struct range_list *rl)
217 struct data_info *ret;
219 ret = alloc_dinfo();
220 ret->value_ranges = rl;
221 return ret;
224 static struct data_info *clone_dinfo(struct data_info *dinfo)
226 struct data_info *ret;
228 ret = alloc_dinfo();
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;
233 return ret;
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));
243 return ret;
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);
253 state->data = dinfo;
254 state->name = "";
255 return state;
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);
268 ret->name = "empty";
269 ret->data = alloc_dinfo();
270 return ret;
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);
282 return state;
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);
292 return state;
295 struct smatch_state *alloc_estate_rl(struct range_list *rl)
297 struct smatch_state *state;
299 if (!rl)
300 return extra_empty();
302 state = __alloc_smatch_state(0);
303 state->data = alloc_dinfo_range_list(rl);
304 state->name = show_rl(rl);
305 return state;
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);
314 if (state)
315 return state;
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;
327 if (!orig)
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));
336 return state;
339 struct smatch_state *estate_filter_sval(struct smatch_state *orig, sval_t sval)
341 return estate_filter_range(orig, sval, sval);