locking: spin_trylock() returns 1 on success
[smatch.git] / smatch_slist.c
blob8cc9a3ea3bbc6fbfe103ea29c1f7db7eccbe05fd
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_possible_sm(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_possible_sm(tmp, new, preserve) < 0)
206 continue;
207 else if (cmp_possible_sm(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 *one, struct sm_state *two)
219 struct sm_state *large = one;
220 struct sm_state *small = two;
221 struct sm_state *tmp;
224 * We spend a lot of time copying the possible lists. I've tried to
225 * optimize the process a bit.
229 if (ptr_list_size((struct ptr_list *)two->possible) >
230 ptr_list_size((struct ptr_list *)one->possible)) {
231 large = two;
232 small = one;
235 to->possible = clone_slist(large->possible);
236 add_possible_sm(to, to);
237 FOR_EACH_PTR(small->possible, tmp) {
238 add_possible_sm(to, tmp);
239 } END_FOR_EACH_PTR(tmp);
242 char *alloc_sname(const char *str)
244 char *tmp;
246 if (!str)
247 return NULL;
248 tmp = __alloc_sname(strlen(str) + 1);
249 strcpy(tmp, str);
250 return tmp;
253 int out_of_memory(void)
256 * I decided to use 50M here based on trial and error.
257 * It works out OK for the kernel and so it should work
258 * for most other projects as well.
260 if (sm_state_counter * sizeof(struct sm_state) >= 100000000)
261 return 1;
262 return 0;
265 int low_on_memory(void)
267 if (sm_state_counter * sizeof(struct sm_state) >= 25000000)
268 return 1;
269 return 0;
272 static void free_sm_state(struct sm_state *sm)
274 free_slist(&sm->possible);
276 * fixme. Free the actual state.
277 * Right now we leave it until the end of the function
278 * because we don't want to double free it.
279 * Use the freelist to not double free things
283 static void free_all_sm_states(struct allocation_blob *blob)
285 unsigned int size = sizeof(struct sm_state);
286 unsigned int offset = 0;
288 while (offset < blob->offset) {
289 free_sm_state((struct sm_state *)(blob->data + offset));
290 offset += size;
294 /* At the end of every function we free all the sm_states */
295 void free_every_single_sm_state(void)
297 struct allocator_struct *desc = &sm_state_allocator;
298 struct allocation_blob *blob = desc->blobs;
300 desc->blobs = NULL;
301 desc->allocations = 0;
302 desc->total_bytes = 0;
303 desc->useful_bytes = 0;
304 desc->freelist = NULL;
305 while (blob) {
306 struct allocation_blob *next = blob->next;
307 free_all_sm_states(blob);
308 blob_free(blob, desc->chunking);
309 blob = next;
311 clear_sname_alloc();
312 clear_smatch_state_alloc();
314 free_stack_and_strees(&all_pools);
315 sm_state_counter = 0;
318 unsigned long get_pool_count(void)
320 return ptr_list_size((struct ptr_list *)all_pools);
323 struct sm_state *clone_sm(struct sm_state *s)
325 struct sm_state *ret;
327 ret = alloc_state_no_name(s->owner, s->name, s->sym, s->state);
328 ret->merged = s->merged;
329 ret->line = s->line;
330 /* clone_sm() doesn't copy the pools. Each state needs to have
331 only one pool. */
332 ret->possible = clone_slist(s->possible);
333 ret->left = s->left;
334 ret->right = s->right;
335 ret->nr_children = s->nr_children;
336 return ret;
339 int is_merged(struct sm_state *sm)
341 return sm->merged;
344 int is_leaf(struct sm_state *sm)
346 return !sm->merged;
349 int slist_has_state(struct state_list *slist, struct smatch_state *state)
351 struct sm_state *tmp;
353 FOR_EACH_PTR(slist, tmp) {
354 if (tmp->state == state)
355 return 1;
356 } END_FOR_EACH_PTR(tmp);
357 return 0;
360 struct state_list *clone_slist(struct state_list *from_slist)
362 struct sm_state *sm;
363 struct state_list *to_slist = NULL;
365 FOR_EACH_PTR(from_slist, sm) {
366 add_ptr_list(&to_slist, sm);
367 } END_FOR_EACH_PTR(sm);
368 return to_slist;
371 static struct smatch_state *merge_states(int owner, const char *name,
372 struct symbol *sym,
373 struct smatch_state *state1,
374 struct smatch_state *state2)
376 struct smatch_state *ret;
378 if (state1 == state2)
379 ret = state1;
380 else if (__has_merge_function(owner))
381 ret = __client_merge_function(owner, state1, state2);
382 else if (state1 == &ghost)
383 ret = state2;
384 else if (state2 == &ghost)
385 ret = state1;
386 else if (!state1 || !state2)
387 ret = &undefined;
388 else
389 ret = &merged;
390 return ret;
393 struct sm_state *merge_sm_states(struct sm_state *one, struct sm_state *two)
395 struct smatch_state *s;
396 struct sm_state *result;
397 static int warned;
399 if (one == two)
400 return one;
401 if (out_of_memory()) {
402 if (!warned)
403 sm_warning("Function too hairy. No more merges.");
404 warned = 1;
405 return one;
407 warned = 0;
408 s = merge_states(one->owner, one->name, one->sym, one->state, two->state);
409 result = alloc_state_no_name(one->owner, one->name, one->sym, s);
410 result->merged = 1;
411 result->left = one;
412 result->right = two;
413 if (one->nr_children + two->nr_children <= MAX_CHILDREN)
414 result->nr_children = one->nr_children + two->nr_children;
415 else
416 result->nr_children = MAX_CHILDREN;
418 copy_possibles(result, one, two);
421 * The ->line information is used by deref_check where we complain about
422 * checking pointers that have already been dereferenced. Let's say we
423 * dereference a pointer on both the true and false paths and then merge
424 * the states here. The result state is &derefed, but the ->line number
425 * is on the line where the pointer is merged not where it was
426 * dereferenced..
428 * So in that case, let's just pick one dereference and set the ->line
429 * to point at it.
433 if (result->state == one->state)
434 result->line = one->line;
435 if (result->state == two->state)
436 result->line = two->line;
438 if (option_debug ||
439 strcmp(check_name(one->owner), option_debug_check) == 0) {
440 struct sm_state *tmp;
441 int i = 0;
443 printf("%s:%d %s() merge [%s] '%s' %s(L %d) + %s(L %d) => %s (",
444 get_filename(), get_lineno(), get_function(),
445 check_name(one->owner), one->name,
446 show_state(one->state), one->line,
447 show_state(two->state), two->line,
448 show_state(s));
450 FOR_EACH_PTR(result->possible, tmp) {
451 if (i++)
452 printf(", ");
453 printf("%s", show_state(tmp->state));
454 } END_FOR_EACH_PTR(tmp);
455 printf(")\n");
458 return result;
461 struct sm_state *get_sm_state_stree(struct stree *stree, int owner, const char *name,
462 struct symbol *sym)
464 struct tracker tracker = {
465 .owner = owner,
466 .name = (char *)name,
467 .sym = sym,
470 if (!name)
471 return NULL;
474 return avl_lookup(stree, (struct sm_state *)&tracker);
477 struct smatch_state *get_state_stree(struct stree *stree,
478 int owner, const char *name,
479 struct symbol *sym)
481 struct sm_state *sm;
483 sm = get_sm_state_stree(stree, owner, name, sym);
484 if (sm)
485 return sm->state;
486 return NULL;
489 /* FIXME: this is almost exactly the same as set_sm_state_slist() */
490 void overwrite_sm_state_stree(struct stree **stree, struct sm_state *new)
492 avl_insert(stree, new);
495 void overwrite_sm_state_stree_stack(struct stree_stack **stack,
496 struct sm_state *sm)
498 struct stree *stree;
500 stree = pop_stree(stack);
501 overwrite_sm_state_stree(&stree, sm);
502 push_stree(stack, stree);
505 struct sm_state *set_state_stree(struct stree **stree, int owner, const char *name,
506 struct symbol *sym, struct smatch_state *state)
508 struct sm_state *new = alloc_sm_state(owner, name, sym, state);
510 avl_insert(stree, new);
511 return new;
514 void set_state_stree_perm(struct stree **stree, int owner, const char *name,
515 struct symbol *sym, struct smatch_state *state)
517 struct sm_state *sm;
519 sm = malloc(sizeof(*sm) + strlen(name) + 1);
520 memset(sm, 0, sizeof(*sm));
521 sm->owner = owner;
522 sm->name = (char *)(sm + 1);
523 strcpy((char *)sm->name, name);
524 sm->sym = sym;
525 sm->state = state;
527 overwrite_sm_state_stree(stree, sm);
530 void delete_state_stree(struct stree **stree, int owner, const char *name,
531 struct symbol *sym)
533 struct tracker tracker = {
534 .owner = owner,
535 .name = (char *)name,
536 .sym = sym,
539 avl_remove(stree, (struct sm_state *)&tracker);
542 void delete_state_stree_stack(struct stree_stack **stack, int owner, const char *name,
543 struct symbol *sym)
545 struct stree *stree;
547 stree = pop_stree(stack);
548 delete_state_stree(&stree, owner, name, sym);
549 push_stree(stack, stree);
552 void push_stree(struct stree_stack **stack, struct stree *stree)
554 add_ptr_list(stack, stree);
557 struct stree *pop_stree(struct stree_stack **stack)
559 struct stree *stree;
561 stree = last_ptr_list((struct ptr_list *)*stack);
562 delete_ptr_list_last((struct ptr_list **)stack);
563 return stree;
566 struct stree *top_stree(struct stree_stack *stack)
568 return last_ptr_list((struct ptr_list *)stack);
571 void free_slist(struct state_list **slist)
573 __free_ptr_list((struct ptr_list **)slist);
576 void free_stree_stack(struct stree_stack **stack)
578 __free_ptr_list((struct ptr_list **)stack);
581 void free_stack_and_strees(struct stree_stack **stree_stack)
583 struct stree *stree;
585 FOR_EACH_PTR(*stree_stack, stree) {
586 free_stree(&stree);
587 } END_FOR_EACH_PTR(stree);
588 free_stree_stack(stree_stack);
591 struct sm_state *set_state_stree_stack(struct stree_stack **stack, int owner, const char *name,
592 struct symbol *sym, struct smatch_state *state)
594 struct stree *stree;
595 struct sm_state *sm;
597 stree = pop_stree(stack);
598 sm = set_state_stree(&stree, owner, name, sym, state);
599 push_stree(stack, stree);
601 return sm;
605 * get_sm_state_stack() gets the state for the top slist on the stack.
607 struct sm_state *get_sm_state_stree_stack(struct stree_stack *stack,
608 int owner, const char *name,
609 struct symbol *sym)
611 struct stree *stree;
612 struct sm_state *ret;
614 stree = pop_stree(&stack);
615 ret = get_sm_state_stree(stree, owner, name, sym);
616 push_stree(&stack, stree);
617 return ret;
620 struct smatch_state *get_state_stree_stack(struct stree_stack *stack,
621 int owner, const char *name,
622 struct symbol *sym)
624 struct sm_state *sm;
626 sm = get_sm_state_stree_stack(stack, owner, name, sym);
627 if (sm)
628 return sm->state;
629 return NULL;
632 static void match_states_stree(struct stree **one, struct stree **two)
634 struct smatch_state *tmp_state;
635 struct sm_state *sm;
636 struct state_list *add_to_one = NULL;
637 struct state_list *add_to_two = NULL;
638 AvlIter one_iter;
639 AvlIter two_iter;
641 avl_iter_begin(&one_iter, *one, FORWARD);
642 avl_iter_begin(&two_iter, *two, FORWARD);
644 for (;;) {
645 if (!one_iter.sm && !two_iter.sm)
646 break;
647 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
648 __set_fake_cur_stree_fast(*two);
649 tmp_state = __client_unmatched_state_function(one_iter.sm);
650 __pop_fake_cur_stree_fast();
651 sm = alloc_state_no_name(one_iter.sm->owner, one_iter.sm->name,
652 one_iter.sm->sym, tmp_state);
653 add_ptr_list(&add_to_two, sm);
654 avl_iter_next(&one_iter);
655 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
656 avl_iter_next(&one_iter);
657 avl_iter_next(&two_iter);
658 } else {
659 __set_fake_cur_stree_fast(*one);
660 tmp_state = __client_unmatched_state_function(two_iter.sm);
661 __pop_fake_cur_stree_fast();
662 sm = alloc_state_no_name(two_iter.sm->owner, two_iter.sm->name,
663 two_iter.sm->sym, tmp_state);
664 add_ptr_list(&add_to_one, sm);
665 avl_iter_next(&two_iter);
669 FOR_EACH_PTR(add_to_one, sm) {
670 avl_insert(one, sm);
671 } END_FOR_EACH_PTR(sm);
673 FOR_EACH_PTR(add_to_two, sm) {
674 avl_insert(two, sm);
675 } END_FOR_EACH_PTR(sm);
677 free_slist(&add_to_one);
678 free_slist(&add_to_two);
681 static void call_pre_merge_hooks(struct stree **one, struct stree **two)
683 struct sm_state *sm, *other;
685 save_all_states();
687 __swap_cur_stree(*one);
688 FOR_EACH_SM(*two, sm) {
689 other = get_sm_state(sm->owner, sm->name, sm->sym);
690 if (other == sm)
691 continue;
692 call_pre_merge_hook(sm);
693 } END_FOR_EACH_SM(sm);
694 *one = clone_stree(__get_cur_stree());
696 __swap_cur_stree(*two);
697 FOR_EACH_SM(*one, sm) {
698 other = get_sm_state(sm->owner, sm->name, sm->sym);
699 if (other == sm)
700 continue;
701 call_pre_merge_hook(sm);
702 } END_FOR_EACH_SM(sm);
703 *two = clone_stree(__get_cur_stree());
705 restore_all_states();
708 static void clone_pool_havers_stree(struct stree **stree)
710 struct sm_state *sm, *tmp;
711 struct state_list *slist = NULL;
713 FOR_EACH_SM(*stree, sm) {
714 if (sm->pool) {
715 tmp = clone_sm(sm);
716 add_ptr_list(&slist, tmp);
718 } END_FOR_EACH_SM(sm);
720 FOR_EACH_PTR(slist, sm) {
721 avl_insert(stree, sm);
722 } END_FOR_EACH_PTR(sm);
724 free_slist(&slist);
727 int __stree_id;
730 * merge_slist() is called whenever paths merge, such as after
731 * an if statement. It takes the two slists and creates one.
733 static void __merge_stree(struct stree **to, struct stree *stree, int add_pool)
735 struct stree *results = NULL;
736 struct stree *implied_one = NULL;
737 struct stree *implied_two = NULL;
738 AvlIter one_iter;
739 AvlIter two_iter;
740 struct sm_state *one, *two, *res;
742 if (out_of_memory())
743 return;
745 /* merging a null and nonnull path gives you only the nonnull path */
746 if (!stree)
747 return;
748 if (*to == stree)
749 return;
751 if (!*to) {
752 *to = clone_stree(stree);
753 return;
756 implied_one = clone_stree(*to);
757 implied_two = clone_stree(stree);
759 match_states_stree(&implied_one, &implied_two);
760 call_pre_merge_hooks(&implied_one, &implied_two);
762 if (add_pool) {
763 clone_pool_havers_stree(&implied_one);
764 clone_pool_havers_stree(&implied_two);
766 set_stree_id(&implied_one, ++__stree_id);
767 set_stree_id(&implied_two, ++__stree_id);
768 if (implied_one->base_stree)
769 set_stree_id(&implied_one->base_stree, ++__stree_id);
770 if (implied_two->base_stree)
771 set_stree_id(&implied_two->base_stree, ++__stree_id);
774 push_stree(&all_pools, implied_one);
775 push_stree(&all_pools, implied_two);
777 avl_iter_begin(&one_iter, implied_one, FORWARD);
778 avl_iter_begin(&two_iter, implied_two, FORWARD);
780 for (;;) {
781 if (!one_iter.sm || !two_iter.sm)
782 break;
784 one = one_iter.sm;
785 two = two_iter.sm;
787 if (one == two) {
788 avl_insert(&results, one);
789 goto next;
792 if (add_pool) {
793 one->pool = implied_one;
794 if (implied_one->base_stree)
795 one->pool = implied_one->base_stree;
796 two->pool = implied_two;
797 if (implied_two->base_stree)
798 two->pool = implied_two->base_stree;
800 res = merge_sm_states(one, two);
801 add_possible_sm(res, one);
802 add_possible_sm(res, two);
803 avl_insert(&results, res);
804 next:
805 avl_iter_next(&one_iter);
806 avl_iter_next(&two_iter);
809 free_stree(to);
810 *to = results;
813 void merge_stree(struct stree **to, struct stree *stree)
815 __merge_stree(to, stree, 1);
818 void merge_stree_no_pools(struct stree **to, struct stree *stree)
820 __merge_stree(to, stree, 0);
824 * This is unfortunately a bit subtle... The problem is that if a
825 * state is set on one fake stree but not the other then we should
826 * look up the the original state and use that as the unset state.
827 * Fortunately, after you pop your fake stree then the cur_slist should
828 * reflect the original state.
830 void merge_fake_stree(struct stree **to, struct stree *stree)
832 struct stree *one = *to;
833 struct stree *two = stree;
834 struct sm_state *sm;
835 struct state_list *add_to_one = NULL;
836 struct state_list *add_to_two = NULL;
837 AvlIter one_iter;
838 AvlIter two_iter;
840 if (!stree)
841 return;
842 if (*to == stree)
843 return;
844 if (!*to) {
845 *to = clone_stree(stree);
846 return;
849 avl_iter_begin(&one_iter, one, FORWARD);
850 avl_iter_begin(&two_iter, two, FORWARD);
852 for (;;) {
853 if (!one_iter.sm && !two_iter.sm)
854 break;
855 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
856 sm = get_sm_state(one_iter.sm->owner, one_iter.sm->name,
857 one_iter.sm->sym);
858 if (sm)
859 add_ptr_list(&add_to_two, sm);
860 avl_iter_next(&one_iter);
861 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
862 avl_iter_next(&one_iter);
863 avl_iter_next(&two_iter);
864 } else {
865 sm = get_sm_state(two_iter.sm->owner, two_iter.sm->name,
866 two_iter.sm->sym);
867 if (sm)
868 add_ptr_list(&add_to_one, sm);
869 avl_iter_next(&two_iter);
873 FOR_EACH_PTR(add_to_one, sm) {
874 avl_insert(&one, sm);
875 } END_FOR_EACH_PTR(sm);
877 FOR_EACH_PTR(add_to_two, sm) {
878 avl_insert(&two, sm);
879 } END_FOR_EACH_PTR(sm);
881 one->base_stree = clone_stree(__get_cur_stree());
882 FOR_EACH_SM(one, sm) {
883 avl_insert(&one->base_stree, sm);
884 } END_FOR_EACH_SM(sm);
886 two->base_stree = clone_stree(__get_cur_stree());
887 FOR_EACH_SM(two, sm) {
888 avl_insert(&two->base_stree, sm);
889 } END_FOR_EACH_SM(sm);
891 free_slist(&add_to_one);
892 free_slist(&add_to_two);
894 __merge_stree(&one, two, 1);
896 *to = one;
900 * filter_slist() removes any sm states "slist" holds in common with "filter"
902 void filter_stree(struct stree **stree, struct stree *filter)
904 struct stree *results = NULL;
905 AvlIter one_iter;
906 AvlIter two_iter;
908 avl_iter_begin(&one_iter, *stree, FORWARD);
909 avl_iter_begin(&two_iter, filter, FORWARD);
911 /* FIXME: This should probably be re-written with trees in mind */
913 for (;;) {
914 if (!one_iter.sm && !two_iter.sm)
915 break;
916 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
917 avl_insert(&results, one_iter.sm);
918 avl_iter_next(&one_iter);
919 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
920 if (one_iter.sm != two_iter.sm)
921 avl_insert(&results, one_iter.sm);
922 avl_iter_next(&one_iter);
923 avl_iter_next(&two_iter);
924 } else {
925 avl_iter_next(&two_iter);
929 free_stree(stree);
930 *stree = results;
935 * and_slist_stack() pops the top two slists, overwriting the one with
936 * the other and pushing it back on the stack.
938 void and_stree_stack(struct stree_stack **stack)
940 struct sm_state *tmp;
941 struct stree *right_stree = pop_stree(stack);
943 FOR_EACH_SM(right_stree, tmp) {
944 overwrite_sm_state_stree_stack(stack, tmp);
945 } END_FOR_EACH_SM(tmp);
946 free_stree(&right_stree);
950 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
951 * It pops the two slists from the top of the stack and merges them
952 * together in a way that preserves the things they have in common
953 * but creates a merged state for most of the rest.
954 * You could have code that had: if (foo || foo) { foo->baz;
955 * It's this function which ensures smatch does the right thing.
957 void or_stree_stack(struct stree_stack **pre_conds,
958 struct stree *cur_stree,
959 struct stree_stack **stack)
961 struct stree *new;
962 struct stree *old;
963 struct stree *pre_stree;
964 struct stree *res;
965 struct stree *tmp_stree;
967 new = pop_stree(stack);
968 old = pop_stree(stack);
970 pre_stree = pop_stree(pre_conds);
971 push_stree(pre_conds, clone_stree(pre_stree));
973 res = clone_stree(pre_stree);
974 overwrite_stree(old, &res);
976 tmp_stree = clone_stree(cur_stree);
977 overwrite_stree(new, &tmp_stree);
979 merge_stree(&res, tmp_stree);
980 filter_stree(&res, pre_stree);
982 push_stree(stack, res);
983 free_stree(&tmp_stree);
984 free_stree(&pre_stree);
985 free_stree(&new);
986 free_stree(&old);
990 * get_named_stree() is only used for gotos.
992 struct stree **get_named_stree(struct named_stree_stack *stack,
993 const char *name,
994 struct symbol *sym)
996 struct named_stree *tmp;
998 FOR_EACH_PTR(stack, tmp) {
999 if (tmp->sym == sym &&
1000 strcmp(tmp->name, name) == 0)
1001 return &tmp->stree;
1002 } END_FOR_EACH_PTR(tmp);
1003 return NULL;
1006 /* FIXME: These parameters are in a different order from expected */
1007 void overwrite_stree(struct stree *from, struct stree **to)
1009 struct sm_state *tmp;
1011 FOR_EACH_SM(from, tmp) {
1012 overwrite_sm_state_stree(to, tmp);
1013 } END_FOR_EACH_SM(tmp);