sparse: add -Wbig-constants
[smatch.git] / smatch_slist.c
blob4c74caf73ff56ab08096a26aa1ebe285ba0c03d6
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 struct sm_state *clone_sm(struct sm_state *s)
304 struct sm_state *ret;
306 ret = alloc_state_no_name(s->owner, s->name, s->sym, s->state);
307 ret->merged = s->merged;
308 ret->line = s->line;
309 /* clone_sm() doesn't copy the pools. Each state needs to have
310 only one pool. */
311 ret->possible = clone_slist(s->possible);
312 ret->left = s->left;
313 ret->right = s->right;
314 ret->nr_children = s->nr_children;
315 return ret;
318 int is_merged(struct sm_state *sm)
320 return sm->merged;
323 int is_leaf(struct sm_state *sm)
325 return !sm->merged;
328 int slist_has_state(struct state_list *slist, struct smatch_state *state)
330 struct sm_state *tmp;
332 FOR_EACH_PTR(slist, tmp) {
333 if (tmp->state == state)
334 return 1;
335 } END_FOR_EACH_PTR(tmp);
336 return 0;
339 struct state_list *clone_slist(struct state_list *from_slist)
341 struct sm_state *sm;
342 struct state_list *to_slist = NULL;
344 FOR_EACH_PTR(from_slist, sm) {
345 add_ptr_list(&to_slist, sm);
346 } END_FOR_EACH_PTR(sm);
347 return to_slist;
350 static struct smatch_state *merge_states(int owner, const char *name,
351 struct symbol *sym,
352 struct smatch_state *state1,
353 struct smatch_state *state2)
355 struct smatch_state *ret;
357 if (state1 == state2)
358 ret = state1;
359 else if (__has_merge_function(owner))
360 ret = __client_merge_function(owner, state1, state2);
361 else if (state1 == &ghost)
362 ret = state2;
363 else if (state2 == &ghost)
364 ret = state1;
365 else if (!state1 || !state2)
366 ret = &undefined;
367 else
368 ret = &merged;
369 return ret;
372 struct sm_state *merge_sm_states(struct sm_state *one, struct sm_state *two)
374 struct smatch_state *s;
375 struct sm_state *result;
376 static int warned;
378 if (one == two)
379 return one;
380 if (out_of_memory()) {
381 if (!warned)
382 sm_msg("Function too hairy. No more merges.");
383 warned = 1;
384 return one;
386 warned = 0;
387 s = merge_states(one->owner, one->name, one->sym, one->state, two->state);
388 result = alloc_state_no_name(one->owner, one->name, one->sym, s);
389 result->merged = 1;
390 result->left = one;
391 result->right = two;
392 result->nr_children = one->nr_children + two->nr_children;
393 copy_possibles(result, one);
394 copy_possibles(result, two);
397 * The ->line information is used by deref_check where we complain about
398 * checking pointers that have already been dereferenced. Let's say we
399 * dereference a pointer on both the true and false paths and then merge
400 * the states here. The result state is &derefed, but the ->line number
401 * is on the line where the pointer is merged not where it was
402 * dereferenced..
404 * So in that case, let's just pick one dereference and set the ->line
405 * to point at it.
409 if (result->state == one->state)
410 result->line = one->line;
411 if (result->state == two->state)
412 result->line = two->line;
414 if (option_debug ||
415 strcmp(check_name(one->owner), option_debug_check) == 0) {
416 struct sm_state *tmp;
417 int i = 0;
419 printf("%s:%d %s() merge [%s] '%s' %s(L %d) + %s(L %d) => %s (",
420 get_filename(), get_lineno(), get_function(),
421 check_name(one->owner), one->name,
422 show_state(one->state), one->line,
423 show_state(two->state), two->line,
424 show_state(s));
426 FOR_EACH_PTR(result->possible, tmp) {
427 if (i++)
428 printf(", ");
429 printf("%s", show_state(tmp->state));
430 } END_FOR_EACH_PTR(tmp);
431 printf(")\n");
434 return result;
437 struct sm_state *get_sm_state_stree(struct stree *stree, int owner, const char *name,
438 struct symbol *sym)
440 struct tracker tracker = {
441 .owner = owner,
442 .name = (char *)name,
443 .sym = sym,
446 if (!name)
447 return NULL;
450 return avl_lookup(stree, (struct sm_state *)&tracker);
453 struct smatch_state *get_state_stree(struct stree *stree,
454 int owner, const char *name,
455 struct symbol *sym)
457 struct sm_state *sm;
459 sm = get_sm_state_stree(stree, owner, name, sym);
460 if (sm)
461 return sm->state;
462 return NULL;
465 /* FIXME: this is almost exactly the same as set_sm_state_slist() */
466 void overwrite_sm_state_stree(struct stree **stree, struct sm_state *new)
468 avl_insert(stree, new);
471 void overwrite_sm_state_stree_stack(struct stree_stack **stack,
472 struct sm_state *sm)
474 struct stree *stree;
476 stree = pop_stree(stack);
477 overwrite_sm_state_stree(&stree, sm);
478 push_stree(stack, stree);
481 struct sm_state *set_state_stree(struct stree **stree, int owner, const char *name,
482 struct symbol *sym, struct smatch_state *state)
484 struct sm_state *new = alloc_sm_state(owner, name, sym, state);
486 avl_insert(stree, new);
487 return new;
490 void set_state_stree_perm(struct stree **stree, int owner, const char *name,
491 struct symbol *sym, struct smatch_state *state)
493 struct sm_state *sm;
495 sm = malloc(sizeof(*sm) + strlen(name) + 1);
496 memset(sm, 0, sizeof(*sm));
497 sm->owner = owner;
498 sm->name = (char *)(sm + 1);
499 strcpy((char *)sm->name, name);
500 sm->sym = sym;
501 sm->state = state;
503 overwrite_sm_state_stree(stree, sm);
506 void delete_state_stree(struct stree **stree, int owner, const char *name,
507 struct symbol *sym)
509 struct tracker tracker = {
510 .owner = owner,
511 .name = (char *)name,
512 .sym = sym,
515 avl_remove(stree, (struct sm_state *)&tracker);
518 void delete_state_stree_stack(struct stree_stack **stack, int owner, const char *name,
519 struct symbol *sym)
521 struct stree *stree;
523 stree = pop_stree(stack);
524 delete_state_stree(&stree, owner, name, sym);
525 push_stree(stack, stree);
528 void push_stree(struct stree_stack **stack, struct stree *stree)
530 add_ptr_list(stack, stree);
533 struct stree *pop_stree(struct stree_stack **stack)
535 struct stree *stree;
537 stree = last_ptr_list((struct ptr_list *)*stack);
538 delete_ptr_list_last((struct ptr_list **)stack);
539 return stree;
542 struct stree *top_stree(struct stree_stack *stack)
544 return last_ptr_list((struct ptr_list *)stack);
547 void free_slist(struct state_list **slist)
549 __free_ptr_list((struct ptr_list **)slist);
552 void free_stree_stack(struct stree_stack **stack)
554 __free_ptr_list((struct ptr_list **)stack);
557 void free_stack_and_strees(struct stree_stack **stree_stack)
559 struct stree *stree;
561 FOR_EACH_PTR(*stree_stack, stree) {
562 free_stree(&stree);
563 } END_FOR_EACH_PTR(stree);
564 free_stree_stack(stree_stack);
567 struct sm_state *set_state_stree_stack(struct stree_stack **stack, int owner, const char *name,
568 struct symbol *sym, struct smatch_state *state)
570 struct stree *stree;
571 struct sm_state *sm;
573 stree = pop_stree(stack);
574 sm = set_state_stree(&stree, owner, name, sym, state);
575 push_stree(stack, stree);
577 return sm;
581 * get_sm_state_stack() gets the state for the top slist on the stack.
583 struct sm_state *get_sm_state_stree_stack(struct stree_stack *stack,
584 int owner, const char *name,
585 struct symbol *sym)
587 struct stree *stree;
588 struct sm_state *ret;
590 stree = pop_stree(&stack);
591 ret = get_sm_state_stree(stree, owner, name, sym);
592 push_stree(&stack, stree);
593 return ret;
596 struct smatch_state *get_state_stree_stack(struct stree_stack *stack,
597 int owner, const char *name,
598 struct symbol *sym)
600 struct sm_state *sm;
602 sm = get_sm_state_stree_stack(stack, owner, name, sym);
603 if (sm)
604 return sm->state;
605 return NULL;
608 static void match_states_stree(struct stree **one, struct stree **two)
610 struct smatch_state *tmp_state;
611 struct sm_state *sm;
612 struct state_list *add_to_one = NULL;
613 struct state_list *add_to_two = NULL;
614 AvlIter one_iter;
615 AvlIter two_iter;
617 avl_iter_begin(&one_iter, *one, FORWARD);
618 avl_iter_begin(&two_iter, *two, FORWARD);
620 for (;;) {
621 if (!one_iter.sm && !two_iter.sm)
622 break;
623 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
624 __set_fake_cur_stree_fast(*two);
625 tmp_state = __client_unmatched_state_function(one_iter.sm);
626 __pop_fake_cur_stree_fast();
627 sm = alloc_state_no_name(one_iter.sm->owner, one_iter.sm->name,
628 one_iter.sm->sym, tmp_state);
629 add_ptr_list(&add_to_two, sm);
630 avl_iter_next(&one_iter);
631 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
632 avl_iter_next(&one_iter);
633 avl_iter_next(&two_iter);
634 } else {
635 __set_fake_cur_stree_fast(*one);
636 tmp_state = __client_unmatched_state_function(two_iter.sm);
637 __pop_fake_cur_stree_fast();
638 sm = alloc_state_no_name(two_iter.sm->owner, two_iter.sm->name,
639 two_iter.sm->sym, tmp_state);
640 add_ptr_list(&add_to_one, sm);
641 avl_iter_next(&two_iter);
645 FOR_EACH_PTR(add_to_one, sm) {
646 avl_insert(one, sm);
647 } END_FOR_EACH_PTR(sm);
649 FOR_EACH_PTR(add_to_two, sm) {
650 avl_insert(two, sm);
651 } END_FOR_EACH_PTR(sm);
653 free_slist(&add_to_one);
654 free_slist(&add_to_two);
657 static void call_pre_merge_hooks(struct stree **one, struct stree **two)
659 struct sm_state *sm, *other;
661 save_all_states();
663 __swap_cur_stree(*one);
664 FOR_EACH_SM(*two, sm) {
665 other = get_sm_state(sm->owner, sm->name, sm->sym);
666 if (other == sm)
667 continue;
668 call_pre_merge_hook(sm);
669 } END_FOR_EACH_SM(sm);
670 *one = clone_stree(__get_cur_stree());
672 __swap_cur_stree(*two);
673 FOR_EACH_SM(*one, sm) {
674 other = get_sm_state(sm->owner, sm->name, sm->sym);
675 if (other == sm)
676 continue;
677 call_pre_merge_hook(sm);
678 } END_FOR_EACH_SM(sm);
679 *two = clone_stree(__get_cur_stree());
681 restore_all_states();
684 static void clone_pool_havers_stree(struct stree **stree)
686 struct sm_state *sm, *tmp;
687 struct state_list *slist = NULL;
689 FOR_EACH_SM(*stree, sm) {
690 if (sm->pool) {
691 tmp = clone_sm(sm);
692 add_ptr_list(&slist, tmp);
694 } END_FOR_EACH_SM(sm);
696 FOR_EACH_PTR(slist, sm) {
697 avl_insert(stree, sm);
698 } END_FOR_EACH_PTR(sm);
700 free_slist(&slist);
703 int __stree_id;
706 * merge_slist() is called whenever paths merge, such as after
707 * an if statement. It takes the two slists and creates one.
709 static void __merge_stree(struct stree **to, struct stree *stree, int add_pool)
711 struct stree *results = NULL;
712 struct stree *implied_one = NULL;
713 struct stree *implied_two = NULL;
714 AvlIter one_iter;
715 AvlIter two_iter;
716 struct sm_state *tmp_sm;
718 if (out_of_memory())
719 return;
721 /* merging a null and nonnull path gives you only the nonnull path */
722 if (!stree)
723 return;
724 if (*to == stree)
725 return;
727 if (!*to) {
728 *to = clone_stree(stree);
729 return;
732 implied_one = clone_stree(*to);
733 implied_two = clone_stree(stree);
735 match_states_stree(&implied_one, &implied_two);
736 call_pre_merge_hooks(&implied_one, &implied_two);
738 if (add_pool) {
739 clone_pool_havers_stree(&implied_one);
740 clone_pool_havers_stree(&implied_two);
742 set_stree_id(&implied_one, ++__stree_id);
743 set_stree_id(&implied_two, ++__stree_id);
744 if (implied_one->base_stree)
745 set_stree_id(&implied_one->base_stree, ++__stree_id);
746 if (implied_two->base_stree)
747 set_stree_id(&implied_two->base_stree, ++__stree_id);
750 push_stree(&all_pools, implied_one);
751 push_stree(&all_pools, implied_two);
753 avl_iter_begin(&one_iter, implied_one, FORWARD);
754 avl_iter_begin(&two_iter, implied_two, FORWARD);
756 for (;;) {
757 if (!one_iter.sm || !two_iter.sm)
758 break;
759 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
760 sm_msg("error: Internal smatch error.");
761 avl_iter_next(&one_iter);
762 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
763 if (add_pool && one_iter.sm != two_iter.sm) {
764 one_iter.sm->pool = implied_one;
765 if (implied_one->base_stree)
766 one_iter.sm->pool = implied_one->base_stree;
767 two_iter.sm->pool = implied_two;
768 if (implied_two->base_stree)
769 two_iter.sm->pool = implied_two->base_stree;
771 tmp_sm = merge_sm_states(one_iter.sm, two_iter.sm);
772 add_possible_sm(tmp_sm, one_iter.sm);
773 add_possible_sm(tmp_sm, two_iter.sm);
774 avl_insert(&results, tmp_sm);
775 avl_iter_next(&one_iter);
776 avl_iter_next(&two_iter);
777 } else {
778 sm_msg("error: Internal smatch error.");
779 avl_iter_next(&two_iter);
783 free_stree(to);
784 *to = results;
787 void merge_stree(struct stree **to, struct stree *stree)
789 __merge_stree(to, stree, 1);
792 void merge_stree_no_pools(struct stree **to, struct stree *stree)
794 __merge_stree(to, stree, 0);
798 * This is unfortunately a bit subtle... The problem is that if a
799 * state is set on one fake stree but not the other then we should
800 * look up the the original state and use that as the unset state.
801 * Fortunately, after you pop your fake stree then the cur_slist should
802 * reflect the original state.
804 void merge_fake_stree(struct stree **to, struct stree *stree)
806 struct stree *one = *to;
807 struct stree *two = stree;
808 struct sm_state *sm;
809 struct state_list *add_to_one = NULL;
810 struct state_list *add_to_two = NULL;
811 AvlIter one_iter;
812 AvlIter two_iter;
814 if (!stree)
815 return;
816 if (*to == stree)
817 return;
818 if (!*to) {
819 *to = clone_stree(stree);
820 return;
823 avl_iter_begin(&one_iter, one, FORWARD);
824 avl_iter_begin(&two_iter, two, FORWARD);
826 for (;;) {
827 if (!one_iter.sm && !two_iter.sm)
828 break;
829 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
830 sm = get_sm_state(one_iter.sm->owner, one_iter.sm->name,
831 one_iter.sm->sym);
832 if (sm)
833 add_ptr_list(&add_to_two, sm);
834 avl_iter_next(&one_iter);
835 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
836 avl_iter_next(&one_iter);
837 avl_iter_next(&two_iter);
838 } else {
839 sm = get_sm_state(two_iter.sm->owner, two_iter.sm->name,
840 two_iter.sm->sym);
841 if (sm)
842 add_ptr_list(&add_to_one, sm);
843 avl_iter_next(&two_iter);
847 FOR_EACH_PTR(add_to_one, sm) {
848 avl_insert(&one, sm);
849 } END_FOR_EACH_PTR(sm);
851 FOR_EACH_PTR(add_to_two, sm) {
852 avl_insert(&two, sm);
853 } END_FOR_EACH_PTR(sm);
855 one->base_stree = clone_stree(__get_cur_stree());
856 FOR_EACH_SM(one, sm) {
857 avl_insert(&one->base_stree, sm);
858 } END_FOR_EACH_SM(sm);
860 two->base_stree = clone_stree(__get_cur_stree());
861 FOR_EACH_SM(two, sm) {
862 avl_insert(&two->base_stree, sm);
863 } END_FOR_EACH_SM(sm);
865 free_slist(&add_to_one);
866 free_slist(&add_to_two);
868 __merge_stree(&one, two, 1);
870 *to = one;
874 * filter_slist() removes any sm states "slist" holds in common with "filter"
876 void filter_stree(struct stree **stree, struct stree *filter)
878 struct stree *results = NULL;
879 AvlIter one_iter;
880 AvlIter two_iter;
882 avl_iter_begin(&one_iter, *stree, FORWARD);
883 avl_iter_begin(&two_iter, filter, FORWARD);
885 /* FIXME: This should probably be re-written with trees in mind */
887 for (;;) {
888 if (!one_iter.sm && !two_iter.sm)
889 break;
890 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
891 avl_insert(&results, one_iter.sm);
892 avl_iter_next(&one_iter);
893 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
894 if (one_iter.sm != two_iter.sm)
895 avl_insert(&results, one_iter.sm);
896 avl_iter_next(&one_iter);
897 avl_iter_next(&two_iter);
898 } else {
899 avl_iter_next(&two_iter);
903 free_stree(stree);
904 *stree = results;
909 * and_slist_stack() pops the top two slists, overwriting the one with
910 * the other and pushing it back on the stack.
912 void and_stree_stack(struct stree_stack **stack)
914 struct sm_state *tmp;
915 struct stree *right_stree = pop_stree(stack);
917 FOR_EACH_SM(right_stree, tmp) {
918 overwrite_sm_state_stree_stack(stack, tmp);
919 } END_FOR_EACH_SM(tmp);
920 free_stree(&right_stree);
924 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
925 * It pops the two slists from the top of the stack and merges them
926 * together in a way that preserves the things they have in common
927 * but creates a merged state for most of the rest.
928 * You could have code that had: if (foo || foo) { foo->baz;
929 * It's this function which ensures smatch does the right thing.
931 void or_stree_stack(struct stree_stack **pre_conds,
932 struct stree *cur_stree,
933 struct stree_stack **stack)
935 struct stree *new;
936 struct stree *old;
937 struct stree *pre_stree;
938 struct stree *res;
939 struct stree *tmp_stree;
941 new = pop_stree(stack);
942 old = pop_stree(stack);
944 pre_stree = pop_stree(pre_conds);
945 push_stree(pre_conds, clone_stree(pre_stree));
947 res = clone_stree(pre_stree);
948 overwrite_stree(old, &res);
950 tmp_stree = clone_stree(cur_stree);
951 overwrite_stree(new, &tmp_stree);
953 merge_stree(&res, tmp_stree);
954 filter_stree(&res, pre_stree);
956 push_stree(stack, res);
957 free_stree(&tmp_stree);
958 free_stree(&pre_stree);
959 free_stree(&new);
960 free_stree(&old);
964 * get_named_stree() is only used for gotos.
966 struct stree **get_named_stree(struct named_stree_stack *stack,
967 const char *name,
968 struct symbol *sym)
970 struct named_stree *tmp;
972 FOR_EACH_PTR(stack, tmp) {
973 if (tmp->sym == sym &&
974 strcmp(tmp->name, name) == 0)
975 return &tmp->stree;
976 } END_FOR_EACH_PTR(tmp);
977 return NULL;
980 /* FIXME: These parameters are in a different order from expected */
981 void overwrite_stree(struct stree *from, struct stree **to)
983 struct sm_state *tmp;
985 FOR_EACH_SM(from, tmp) {
986 overwrite_sm_state_stree(to, tmp);
987 } END_FOR_EACH_SM(tmp);