Get rid of unused line history code.
[smatch.git] / smatch_slist.c
blobc8cb310c1387d2d35ccc104125de02d33324265b
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 *tmp;
171 struct sm_state *poss;
173 ret = alloc_state(s->name, s->owner, s->sym, s->state);
174 ret->my_pools = clone_stack(s->my_pools);
175 ret->all_pools = clone_stack(s->all_pools);
176 FOR_EACH_PTR(s->possible, poss) {
177 tmp = alloc_state(s->name, s->owner, s->sym, poss->state);
178 add_sm_state_slist(&ret->possible, tmp);
179 } END_FOR_EACH_PTR(poss);
180 return ret;
183 int slist_has_state(struct state_list *slist, struct smatch_state *state)
185 struct sm_state *tmp;
187 FOR_EACH_PTR(slist, tmp) {
188 if (tmp->state == state)
189 return 1;
190 } END_FOR_EACH_PTR(tmp);
191 return 0;
194 #ifdef CHECKORDER
195 static void check_order(struct state_list *slist)
197 struct sm_state *state;
198 struct sm_state *last = NULL;
199 int printed = 0;
201 FOR_EACH_PTR(slist, state) {
202 if (last && cmp_tracker(state, last) <= 0) {
203 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
204 "%s vs %s\n", last->owner, state->owner,
205 last->sym, state->sym, last->name, state->name);
206 printed = 1;
208 last = state;
209 } END_FOR_EACH_PTR(state);
211 if (printed)
212 printf("======\n");
214 #endif
216 struct state_list *clone_slist(struct state_list *from_slist)
218 struct sm_state *state;
219 struct sm_state *tmp;
220 struct state_list *to_slist = NULL;
222 FOR_EACH_PTR(from_slist, state) {
223 tmp = clone_state(state);
224 add_ptr_list(&to_slist, tmp);
225 } END_FOR_EACH_PTR(state);
226 #ifdef CHECKORDER
227 check_order(to_slist);
228 #endif
229 return to_slist;
232 struct state_list_stack *clone_stack(struct state_list_stack *from_stack)
234 struct state_list *slist;
235 struct state_list_stack *to_stack = NULL;
237 FOR_EACH_PTR(from_stack, slist) {
238 push_slist(&to_stack, slist);
239 } END_FOR_EACH_PTR(slist);
240 return to_stack;
243 struct smatch_state *merge_states(const char *name, int owner,
244 struct symbol *sym,
245 struct smatch_state *state1,
246 struct smatch_state *state2)
248 struct smatch_state *ret;
250 if (state1 == state2)
251 ret = state1;
252 else if (__has_merge_function(owner))
253 ret = __client_merge_function(owner, name, sym, state1, state2);
254 else if (!state1 || !state2)
255 ret = &undefined;
256 else
257 ret = &merged;
258 return ret;
262 * add_pool() adds a slist to ->pools. If the slist has already been
263 * added earlier then it doesn't get added a second time.
265 static void add_pool(struct state_list_stack **pools, struct state_list *new)
267 struct state_list *tmp;
269 FOR_EACH_PTR(*pools, tmp) {
270 if (tmp < new)
271 continue;
272 else if (tmp == new) {
273 return;
274 } else {
275 INSERT_CURRENT(new, tmp);
276 return;
278 } END_FOR_EACH_PTR(tmp);
279 add_ptr_list(pools, new);
282 static void copy_pools(struct sm_state *to, struct sm_state *sm)
284 struct state_list *tmp;
286 if (!sm)
287 return;
289 FOR_EACH_PTR(sm->my_pools, tmp) {
290 add_pool(&to->my_pools, tmp);
291 } END_FOR_EACH_PTR(tmp);
293 FOR_EACH_PTR(sm->all_pools, tmp) {
294 add_pool(&to->all_pools, tmp);
295 } END_FOR_EACH_PTR(tmp);
298 struct sm_state *merge_sm_states(struct sm_state *one, struct sm_state *two)
300 struct smatch_state *s;
301 struct sm_state *result;
303 s = merge_states(one->name, one->owner, one->sym, one->state,
304 (two?two->state:NULL));
305 result = alloc_state(one->name, one->owner, one->sym, s);
306 add_possible(result, one);
307 add_possible(result, two);
308 copy_pools(result, one);
309 copy_pools(result, two);
311 if (debug_states) {
312 struct sm_state *tmp;
313 int i = 0;
315 printf("%d merge name='%s' owner=%d: %s + %s => %s (",
316 get_lineno(), one->name, one->owner,
317 show_state(one->state), show_state(two?two->state:NULL),
318 show_state(s));
320 FOR_EACH_PTR(result->possible, tmp) {
321 if (i++) {
322 printf(", ");
324 printf("%s", show_state(tmp->state));
325 } END_FOR_EACH_PTR(tmp);
326 printf(")\n");
329 return result;
332 struct sm_state *get_sm_state_slist(struct state_list *slist, const char *name,
333 int owner, struct symbol *sym)
335 struct sm_state *state;
337 if (!name)
338 return NULL;
340 FOR_EACH_PTR(slist, state) {
341 if (state->owner == owner && state->sym == sym
342 && !strcmp(state->name, name))
343 return state;
344 } END_FOR_EACH_PTR(state);
345 return NULL;
348 struct smatch_state *get_state_slist(struct state_list *slist,
349 const char *name, int owner,
350 struct symbol *sym)
352 struct sm_state *state;
354 state = get_sm_state_slist(slist, name, owner, sym);
355 if (state)
356 return state->state;
357 return NULL;
360 void overwrite_sm_state(struct state_list **slist, struct sm_state *state)
362 struct sm_state *tmp;
363 struct sm_state *new = clone_state(state); //fixme. why?
365 FOR_EACH_PTR(*slist, tmp) {
366 if (cmp_tracker(tmp, new) < 0)
367 continue;
368 else if (cmp_tracker(tmp, new) == 0) {
369 REPLACE_CURRENT_PTR(tmp, new);
370 return;
371 } else {
372 INSERT_CURRENT(new, tmp);
373 return;
375 } END_FOR_EACH_PTR(tmp);
376 add_ptr_list(slist, new);
379 void overwrite_sm_state_stack(struct state_list_stack **stack,
380 struct sm_state *state)
382 struct state_list *slist;
384 slist = pop_slist(stack);
385 overwrite_sm_state(&slist, state);
386 push_slist(stack, slist);
389 void set_state_slist(struct state_list **slist, const char *name, int owner,
390 struct symbol *sym, struct smatch_state *state)
392 struct sm_state *tmp;
393 struct sm_state *new = alloc_state(name, owner, sym, state);
395 FOR_EACH_PTR(*slist, tmp) {
396 if (cmp_tracker(tmp, new) < 0)
397 continue;
398 else if (cmp_tracker(tmp, new) == 0) {
399 tmp->state = state;
400 tmp->my_pools = NULL;
401 tmp->all_pools = NULL;
402 tmp->possible = NULL;
403 add_ptr_list(&tmp->possible, tmp);
404 __free_sm_state(new);
405 return;
406 } else {
407 INSERT_CURRENT(new, tmp);
408 return;
410 } END_FOR_EACH_PTR(tmp);
411 add_ptr_list(slist, new);
414 void delete_state_slist(struct state_list **slist, const char *name, int owner,
415 struct symbol *sym)
417 struct sm_state *state;
419 FOR_EACH_PTR(*slist, state) {
420 if (state->owner == owner && state->sym == sym
421 && !strcmp(state->name, name)){
422 delete_ptr_list_entry((struct ptr_list **)slist,
423 state, 1);
424 __free_sm_state(state);
425 return;
427 } END_FOR_EACH_PTR(state);
431 void push_slist(struct state_list_stack **list_stack, struct state_list *slist)
433 add_ptr_list(list_stack, slist);
436 struct state_list *pop_slist(struct state_list_stack **list_stack)
438 struct state_list *slist;
440 slist = last_ptr_list((struct ptr_list *)*list_stack);
441 delete_ptr_list_last((struct ptr_list **)list_stack);
442 return slist;
445 void free_slist(struct state_list **slist)
447 __free_ptr_list((struct ptr_list **)slist);
450 void free_stack(struct state_list_stack **stack)
452 __free_ptr_list((struct ptr_list **)stack);
455 void free_stack_and_slists(struct state_list_stack **slist_stack)
457 struct state_list *slist;
459 FOR_EACH_PTR(*slist_stack, slist) {
460 free_slist(&slist);
461 } END_FOR_EACH_PTR(slist);
462 free_stack(slist_stack);
466 * set_state_stack() sets the state for the top slist on the stack.
468 void set_state_stack(struct state_list_stack **stack, const char *name,
469 int owner, struct symbol *sym, struct smatch_state *state)
471 struct state_list *slist;
473 slist = pop_slist(stack);
474 set_state_slist(&slist, name, owner, sym, state);
475 push_slist(stack, slist);
479 * get_state_stack() gets the state for the top slist on the stack.
481 struct smatch_state *get_state_stack(struct state_list_stack *stack,
482 const char *name, int owner,
483 struct symbol *sym)
485 struct state_list *slist;
486 struct smatch_state *ret;
488 slist = pop_slist(&stack);
489 ret = get_state_slist(slist, name, owner, sym);
490 push_slist(&stack, slist);
491 return ret;
495 * We want to find which states have been modified inside a branch.
496 * If you have 2 &merged states they could be different states really
497 * and maybe one or both were modified. We say it is unchanged if
498 * the ->state pointers are the same and they belong to the same pools.
499 * If they have been modified on both sides of a branch to the same thing,
500 * it's still OK to say they are the same, because that means they won't
501 * belong to any pools.
503 static int is_really_same(struct sm_state *one, struct sm_state *two)
505 struct state_list *tmp1;
506 struct state_list *tmp2;
508 if (one->state != two->state)
509 return 0;
511 PREPARE_PTR_LIST(one->my_pools, tmp1);
512 PREPARE_PTR_LIST(two->my_pools, tmp2);
513 for (;;) {
514 if (!tmp1 && !tmp2)
515 return 1;
516 if (tmp1 < tmp2) {
517 return 0;
518 } else if (tmp1 == tmp2) {
519 NEXT_PTR_LIST(tmp1);
520 NEXT_PTR_LIST(tmp2);
521 } else {
522 return 0;
525 FINISH_PTR_LIST(tmp2);
526 FINISH_PTR_LIST(tmp1);
527 return 1;
531 * merge_slist() is called whenever paths merge, such as after
532 * an if statement. It takes the two slists and creates one.
534 void merge_slist(struct state_list **to, struct state_list *slist)
536 struct sm_state *to_state, *state, *tmp;
537 struct state_list *results = NULL;
538 struct state_list *implied_to = NULL;
539 struct state_list *implied_from = NULL;
541 #ifdef CHECKORDER
542 check_order(*to);
543 check_order(slist);
544 #endif
546 /* merging a null and nonnull path gives you only the nonnull path */
547 if (!slist) {
548 return;
550 if (!*to) {
551 *to = clone_slist(slist);
552 return;
555 implied_to = clone_slist(*to);
556 implied_from = clone_slist(slist);
558 PREPARE_PTR_LIST(*to, to_state);
559 PREPARE_PTR_LIST(slist, state);
560 for (;;) {
561 if (!to_state && !state)
562 break;
563 if (cmp_tracker(to_state, state) < 0) {
564 tmp = merge_sm_states(to_state, NULL);
565 add_pool(&tmp->my_pools, implied_to);
566 add_pool(&tmp->all_pools, implied_to);
567 add_ptr_list(&results, tmp);
568 NEXT_PTR_LIST(to_state);
569 } else if (cmp_tracker(to_state, state) == 0) {
570 tmp = merge_sm_states(to_state, state);
571 if (!is_really_same(to_state, state)) {
572 add_pool(&tmp->my_pools, implied_to);
573 add_pool(&tmp->my_pools, implied_from);
575 add_pool(&tmp->all_pools, implied_to);
576 add_pool(&tmp->all_pools, implied_from);
577 add_ptr_list(&results, tmp);
578 NEXT_PTR_LIST(to_state);
579 NEXT_PTR_LIST(state);
580 } else {
581 tmp = merge_sm_states(state, NULL);
582 add_pool(&tmp->my_pools, implied_from);
583 add_pool(&tmp->all_pools, implied_from);
584 add_ptr_list(&results, tmp);
585 NEXT_PTR_LIST(state);
588 FINISH_PTR_LIST(state);
589 FINISH_PTR_LIST(to_state);
591 free_slist(to);
592 *to = results;
594 push_slist(&implied_pools, implied_from);
595 push_slist(&implied_pools, implied_to);
598 static int pool_in_pools(struct state_list_stack *pools,
599 struct state_list *pool)
601 struct state_list *tmp;
603 FOR_EACH_PTR(pools, tmp) {
604 if (tmp == pool)
605 return 1;
606 } END_FOR_EACH_PTR(tmp);
607 return 0;
610 struct state_list *clone_states_in_pool(struct state_list *pool,
611 struct state_list *cur_slist)
613 struct sm_state *state;
614 struct sm_state *cur_state;
615 struct sm_state *tmp;
616 struct state_list *to_slist = NULL;
618 FOR_EACH_PTR(pool, state) {
619 cur_state = get_sm_state_slist(cur_slist, state->name,
620 state->owner, state->sym);
621 if (!cur_state)
622 continue;
623 if (is_really_same(state, cur_state))
624 continue;
625 if (pool_in_pools(cur_state->all_pools, pool)) {
626 tmp = clone_state(state);
627 add_ptr_list(&to_slist, tmp);
629 } END_FOR_EACH_PTR(state);
630 return to_slist;
634 * merge_implied() takes an implied state and another possibly implied state
635 * from another pool. It checks that the second pool is reachable from
636 * cur_slist then merges the two states and returns the result.
638 struct sm_state *merge_implied(struct sm_state *one, struct sm_state *two,
639 struct state_list *pool,
640 struct state_list *cur_slist)
642 struct sm_state *cur_state;
644 // fixme: do we not need to check this?
645 cur_state = get_sm_state_slist(cur_slist, two->name, two->owner,
646 two->sym);
647 if (!cur_state)
648 return NULL; /* this can't actually happen */
649 if (!pool_in_pools(cur_state->all_pools, pool))
650 return NULL;
651 return merge_sm_states(one, two);
655 * filter() is used to find what states are the same across
656 * a series of slists.
657 * It takes a **slist and a *filter.
658 * It removes everything from **slist that isn't in *filter.
659 * The reason you would want to do this is if you want to
660 * know what other states are true if one state is true. (smatch_implied).
662 void filter(struct state_list **slist, struct state_list *filter,
663 struct state_list *cur_slist)
665 struct sm_state *s_one, *s_two;
666 struct state_list *results = NULL;
667 struct sm_state *tmp;
669 #ifdef CHECKORDER
670 check_order(*slist);
671 check_order(filter);
672 #endif
674 PREPARE_PTR_LIST(*slist, s_one);
675 PREPARE_PTR_LIST(filter, s_two);
676 for (;;) {
677 if (!s_one || !s_two)
678 break;
679 if (cmp_tracker(s_one, s_two) < 0) {
680 NEXT_PTR_LIST(s_one);
681 } else if (cmp_tracker(s_one, s_two) == 0) {
682 tmp = merge_implied(s_one, s_two, filter, cur_slist);
683 if (tmp)
684 add_ptr_list(&results, tmp);
685 NEXT_PTR_LIST(s_one);
686 NEXT_PTR_LIST(s_two);
687 } else {
688 NEXT_PTR_LIST(s_two);
691 FINISH_PTR_LIST(s_two);
692 FINISH_PTR_LIST(s_one);
694 free_slist(slist);
695 *slist = results;
699 * and_slist_stack() is basically the same as popping the top two slists,
700 * overwriting the one with the other and pushing it back on the stack.
701 * The difference is that it checks to see that a mutually exclusive
702 * state isn't included in both stacks. If smatch sees something like
703 * "if (a && !a)" it prints a warning.
705 void and_slist_stack(struct state_list_stack **slist_stack)
707 struct sm_state *tmp;
708 struct smatch_state *tmp_state;
709 struct state_list *tmp_slist = pop_slist(slist_stack);
711 FOR_EACH_PTR(tmp_slist, tmp) {
712 tmp_state = get_state_stack(*slist_stack, tmp->name,
713 tmp->owner, tmp->sym);
714 if (tmp_state && tmp_state != tmp->state) {
715 smatch_msg("mutually exclusive 'and' conditions states "
716 "'%s': %s + %s",
717 tmp->name, show_state(tmp_state),
718 show_state(tmp->state));
720 set_state_stack(slist_stack, tmp->name, tmp->owner, tmp->sym,
721 tmp->state);
722 } END_FOR_EACH_PTR(tmp);
723 free_slist(&tmp_slist);
727 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
728 * It pops the two slists from the top of the stack and merges them
729 * together in a way that preserves the things they have in common
730 * but creates a merged state for most of the rest.
731 * You could have code that had: if (foo || foo) { foo->baz;
732 * It's this function which ensures smatch does the right thing.
734 void or_slist_stack(struct state_list_stack **slist_stack)
736 struct state_list *one;
737 struct state_list *two;
738 struct state_list *res = NULL;
739 struct sm_state *tmp;
740 struct sm_state *sm;
741 struct sm_state *new_sm;
743 one = pop_slist(slist_stack);
744 two = pop_slist(slist_stack);
746 FOR_EACH_PTR(one, tmp) {
747 sm = get_sm_state_slist(two, tmp->name, tmp->owner, tmp->sym);
748 new_sm = merge_sm_states(tmp, sm);
749 add_ptr_list(&res, new_sm);
750 } END_FOR_EACH_PTR(tmp);
752 FOR_EACH_PTR(two, tmp) {
753 sm = get_sm_state_slist(one, tmp->name, tmp->owner, tmp->sym);
754 new_sm = merge_sm_states(tmp, sm);
755 add_ptr_list(&res, new_sm);
756 } END_FOR_EACH_PTR(tmp);
758 push_slist(slist_stack, res);
760 free_slist(&one);
761 free_slist(&two);
765 * get_slist_from_named_stack() is only used for gotos.
767 struct state_list **get_slist_from_named_stack(struct named_stack *stack,
768 const char *name)
770 struct named_slist *tmp;
772 FOR_EACH_PTR(stack, tmp) {
773 if (!strcmp(tmp->name, name))
774 return &tmp->slist;
775 } END_FOR_EACH_PTR(tmp);
776 return NULL;
779 void overwrite_slist(struct state_list *from, struct state_list **to)
781 struct sm_state *tmp;
783 FOR_EACH_PTR(from, tmp) {
784 overwrite_sm_state(to, tmp);
785 } END_FOR_EACH_PTR(tmp);