2 * sparse/smatch_slist.c
4 * Copyright (C) 2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
14 #include "smatch_slist.h"
16 ALLOCATOR(sm_state
, "smatch state");
17 ALLOCATOR(named_slist
, "named slist");
21 void add_history(struct sm_state
*state
)
23 struct state_history
*tmp
;
27 tmp
= malloc(sizeof(*tmp
));
28 tmp
->loc
= get_lineno();
29 add_ptr_list(&state
->line_history
, tmp
);
32 struct sm_state
*alloc_state(const char *name
, int owner
,
33 struct symbol
*sym
, struct smatch_state
*state
)
35 struct sm_state
*sm_state
= __alloc_sm_state(0);
37 sm_state
->name
= (char *)name
;
38 sm_state
->owner
= owner
;
40 sm_state
->state
= state
;
41 sm_state
->line_history
= NULL
;
42 add_history(sm_state
);
43 sm_state
->pools
= NULL
;
47 struct sm_state
*clone_state(struct sm_state
*s
)
49 return alloc_state(s
->name
, s
->owner
, s
->sym
, s
->state
);
52 /* NULL states go at the end to simplify merge_slist */
53 static int cmp_sm_states(const void *_a
, const void *_b
)
55 const struct sm_state
*a
= _a
;
56 const struct sm_state
*b
= _b
;
66 if (a
->owner
> b
->owner
)
68 if (a
->owner
< b
->owner
)
71 ret
= strcmp(a
->name
, b
->name
);
75 if (!b
->sym
&& a
->sym
)
77 if (!a
->sym
&& b
->sym
)
88 static void check_order(struct state_list
*slist
)
90 struct sm_state
*state
;
91 struct sm_state
*last
= NULL
;
94 FOR_EACH_PTR(slist
, state
) {
95 if (last
&& cmp_sm_states(state
, last
) <= 0) {
96 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
97 "%s vs %s\n", last
->owner
, state
->owner
,
98 last
->sym
, state
->sym
, last
->name
, state
->name
);
102 } END_FOR_EACH_PTR(state
);
109 struct state_list
*clone_slist(struct state_list
*from_slist
)
111 struct sm_state
*state
;
112 struct sm_state
*tmp
;
113 struct state_list
*to_slist
= NULL
;
115 FOR_EACH_PTR(from_slist
, state
) {
116 tmp
= clone_state(state
);
117 add_ptr_list(&to_slist
, tmp
);
118 } END_FOR_EACH_PTR(state
);
120 check_order(to_slist
);
125 // FIXME... shouldn't we free some of these state pointers?
126 struct smatch_state
*merge_states(const char *name
, int owner
,
128 struct smatch_state
*state1
,
129 struct smatch_state
*state2
)
131 struct smatch_state
*ret
;
133 if (state1
== state2
)
135 else if (__has_merge_function(owner
))
136 ret
= __client_merge_function(owner
, name
, sym
, state1
, state2
);
140 SM_DEBUG("%d merge name='%s' owner=%d: %s + %s => %s\n",
141 get_lineno(), name
, owner
, show_state(state1
),
142 show_state(state2
), show_state(ret
));
147 struct smatch_state
*get_state_slist(struct state_list
*slist
, const char *name
, int owner
,
150 struct sm_state
*state
;
155 FOR_EACH_PTR(slist
, state
) {
156 if (state
->owner
== owner
&& state
->sym
== sym
157 && !strcmp(state
->name
, name
))
159 } END_FOR_EACH_PTR(state
);
163 struct sm_state
*get_sm_state_slist(struct state_list
*slist
, const char *name
, int owner
,
166 struct sm_state
*state
;
171 FOR_EACH_PTR(slist
, state
) {
172 if (state
->owner
== owner
&& state
->sym
== sym
173 && !strcmp(state
->name
, name
))
175 } END_FOR_EACH_PTR(state
);
179 void set_state_slist(struct state_list
**slist
, const char *name
, int owner
,
180 struct symbol
*sym
, struct smatch_state
*state
)
182 struct sm_state
*tmp
;
183 struct sm_state
*new = alloc_state(name
, owner
, sym
, state
);
185 FOR_EACH_PTR(*slist
, tmp
) {
186 if (cmp_sm_states(tmp
, new) < 0)
188 else if (cmp_sm_states(tmp
, new) == 0) {
189 __free_sm_state(new);
193 INSERT_CURRENT(new, tmp
);
196 } END_FOR_EACH_PTR(tmp
);
197 add_ptr_list(slist
, new);
200 void delete_state_slist(struct state_list
**slist
, const char *name
, int owner
,
203 struct sm_state
*state
;
205 FOR_EACH_PTR(*slist
, state
) {
206 if (state
->owner
== owner
&& state
->sym
== sym
207 && !strcmp(state
->name
, name
)){
208 delete_ptr_list_entry((struct ptr_list
**)slist
,
210 __free_sm_state(state
);
213 } END_FOR_EACH_PTR(state
);
217 void push_slist(struct state_list_stack
**list_stack
, struct state_list
*slist
)
219 add_ptr_list(list_stack
, slist
);
222 struct state_list
*pop_slist(struct state_list_stack
**list_stack
)
224 struct state_list
*slist
;
226 slist
= last_ptr_list((struct ptr_list
*)*list_stack
);
227 delete_ptr_list_last((struct ptr_list
**)list_stack
);
231 void del_slist(struct state_list
**slist
)
233 __free_ptr_list((struct ptr_list
**)slist
);
236 void del_slist_stack(struct state_list_stack
**slist_stack
)
238 struct state_list
*slist
;
240 FOR_EACH_PTR(*slist_stack
, slist
) {
241 __free_ptr_list((struct ptr_list
**)&slist
);
242 } END_FOR_EACH_PTR(slist
);
243 __free_ptr_list((struct ptr_list
**)slist_stack
);
247 * set_state_stack() sets the state for the top slist on the stack.
249 void set_state_stack(struct state_list_stack
**stack
, const char *name
,
250 int owner
, struct symbol
*sym
, struct smatch_state
*state
)
252 struct state_list
*slist
;
254 slist
= pop_slist(stack
);
255 set_state_slist(&slist
, name
, owner
, sym
, state
);
256 push_slist(stack
, slist
);
260 * get_state_stack() gets the state for the top slist on the stack.
262 struct smatch_state
*get_state_stack(struct state_list_stack
*stack
,
263 const char *name
, int owner
,
266 struct state_list
*slist
;
267 struct smatch_state
*ret
;
269 slist
= pop_slist(&stack
);
270 ret
= get_state_slist(slist
, name
, owner
, sym
);
271 push_slist(&stack
, slist
);
275 static void add_pool(struct sm_state
*state
, struct state_list
**pool
)
277 add_ptr_list(pool
, state
);
278 push_slist(&state
->pools
, *pool
);
281 void merge_slist(struct state_list
**to
, struct state_list
*slist
)
283 struct sm_state
*to_state
, *state
, *tmp
;
284 struct state_list
**results
;
285 struct smatch_state
*s
;
286 struct state_list
*implied_to
= NULL
;
287 struct state_list
*implied_from
= NULL
;
294 /* merging a null and nonnull path gives you only the nonnull path */
299 *to
= clone_slist(slist
);
303 results
= malloc(sizeof(*results
));
306 PREPARE_PTR_LIST(*to
, to_state
);
307 PREPARE_PTR_LIST(slist
, state
);
309 if (!to_state
&& !state
)
311 if (cmp_sm_states(to_state
, state
) < 0) {
312 s
= merge_states(to_state
->name
, to_state
->owner
,
313 to_state
->sym
, to_state
->state
, NULL
);
314 tmp
= alloc_state(to_state
->name
, to_state
->owner
,
316 add_ptr_list(results
, tmp
);
317 add_pool(to_state
, &implied_to
);
318 NEXT_PTR_LIST(to_state
);
319 } else if (cmp_sm_states(to_state
, state
) == 0) {
320 if (to_state
->state
== state
->state
) {
323 s
= merge_states(to_state
->name
,
325 to_state
->sym
, to_state
->state
,
327 add_pool(to_state
, &implied_to
);
328 add_pool(state
, &implied_from
);
330 tmp
= alloc_state(to_state
->name
, to_state
->owner
,
332 add_ptr_list(results
, tmp
);
333 NEXT_PTR_LIST(to_state
);
334 NEXT_PTR_LIST(state
);
336 s
= merge_states(state
->name
, state
->owner
,
337 state
->sym
, state
->state
, NULL
);
338 tmp
= alloc_state(state
->name
, state
->owner
,
340 add_ptr_list(results
, tmp
);
341 add_pool(state
, &implied_from
);
342 NEXT_PTR_LIST(state
);
345 FINISH_PTR_LIST(state
);
346 FINISH_PTR_LIST(to_state
);
352 push_slist(&implied_pools
, implied_from
);
354 push_slist(&implied_pools
, implied_to
);
357 void filter(struct state_list
**slist
, struct state_list
*filter
)
359 struct sm_state
*s_one
, *s_two
;
360 struct state_list
**results
;
367 results
= malloc(sizeof(*results
));
370 PREPARE_PTR_LIST(*slist
, s_one
);
371 PREPARE_PTR_LIST(filter
, s_two
);
373 if (!s_one
|| !s_two
)
375 if (cmp_sm_states(s_one
, s_two
) < 0) {
376 SM_DEBUG("different missing in new %s\n", s_one
->name
);
377 NEXT_PTR_LIST(s_one
);
378 } else if (cmp_sm_states(s_one
, s_two
) == 0) {
379 if (s_one
->state
== s_two
->state
) {
380 add_ptr_list(results
, s_one
);
381 SM_DEBUG("same %s\n", s_one
->name
);
383 SM_DEBUG("different %s %s vs %s\n", s_one
->name
,
384 show_state(s_one
->state
),
385 show_state(s_two
->state
));
386 NEXT_PTR_LIST(s_one
);
387 NEXT_PTR_LIST(s_two
);
389 SM_DEBUG("different missing in old%s\n", s_two
->name
);
390 NEXT_PTR_LIST(s_two
);
393 FINISH_PTR_LIST(s_two
);
394 FINISH_PTR_LIST(s_one
);
401 * This function is basically the same as popping the top two slists,
402 * overwriting the one with the other and pushing it back on the stack.
403 * The difference is that it checks to see that a mutually exclusive
404 * state isn't included in both stacks.
407 void and_slist_stack(struct state_list_stack
**slist_stack
)
409 struct sm_state
*tmp
;
410 struct smatch_state
*tmp_state
;
411 struct state_list
*tmp_slist
= pop_slist(slist_stack
);
413 FOR_EACH_PTR(tmp_slist
, tmp
) {
414 tmp_state
= get_state_stack(*slist_stack
, tmp
->name
,
415 tmp
->owner
, tmp
->sym
);
416 if (tmp_state
&& tmp_state
!= tmp
->state
) {
417 smatch_msg("mutually exclusive 'and' conditions states "
419 tmp
->name
, show_state(tmp_state
),
420 show_state(tmp
->state
));
421 tmp
->state
= merge_states(tmp
->name
, tmp
->owner
,
422 tmp
->sym
, tmp
->state
,
425 set_state_stack(slist_stack
, tmp
->name
, tmp
->owner
, tmp
->sym
,
428 } END_FOR_EACH_PTR(tmp
);
429 del_slist(&tmp_slist
);
432 void or_slist_stack(struct state_list_stack
**slist_stack
)
434 struct state_list
*one
;
435 struct state_list
*two
;
436 struct state_list
*res
= NULL
;
437 struct sm_state
*tmp
;
438 struct smatch_state
*s
;
440 one
= pop_slist(slist_stack
);
441 two
= pop_slist(slist_stack
);
443 FOR_EACH_PTR(one
, tmp
) {
444 s
= get_state_slist(two
, tmp
->name
, tmp
->owner
, tmp
->sym
);
445 s
= merge_states(tmp
->name
, tmp
->owner
, tmp
->sym
,
447 set_state_slist(&res
, tmp
->name
, tmp
->owner
, tmp
->sym
, s
);
448 } END_FOR_EACH_PTR(tmp
);
451 FOR_EACH_PTR(two
, tmp
) {
452 s
= get_state_slist(one
, tmp
->name
, tmp
->owner
, tmp
->sym
);
453 s
= merge_states(tmp
->name
, tmp
->owner
, tmp
->sym
,
455 set_state_slist(&res
, tmp
->name
, tmp
->owner
, tmp
->sym
, s
);
456 } END_FOR_EACH_PTR(tmp
);
458 push_slist(slist_stack
, res
);
464 struct state_list
**get_slist_from_slist_stack(struct slist_stack
*stack
,
467 struct named_slist
*tmp
;
469 FOR_EACH_PTR(stack
, tmp
) {
470 if (!strcmp(tmp
->name
, name
))
472 } END_FOR_EACH_PTR(tmp
);
476 void overwrite_slist(struct state_list
*from
, struct state_list
**to
)
478 struct sm_state
*tmp
;
480 FOR_EACH_PTR(from
, tmp
) {
481 set_state_slist(to
, tmp
->name
, tmp
->owner
, tmp
->sym
, tmp
->state
);
482 } END_FOR_EACH_PTR(tmp
);