nospec: use statement count to mark things as nospec
[smatch.git] / smatch_slist.c
blobb89bdc7285e1544b91756b9e17a3bc3b47c91682
1 /*
2 * Copyright (C) 2008,2009 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
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include "smatch.h"
21 #include "smatch_slist.h"
23 #undef CHECKORDER
25 ALLOCATOR(smatch_state, "smatch state");
26 ALLOCATOR(sm_state, "sm state");
27 ALLOCATOR(named_stree, "named slist");
28 __DO_ALLOCATOR(char, 1, 4, "state names", sname);
30 int sm_state_counter;
32 static struct stree_stack *all_pools;
34 const char *show_sm(struct sm_state *sm)
36 static char buf[256];
37 struct sm_state *tmp;
38 int pos;
39 int i;
41 if (!sm)
42 return "<none>";
44 pos = snprintf(buf, sizeof(buf), "[%s] '%s' = '%s'",
45 check_name(sm->owner), sm->name, show_state(sm->state));
46 if (pos > sizeof(buf))
47 goto truncate;
49 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
50 return buf;
52 pos += snprintf(buf + pos, sizeof(buf) - pos, " (");
53 if (pos > sizeof(buf))
54 goto truncate;
55 i = 0;
56 FOR_EACH_PTR(sm->possible, tmp) {
57 if (i++)
58 pos += snprintf(buf + pos, sizeof(buf) - pos, ", ");
59 if (pos > sizeof(buf))
60 goto truncate;
61 pos += snprintf(buf + pos, sizeof(buf) - pos, "%s",
62 show_state(tmp->state));
63 if (pos > sizeof(buf))
64 goto truncate;
65 } END_FOR_EACH_PTR(tmp);
66 snprintf(buf + pos, sizeof(buf) - pos, ")");
68 return buf;
70 truncate:
71 for (i = 0; i < 3; i++)
72 buf[sizeof(buf) - 2 - i] = '.';
73 return buf;
76 void __print_stree(struct stree *stree)
78 struct sm_state *sm;
80 printf("dumping stree at %d [%ld states]\n", get_lineno(), stree_count(stree));
81 FOR_EACH_SM(stree, sm) {
82 printf("%s\n", show_sm(sm));
83 } END_FOR_EACH_SM(sm);
84 printf("---\n");
87 /* NULL states go at the end to simplify merge_slist */
88 int cmp_tracker(const struct sm_state *a, const struct sm_state *b)
90 int ret;
92 if (a == b)
93 return 0;
94 if (!b)
95 return -1;
96 if (!a)
97 return 1;
99 if (a->owner > b->owner)
100 return -1;
101 if (a->owner < b->owner)
102 return 1;
104 ret = strcmp(a->name, b->name);
105 if (ret < 0)
106 return -1;
107 if (ret > 0)
108 return 1;
110 if (!b->sym && a->sym)
111 return -1;
112 if (!a->sym && b->sym)
113 return 1;
114 if (a->sym < b->sym)
115 return -1;
116 if (a->sym > b->sym)
117 return 1;
119 return 0;
122 static int cmp_sm_states(const struct sm_state *a, const struct sm_state *b, int preserve)
124 int ret;
126 ret = cmp_tracker(a, b);
127 if (ret)
128 return ret;
130 /* todo: add hook for smatch_extra.c */
131 if (a->state > b->state)
132 return -1;
133 if (a->state < b->state)
134 return 1;
135 /* This is obviously a massive disgusting hack but we need to preserve
136 * the unmerged states for smatch extra because we use them in
137 * smatch_db.c. Meanwhile if we preserve all the other unmerged states
138 * then it uses a lot of memory and we don't use it. Hence this hack.
140 * Also sometimes even just preserving every possible SMATCH_EXTRA state
141 * takes too much resources so we have to cap that. Capping is probably
142 * not often a problem in real life.
144 if (a->owner == SMATCH_EXTRA && preserve) {
145 if (a == b)
146 return 0;
147 if (a->merged == 1 && b->merged == 0)
148 return -1;
149 if (a->merged == 0)
150 return 1;
153 return 0;
156 struct sm_state *alloc_sm_state(int owner, const char *name,
157 struct symbol *sym, struct smatch_state *state)
159 struct sm_state *sm_state = __alloc_sm_state(0);
161 sm_state_counter++;
163 sm_state->name = alloc_sname(name);
164 sm_state->owner = owner;
165 sm_state->sym = sym;
166 sm_state->state = state;
167 sm_state->line = get_lineno();
168 sm_state->merged = 0;
169 sm_state->pool = NULL;
170 sm_state->left = NULL;
171 sm_state->right = NULL;
172 sm_state->nr_children = 1;
173 sm_state->possible = NULL;
174 add_ptr_list(&sm_state->possible, sm_state);
175 return sm_state;
178 static struct sm_state *alloc_state_no_name(int owner, const char *name,
179 struct symbol *sym,
180 struct smatch_state *state)
182 struct sm_state *tmp;
184 tmp = alloc_sm_state(owner, NULL, sym, state);
185 tmp->name = name;
186 return tmp;
189 int too_many_possible(struct sm_state *sm)
191 if (ptr_list_size((struct ptr_list *)sm->possible) >= 100)
192 return 1;
193 return 0;
196 void add_possible_sm(struct sm_state *to, struct sm_state *new)
198 struct sm_state *tmp;
199 int preserve = 1;
201 if (too_many_possible(to))
202 preserve = 0;
204 FOR_EACH_PTR(to->possible, tmp) {
205 if (cmp_sm_states(tmp, new, preserve) < 0)
206 continue;
207 else if (cmp_sm_states(tmp, new, preserve) == 0) {
208 return;
209 } else {
210 INSERT_CURRENT(new, tmp);
211 return;
213 } END_FOR_EACH_PTR(tmp);
214 add_ptr_list(&to->possible, new);
217 static void copy_possibles(struct sm_state *to, struct sm_state *from)
219 struct sm_state *tmp;
221 FOR_EACH_PTR(from->possible, tmp) {
222 add_possible_sm(to, tmp);
223 } END_FOR_EACH_PTR(tmp);
226 char *alloc_sname(const char *str)
228 char *tmp;
230 if (!str)
231 return NULL;
232 tmp = __alloc_sname(strlen(str) + 1);
233 strcpy(tmp, str);
234 return tmp;
237 int out_of_memory(void)
240 * I decided to use 50M here based on trial and error.
241 * It works out OK for the kernel and so it should work
242 * for most other projects as well.
244 if (sm_state_counter * sizeof(struct sm_state) >= 100000000)
245 return 1;
246 return 0;
249 int low_on_memory(void)
251 if (sm_state_counter * sizeof(struct sm_state) >= 25000000)
252 return 1;
253 return 0;
256 static void free_sm_state(struct sm_state *sm)
258 free_slist(&sm->possible);
260 * fixme. Free the actual state.
261 * Right now we leave it until the end of the function
262 * because we don't want to double free it.
263 * Use the freelist to not double free things
267 static void free_all_sm_states(struct allocation_blob *blob)
269 unsigned int size = sizeof(struct sm_state);
270 unsigned int offset = 0;
272 while (offset < blob->offset) {
273 free_sm_state((struct sm_state *)(blob->data + offset));
274 offset += size;
278 /* At the end of every function we free all the sm_states */
279 void free_every_single_sm_state(void)
281 struct allocator_struct *desc = &sm_state_allocator;
282 struct allocation_blob *blob = desc->blobs;
284 desc->blobs = NULL;
285 desc->allocations = 0;
286 desc->total_bytes = 0;
287 desc->useful_bytes = 0;
288 desc->freelist = NULL;
289 while (blob) {
290 struct allocation_blob *next = blob->next;
291 free_all_sm_states(blob);
292 blob_free(blob, desc->chunking);
293 blob = next;
295 clear_sname_alloc();
296 clear_smatch_state_alloc();
298 free_stack_and_strees(&all_pools);
299 sm_state_counter = 0;
302 unsigned long get_pool_count(void)
304 return ptr_list_size((struct ptr_list *)all_pools);
307 struct sm_state *clone_sm(struct sm_state *s)
309 struct sm_state *ret;
311 ret = alloc_state_no_name(s->owner, s->name, s->sym, s->state);
312 ret->merged = s->merged;
313 ret->line = s->line;
314 /* clone_sm() doesn't copy the pools. Each state needs to have
315 only one pool. */
316 ret->possible = clone_slist(s->possible);
317 ret->left = s->left;
318 ret->right = s->right;
319 ret->nr_children = s->nr_children;
320 return ret;
323 int is_merged(struct sm_state *sm)
325 return sm->merged;
328 int is_leaf(struct sm_state *sm)
330 return !sm->merged;
333 int slist_has_state(struct state_list *slist, struct smatch_state *state)
335 struct sm_state *tmp;
337 FOR_EACH_PTR(slist, tmp) {
338 if (tmp->state == state)
339 return 1;
340 } END_FOR_EACH_PTR(tmp);
341 return 0;
344 struct state_list *clone_slist(struct state_list *from_slist)
346 struct sm_state *sm;
347 struct state_list *to_slist = NULL;
349 FOR_EACH_PTR(from_slist, sm) {
350 add_ptr_list(&to_slist, sm);
351 } END_FOR_EACH_PTR(sm);
352 return to_slist;
355 static struct smatch_state *merge_states(int owner, const char *name,
356 struct symbol *sym,
357 struct smatch_state *state1,
358 struct smatch_state *state2)
360 struct smatch_state *ret;
362 if (state1 == state2)
363 ret = state1;
364 else if (__has_merge_function(owner))
365 ret = __client_merge_function(owner, state1, state2);
366 else if (state1 == &ghost)
367 ret = state2;
368 else if (state2 == &ghost)
369 ret = state1;
370 else if (!state1 || !state2)
371 ret = &undefined;
372 else
373 ret = &merged;
374 return ret;
377 struct sm_state *merge_sm_states(struct sm_state *one, struct sm_state *two)
379 struct smatch_state *s;
380 struct sm_state *result;
381 static int warned;
383 if (one == two)
384 return one;
385 if (out_of_memory()) {
386 if (!warned)
387 sm_warning("Function too hairy. No more merges.");
388 warned = 1;
389 return one;
391 warned = 0;
392 s = merge_states(one->owner, one->name, one->sym, one->state, two->state);
393 result = alloc_state_no_name(one->owner, one->name, one->sym, s);
394 result->merged = 1;
395 result->left = one;
396 result->right = two;
397 if (one->nr_children + two->nr_children <= MAX_CHILDREN)
398 result->nr_children = one->nr_children + two->nr_children;
399 else
400 result->nr_children = MAX_CHILDREN;
401 copy_possibles(result, one);
402 copy_possibles(result, two);
405 * The ->line information is used by deref_check where we complain about
406 * checking pointers that have already been dereferenced. Let's say we
407 * dereference a pointer on both the true and false paths and then merge
408 * the states here. The result state is &derefed, but the ->line number
409 * is on the line where the pointer is merged not where it was
410 * dereferenced..
412 * So in that case, let's just pick one dereference and set the ->line
413 * to point at it.
417 if (result->state == one->state)
418 result->line = one->line;
419 if (result->state == two->state)
420 result->line = two->line;
422 if (option_debug ||
423 strcmp(check_name(one->owner), option_debug_check) == 0) {
424 struct sm_state *tmp;
425 int i = 0;
427 printf("%s:%d %s() merge [%s] '%s' %s(L %d) + %s(L %d) => %s (",
428 get_filename(), get_lineno(), get_function(),
429 check_name(one->owner), one->name,
430 show_state(one->state), one->line,
431 show_state(two->state), two->line,
432 show_state(s));
434 FOR_EACH_PTR(result->possible, tmp) {
435 if (i++)
436 printf(", ");
437 printf("%s", show_state(tmp->state));
438 } END_FOR_EACH_PTR(tmp);
439 printf(")\n");
442 return result;
445 struct sm_state *get_sm_state_stree(struct stree *stree, int owner, const char *name,
446 struct symbol *sym)
448 struct tracker tracker = {
449 .owner = owner,
450 .name = (char *)name,
451 .sym = sym,
454 if (!name)
455 return NULL;
458 return avl_lookup(stree, (struct sm_state *)&tracker);
461 struct smatch_state *get_state_stree(struct stree *stree,
462 int owner, const char *name,
463 struct symbol *sym)
465 struct sm_state *sm;
467 sm = get_sm_state_stree(stree, owner, name, sym);
468 if (sm)
469 return sm->state;
470 return NULL;
473 /* FIXME: this is almost exactly the same as set_sm_state_slist() */
474 void overwrite_sm_state_stree(struct stree **stree, struct sm_state *new)
476 avl_insert(stree, new);
479 void overwrite_sm_state_stree_stack(struct stree_stack **stack,
480 struct sm_state *sm)
482 struct stree *stree;
484 stree = pop_stree(stack);
485 overwrite_sm_state_stree(&stree, sm);
486 push_stree(stack, stree);
489 struct sm_state *set_state_stree(struct stree **stree, int owner, const char *name,
490 struct symbol *sym, struct smatch_state *state)
492 struct sm_state *new = alloc_sm_state(owner, name, sym, state);
494 avl_insert(stree, new);
495 return new;
498 void set_state_stree_perm(struct stree **stree, int owner, const char *name,
499 struct symbol *sym, struct smatch_state *state)
501 struct sm_state *sm;
503 sm = malloc(sizeof(*sm) + strlen(name) + 1);
504 memset(sm, 0, sizeof(*sm));
505 sm->owner = owner;
506 sm->name = (char *)(sm + 1);
507 strcpy((char *)sm->name, name);
508 sm->sym = sym;
509 sm->state = state;
511 overwrite_sm_state_stree(stree, sm);
514 void delete_state_stree(struct stree **stree, int owner, const char *name,
515 struct symbol *sym)
517 struct tracker tracker = {
518 .owner = owner,
519 .name = (char *)name,
520 .sym = sym,
523 avl_remove(stree, (struct sm_state *)&tracker);
526 void delete_state_stree_stack(struct stree_stack **stack, int owner, const char *name,
527 struct symbol *sym)
529 struct stree *stree;
531 stree = pop_stree(stack);
532 delete_state_stree(&stree, owner, name, sym);
533 push_stree(stack, stree);
536 void push_stree(struct stree_stack **stack, struct stree *stree)
538 add_ptr_list(stack, stree);
541 struct stree *pop_stree(struct stree_stack **stack)
543 struct stree *stree;
545 stree = last_ptr_list((struct ptr_list *)*stack);
546 delete_ptr_list_last((struct ptr_list **)stack);
547 return stree;
550 struct stree *top_stree(struct stree_stack *stack)
552 return last_ptr_list((struct ptr_list *)stack);
555 void free_slist(struct state_list **slist)
557 __free_ptr_list((struct ptr_list **)slist);
560 void free_stree_stack(struct stree_stack **stack)
562 __free_ptr_list((struct ptr_list **)stack);
565 void free_stack_and_strees(struct stree_stack **stree_stack)
567 struct stree *stree;
569 FOR_EACH_PTR(*stree_stack, stree) {
570 free_stree(&stree);
571 } END_FOR_EACH_PTR(stree);
572 free_stree_stack(stree_stack);
575 struct sm_state *set_state_stree_stack(struct stree_stack **stack, int owner, const char *name,
576 struct symbol *sym, struct smatch_state *state)
578 struct stree *stree;
579 struct sm_state *sm;
581 stree = pop_stree(stack);
582 sm = set_state_stree(&stree, owner, name, sym, state);
583 push_stree(stack, stree);
585 return sm;
589 * get_sm_state_stack() gets the state for the top slist on the stack.
591 struct sm_state *get_sm_state_stree_stack(struct stree_stack *stack,
592 int owner, const char *name,
593 struct symbol *sym)
595 struct stree *stree;
596 struct sm_state *ret;
598 stree = pop_stree(&stack);
599 ret = get_sm_state_stree(stree, owner, name, sym);
600 push_stree(&stack, stree);
601 return ret;
604 struct smatch_state *get_state_stree_stack(struct stree_stack *stack,
605 int owner, const char *name,
606 struct symbol *sym)
608 struct sm_state *sm;
610 sm = get_sm_state_stree_stack(stack, owner, name, sym);
611 if (sm)
612 return sm->state;
613 return NULL;
616 static void match_states_stree(struct stree **one, struct stree **two)
618 struct smatch_state *tmp_state;
619 struct sm_state *sm;
620 struct state_list *add_to_one = NULL;
621 struct state_list *add_to_two = NULL;
622 AvlIter one_iter;
623 AvlIter two_iter;
625 avl_iter_begin(&one_iter, *one, FORWARD);
626 avl_iter_begin(&two_iter, *two, FORWARD);
628 for (;;) {
629 if (!one_iter.sm && !two_iter.sm)
630 break;
631 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
632 __set_fake_cur_stree_fast(*two);
633 tmp_state = __client_unmatched_state_function(one_iter.sm);
634 __pop_fake_cur_stree_fast();
635 sm = alloc_state_no_name(one_iter.sm->owner, one_iter.sm->name,
636 one_iter.sm->sym, tmp_state);
637 add_ptr_list(&add_to_two, sm);
638 avl_iter_next(&one_iter);
639 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
640 avl_iter_next(&one_iter);
641 avl_iter_next(&two_iter);
642 } else {
643 __set_fake_cur_stree_fast(*one);
644 tmp_state = __client_unmatched_state_function(two_iter.sm);
645 __pop_fake_cur_stree_fast();
646 sm = alloc_state_no_name(two_iter.sm->owner, two_iter.sm->name,
647 two_iter.sm->sym, tmp_state);
648 add_ptr_list(&add_to_one, sm);
649 avl_iter_next(&two_iter);
653 FOR_EACH_PTR(add_to_one, sm) {
654 avl_insert(one, sm);
655 } END_FOR_EACH_PTR(sm);
657 FOR_EACH_PTR(add_to_two, sm) {
658 avl_insert(two, sm);
659 } END_FOR_EACH_PTR(sm);
661 free_slist(&add_to_one);
662 free_slist(&add_to_two);
665 static void call_pre_merge_hooks(struct stree **one, struct stree **two)
667 struct sm_state *sm, *other;
669 save_all_states();
671 __swap_cur_stree(*one);
672 FOR_EACH_SM(*two, sm) {
673 other = get_sm_state(sm->owner, sm->name, sm->sym);
674 if (other == sm)
675 continue;
676 call_pre_merge_hook(sm);
677 } END_FOR_EACH_SM(sm);
678 *one = clone_stree(__get_cur_stree());
680 __swap_cur_stree(*two);
681 FOR_EACH_SM(*one, sm) {
682 other = get_sm_state(sm->owner, sm->name, sm->sym);
683 if (other == sm)
684 continue;
685 call_pre_merge_hook(sm);
686 } END_FOR_EACH_SM(sm);
687 *two = clone_stree(__get_cur_stree());
689 restore_all_states();
692 static void clone_pool_havers_stree(struct stree **stree)
694 struct sm_state *sm, *tmp;
695 struct state_list *slist = NULL;
697 FOR_EACH_SM(*stree, sm) {
698 if (sm->pool) {
699 tmp = clone_sm(sm);
700 add_ptr_list(&slist, tmp);
702 } END_FOR_EACH_SM(sm);
704 FOR_EACH_PTR(slist, sm) {
705 avl_insert(stree, sm);
706 } END_FOR_EACH_PTR(sm);
708 free_slist(&slist);
711 int __stree_id;
714 * merge_slist() is called whenever paths merge, such as after
715 * an if statement. It takes the two slists and creates one.
717 static void __merge_stree(struct stree **to, struct stree *stree, int add_pool)
719 struct stree *results = NULL;
720 struct stree *implied_one = NULL;
721 struct stree *implied_two = NULL;
722 AvlIter one_iter;
723 AvlIter two_iter;
724 struct sm_state *one, *two, *res;
726 if (out_of_memory())
727 return;
729 /* merging a null and nonnull path gives you only the nonnull path */
730 if (!stree)
731 return;
732 if (*to == stree)
733 return;
735 if (!*to) {
736 *to = clone_stree(stree);
737 return;
740 implied_one = clone_stree(*to);
741 implied_two = clone_stree(stree);
743 match_states_stree(&implied_one, &implied_two);
744 call_pre_merge_hooks(&implied_one, &implied_two);
746 if (add_pool) {
747 clone_pool_havers_stree(&implied_one);
748 clone_pool_havers_stree(&implied_two);
750 set_stree_id(&implied_one, ++__stree_id);
751 set_stree_id(&implied_two, ++__stree_id);
752 if (implied_one->base_stree)
753 set_stree_id(&implied_one->base_stree, ++__stree_id);
754 if (implied_two->base_stree)
755 set_stree_id(&implied_two->base_stree, ++__stree_id);
758 push_stree(&all_pools, implied_one);
759 push_stree(&all_pools, implied_two);
761 avl_iter_begin(&one_iter, implied_one, FORWARD);
762 avl_iter_begin(&two_iter, implied_two, FORWARD);
764 for (;;) {
765 if (!one_iter.sm || !two_iter.sm)
766 break;
768 one = one_iter.sm;
769 two = two_iter.sm;
771 if (one == two) {
772 avl_insert(&results, one);
773 goto next;
776 if (add_pool) {
777 one->pool = implied_one;
778 if (implied_one->base_stree)
779 one->pool = implied_one->base_stree;
780 two->pool = implied_two;
781 if (implied_two->base_stree)
782 two->pool = implied_two->base_stree;
784 res = merge_sm_states(one, two);
785 add_possible_sm(res, one);
786 add_possible_sm(res, two);
787 avl_insert(&results, res);
788 next:
789 avl_iter_next(&one_iter);
790 avl_iter_next(&two_iter);
793 free_stree(to);
794 *to = results;
797 void merge_stree(struct stree **to, struct stree *stree)
799 __merge_stree(to, stree, 1);
802 void merge_stree_no_pools(struct stree **to, struct stree *stree)
804 __merge_stree(to, stree, 0);
808 * This is unfortunately a bit subtle... The problem is that if a
809 * state is set on one fake stree but not the other then we should
810 * look up the the original state and use that as the unset state.
811 * Fortunately, after you pop your fake stree then the cur_slist should
812 * reflect the original state.
814 void merge_fake_stree(struct stree **to, struct stree *stree)
816 struct stree *one = *to;
817 struct stree *two = stree;
818 struct sm_state *sm;
819 struct state_list *add_to_one = NULL;
820 struct state_list *add_to_two = NULL;
821 AvlIter one_iter;
822 AvlIter two_iter;
824 if (!stree)
825 return;
826 if (*to == stree)
827 return;
828 if (!*to) {
829 *to = clone_stree(stree);
830 return;
833 avl_iter_begin(&one_iter, one, FORWARD);
834 avl_iter_begin(&two_iter, two, FORWARD);
836 for (;;) {
837 if (!one_iter.sm && !two_iter.sm)
838 break;
839 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
840 sm = get_sm_state(one_iter.sm->owner, one_iter.sm->name,
841 one_iter.sm->sym);
842 if (sm)
843 add_ptr_list(&add_to_two, sm);
844 avl_iter_next(&one_iter);
845 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
846 avl_iter_next(&one_iter);
847 avl_iter_next(&two_iter);
848 } else {
849 sm = get_sm_state(two_iter.sm->owner, two_iter.sm->name,
850 two_iter.sm->sym);
851 if (sm)
852 add_ptr_list(&add_to_one, sm);
853 avl_iter_next(&two_iter);
857 FOR_EACH_PTR(add_to_one, sm) {
858 avl_insert(&one, sm);
859 } END_FOR_EACH_PTR(sm);
861 FOR_EACH_PTR(add_to_two, sm) {
862 avl_insert(&two, sm);
863 } END_FOR_EACH_PTR(sm);
865 one->base_stree = clone_stree(__get_cur_stree());
866 FOR_EACH_SM(one, sm) {
867 avl_insert(&one->base_stree, sm);
868 } END_FOR_EACH_SM(sm);
870 two->base_stree = clone_stree(__get_cur_stree());
871 FOR_EACH_SM(two, sm) {
872 avl_insert(&two->base_stree, sm);
873 } END_FOR_EACH_SM(sm);
875 free_slist(&add_to_one);
876 free_slist(&add_to_two);
878 __merge_stree(&one, two, 1);
880 *to = one;
884 * filter_slist() removes any sm states "slist" holds in common with "filter"
886 void filter_stree(struct stree **stree, struct stree *filter)
888 struct stree *results = NULL;
889 AvlIter one_iter;
890 AvlIter two_iter;
892 avl_iter_begin(&one_iter, *stree, FORWARD);
893 avl_iter_begin(&two_iter, filter, FORWARD);
895 /* FIXME: This should probably be re-written with trees in mind */
897 for (;;) {
898 if (!one_iter.sm && !two_iter.sm)
899 break;
900 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
901 avl_insert(&results, one_iter.sm);
902 avl_iter_next(&one_iter);
903 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
904 if (one_iter.sm != two_iter.sm)
905 avl_insert(&results, one_iter.sm);
906 avl_iter_next(&one_iter);
907 avl_iter_next(&two_iter);
908 } else {
909 avl_iter_next(&two_iter);
913 free_stree(stree);
914 *stree = results;
919 * and_slist_stack() pops the top two slists, overwriting the one with
920 * the other and pushing it back on the stack.
922 void and_stree_stack(struct stree_stack **stack)
924 struct sm_state *tmp;
925 struct stree *right_stree = pop_stree(stack);
927 FOR_EACH_SM(right_stree, tmp) {
928 overwrite_sm_state_stree_stack(stack, tmp);
929 } END_FOR_EACH_SM(tmp);
930 free_stree(&right_stree);
934 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
935 * It pops the two slists from the top of the stack and merges them
936 * together in a way that preserves the things they have in common
937 * but creates a merged state for most of the rest.
938 * You could have code that had: if (foo || foo) { foo->baz;
939 * It's this function which ensures smatch does the right thing.
941 void or_stree_stack(struct stree_stack **pre_conds,
942 struct stree *cur_stree,
943 struct stree_stack **stack)
945 struct stree *new;
946 struct stree *old;
947 struct stree *pre_stree;
948 struct stree *res;
949 struct stree *tmp_stree;
951 new = pop_stree(stack);
952 old = pop_stree(stack);
954 pre_stree = pop_stree(pre_conds);
955 push_stree(pre_conds, clone_stree(pre_stree));
957 res = clone_stree(pre_stree);
958 overwrite_stree(old, &res);
960 tmp_stree = clone_stree(cur_stree);
961 overwrite_stree(new, &tmp_stree);
963 merge_stree(&res, tmp_stree);
964 filter_stree(&res, pre_stree);
966 push_stree(stack, res);
967 free_stree(&tmp_stree);
968 free_stree(&pre_stree);
969 free_stree(&new);
970 free_stree(&old);
974 * get_named_stree() is only used for gotos.
976 struct stree **get_named_stree(struct named_stree_stack *stack,
977 const char *name,
978 struct symbol *sym)
980 struct named_stree *tmp;
982 FOR_EACH_PTR(stack, tmp) {
983 if (tmp->sym == sym &&
984 strcmp(tmp->name, name) == 0)
985 return &tmp->stree;
986 } END_FOR_EACH_PTR(tmp);
987 return NULL;
990 /* FIXME: These parameters are in a different order from expected */
991 void overwrite_stree(struct stree *from, struct stree **to)
993 struct sm_state *tmp;
995 FOR_EACH_SM(from, tmp) {
996 overwrite_sm_state_stree(to, tmp);
997 } END_FOR_EACH_SM(tmp);