Remove unneeded allocation.
[smatch.git] / smatch_slist.c
blobd0dba3ba479ccdfc6f25caef69b66da1847245d0
1 /*
2 * sparse/smatch_slist.c
4 * Copyright (C) 2008,2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include "smatch.h"
13 #include "smatch_slist.h"
15 #undef CHECKORDER
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;
23 struct sm_state *poss;
24 int i;
26 printf("dumping slist at %d\n", get_lineno());
27 FOR_EACH_PTR(slist, state) {
28 printf("%d '%s'=%s (", state->owner, state->name,
29 show_state(state->state));
30 i = 0;
31 FOR_EACH_PTR(state->possible, poss) {
32 if (i++)
33 printf(", ");
34 printf("%s", show_state(poss->state));
35 } END_FOR_EACH_PTR(poss);
36 printf(")\n");
37 } END_FOR_EACH_PTR(state);
38 printf("---\n");
42 /* NULL states go at the end to simplify merge_slist */
43 int cmp_tracker(const struct sm_state *a, const struct sm_state *b)
45 int ret;
47 if (!a && !b)
48 return 0;
49 if (!b)
50 return -1;
51 if (!a)
52 return 1;
54 if (a->owner > b->owner)
55 return -1;
56 if (a->owner < b->owner)
57 return 1;
59 ret = strcmp(a->name, b->name);
60 if (ret)
61 return ret;
63 if (!b->sym && a->sym)
64 return -1;
65 if (!a->sym && b->sym)
66 return 1;
67 if (a->sym > b->sym)
68 return -1;
69 if (a->sym < b->sym)
70 return 1;
72 return 0;
75 static int cmp_sm_states(const struct sm_state *a, const struct sm_state *b)
77 int ret;
79 ret = cmp_tracker(a, b);
80 if (ret)
81 return ret;
83 /* todo: add hook for smatch_extra.c */
84 if (a->state > b->state)
85 return -1;
86 if (a->state < b->state)
87 return 1;
88 return 0;
91 void add_sm_state_slist(struct state_list **slist, struct sm_state *new)
93 struct sm_state *tmp;
95 FOR_EACH_PTR(*slist, tmp) {
96 if (cmp_sm_states(tmp, new) < 0)
97 continue;
98 else if (cmp_sm_states(tmp, new) == 0) {
99 return;
100 } else {
101 INSERT_CURRENT(new, tmp);
102 return;
104 } END_FOR_EACH_PTR(tmp);
105 add_ptr_list(slist, new);
108 static void add_possible(struct sm_state *sm, struct sm_state *new)
110 struct sm_state *tmp;
111 struct sm_state *tmp2;
113 if (!new) {
114 struct smatch_state *s;
116 s = merge_states(sm->name, sm->owner, sm->sym, sm->state, NULL);
117 tmp = alloc_state(sm->name, sm->owner, sm->sym, s);
118 add_sm_state_slist(&sm->possible, tmp);
119 return;
122 FOR_EACH_PTR(new->possible, tmp) {
123 tmp2 = alloc_state(tmp->name, tmp->owner, tmp->sym, tmp->state);
124 add_sm_state_slist(&sm->possible, tmp2);
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;
135 sm_state->sym = sym;
136 sm_state->state = state;
137 sm_state->my_pools = NULL;
138 sm_state->all_pools = NULL;
139 sm_state->possible = NULL;
140 add_ptr_list(&sm_state->possible, sm_state);
141 return sm_state;
144 /* At the end of every function we free all the sm_states */
145 void free_every_single_sm_state()
147 struct allocator_struct *desc = &sm_state_allocator;
148 struct allocation_blob *blob = desc->blobs;
150 desc->blobs = NULL;
151 desc->allocations = 0;
152 desc->total_bytes = 0;
153 desc->useful_bytes = 0;
154 desc->freelist = NULL;
155 while (blob) {
156 struct allocation_blob *next = blob->next;
157 struct sm_state *sm = (struct sm_state *)blob->data;
159 free_slist(&sm->possible);
160 free_stack(&sm->my_pools);
161 free_stack(&sm->all_pools);
162 blob_free(blob, desc->chunking);
163 blob = next;
167 struct sm_state *clone_state(struct sm_state *s)
169 struct sm_state *ret;
170 struct sm_state *poss;
172 ret = alloc_state(s->name, s->owner, s->sym, s->state);
173 ret->my_pools = clone_stack(s->my_pools);
174 ret->all_pools = clone_stack(s->all_pools);
175 FOR_EACH_PTR(s->possible, poss) {
176 add_sm_state_slist(&ret->possible, poss);
177 } END_FOR_EACH_PTR(poss);
178 return ret;
181 int slist_has_state(struct state_list *slist, struct smatch_state *state)
183 struct sm_state *tmp;
185 FOR_EACH_PTR(slist, tmp) {
186 if (tmp->state == state)
187 return 1;
188 } END_FOR_EACH_PTR(tmp);
189 return 0;
192 #ifdef CHECKORDER
193 static void check_order(struct state_list *slist)
195 struct sm_state *state;
196 struct sm_state *last = NULL;
197 int printed = 0;
199 FOR_EACH_PTR(slist, state) {
200 if (last && cmp_tracker(state, last) <= 0) {
201 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
202 "%s vs %s\n", last->owner, state->owner,
203 last->sym, state->sym, last->name, state->name);
204 printed = 1;
206 last = state;
207 } END_FOR_EACH_PTR(state);
209 if (printed)
210 printf("======\n");
212 #endif
214 struct state_list *clone_slist(struct state_list *from_slist)
216 struct sm_state *state;
217 struct sm_state *tmp;
218 struct state_list *to_slist = NULL;
220 FOR_EACH_PTR(from_slist, state) {
221 tmp = clone_state(state);
222 add_ptr_list(&to_slist, tmp);
223 } END_FOR_EACH_PTR(state);
224 #ifdef CHECKORDER
225 check_order(to_slist);
226 #endif
227 return to_slist;
230 struct state_list_stack *clone_stack(struct state_list_stack *from_stack)
232 struct state_list *slist;
233 struct state_list_stack *to_stack = NULL;
235 FOR_EACH_PTR(from_stack, slist) {
236 push_slist(&to_stack, slist);
237 } END_FOR_EACH_PTR(slist);
238 return to_stack;
241 struct smatch_state *merge_states(const char *name, int owner,
242 struct symbol *sym,
243 struct smatch_state *state1,
244 struct smatch_state *state2)
246 struct smatch_state *ret;
248 if (state1 == state2)
249 ret = state1;
250 else if (__has_merge_function(owner))
251 ret = __client_merge_function(owner, name, sym, state1, state2);
252 else if (!state1 || !state2)
253 ret = &undefined;
254 else
255 ret = &merged;
256 return ret;
260 * add_pool() adds a slist to ->pools. If the slist has already been
261 * added earlier then it doesn't get added a second time.
263 static void add_pool(struct state_list_stack **pools, struct state_list *new)
265 struct state_list *tmp;
267 FOR_EACH_PTR(*pools, tmp) {
268 if (tmp < new)
269 continue;
270 else if (tmp == new) {
271 return;
272 } else {
273 INSERT_CURRENT(new, tmp);
274 return;
276 } END_FOR_EACH_PTR(tmp);
277 add_ptr_list(pools, new);
280 static void copy_pools(struct sm_state *to, struct sm_state *sm)
282 struct state_list *tmp;
284 if (!sm)
285 return;
287 FOR_EACH_PTR(sm->my_pools, tmp) {
288 add_pool(&to->my_pools, tmp);
289 } END_FOR_EACH_PTR(tmp);
291 FOR_EACH_PTR(sm->all_pools, tmp) {
292 add_pool(&to->all_pools, tmp);
293 } END_FOR_EACH_PTR(tmp);
296 struct sm_state *merge_sm_states(struct sm_state *one, struct sm_state *two)
298 struct smatch_state *s;
299 struct sm_state *result;
301 s = merge_states(one->name, one->owner, one->sym, one->state,
302 (two?two->state:NULL));
303 result = alloc_state(one->name, one->owner, one->sym, s);
304 add_possible(result, one);
305 add_possible(result, two);
306 copy_pools(result, one);
307 copy_pools(result, two);
309 if (debug_states) {
310 struct sm_state *tmp;
311 int i = 0;
313 printf("%d merge name='%s' owner=%d: %s + %s => %s (",
314 get_lineno(), one->name, one->owner,
315 show_state(one->state), show_state(two?two->state:NULL),
316 show_state(s));
318 FOR_EACH_PTR(result->possible, tmp) {
319 if (i++) {
320 printf(", ");
322 printf("%s", show_state(tmp->state));
323 } END_FOR_EACH_PTR(tmp);
324 printf(")\n");
327 return result;
330 struct sm_state *get_sm_state_slist(struct state_list *slist, const char *name,
331 int owner, struct symbol *sym)
333 struct sm_state *state;
335 if (!name)
336 return NULL;
338 FOR_EACH_PTR(slist, state) {
339 if (state->owner == owner && state->sym == sym
340 && !strcmp(state->name, name))
341 return state;
342 } END_FOR_EACH_PTR(state);
343 return NULL;
346 struct smatch_state *get_state_slist(struct state_list *slist,
347 const char *name, int owner,
348 struct symbol *sym)
350 struct sm_state *state;
352 state = get_sm_state_slist(slist, name, owner, sym);
353 if (state)
354 return state->state;
355 return NULL;
358 void overwrite_sm_state(struct state_list **slist, struct sm_state *new)
360 struct sm_state *tmp;
362 FOR_EACH_PTR(*slist, tmp) {
363 if (cmp_tracker(tmp, new) < 0)
364 continue;
365 else if (cmp_tracker(tmp, new) == 0) {
366 REPLACE_CURRENT_PTR(tmp, new);
367 return;
368 } else {
369 INSERT_CURRENT(new, tmp);
370 return;
372 } END_FOR_EACH_PTR(tmp);
373 add_ptr_list(slist, new);
376 void overwrite_sm_state_stack(struct state_list_stack **stack,
377 struct sm_state *state)
379 struct state_list *slist;
381 slist = pop_slist(stack);
382 overwrite_sm_state(&slist, state);
383 push_slist(stack, slist);
386 void set_state_slist(struct state_list **slist, const char *name, int owner,
387 struct symbol *sym, struct smatch_state *state)
389 struct sm_state *tmp;
390 struct sm_state *new = alloc_state(name, owner, sym, state);
392 FOR_EACH_PTR(*slist, tmp) {
393 if (cmp_tracker(tmp, new) < 0)
394 continue;
395 else if (cmp_tracker(tmp, new) == 0) {
396 REPLACE_CURRENT_PTR(tmp, new);
397 return;
398 } else {
399 INSERT_CURRENT(new, tmp);
400 return;
402 } END_FOR_EACH_PTR(tmp);
403 add_ptr_list(slist, new);
406 void delete_state_slist(struct state_list **slist, const char *name, int owner,
407 struct symbol *sym)
409 struct sm_state *state;
411 FOR_EACH_PTR(*slist, state) {
412 if (state->owner == owner && state->sym == sym
413 && !strcmp(state->name, name)){
414 delete_ptr_list_entry((struct ptr_list **)slist,
415 state, 1);
416 return;
418 } END_FOR_EACH_PTR(state);
422 void push_slist(struct state_list_stack **list_stack, struct state_list *slist)
424 add_ptr_list(list_stack, slist);
427 struct state_list *pop_slist(struct state_list_stack **list_stack)
429 struct state_list *slist;
431 slist = last_ptr_list((struct ptr_list *)*list_stack);
432 delete_ptr_list_last((struct ptr_list **)list_stack);
433 return slist;
436 void free_slist(struct state_list **slist)
438 __free_ptr_list((struct ptr_list **)slist);
441 void free_stack(struct state_list_stack **stack)
443 __free_ptr_list((struct ptr_list **)stack);
446 void free_stack_and_slists(struct state_list_stack **slist_stack)
448 struct state_list *slist;
450 FOR_EACH_PTR(*slist_stack, slist) {
451 free_slist(&slist);
452 } END_FOR_EACH_PTR(slist);
453 free_stack(slist_stack);
457 * set_state_stack() sets the state for the top slist on the stack.
459 void set_state_stack(struct state_list_stack **stack, const char *name,
460 int owner, struct symbol *sym, struct smatch_state *state)
462 struct state_list *slist;
464 slist = pop_slist(stack);
465 set_state_slist(&slist, name, owner, sym, state);
466 push_slist(stack, slist);
470 * get_state_stack() gets the state for the top slist on the stack.
472 struct smatch_state *get_state_stack(struct state_list_stack *stack,
473 const char *name, int owner,
474 struct symbol *sym)
476 struct state_list *slist;
477 struct smatch_state *ret;
479 slist = pop_slist(&stack);
480 ret = get_state_slist(slist, name, owner, sym);
481 push_slist(&stack, slist);
482 return ret;
486 * We want to find which states have been modified inside a branch.
487 * If you have 2 &merged states they could be different states really
488 * and maybe one or both were modified. We say it is unchanged if
489 * the ->state pointers are the same and they belong to the same pools.
490 * If they have been modified on both sides of a branch to the same thing,
491 * it's still OK to say they are the same, because that means they won't
492 * belong to any pools.
494 static int is_really_same(struct sm_state *one, struct sm_state *two)
496 struct state_list *tmp1;
497 struct state_list *tmp2;
499 if (one->state != two->state)
500 return 0;
502 PREPARE_PTR_LIST(one->my_pools, tmp1);
503 PREPARE_PTR_LIST(two->my_pools, tmp2);
504 for (;;) {
505 if (!tmp1 && !tmp2)
506 return 1;
507 if (tmp1 < tmp2) {
508 return 0;
509 } else if (tmp1 == tmp2) {
510 NEXT_PTR_LIST(tmp1);
511 NEXT_PTR_LIST(tmp2);
512 } else {
513 return 0;
516 FINISH_PTR_LIST(tmp2);
517 FINISH_PTR_LIST(tmp1);
518 return 1;
522 * merge_slist() is called whenever paths merge, such as after
523 * an if statement. It takes the two slists and creates one.
525 void merge_slist(struct state_list **to, struct state_list *slist)
527 struct sm_state *to_state, *state, *tmp;
528 struct state_list *results = NULL;
529 struct state_list *implied_to = NULL;
530 struct state_list *implied_from = NULL;
532 #ifdef CHECKORDER
533 check_order(*to);
534 check_order(slist);
535 #endif
537 /* merging a null and nonnull path gives you only the nonnull path */
538 if (!slist) {
539 return;
541 if (!*to) {
542 *to = clone_slist(slist);
543 return;
546 implied_to = clone_slist(*to);
547 implied_from = clone_slist(slist);
549 PREPARE_PTR_LIST(*to, to_state);
550 PREPARE_PTR_LIST(slist, state);
551 for (;;) {
552 if (!to_state && !state)
553 break;
554 if (cmp_tracker(to_state, state) < 0) {
555 tmp = merge_sm_states(to_state, NULL);
556 add_pool(&tmp->my_pools, implied_to);
557 add_pool(&tmp->all_pools, implied_to);
558 add_ptr_list(&results, tmp);
559 NEXT_PTR_LIST(to_state);
560 } else if (cmp_tracker(to_state, state) == 0) {
561 tmp = merge_sm_states(to_state, state);
562 if (!is_really_same(to_state, state)) {
563 add_pool(&tmp->my_pools, implied_to);
564 add_pool(&tmp->my_pools, implied_from);
566 add_pool(&tmp->all_pools, implied_to);
567 add_pool(&tmp->all_pools, implied_from);
568 add_ptr_list(&results, tmp);
569 NEXT_PTR_LIST(to_state);
570 NEXT_PTR_LIST(state);
571 } else {
572 tmp = merge_sm_states(state, NULL);
573 add_pool(&tmp->my_pools, implied_from);
574 add_pool(&tmp->all_pools, implied_from);
575 add_ptr_list(&results, tmp);
576 NEXT_PTR_LIST(state);
579 FINISH_PTR_LIST(state);
580 FINISH_PTR_LIST(to_state);
582 free_slist(to);
583 *to = results;
585 push_slist(&implied_pools, implied_from);
586 push_slist(&implied_pools, implied_to);
589 static int pool_in_pools(struct state_list_stack *pools,
590 struct state_list *pool)
592 struct state_list *tmp;
594 FOR_EACH_PTR(pools, tmp) {
595 if (tmp == pool)
596 return 1;
597 } END_FOR_EACH_PTR(tmp);
598 return 0;
601 struct state_list *clone_states_in_pool(struct state_list *pool,
602 struct state_list *cur_slist)
604 struct sm_state *state;
605 struct sm_state *cur_state;
606 struct sm_state *tmp;
607 struct state_list *to_slist = NULL;
609 FOR_EACH_PTR(pool, state) {
610 cur_state = get_sm_state_slist(cur_slist, state->name,
611 state->owner, state->sym);
612 if (!cur_state)
613 continue;
614 if (is_really_same(state, cur_state))
615 continue;
616 if (pool_in_pools(cur_state->all_pools, pool)) {
617 tmp = clone_state(state);
618 add_ptr_list(&to_slist, tmp);
620 } END_FOR_EACH_PTR(state);
621 return to_slist;
625 * merge_implied() takes an implied state and another possibly implied state
626 * from another pool. It checks that the second pool is reachable from
627 * cur_slist then merges the two states and returns the result.
629 struct sm_state *merge_implied(struct sm_state *one, struct sm_state *two,
630 struct state_list *pool,
631 struct state_list *cur_slist)
633 struct sm_state *cur_state;
635 cur_state = get_sm_state_slist(cur_slist, two->name, two->owner,
636 two->sym);
637 if (!cur_state)
638 return NULL; /* this can't actually happen */
639 if (!pool_in_pools(cur_state->all_pools, pool))
640 return NULL;
641 return merge_sm_states(one, two);
645 * filter() is used to find what states are the same across
646 * a series of slists.
647 * It takes a **slist and a *filter.
648 * It removes everything from **slist that isn't in *filter.
649 * The reason you would want to do this is if you want to
650 * know what other states are true if one state is true. (smatch_implied).
652 void filter(struct state_list **slist, struct state_list *filter,
653 struct state_list *cur_slist)
655 struct sm_state *s_one, *s_two;
656 struct state_list *results = NULL;
657 struct sm_state *tmp;
659 #ifdef CHECKORDER
660 check_order(*slist);
661 check_order(filter);
662 #endif
664 PREPARE_PTR_LIST(*slist, s_one);
665 PREPARE_PTR_LIST(filter, s_two);
666 for (;;) {
667 if (!s_one || !s_two)
668 break;
669 if (cmp_tracker(s_one, s_two) < 0) {
670 NEXT_PTR_LIST(s_one);
671 } else if (cmp_tracker(s_one, s_two) == 0) {
672 tmp = merge_implied(s_one, s_two, filter, cur_slist);
673 if (tmp)
674 add_ptr_list(&results, tmp);
675 NEXT_PTR_LIST(s_one);
676 NEXT_PTR_LIST(s_two);
677 } else {
678 NEXT_PTR_LIST(s_two);
681 FINISH_PTR_LIST(s_two);
682 FINISH_PTR_LIST(s_one);
684 free_slist(slist);
685 *slist = results;
689 * and_slist_stack() is basically the same as popping the top two slists,
690 * overwriting the one with the other and pushing it back on the stack.
691 * The difference is that it checks to see that a mutually exclusive
692 * state isn't included in both stacks. If smatch sees something like
693 * "if (a && !a)" it prints a warning.
695 void and_slist_stack(struct state_list_stack **slist_stack)
697 struct sm_state *tmp;
698 struct smatch_state *tmp_state;
699 struct state_list *tmp_slist = pop_slist(slist_stack);
701 FOR_EACH_PTR(tmp_slist, tmp) {
702 tmp_state = get_state_stack(*slist_stack, tmp->name,
703 tmp->owner, tmp->sym);
704 if (tmp_state && tmp_state != tmp->state) {
705 smatch_msg("mutually exclusive 'and' conditions states "
706 "'%s': %s + %s",
707 tmp->name, show_state(tmp_state),
708 show_state(tmp->state));
710 set_state_stack(slist_stack, tmp->name, tmp->owner, tmp->sym,
711 tmp->state);
712 } END_FOR_EACH_PTR(tmp);
713 free_slist(&tmp_slist);
717 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
718 * It pops the two slists from the top of the stack and merges them
719 * together in a way that preserves the things they have in common
720 * but creates a merged state for most of the rest.
721 * You could have code that had: if (foo || foo) { foo->baz;
722 * It's this function which ensures smatch does the right thing.
724 void or_slist_stack(struct state_list_stack **slist_stack)
726 struct state_list *one;
727 struct state_list *two;
728 struct state_list *res = NULL;
729 struct sm_state *tmp;
730 struct sm_state *sm;
731 struct sm_state *new_sm;
733 one = pop_slist(slist_stack);
734 two = pop_slist(slist_stack);
736 FOR_EACH_PTR(one, tmp) {
737 sm = get_sm_state_slist(two, tmp->name, tmp->owner, tmp->sym);
738 new_sm = merge_sm_states(tmp, sm);
739 add_ptr_list(&res, new_sm);
740 } END_FOR_EACH_PTR(tmp);
742 FOR_EACH_PTR(two, tmp) {
743 sm = get_sm_state_slist(one, tmp->name, tmp->owner, tmp->sym);
744 new_sm = merge_sm_states(tmp, sm);
745 add_ptr_list(&res, new_sm);
746 } END_FOR_EACH_PTR(tmp);
748 push_slist(slist_stack, res);
750 free_slist(&one);
751 free_slist(&two);
755 * get_slist_from_named_stack() is only used for gotos.
757 struct state_list **get_slist_from_named_stack(struct named_stack *stack,
758 const char *name)
760 struct named_slist *tmp;
762 FOR_EACH_PTR(stack, tmp) {
763 if (!strcmp(tmp->name, name))
764 return &tmp->slist;
765 } END_FOR_EACH_PTR(tmp);
766 return NULL;
769 void overwrite_slist(struct state_list *from, struct state_list **to)
771 struct sm_state *tmp;
773 FOR_EACH_PTR(from, tmp) {
774 overwrite_sm_state(to, tmp);
775 } END_FOR_EACH_PTR(tmp);