2 * sparse/smatch_slist.c
4 * Copyright (C) 2008,2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
13 #include "smatch_slist.h"
17 ALLOCATOR(sm_state
, "smatch state");
18 ALLOCATOR(named_slist
, "named slist");
20 void __print_slist(struct state_list
*slist
)
22 struct sm_state
*state
;
24 printf("dumping slist at %d\n", get_lineno());
25 FOR_EACH_PTR(slist
, state
) {
26 printf("%d '%s'=%s\n", state
->owner
, state
->name
,
27 show_state(state
->state
));
28 } END_FOR_EACH_PTR(state
);
32 void add_history(struct sm_state
*state
)
34 struct state_history
*tmp
;
38 tmp
= malloc(sizeof(*tmp
));
39 tmp
->loc
= get_lineno();
40 add_ptr_list(&state
->line_history
, tmp
);
44 /* NULL states go at the end to simplify merge_slist */
45 int cmp_tracker(const struct sm_state
*a
, const struct sm_state
*b
)
56 if (a
->owner
> b
->owner
)
58 if (a
->owner
< b
->owner
)
61 ret
= strcmp(a
->name
, b
->name
);
65 if (!b
->sym
&& a
->sym
)
67 if (!a
->sym
&& b
->sym
)
77 static int cmp_sm_states(const struct sm_state
*a
, const struct sm_state
*b
)
81 ret
= cmp_tracker(a
, b
);
85 /* todo: add hook for smatch_extra.c */
86 if (a
->state
> b
->state
)
88 if (a
->state
< b
->state
)
93 void add_sm_state_slist(struct state_list
**slist
, struct sm_state
*new)
97 FOR_EACH_PTR(*slist
, tmp
) {
98 if (cmp_sm_states(tmp
, new) < 0)
100 else if (cmp_sm_states(tmp
, new) == 0) {
103 INSERT_CURRENT(new, tmp
);
106 } END_FOR_EACH_PTR(tmp
);
107 add_ptr_list(slist
, new);
110 static void add_possible(struct sm_state
*sm
, struct sm_state
*new)
112 struct sm_state
*tmp
;
116 struct smatch_state
*s
;
118 s
= merge_states(sm
->name
, sm
->owner
, sm
->sym
, sm
->state
, NULL
);
119 tmp
= alloc_state(sm
->name
, sm
->owner
, sm
->sym
, s
);
120 add_sm_state_slist(&sm
->possible
, tmp
);
123 FOR_EACH_PTR(new->possible
, tmp
) {
124 add_sm_state_slist(&sm
->possible
, tmp
);
125 } END_FOR_EACH_PTR(tmp
);
128 struct sm_state
*alloc_state(const char *name
, int owner
,
129 struct symbol
*sym
, struct smatch_state
*state
)
131 struct sm_state
*sm_state
= __alloc_sm_state(0);
133 sm_state
->name
= (char *)name
;
134 sm_state
->owner
= owner
;
136 sm_state
->state
= state
;
137 sm_state
->line_history
= NULL
;
138 add_history(sm_state
);
139 sm_state
->pools
= NULL
;
140 sm_state
->possible
= NULL
;
141 add_ptr_list(&sm_state
->possible
, sm_state
);
145 struct sm_state
*clone_state(struct sm_state
*s
)
147 struct sm_state
*tmp
;
149 tmp
= alloc_state(s
->name
, s
->owner
, s
->sym
, s
->state
);
150 tmp
->pools
= clone_stack(s
->pools
);
151 tmp
->possible
= s
->possible
;
155 int slist_has_state(struct state_list
*slist
, struct smatch_state
*state
)
157 struct sm_state
*tmp
;
159 FOR_EACH_PTR(slist
, tmp
) {
160 if (tmp
->state
== state
)
162 } END_FOR_EACH_PTR(tmp
);
167 static void check_order(struct state_list
*slist
)
169 struct sm_state
*state
;
170 struct sm_state
*last
= NULL
;
173 FOR_EACH_PTR(slist
, state
) {
174 if (last
&& cmp_tracker(state
, last
) <= 0) {
175 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
176 "%s vs %s\n", last
->owner
, state
->owner
,
177 last
->sym
, state
->sym
, last
->name
, state
->name
);
181 } END_FOR_EACH_PTR(state
);
188 struct state_list
*clone_slist(struct state_list
*from_slist
)
190 struct sm_state
*state
;
191 struct sm_state
*tmp
;
192 struct state_list
*to_slist
= NULL
;
194 FOR_EACH_PTR(from_slist
, state
) {
195 tmp
= clone_state(state
);
196 add_ptr_list(&to_slist
, tmp
);
197 } END_FOR_EACH_PTR(state
);
199 check_order(to_slist
);
204 struct state_list_stack
*clone_stack(struct state_list_stack
*from_stack
)
206 struct state_list
*slist
;
207 struct state_list_stack
*to_stack
= NULL
;
209 FOR_EACH_PTR(from_stack
, slist
) {
210 push_slist(&to_stack
, slist
);
211 } END_FOR_EACH_PTR(slist
);
215 // FIXME... shouldn't we free some of these state pointers?
216 struct smatch_state
*merge_states(const char *name
, int owner
,
218 struct smatch_state
*state1
,
219 struct smatch_state
*state2
)
221 struct smatch_state
*ret
;
223 if (state1
== state2
)
225 else if (__has_merge_function(owner
))
226 ret
= __client_merge_function(owner
, name
, sym
, state1
, state2
);
227 else if (!state1
|| !state2
)
235 * add_pool() adds a slist to ->pools. If the slist has already been
236 * added earlier then it doesn't get added a second time.
238 static void add_pool(struct sm_state
*to
, struct state_list
*new)
240 struct state_list
*tmp
;
242 FOR_EACH_PTR(to
->pools
, tmp
) {
245 else if (tmp
== new) {
248 INSERT_CURRENT(new, tmp
);
251 } END_FOR_EACH_PTR(tmp
);
252 add_ptr_list(&to
->pools
, new);
255 static void copy_pools(struct sm_state
*to
, struct sm_state
*sm
)
257 struct state_list
*tmp
;
262 FOR_EACH_PTR(sm
->pools
, tmp
) {
264 } END_FOR_EACH_PTR(tmp
);
267 struct sm_state
*merge_sm_states(struct sm_state
*one
, struct sm_state
*two
)
269 struct smatch_state
*s
;
270 struct sm_state
*result
;
272 s
= merge_states(one
->name
, one
->owner
, one
->sym
, one
->state
,
273 (two
?two
->state
:NULL
));
274 result
= alloc_state(one
->name
, one
->owner
, one
->sym
, s
);
275 add_possible(result
, one
);
276 add_possible(result
, two
);
277 copy_pools(result
, one
);
278 copy_pools(result
, two
);
281 struct sm_state
*tmp
;
284 printf("%d merge name='%s' owner=%d: %s + %s => %s (",
285 get_lineno(), one
->name
, one
->owner
,
286 show_state(one
->state
), show_state(two
?two
->state
:NULL
),
289 FOR_EACH_PTR(result
->possible
, tmp
) {
293 printf("%s", show_state(tmp
->state
));
294 } END_FOR_EACH_PTR(tmp
);
301 struct sm_state
*get_sm_state_slist(struct state_list
*slist
, const char *name
,
302 int owner
, struct symbol
*sym
)
304 struct sm_state
*state
;
309 FOR_EACH_PTR(slist
, state
) {
310 if (state
->owner
== owner
&& state
->sym
== sym
311 && !strcmp(state
->name
, name
))
313 } END_FOR_EACH_PTR(state
);
317 struct smatch_state
*get_state_slist(struct state_list
*slist
,
318 const char *name
, int owner
,
321 struct sm_state
*state
;
323 state
= get_sm_state_slist(slist
, name
, owner
, sym
);
329 void overwrite_sm_state(struct state_list
**slist
, struct sm_state
*state
)
331 struct sm_state
*tmp
;
332 struct sm_state
*new = clone_state(state
); //fixme. why?
334 FOR_EACH_PTR(*slist
, tmp
) {
335 if (cmp_tracker(tmp
, new) < 0)
337 else if (cmp_tracker(tmp
, new) == 0) {
338 tmp
->state
= new->state
;
339 tmp
->pools
= new->pools
;
340 tmp
->possible
= new->possible
;
341 __free_sm_state(new);
344 INSERT_CURRENT(new, tmp
);
347 } END_FOR_EACH_PTR(tmp
);
348 add_ptr_list(slist
, new);
351 void overwrite_sm_state_stack(struct state_list_stack
**stack
,
352 struct sm_state
*state
)
354 struct state_list
*slist
;
356 slist
= pop_slist(stack
);
357 overwrite_sm_state(&slist
, state
);
358 push_slist(stack
, slist
);
361 void set_state_slist(struct state_list
**slist
, const char *name
, int owner
,
362 struct symbol
*sym
, struct smatch_state
*state
)
364 struct sm_state
*tmp
;
365 struct sm_state
*new = alloc_state(name
, owner
, sym
, state
);
367 FOR_EACH_PTR(*slist
, tmp
) {
368 if (cmp_tracker(tmp
, new) < 0)
370 else if (cmp_tracker(tmp
, new) == 0) {
373 tmp
->possible
= NULL
;
374 add_ptr_list(&tmp
->possible
, tmp
);
375 __free_sm_state(new);
378 INSERT_CURRENT(new, tmp
);
381 } END_FOR_EACH_PTR(tmp
);
382 add_ptr_list(slist
, new);
385 void delete_state_slist(struct state_list
**slist
, const char *name
, int owner
,
388 struct sm_state
*state
;
390 FOR_EACH_PTR(*slist
, state
) {
391 if (state
->owner
== owner
&& state
->sym
== sym
392 && !strcmp(state
->name
, name
)){
393 delete_ptr_list_entry((struct ptr_list
**)slist
,
395 __free_sm_state(state
);
398 } END_FOR_EACH_PTR(state
);
402 void push_slist(struct state_list_stack
**list_stack
, struct state_list
*slist
)
404 add_ptr_list(list_stack
, slist
);
407 struct state_list
*pop_slist(struct state_list_stack
**list_stack
)
409 struct state_list
*slist
;
411 slist
= last_ptr_list((struct ptr_list
*)*list_stack
);
412 delete_ptr_list_last((struct ptr_list
**)list_stack
);
416 void del_slist(struct state_list
**slist
)
418 __free_ptr_list((struct ptr_list
**)slist
);
421 void del_slist_stack(struct state_list_stack
**slist_stack
)
423 struct state_list
*slist
;
425 FOR_EACH_PTR(*slist_stack
, slist
) {
426 __free_ptr_list((struct ptr_list
**)&slist
);
427 } END_FOR_EACH_PTR(slist
);
428 __free_ptr_list((struct ptr_list
**)slist_stack
);
432 * set_state_stack() sets the state for the top slist on the stack.
434 void set_state_stack(struct state_list_stack
**stack
, const char *name
,
435 int owner
, struct symbol
*sym
, struct smatch_state
*state
)
437 struct state_list
*slist
;
439 slist
= pop_slist(stack
);
440 set_state_slist(&slist
, name
, owner
, sym
, state
);
441 push_slist(stack
, slist
);
445 * get_state_stack() gets the state for the top slist on the stack.
447 struct smatch_state
*get_state_stack(struct state_list_stack
*stack
,
448 const char *name
, int owner
,
451 struct state_list
*slist
;
452 struct smatch_state
*ret
;
454 slist
= pop_slist(&stack
);
455 ret
= get_state_slist(slist
, name
, owner
, sym
);
456 push_slist(&stack
, slist
);
461 * We want to find which states have been modified inside a branch.
462 * If you have 2 &merged states they could be different states really
463 * and maybe one or both were modified. We say it is unchanged if
464 * the ->state pointers are the same and they belong to the same pools.
465 * If they have been modified on both sides of a branch to the same thing,
466 * it's still OK to say they are the same, because that means they won't
467 * belong to any pools.
469 static int is_really_same(struct sm_state
*one
, struct sm_state
*two
)
471 struct state_list
*tmp1
;
472 struct state_list
*tmp2
;
474 if (one
->state
!= two
->state
)
477 PREPARE_PTR_LIST(one
->pools
, tmp1
);
478 PREPARE_PTR_LIST(two
->pools
, tmp2
);
484 } else if (tmp1
== tmp2
) {
491 FINISH_PTR_LIST(tmp2
);
492 FINISH_PTR_LIST(tmp1
);
497 * merge_slist() is called whenever paths merge, such as after
498 * an if statement. It takes the two slists and creates one.
500 void merge_slist(struct state_list
**to
, struct state_list
*slist
)
502 struct sm_state
*to_state
, *state
, *tmp
;
503 struct state_list
*results
= NULL
;
504 struct state_list
*implied_to
= NULL
;
505 struct state_list
*implied_from
= NULL
;
512 /* merging a null and nonnull path gives you only the nonnull path */
517 *to
= clone_slist(slist
);
521 implied_to
= clone_slist(*to
);
522 implied_from
= clone_slist(slist
);
524 PREPARE_PTR_LIST(*to
, to_state
);
525 PREPARE_PTR_LIST(slist
, state
);
527 if (!to_state
&& !state
)
529 if (cmp_tracker(to_state
, state
) < 0) {
530 tmp
= merge_sm_states(to_state
, NULL
);
531 add_pool(tmp
, implied_to
);
532 add_ptr_list(&results
, tmp
);
533 NEXT_PTR_LIST(to_state
);
534 } else if (cmp_tracker(to_state
, state
) == 0) {
535 tmp
= merge_sm_states(to_state
, state
);
536 if (!is_really_same(to_state
, state
)) {
537 add_pool(tmp
, implied_to
);
538 add_pool(tmp
, implied_from
);
540 add_ptr_list(&results
, tmp
);
541 NEXT_PTR_LIST(to_state
);
542 NEXT_PTR_LIST(state
);
544 tmp
= merge_sm_states(state
, NULL
);
545 add_pool(tmp
, implied_from
);
546 add_ptr_list(&results
, tmp
);
547 NEXT_PTR_LIST(state
);
550 FINISH_PTR_LIST(state
);
551 FINISH_PTR_LIST(to_state
);
556 push_slist(&implied_pools
, implied_from
);
557 push_slist(&implied_pools
, implied_to
);
561 * is_currently_in_pool() is used because we remove states from pools.
562 * When set_state() is called then we set ->pools to NULL, but on
563 * other paths the state is still a member of those pools.
574 static int is_currently_in_pool(struct sm_state
*sm
, struct state_list
*pool
,
575 struct state_list
*cur_slist
)
577 struct sm_state
*cur_state
;
578 struct state_list
*tmp
;
580 cur_state
= get_sm_state_slist(cur_slist
, sm
->name
, sm
->owner
, sm
->sym
);
584 /* if it's the current state return false because then it's the state
585 itself, not the state in the pool. */
586 /* fixme: The above confusing comment is a load of rubbish.
587 this doesn't belong here, it belongs somewhere else. */
588 if (sm
->state
== cur_state
->state
)
592 FOR_EACH_PTR(cur_state
->pools
, tmp
) {
595 } END_FOR_EACH_PTR(tmp
);
599 struct state_list
*clone_states_in_pool(struct state_list
*pool
,
600 struct state_list
*cur_slist
)
602 struct sm_state
*state
;
603 struct sm_state
*tmp
;
604 struct state_list
*to_slist
= NULL
;
606 FOR_EACH_PTR(pool
, state
) {
607 if (state
->state
== &merged
)
609 if (is_currently_in_pool(state
, pool
, cur_slist
)) {
610 tmp
= clone_state(state
);
611 add_ptr_list(&to_slist
, tmp
);
613 } END_FOR_EACH_PTR(state
);
618 * filter() is used to find what states are the same across
619 * a series of slists.
620 * It takes a **slist and a *filter.
621 * It removes everything from **slist that isn't in *filter.
622 * The reason you would want to do this is if you want to
623 * know what other states are true if one state is true. (smatch_implied).
625 void filter(struct state_list
**slist
, struct state_list
*filter
,
626 struct state_list
*cur_slist
)
628 struct sm_state
*s_one
, *s_two
;
629 struct state_list
*results
= NULL
;
636 PREPARE_PTR_LIST(*slist
, s_one
);
637 PREPARE_PTR_LIST(filter
, s_two
);
639 if (!s_one
|| !s_two
)
641 if (cmp_tracker(s_one
, s_two
) < 0) {
642 NEXT_PTR_LIST(s_one
);
643 } else if (cmp_tracker(s_one
, s_two
) == 0) {
644 /* todo. pointer comparison works fine for most things
645 except smatch_extra. we may need a hook here. */
646 if (s_one
->state
== s_two
->state
&&
647 is_currently_in_pool(s_two
, filter
, cur_slist
)
648 && s_one
->state
!= &merged
) {
649 add_ptr_list(&results
, s_one
);
651 NEXT_PTR_LIST(s_one
);
652 NEXT_PTR_LIST(s_two
);
654 NEXT_PTR_LIST(s_two
);
657 FINISH_PTR_LIST(s_two
);
658 FINISH_PTR_LIST(s_one
);
665 * and_slist_stack() is basically the same as popping the top two slists,
666 * overwriting the one with the other and pushing it back on the stack.
667 * The difference is that it checks to see that a mutually exclusive
668 * state isn't included in both stacks. If smatch sees something like
669 * "if (a && !a)" it prints a warning.
671 void and_slist_stack(struct state_list_stack
**slist_stack
)
673 struct sm_state
*tmp
;
674 struct smatch_state
*tmp_state
;
675 struct state_list
*tmp_slist
= pop_slist(slist_stack
);
677 FOR_EACH_PTR(tmp_slist
, tmp
) {
678 tmp_state
= get_state_stack(*slist_stack
, tmp
->name
,
679 tmp
->owner
, tmp
->sym
);
680 if (tmp_state
&& tmp_state
!= tmp
->state
) {
681 smatch_msg("mutually exclusive 'and' conditions states "
683 tmp
->name
, show_state(tmp_state
),
684 show_state(tmp
->state
));
686 set_state_stack(slist_stack
, tmp
->name
, tmp
->owner
, tmp
->sym
,
688 } END_FOR_EACH_PTR(tmp
);
689 del_slist(&tmp_slist
);
693 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
694 * It pops the two slists from the top of the stack and merges them
695 * together in a way that preserves the things they have in common
696 * but creates a merged state for most of the rest.
697 * You could have code that had: if (foo || foo) { foo->baz;
698 * It's this function which ensures smatch does the right thing.
700 void or_slist_stack(struct state_list_stack
**slist_stack
)
702 struct state_list
*one
;
703 struct state_list
*two
;
704 struct state_list
*res
= NULL
;
705 struct sm_state
*tmp
;
707 struct sm_state
*new_sm
;
709 one
= pop_slist(slist_stack
);
710 two
= pop_slist(slist_stack
);
712 FOR_EACH_PTR(one
, tmp
) {
713 sm
= get_sm_state_slist(two
, tmp
->name
, tmp
->owner
, tmp
->sym
);
714 new_sm
= merge_sm_states(tmp
, sm
);
715 add_ptr_list(&res
, new_sm
);
716 } END_FOR_EACH_PTR(tmp
);
718 FOR_EACH_PTR(two
, tmp
) {
719 sm
= get_sm_state_slist(one
, tmp
->name
, tmp
->owner
, tmp
->sym
);
720 new_sm
= merge_sm_states(tmp
, sm
);
721 add_ptr_list(&res
, new_sm
);
722 } END_FOR_EACH_PTR(tmp
);
724 push_slist(slist_stack
, res
);
731 * get_slist_from_named_stack() is only used for gotos.
733 struct state_list
**get_slist_from_named_stack(struct named_stack
*stack
,
736 struct named_slist
*tmp
;
738 FOR_EACH_PTR(stack
, tmp
) {
739 if (!strcmp(tmp
->name
, name
))
741 } END_FOR_EACH_PTR(tmp
);
745 void overwrite_slist(struct state_list
*from
, struct state_list
**to
)
747 struct sm_state
*tmp
;
749 FOR_EACH_PTR(from
, tmp
) {
750 overwrite_sm_state(to
, tmp
);
751 } END_FOR_EACH_PTR(tmp
);