validation/sm_WtoA: add single quotes in output
[smatch.git] / smatch_slist.c
blob6fa4da5e730d9be927cfdc52762cfb7743727b57
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 static int sm_state_counter;
32 static struct stree_stack *all_pools;
34 char *show_sm(struct sm_state *sm)
36 static char buf[256];
37 struct sm_state *tmp;
38 int pos;
39 int i;
41 pos = snprintf(buf, sizeof(buf), "[%s] '%s' = '%s'",
42 check_name(sm->owner), sm->name, show_state(sm->state));
43 if (pos > sizeof(buf))
44 goto truncate;
46 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
47 return buf;
49 pos += snprintf(buf + pos, sizeof(buf) - pos, " (");
50 if (pos > sizeof(buf))
51 goto truncate;
52 i = 0;
53 FOR_EACH_PTR(sm->possible, tmp) {
54 if (i++)
55 pos += snprintf(buf + pos, sizeof(buf) - pos, ", ");
56 if (pos > sizeof(buf))
57 goto truncate;
58 pos += snprintf(buf + pos, sizeof(buf) - pos, "%s",
59 show_state(tmp->state));
60 if (pos > sizeof(buf))
61 goto truncate;
62 } END_FOR_EACH_PTR(tmp);
63 snprintf(buf + pos, sizeof(buf) - pos, ")");
65 return buf;
67 truncate:
68 for (i = 0; i < 3; i++)
69 buf[sizeof(buf) - 2 - i] = '.';
70 return buf;
73 void __print_stree(struct stree *stree)
75 struct sm_state *sm;
77 printf("dumping stree at %d\n", get_lineno());
78 FOR_EACH_SM(stree, sm) {
79 printf("%s\n", show_sm(sm));
80 } END_FOR_EACH_SM(sm);
81 printf("---\n");
84 /* NULL states go at the end to simplify merge_slist */
85 int cmp_tracker(const struct sm_state *a, const struct sm_state *b)
87 int ret;
89 if (a == b)
90 return 0;
91 if (!b)
92 return -1;
93 if (!a)
94 return 1;
96 if (a->owner > b->owner)
97 return -1;
98 if (a->owner < b->owner)
99 return 1;
101 ret = strcmp(a->name, b->name);
102 if (ret < 0)
103 return -1;
104 if (ret > 0)
105 return 1;
107 if (!b->sym && a->sym)
108 return -1;
109 if (!a->sym && b->sym)
110 return 1;
111 if (a->sym > b->sym)
112 return -1;
113 if (a->sym < b->sym)
114 return 1;
116 return 0;
119 static int cmp_sm_states(const struct sm_state *a, const struct sm_state *b, int preserve)
121 int ret;
123 ret = cmp_tracker(a, b);
124 if (ret)
125 return ret;
127 /* todo: add hook for smatch_extra.c */
128 if (a->state > b->state)
129 return -1;
130 if (a->state < b->state)
131 return 1;
132 /* This is obviously a massive disgusting hack but we need to preserve
133 * the unmerged states for smatch extra because we use them in
134 * smatch_db.c. Meanwhile if we preserve all the other unmerged states
135 * then it uses a lot of memory and we don't use it. Hence this hack.
137 * Also sometimes even just preserving every possible SMATCH_EXTRA state
138 * takes too much resources so we have to cap that. Capping is probably
139 * not often a problem in real life.
141 if (a->owner == SMATCH_EXTRA && preserve) {
142 if (a == b)
143 return 0;
144 if (a->merged == 1 && b->merged == 0)
145 return -1;
146 if (a->merged == 0)
147 return 1;
150 return 0;
153 struct sm_state *alloc_sm_state(int owner, const char *name,
154 struct symbol *sym, struct smatch_state *state)
156 struct sm_state *sm_state = __alloc_sm_state(0);
158 sm_state_counter++;
160 sm_state->name = alloc_sname(name);
161 sm_state->owner = owner;
162 sm_state->sym = sym;
163 sm_state->state = state;
164 sm_state->line = get_lineno();
165 sm_state->merged = 0;
166 sm_state->implied = 0;
167 sm_state->pool = NULL;
168 sm_state->left = NULL;
169 sm_state->right = NULL;
170 sm_state->nr_children = 1;
171 sm_state->possible = NULL;
172 add_ptr_list(&sm_state->possible, sm_state);
173 return sm_state;
176 static struct sm_state *alloc_state_no_name(int owner, const char *name,
177 struct symbol *sym,
178 struct smatch_state *state)
180 struct sm_state *tmp;
182 tmp = alloc_sm_state(owner, NULL, sym, state);
183 tmp->name = name;
184 return tmp;
187 int too_many_possible(struct sm_state *sm)
189 if (ptr_list_size((struct ptr_list *)sm->possible) >= 100)
190 return 1;
191 return 0;
194 void add_possible_sm(struct sm_state *to, struct sm_state *new)
196 struct sm_state *tmp;
197 int preserve = 1;
199 if (too_many_possible(to))
200 preserve = 0;
202 FOR_EACH_PTR(to->possible, tmp) {
203 if (cmp_sm_states(tmp, new, preserve) < 0)
204 continue;
205 else if (cmp_sm_states(tmp, new, preserve) == 0) {
206 return;
207 } else {
208 INSERT_CURRENT(new, tmp);
209 return;
211 } END_FOR_EACH_PTR(tmp);
212 add_ptr_list(&to->possible, new);
215 static void copy_possibles(struct sm_state *to, struct sm_state *from)
217 struct sm_state *tmp;
219 FOR_EACH_PTR(from->possible, tmp) {
220 add_possible_sm(to, tmp);
221 } END_FOR_EACH_PTR(tmp);
224 char *alloc_sname(const char *str)
226 char *tmp;
228 if (!str)
229 return NULL;
230 tmp = __alloc_sname(strlen(str) + 1);
231 strcpy(tmp, str);
232 return tmp;
235 int out_of_memory(void)
238 * I decided to use 50M here based on trial and error.
239 * It works out OK for the kernel and so it should work
240 * for most other projects as well.
242 if (sm_state_counter * sizeof(struct sm_state) >= 50000000)
243 return 1;
244 return 0;
247 int low_on_memory(void)
249 if (sm_state_counter * sizeof(struct sm_state) >= 25000000)
250 return 1;
251 return 0;
254 static void free_sm_state(struct sm_state *sm)
256 free_slist(&sm->possible);
258 * fixme. Free the actual state.
259 * Right now we leave it until the end of the function
260 * because we don't want to double free it.
261 * Use the freelist to not double free things
265 static void free_all_sm_states(struct allocation_blob *blob)
267 unsigned int size = sizeof(struct sm_state);
268 unsigned int offset = 0;
270 while (offset < blob->offset) {
271 free_sm_state((struct sm_state *)(blob->data + offset));
272 offset += size;
276 /* At the end of every function we free all the sm_states */
277 void free_every_single_sm_state(void)
279 struct allocator_struct *desc = &sm_state_allocator;
280 struct allocation_blob *blob = desc->blobs;
282 desc->blobs = NULL;
283 desc->allocations = 0;
284 desc->total_bytes = 0;
285 desc->useful_bytes = 0;
286 desc->freelist = NULL;
287 while (blob) {
288 struct allocation_blob *next = blob->next;
289 free_all_sm_states(blob);
290 blob_free(blob, desc->chunking);
291 blob = next;
293 clear_sname_alloc();
294 clear_smatch_state_alloc();
296 free_stack_and_strees(&all_pools);
297 sm_state_counter = 0;
300 struct sm_state *clone_sm(struct sm_state *s)
302 struct sm_state *ret;
304 ret = alloc_state_no_name(s->owner, s->name, s->sym, s->state);
305 ret->merged = s->merged;
306 ret->implied = s->implied;
307 ret->line = s->line;
308 /* clone_sm() doesn't copy the pools. Each state needs to have
309 only one pool. */
310 ret->possible = clone_slist(s->possible);
311 ret->left = s->left;
312 ret->right = s->right;
313 ret->nr_children = s->nr_children;
314 return ret;
317 int is_merged(struct sm_state *sm)
319 return sm->merged;
322 int is_leaf(struct sm_state *sm)
324 return !sm->merged;
327 int is_implied(struct sm_state *sm)
329 return sm->implied;
332 int slist_has_state(struct state_list *slist, struct smatch_state *state)
334 struct sm_state *tmp;
336 FOR_EACH_PTR(slist, tmp) {
337 if (tmp->state == state)
338 return 1;
339 } END_FOR_EACH_PTR(tmp);
340 return 0;
343 struct state_list *clone_slist(struct state_list *from_slist)
345 struct sm_state *sm;
346 struct state_list *to_slist = NULL;
348 FOR_EACH_PTR(from_slist, sm) {
349 add_ptr_list(&to_slist, sm);
350 } END_FOR_EACH_PTR(sm);
351 return to_slist;
354 static struct smatch_state *merge_states(int owner, const char *name,
355 struct symbol *sym,
356 struct smatch_state *state1,
357 struct smatch_state *state2)
359 struct smatch_state *ret;
361 if (state1 == state2)
362 ret = state1;
363 else if (__has_merge_function(owner))
364 ret = __client_merge_function(owner, state1, state2);
365 else if (state1 == &ghost)
366 ret = state2;
367 else if (state2 == &ghost)
368 ret = state1;
369 else if (!state1 || !state2)
370 ret = &undefined;
371 else
372 ret = &merged;
373 return ret;
376 struct sm_state *merge_sm_states(struct sm_state *one, struct sm_state *two)
378 struct smatch_state *s;
379 struct sm_state *result;
381 if (one == two)
382 return one;
383 s = merge_states(one->owner, one->name, one->sym, one->state, two->state);
384 result = alloc_state_no_name(one->owner, one->name, one->sym, s);
385 result->merged = 1;
386 result->left = one;
387 result->right = two;
388 result->nr_children = one->nr_children + two->nr_children;
389 copy_possibles(result, one);
390 copy_possibles(result, two);
393 * The ->line information is used by deref_check where we complain about
394 * checking pointers that have already been dereferenced. Let's say we
395 * dereference a pointer on both the true and false paths and then merge
396 * the states here. The result state is &derefed, but the ->line number
397 * is on the line where the pointer is merged not where it was
398 * dereferenced..
400 * So in that case, let's just pick one dereference and set the ->line
401 * to point at it.
405 if (result->state == one->state)
406 result->line = one->line;
407 if (result->state == two->state)
408 result->line = two->line;
410 if (option_debug ||
411 strcmp(check_name(one->owner), option_debug_check) == 0) {
412 struct sm_state *tmp;
413 int i = 0;
415 printf("%s:%d %s() merge [%s] '%s' %s(L %d) + %s(L %d) => %s (",
416 get_filename(), get_lineno(), get_function(),
417 check_name(one->owner), one->name,
418 show_state(one->state), one->line,
419 show_state(two->state), two->line,
420 show_state(s));
422 FOR_EACH_PTR(result->possible, tmp) {
423 if (i++)
424 printf(", ");
425 printf("%s", show_state(tmp->state));
426 } END_FOR_EACH_PTR(tmp);
427 printf(")\n");
430 return result;
433 struct sm_state *get_sm_state_stree(struct stree *stree, int owner, const char *name,
434 struct symbol *sym)
436 struct tracker tracker = {
437 .owner = owner,
438 .name = (char *)name,
439 .sym = sym,
442 if (!name)
443 return NULL;
446 return avl_lookup(stree, (struct sm_state *)&tracker);
449 struct smatch_state *get_state_stree(struct stree *stree,
450 int owner, const char *name,
451 struct symbol *sym)
453 struct sm_state *sm;
455 sm = get_sm_state_stree(stree, owner, name, sym);
456 if (sm)
457 return sm->state;
458 return NULL;
461 /* FIXME: this is almost exactly the same as set_sm_state_slist() */
462 void overwrite_sm_state_stree(struct stree **stree, struct sm_state *new)
464 avl_insert(stree, new);
467 void overwrite_sm_state_stree_stack(struct stree_stack **stack,
468 struct sm_state *sm)
470 struct stree *stree;
472 stree = pop_stree(stack);
473 overwrite_sm_state_stree(&stree, sm);
474 push_stree(stack, stree);
477 struct sm_state *set_state_stree(struct stree **stree, int owner, const char *name,
478 struct symbol *sym, struct smatch_state *state)
480 struct sm_state *new = alloc_sm_state(owner, name, sym, state);
482 avl_insert(stree, new);
483 return new;
486 void set_state_stree_perm(struct stree **stree, int owner, const char *name,
487 struct symbol *sym, struct smatch_state *state)
489 struct sm_state *sm;
491 sm = malloc(sizeof(*sm) + strlen(name) + 1);
492 memset(sm, 0, sizeof(*sm));
493 sm->owner = owner;
494 sm->name = (char *)(sm + 1);
495 strcpy((char *)sm->name, name);
496 sm->sym = sym;
497 sm->state = state;
499 overwrite_sm_state_stree(stree, sm);
502 void delete_state_stree(struct stree **stree, int owner, const char *name,
503 struct symbol *sym)
505 struct tracker tracker = {
506 .owner = owner,
507 .name = (char *)name,
508 .sym = sym,
511 avl_remove(stree, (struct sm_state *)&tracker);
514 void delete_state_stree_stack(struct stree_stack **stack, int owner, const char *name,
515 struct symbol *sym)
517 struct stree *stree;
519 stree = pop_stree(stack);
520 delete_state_stree(&stree, owner, name, sym);
521 push_stree(stack, stree);
524 void push_stree(struct stree_stack **stack, struct stree *stree)
526 add_ptr_list(stack, stree);
529 struct stree *pop_stree(struct stree_stack **stack)
531 struct stree *stree;
533 stree = last_ptr_list((struct ptr_list *)*stack);
534 delete_ptr_list_last((struct ptr_list **)stack);
535 return stree;
538 struct stree *top_stree(struct stree_stack *stack)
540 return last_ptr_list((struct ptr_list *)stack);
543 void free_slist(struct state_list **slist)
545 __free_ptr_list((struct ptr_list **)slist);
548 void free_stree_stack(struct stree_stack **stack)
550 __free_ptr_list((struct ptr_list **)stack);
553 void free_stack_and_strees(struct stree_stack **stree_stack)
555 struct stree *stree;
557 FOR_EACH_PTR(*stree_stack, stree) {
558 free_stree(&stree);
559 } END_FOR_EACH_PTR(stree);
560 free_stree_stack(stree_stack);
563 struct sm_state *set_state_stree_stack(struct stree_stack **stack, int owner, const char *name,
564 struct symbol *sym, struct smatch_state *state)
566 struct stree *stree;
567 struct sm_state *sm;
569 stree = pop_stree(stack);
570 sm = set_state_stree(&stree, owner, name, sym, state);
571 push_stree(stack, stree);
573 return sm;
577 * get_sm_state_stack() gets the state for the top slist on the stack.
579 struct sm_state *get_sm_state_stree_stack(struct stree_stack *stack,
580 int owner, const char *name,
581 struct symbol *sym)
583 struct stree *stree;
584 struct sm_state *ret;
586 stree = pop_stree(&stack);
587 ret = get_sm_state_stree(stree, owner, name, sym);
588 push_stree(&stack, stree);
589 return ret;
592 struct smatch_state *get_state_stree_stack(struct stree_stack *stack,
593 int owner, const char *name,
594 struct symbol *sym)
596 struct sm_state *sm;
598 sm = get_sm_state_stree_stack(stack, owner, name, sym);
599 if (sm)
600 return sm->state;
601 return NULL;
604 static void match_states_stree(struct stree **one, struct stree **two)
606 struct smatch_state *tmp_state;
607 struct sm_state *sm;
608 struct state_list *add_to_one = NULL;
609 struct state_list *add_to_two = NULL;
610 AvlIter one_iter;
611 AvlIter two_iter;
613 avl_iter_begin(&one_iter, *one, FORWARD);
614 avl_iter_begin(&two_iter, *two, FORWARD);
616 for (;;) {
617 if (!one_iter.sm && !two_iter.sm)
618 break;
619 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
620 __set_fake_cur_stree_fast(*two);
621 tmp_state = __client_unmatched_state_function(one_iter.sm);
622 __pop_fake_cur_stree_fast();
623 sm = alloc_state_no_name(one_iter.sm->owner, one_iter.sm->name,
624 one_iter.sm->sym, tmp_state);
625 add_ptr_list(&add_to_two, sm);
626 avl_iter_next(&one_iter);
627 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
628 avl_iter_next(&one_iter);
629 avl_iter_next(&two_iter);
630 } else {
631 __set_fake_cur_stree_fast(*one);
632 tmp_state = __client_unmatched_state_function(two_iter.sm);
633 __pop_fake_cur_stree_fast();
634 sm = alloc_state_no_name(two_iter.sm->owner, two_iter.sm->name,
635 two_iter.sm->sym, tmp_state);
636 add_ptr_list(&add_to_one, sm);
637 avl_iter_next(&two_iter);
641 FOR_EACH_PTR(add_to_one, sm) {
642 avl_insert(one, sm);
643 } END_FOR_EACH_PTR(sm);
645 FOR_EACH_PTR(add_to_two, sm) {
646 avl_insert(two, sm);
647 } END_FOR_EACH_PTR(sm);
649 free_slist(&add_to_one);
650 free_slist(&add_to_two);
653 static void call_pre_merge_hooks(struct stree **one, struct stree **two)
655 struct sm_state *sm;
657 save_all_states();
659 __swap_cur_stree(*one);
660 FOR_EACH_SM(*two, sm) {
661 call_pre_merge_hook(sm);
662 } END_FOR_EACH_SM(sm);
663 *one = clone_stree(__get_cur_stree());
665 __swap_cur_stree(*two);
666 FOR_EACH_SM(*one, sm) {
667 call_pre_merge_hook(sm);
668 } END_FOR_EACH_SM(sm);
669 *two = clone_stree(__get_cur_stree());
671 restore_all_states();
674 static void clone_pool_havers_stree(struct stree **stree)
676 struct sm_state *sm, *tmp;
677 struct state_list *slist = NULL;
679 FOR_EACH_SM(*stree, sm) {
680 if (sm->pool) {
681 tmp = clone_sm(sm);
682 add_ptr_list(&slist, tmp);
684 } END_FOR_EACH_SM(sm);
686 FOR_EACH_PTR(slist, sm) {
687 avl_insert(stree, sm);
688 } END_FOR_EACH_PTR(sm);
690 free_slist(&slist);
693 int __stree_id;
696 * merge_slist() is called whenever paths merge, such as after
697 * an if statement. It takes the two slists and creates one.
699 static void __merge_stree(struct stree **to, struct stree *stree, int add_pool)
701 struct stree *results = NULL;
702 struct stree *implied_one = NULL;
703 struct stree *implied_two = NULL;
704 AvlIter one_iter;
705 AvlIter two_iter;
706 struct sm_state *tmp_sm;
708 if (out_of_memory())
709 return;
711 /* merging a null and nonnull path gives you only the nonnull path */
712 if (!stree)
713 return;
714 if (*to == stree)
715 return;
717 if (!*to) {
718 *to = clone_stree(stree);
719 return;
722 implied_one = clone_stree(*to);
723 implied_two = clone_stree(stree);
725 match_states_stree(&implied_one, &implied_two);
726 call_pre_merge_hooks(&implied_one, &implied_two);
728 if (add_pool) {
729 clone_pool_havers_stree(&implied_one);
730 clone_pool_havers_stree(&implied_two);
732 set_stree_id(&implied_one, ++__stree_id);
733 set_stree_id(&implied_two, ++__stree_id);
734 if (implied_one->base_stree)
735 set_stree_id(&implied_one->base_stree, ++__stree_id);
736 if (implied_two->base_stree)
737 set_stree_id(&implied_two->base_stree, ++__stree_id);
740 push_stree(&all_pools, implied_one);
741 push_stree(&all_pools, implied_two);
743 avl_iter_begin(&one_iter, implied_one, FORWARD);
744 avl_iter_begin(&two_iter, implied_two, FORWARD);
746 for (;;) {
747 if (!one_iter.sm && !two_iter.sm)
748 break;
749 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
750 sm_msg("error: Internal smatch error.");
751 avl_iter_next(&one_iter);
752 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
753 if (add_pool && one_iter.sm != two_iter.sm) {
754 one_iter.sm->pool = implied_one;
755 if (implied_one->base_stree)
756 one_iter.sm->pool = implied_one->base_stree;
757 two_iter.sm->pool = implied_two;
758 if (implied_two->base_stree)
759 two_iter.sm->pool = implied_two->base_stree;
761 tmp_sm = merge_sm_states(one_iter.sm, two_iter.sm);
762 add_possible_sm(tmp_sm, one_iter.sm);
763 add_possible_sm(tmp_sm, two_iter.sm);
764 avl_insert(&results, tmp_sm);
765 avl_iter_next(&one_iter);
766 avl_iter_next(&two_iter);
767 } else {
768 sm_msg("error: Internal smatch error.");
769 avl_iter_next(&two_iter);
773 free_stree(to);
774 *to = results;
777 void merge_stree(struct stree **to, struct stree *stree)
779 __merge_stree(to, stree, 1);
782 void merge_stree_no_pools(struct stree **to, struct stree *stree)
784 __merge_stree(to, stree, 0);
788 * This is unfortunately a bit subtle... The problem is that if a
789 * state is set on one fake stree but not the other then we should
790 * look up the the original state and use that as the unset state.
791 * Fortunately, after you pop your fake stree then the cur_slist should
792 * reflect the original state.
794 void merge_fake_stree(struct stree **to, struct stree *stree)
796 struct stree *one = *to;
797 struct stree *two = stree;
798 struct sm_state *sm;
799 struct state_list *add_to_one = NULL;
800 struct state_list *add_to_two = NULL;
801 AvlIter one_iter;
802 AvlIter two_iter;
804 if (!stree)
805 return;
806 if (*to == stree)
807 return;
808 if (!*to) {
809 *to = clone_stree(stree);
810 return;
813 avl_iter_begin(&one_iter, one, FORWARD);
814 avl_iter_begin(&two_iter, two, FORWARD);
816 for (;;) {
817 if (!one_iter.sm && !two_iter.sm)
818 break;
819 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
820 sm = get_sm_state(one_iter.sm->owner, one_iter.sm->name,
821 one_iter.sm->sym);
822 if (sm)
823 add_ptr_list(&add_to_two, sm);
824 avl_iter_next(&one_iter);
825 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
826 avl_iter_next(&one_iter);
827 avl_iter_next(&two_iter);
828 } else {
829 sm = get_sm_state(two_iter.sm->owner, two_iter.sm->name,
830 two_iter.sm->sym);
831 if (sm)
832 add_ptr_list(&add_to_one, sm);
833 avl_iter_next(&two_iter);
837 FOR_EACH_PTR(add_to_one, sm) {
838 avl_insert(&one, sm);
839 } END_FOR_EACH_PTR(sm);
841 FOR_EACH_PTR(add_to_two, sm) {
842 avl_insert(&two, sm);
843 } END_FOR_EACH_PTR(sm);
845 one->base_stree = clone_stree(__get_cur_stree());
846 FOR_EACH_SM(one, sm) {
847 avl_insert(&one->base_stree, sm);
848 } END_FOR_EACH_SM(sm);
850 two->base_stree = clone_stree(__get_cur_stree());
851 FOR_EACH_SM(two, sm) {
852 avl_insert(&two->base_stree, sm);
853 } END_FOR_EACH_SM(sm);
855 free_slist(&add_to_one);
856 free_slist(&add_to_two);
858 __merge_stree(&one, two, 1);
860 *to = one;
864 * filter_slist() removes any sm states "slist" holds in common with "filter"
866 void filter_stree(struct stree **stree, struct stree *filter)
868 struct stree *results = NULL;
869 AvlIter one_iter;
870 AvlIter two_iter;
872 avl_iter_begin(&one_iter, *stree, FORWARD);
873 avl_iter_begin(&two_iter, filter, FORWARD);
875 /* FIXME: This should probably be re-written with trees in mind */
877 for (;;) {
878 if (!one_iter.sm && !two_iter.sm)
879 break;
880 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
881 avl_insert(&results, one_iter.sm);
882 avl_iter_next(&one_iter);
883 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
884 if (one_iter.sm != two_iter.sm)
885 avl_insert(&results, one_iter.sm);
886 avl_iter_next(&one_iter);
887 avl_iter_next(&two_iter);
888 } else {
889 avl_iter_next(&two_iter);
893 free_stree(stree);
894 *stree = results;
899 * and_slist_stack() pops the top two slists, overwriting the one with
900 * the other and pushing it back on the stack.
902 void and_stree_stack(struct stree_stack **stack)
904 struct sm_state *tmp;
905 struct stree *right_stree = pop_stree(stack);
907 FOR_EACH_SM(right_stree, tmp) {
908 overwrite_sm_state_stree_stack(stack, tmp);
909 } END_FOR_EACH_SM(tmp);
910 free_stree(&right_stree);
914 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
915 * It pops the two slists from the top of the stack and merges them
916 * together in a way that preserves the things they have in common
917 * but creates a merged state for most of the rest.
918 * You could have code that had: if (foo || foo) { foo->baz;
919 * It's this function which ensures smatch does the right thing.
921 void or_stree_stack(struct stree_stack **pre_conds,
922 struct stree *cur_stree,
923 struct stree_stack **stack)
925 struct stree *new;
926 struct stree *old;
927 struct stree *pre_stree;
928 struct stree *res;
929 struct stree *tmp_stree;
931 new = pop_stree(stack);
932 old = pop_stree(stack);
934 pre_stree = pop_stree(pre_conds);
935 push_stree(pre_conds, clone_stree(pre_stree));
937 res = clone_stree(pre_stree);
938 overwrite_stree(old, &res);
940 tmp_stree = clone_stree(cur_stree);
941 overwrite_stree(new, &tmp_stree);
943 merge_stree(&res, tmp_stree);
944 filter_stree(&res, pre_stree);
946 push_stree(stack, res);
947 free_stree(&tmp_stree);
948 free_stree(&pre_stree);
949 free_stree(&new);
950 free_stree(&old);
954 * get_named_stree() is only used for gotos.
956 struct stree **get_named_stree(struct named_stree_stack *stack,
957 const char *name)
959 struct named_stree *tmp;
961 FOR_EACH_PTR(stack, tmp) {
962 if (!strcmp(tmp->name, name))
963 return &tmp->stree;
964 } END_FOR_EACH_PTR(tmp);
965 return NULL;
968 /* FIXME: These parameters are in a different order from expected */
969 void overwrite_stree(struct stree *from, struct stree **to)
971 struct sm_state *tmp;
973 FOR_EACH_SM(from, tmp) {
974 overwrite_sm_state_stree(to, tmp);
975 } END_FOR_EACH_SM(tmp);