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
);
46 if (estate_has_hard_max(s1
) && estate_has_hard_max(s2
))
47 estate_set_hard_max(tmp
);
49 estate_set_fuzzy_max(tmp
, sval_max(estate_get_fuzzy_max(s1
), estate_get_fuzzy_max(s2
)));
54 struct data_info
*get_dinfo(struct smatch_state
*state
)
58 return (struct data_info
*)state
->data
;
61 struct range_list
*estate_rl(struct smatch_state
*state
)
65 return get_dinfo(state
)->value_ranges
;
68 struct related_list
*estate_related(struct smatch_state
*state
)
72 return get_dinfo(state
)->related
;
75 sval_t
estate_get_fuzzy_max(struct smatch_state
*state
)
79 if (!state
|| !get_dinfo(state
))
81 return get_dinfo(state
)->fuzzy_max
;
84 int estate_has_fuzzy_max(struct smatch_state
*state
)
86 if (estate_get_fuzzy_max(state
).type
)
91 void estate_set_fuzzy_max(struct smatch_state
*state
, sval_t fuzzy_max
)
93 if (!rl_has_sval(estate_rl(state
), fuzzy_max
))
95 get_dinfo(state
)->fuzzy_max
= fuzzy_max
;
98 void estate_copy_fuzzy_max(struct smatch_state
*new, struct smatch_state
*old
)
100 if (!estate_has_fuzzy_max(old
))
102 estate_set_fuzzy_max(new, estate_get_fuzzy_max(old
));
105 void estate_clear_fuzzy_max(struct smatch_state
*state
)
109 get_dinfo(state
)->fuzzy_max
= empty
;
112 int estate_has_hard_max(struct smatch_state
*state
)
116 return get_dinfo(state
)->hard_max
;
119 void estate_set_hard_max(struct smatch_state
*state
)
121 get_dinfo(state
)->hard_max
= 1;
124 void estate_clear_hard_max(struct smatch_state
*state
)
126 get_dinfo(state
)->hard_max
= 0;
129 int estate_get_hard_max(struct smatch_state
*state
, sval_t
*sval
)
131 if (!state
|| !get_dinfo(state
)->hard_max
|| !estate_rl(state
))
133 *sval
= rl_max(estate_rl(state
));
137 sval_t
estate_min(struct smatch_state
*state
)
139 return rl_min(estate_rl(state
));
142 sval_t
estate_max(struct smatch_state
*state
)
144 return rl_max(estate_rl(state
));
147 struct symbol
*estate_type(struct smatch_state
*state
)
149 return rl_max(estate_rl(state
)).type
;
152 static int rlists_equiv(struct related_list
*one
, struct related_list
*two
)
154 struct relation
*one_rel
;
155 struct relation
*two_rel
;
157 PREPARE_PTR_LIST(one
, one_rel
);
158 PREPARE_PTR_LIST(two
, two_rel
);
160 if (!one_rel
&& !two_rel
)
162 if (!one_rel
|| !two_rel
)
164 if (one_rel
->sym
!= two_rel
->sym
)
166 if (strcmp(one_rel
->name
, two_rel
->name
))
168 NEXT_PTR_LIST(one_rel
);
169 NEXT_PTR_LIST(two_rel
);
171 FINISH_PTR_LIST(two_rel
);
172 FINISH_PTR_LIST(one_rel
);
177 int estates_equiv(struct smatch_state
*one
, struct smatch_state
*two
)
183 if (!rlists_equiv(estate_related(one
), estate_related(two
)))
185 if (strcmp(one
->name
, two
->name
) == 0)
190 int estate_is_whole(struct smatch_state
*state
)
192 return is_whole_rl(estate_rl(state
));
195 int estate_is_unknown(struct smatch_state
*state
)
197 if (!estate_is_whole(state
))
199 if (estate_related(state
))
201 if (estate_has_fuzzy_max(state
))
206 int estate_get_single_value(struct smatch_state
*state
, sval_t
*sval
)
210 min
= rl_min(estate_rl(state
));
211 max
= rl_max(estate_rl(state
));
212 if (sval_cmp(min
, max
) != 0)
218 static struct data_info
*alloc_dinfo(void)
220 struct data_info
*ret
;
222 ret
= __alloc_data_info(0);
223 memset(ret
, 0, sizeof(*ret
));
227 static struct data_info
*alloc_dinfo_range(sval_t min
, sval_t max
)
229 struct data_info
*ret
;
232 add_range(&ret
->value_ranges
, min
, max
);
236 static struct data_info
*alloc_dinfo_range_list(struct range_list
*rl
)
238 struct data_info
*ret
;
241 ret
->value_ranges
= rl
;
245 static struct data_info
*clone_dinfo(struct data_info
*dinfo
)
247 struct data_info
*ret
;
250 ret
->related
= clone_related_list(dinfo
->related
);
251 ret
->value_ranges
= clone_rl(dinfo
->value_ranges
);
252 ret
->hard_max
= dinfo
->hard_max
;
253 ret
->fuzzy_max
= dinfo
->fuzzy_max
;
257 struct smatch_state
*clone_estate(struct smatch_state
*state
)
259 struct smatch_state
*ret
;
264 ret
= __alloc_smatch_state(0);
265 ret
->name
= state
->name
;
266 ret
->data
= clone_dinfo(get_dinfo(state
));
270 struct smatch_state
*alloc_estate_empty(void)
272 struct smatch_state
*state
;
273 struct data_info
*dinfo
;
275 dinfo
= alloc_dinfo();
276 state
= __alloc_smatch_state(0);
282 struct smatch_state
*alloc_estate_whole(struct symbol
*type
)
284 return alloc_estate_rl(alloc_whole_rl(type
));
287 struct smatch_state
*extra_empty(void)
289 struct smatch_state
*ret
;
291 ret
= __alloc_smatch_state(0);
293 ret
->data
= alloc_dinfo();
297 struct smatch_state
*alloc_estate_sval(sval_t sval
)
299 struct smatch_state
*state
;
301 state
= __alloc_smatch_state(0);
302 state
->data
= alloc_dinfo_range(sval
, sval
);
303 state
->name
= show_rl(get_dinfo(state
)->value_ranges
);
304 estate_set_hard_max(state
);
305 estate_set_fuzzy_max(state
, sval
);
309 struct smatch_state
*alloc_estate_range(sval_t min
, sval_t max
)
311 struct smatch_state
*state
;
313 state
= __alloc_smatch_state(0);
314 state
->data
= alloc_dinfo_range(min
, max
);
315 state
->name
= show_rl(get_dinfo(state
)->value_ranges
);
319 struct smatch_state
*alloc_estate_rl(struct range_list
*rl
)
321 struct smatch_state
*state
;
324 return extra_empty();
326 state
= __alloc_smatch_state(0);
327 state
->data
= alloc_dinfo_range_list(rl
);
328 state
->name
= show_rl(rl
);
332 struct smatch_state
*clone_estate_cast(struct symbol
*type
, struct smatch_state
*state
)
334 struct smatch_state
*ret
;
335 struct data_info
*dinfo
;
340 dinfo
= alloc_dinfo();
341 dinfo
->value_ranges
= clone_rl(cast_rl(type
, estate_rl(state
)));
343 ret
= __alloc_smatch_state(0);
344 ret
->name
= show_rl(dinfo
->value_ranges
);
350 struct smatch_state
*get_implied_estate(struct expression
*expr
)
352 struct smatch_state
*state
;
353 struct range_list
*rl
;
355 state
= get_state_expr(SMATCH_EXTRA
, expr
);
358 if (!get_implied_rl(expr
, &rl
))
359 rl
= alloc_whole_rl(get_type(expr
));
360 return alloc_estate_rl(rl
);
363 struct smatch_state
*estate_filter_range(struct smatch_state
*orig
,
364 sval_t filter_min
, sval_t filter_max
)
366 struct range_list
*rl
;
367 struct smatch_state
*state
;
370 orig
= alloc_estate_whole(filter_min
.type
);
372 rl
= remove_range(estate_rl(orig
), filter_min
, filter_max
);
373 state
= alloc_estate_rl(rl
);
374 if (estate_has_hard_max(orig
))
375 estate_set_hard_max(state
);
376 if (estate_has_fuzzy_max(orig
))
377 estate_set_fuzzy_max(state
, estate_get_fuzzy_max(orig
));
381 struct smatch_state
*estate_filter_sval(struct smatch_state
*orig
, sval_t sval
)
383 return estate_filter_range(orig
, sval
, sval
);
387 * One of the complications is that smatch tries to free a bunch of data at the
388 * end of every function.
390 struct data_info
*clone_dinfo_perm(struct data_info
*dinfo
)
392 struct data_info
*ret
;
394 ret
= malloc(sizeof(*ret
));
395 memset(ret
, 0, sizeof(*ret
));
397 ret
->value_ranges
= clone_rl_permanent(dinfo
->value_ranges
);
399 ret
->fuzzy_max
= dinfo
->fuzzy_max
;
403 struct smatch_state
*clone_estate_perm(struct smatch_state
*state
)
405 struct smatch_state
*ret
;
407 ret
= malloc(sizeof(*ret
));
408 ret
->name
= alloc_string(state
->name
);
409 ret
->data
= clone_dinfo_perm(get_dinfo(state
));