ranges: fix a bug in rl_invert()
[smatch.git] / smatch_slist.c
blob62145d2f61e03ef16ad0673ae9fe13bba534365d
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_implied(struct sm_state *sm)
324 return sm->implied;
327 int slist_has_state(struct state_list *slist, struct smatch_state *state)
329 struct sm_state *tmp;
331 FOR_EACH_PTR(slist, tmp) {
332 if (tmp->state == state)
333 return 1;
334 } END_FOR_EACH_PTR(tmp);
335 return 0;
338 struct state_list *clone_slist(struct state_list *from_slist)
340 struct sm_state *sm;
341 struct state_list *to_slist = NULL;
343 FOR_EACH_PTR(from_slist, sm) {
344 add_ptr_list(&to_slist, sm);
345 } END_FOR_EACH_PTR(sm);
346 return to_slist;
349 static struct smatch_state *merge_states(int owner, const char *name,
350 struct symbol *sym,
351 struct smatch_state *state1,
352 struct smatch_state *state2)
354 struct smatch_state *ret;
356 if (state1 == state2)
357 ret = state1;
358 else if (__has_merge_function(owner))
359 ret = __client_merge_function(owner, state1, state2);
360 else if (state1 == &ghost)
361 ret = state2;
362 else if (state2 == &ghost)
363 ret = state1;
364 else if (!state1 || !state2)
365 ret = &undefined;
366 else
367 ret = &merged;
368 return ret;
371 struct sm_state *merge_sm_states(struct sm_state *one, struct sm_state *two)
373 struct smatch_state *s;
374 struct sm_state *result;
376 if (one == two)
377 return one;
378 s = merge_states(one->owner, one->name, one->sym, one->state, two->state);
379 result = alloc_state_no_name(one->owner, one->name, one->sym, s);
380 result->merged = 1;
381 result->left = one;
382 result->right = two;
383 result->nr_children = one->nr_children + two->nr_children;
384 copy_possibles(result, one);
385 copy_possibles(result, two);
387 if (option_debug ||
388 strcmp(check_name(one->owner), option_debug_check) == 0) {
389 struct sm_state *tmp;
390 int i = 0;
392 printf("%s:%d %s() merge [%s] '%s' %s(L %d) + %s(L %d) => %s (",
393 get_filename(), get_lineno(), get_function(),
394 check_name(one->owner), one->name,
395 show_state(one->state), one->line,
396 show_state(two->state), two->line,
397 show_state(s));
399 FOR_EACH_PTR(result->possible, tmp) {
400 if (i++)
401 printf(", ");
402 printf("%s", show_state(tmp->state));
403 } END_FOR_EACH_PTR(tmp);
404 printf(")\n");
407 return result;
410 struct sm_state *get_sm_state_stree(struct stree *stree, int owner, const char *name,
411 struct symbol *sym)
413 struct tracker tracker = {
414 .owner = owner,
415 .name = (char *)name,
416 .sym = sym,
419 if (!name)
420 return NULL;
423 return avl_lookup(stree, (struct sm_state *)&tracker);
426 struct smatch_state *get_state_stree(struct stree *stree,
427 int owner, const char *name,
428 struct symbol *sym)
430 struct sm_state *sm;
432 sm = get_sm_state_stree(stree, owner, name, sym);
433 if (sm)
434 return sm->state;
435 return NULL;
438 /* FIXME: this is almost exactly the same as set_sm_state_slist() */
439 void overwrite_sm_state_stree(struct stree **stree, struct sm_state *new)
441 avl_insert(stree, new);
444 void overwrite_sm_state_stree_stack(struct stree_stack **stack,
445 struct sm_state *sm)
447 struct stree *stree;
449 stree = pop_stree(stack);
450 overwrite_sm_state_stree(&stree, sm);
451 push_stree(stack, stree);
454 struct sm_state *set_state_stree(struct stree **stree, int owner, const char *name,
455 struct symbol *sym, struct smatch_state *state)
457 struct sm_state *new = alloc_sm_state(owner, name, sym, state);
459 avl_insert(stree, new);
460 return new;
463 void set_state_stree_perm(struct stree **stree, int owner, const char *name,
464 struct symbol *sym, struct smatch_state *state)
466 struct sm_state *sm;
468 sm = malloc(sizeof(*sm) + strlen(name) + 1);
469 memset(sm, 0, sizeof(*sm));
470 sm->owner = owner;
471 sm->name = (char *)(sm + 1);
472 strcpy((char *)sm->name, name);
473 sm->sym = sym;
474 sm->state = state;
476 overwrite_sm_state_stree(stree, sm);
479 void delete_state_stree(struct stree **stree, int owner, const char *name,
480 struct symbol *sym)
482 struct tracker tracker = {
483 .owner = owner,
484 .name = (char *)name,
485 .sym = sym,
488 avl_remove(stree, (struct sm_state *)&tracker);
491 void delete_state_stree_stack(struct stree_stack **stack, int owner, const char *name,
492 struct symbol *sym)
494 struct stree *stree;
496 stree = pop_stree(stack);
497 delete_state_stree(&stree, owner, name, sym);
498 push_stree(stack, stree);
501 void push_stree(struct stree_stack **stack, struct stree *stree)
503 add_ptr_list(stack, stree);
506 struct stree *pop_stree(struct stree_stack **stack)
508 struct stree *stree;
510 stree = last_ptr_list((struct ptr_list *)*stack);
511 delete_ptr_list_last((struct ptr_list **)stack);
512 return stree;
515 struct stree *top_stree(struct stree_stack *stack)
517 return last_ptr_list((struct ptr_list *)stack);
520 void free_slist(struct state_list **slist)
522 __free_ptr_list((struct ptr_list **)slist);
525 void free_stree_stack(struct stree_stack **stack)
527 __free_ptr_list((struct ptr_list **)stack);
530 void free_stack_and_strees(struct stree_stack **stree_stack)
532 struct stree *stree;
534 FOR_EACH_PTR(*stree_stack, stree) {
535 free_stree(&stree);
536 } END_FOR_EACH_PTR(stree);
537 free_stree_stack(stree_stack);
540 struct sm_state *set_state_stree_stack(struct stree_stack **stack, int owner, const char *name,
541 struct symbol *sym, struct smatch_state *state)
543 struct stree *stree;
544 struct sm_state *sm;
546 stree = pop_stree(stack);
547 sm = set_state_stree(&stree, owner, name, sym, state);
548 push_stree(stack, stree);
550 return sm;
554 * get_sm_state_stack() gets the state for the top slist on the stack.
556 struct sm_state *get_sm_state_stree_stack(struct stree_stack *stack,
557 int owner, const char *name,
558 struct symbol *sym)
560 struct stree *stree;
561 struct sm_state *ret;
563 stree = pop_stree(&stack);
564 ret = get_sm_state_stree(stree, owner, name, sym);
565 push_stree(&stack, stree);
566 return ret;
569 struct smatch_state *get_state_stree_stack(struct stree_stack *stack,
570 int owner, const char *name,
571 struct symbol *sym)
573 struct sm_state *sm;
575 sm = get_sm_state_stree_stack(stack, owner, name, sym);
576 if (sm)
577 return sm->state;
578 return NULL;
581 static void match_states_stree(struct stree **one, struct stree **two)
583 struct smatch_state *tmp_state;
584 struct sm_state *sm;
585 struct state_list *add_to_one = NULL;
586 struct state_list *add_to_two = NULL;
587 AvlIter one_iter;
588 AvlIter two_iter;
590 avl_iter_begin(&one_iter, *one, FORWARD);
591 avl_iter_begin(&two_iter, *two, FORWARD);
593 for (;;) {
594 if (!one_iter.sm && !two_iter.sm)
595 break;
596 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
597 __set_fake_cur_stree_fast(*two);
598 tmp_state = __client_unmatched_state_function(one_iter.sm);
599 __pop_fake_cur_stree_fast();
600 sm = alloc_state_no_name(one_iter.sm->owner, one_iter.sm->name,
601 one_iter.sm->sym, tmp_state);
602 add_ptr_list(&add_to_two, sm);
603 avl_iter_next(&one_iter);
604 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
605 avl_iter_next(&one_iter);
606 avl_iter_next(&two_iter);
607 } else {
608 __set_fake_cur_stree_fast(*one);
609 tmp_state = __client_unmatched_state_function(two_iter.sm);
610 __pop_fake_cur_stree_fast();
611 sm = alloc_state_no_name(two_iter.sm->owner, two_iter.sm->name,
612 two_iter.sm->sym, tmp_state);
613 add_ptr_list(&add_to_one, sm);
614 avl_iter_next(&two_iter);
618 FOR_EACH_PTR(add_to_one, sm) {
619 avl_insert(one, sm);
620 } END_FOR_EACH_PTR(sm);
622 FOR_EACH_PTR(add_to_two, sm) {
623 avl_insert(two, sm);
624 } END_FOR_EACH_PTR(sm);
626 free_slist(&add_to_one);
627 free_slist(&add_to_two);
630 static void call_pre_merge_hooks(struct stree **one, struct stree **two)
632 struct sm_state *sm;
634 save_all_states();
636 __swap_cur_stree(*one);
637 FOR_EACH_SM(*two, sm) {
638 call_pre_merge_hook(sm);
639 } END_FOR_EACH_SM(sm);
640 *one = clone_stree(__get_cur_stree());
642 __swap_cur_stree(*two);
643 FOR_EACH_SM(*one, sm) {
644 call_pre_merge_hook(sm);
645 } END_FOR_EACH_SM(sm);
646 *two = clone_stree(__get_cur_stree());
648 restore_all_states();
651 static void clone_pool_havers_stree(struct stree **stree)
653 struct sm_state *sm, *tmp;
654 struct state_list *slist = NULL;
656 FOR_EACH_SM(*stree, sm) {
657 if (sm->pool) {
658 tmp = clone_sm(sm);
659 add_ptr_list(&slist, tmp);
661 } END_FOR_EACH_SM(sm);
663 FOR_EACH_PTR(slist, sm) {
664 avl_insert(stree, sm);
665 } END_FOR_EACH_PTR(sm);
667 free_slist(&slist);
670 int __stree_id;
673 * merge_slist() is called whenever paths merge, such as after
674 * an if statement. It takes the two slists and creates one.
676 static void __merge_stree(struct stree **to, struct stree *stree, int add_pool)
678 struct stree *results = NULL;
679 struct stree *implied_one = NULL;
680 struct stree *implied_two = NULL;
681 AvlIter one_iter;
682 AvlIter two_iter;
683 struct sm_state *tmp_sm;
685 if (out_of_memory())
686 return;
688 /* merging a null and nonnull path gives you only the nonnull path */
689 if (!stree)
690 return;
691 if (*to == stree)
692 return;
694 if (!*to) {
695 *to = clone_stree(stree);
696 return;
699 implied_one = clone_stree(*to);
700 implied_two = clone_stree(stree);
702 match_states_stree(&implied_one, &implied_two);
703 call_pre_merge_hooks(&implied_one, &implied_two);
705 if (add_pool) {
706 clone_pool_havers_stree(&implied_one);
707 clone_pool_havers_stree(&implied_two);
709 set_stree_id(&implied_one, ++__stree_id);
710 set_stree_id(&implied_two, ++__stree_id);
713 push_stree(&all_pools, implied_one);
714 push_stree(&all_pools, implied_two);
716 avl_iter_begin(&one_iter, implied_one, FORWARD);
717 avl_iter_begin(&two_iter, implied_two, FORWARD);
719 for (;;) {
720 if (!one_iter.sm && !two_iter.sm)
721 break;
722 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
723 sm_msg("error: Internal smatch error.");
724 avl_iter_next(&one_iter);
725 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
726 if (add_pool && one_iter.sm != two_iter.sm) {
727 one_iter.sm->pool = implied_one;
728 two_iter.sm->pool = implied_two;
730 tmp_sm = merge_sm_states(one_iter.sm, two_iter.sm);
731 add_possible_sm(tmp_sm, one_iter.sm);
732 add_possible_sm(tmp_sm, two_iter.sm);
733 avl_insert(&results, tmp_sm);
734 avl_iter_next(&one_iter);
735 avl_iter_next(&two_iter);
736 } else {
737 sm_msg("error: Internal smatch error.");
738 avl_iter_next(&two_iter);
742 free_stree(to);
743 *to = results;
746 void merge_stree(struct stree **to, struct stree *stree)
748 __merge_stree(to, stree, 1);
751 void merge_stree_no_pools(struct stree **to, struct stree *stree)
753 __merge_stree(to, stree, 0);
757 * This is unfortunately a bit subtle... The problem is that if a
758 * state is set on one fake stree but not the other then we should
759 * look up the the original state and use that as the unset state.
760 * Fortunately, after you pop your fake stree then the cur_slist should
761 * reflect the original state.
763 void merge_fake_stree(struct stree **to, struct stree *stree)
765 struct stree *one = *to;
766 struct stree *two = stree;
767 struct sm_state *sm;
768 struct state_list *add_to_one = NULL;
769 struct state_list *add_to_two = NULL;
770 AvlIter one_iter;
771 AvlIter two_iter;
773 if (!stree)
774 return;
775 if (*to == stree)
776 return;
777 if (!*to) {
778 *to = clone_stree(stree);
779 return;
782 avl_iter_begin(&one_iter, one, FORWARD);
783 avl_iter_begin(&two_iter, two, FORWARD);
785 for (;;) {
786 if (!one_iter.sm && !two_iter.sm)
787 break;
788 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
789 sm = get_sm_state(one_iter.sm->owner, one_iter.sm->name,
790 one_iter.sm->sym);
791 if (sm)
792 add_ptr_list(&add_to_two, sm);
793 avl_iter_next(&one_iter);
794 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
795 avl_iter_next(&one_iter);
796 avl_iter_next(&two_iter);
797 } else {
798 sm = get_sm_state(two_iter.sm->owner, two_iter.sm->name,
799 two_iter.sm->sym);
800 if (sm)
801 add_ptr_list(&add_to_one, sm);
802 avl_iter_next(&two_iter);
806 FOR_EACH_PTR(add_to_one, sm) {
807 avl_insert(&one, sm);
808 } END_FOR_EACH_PTR(sm);
810 FOR_EACH_PTR(add_to_two, sm) {
811 avl_insert(&two, sm);
812 } END_FOR_EACH_PTR(sm);
814 free_slist(&add_to_one);
815 free_slist(&add_to_two);
817 __merge_stree(&one, two, 1);
819 *to = one;
823 * filter_slist() removes any sm states "slist" holds in common with "filter"
825 void filter_stree(struct stree **stree, struct stree *filter)
827 struct stree *results = NULL;
828 AvlIter one_iter;
829 AvlIter two_iter;
831 avl_iter_begin(&one_iter, *stree, FORWARD);
832 avl_iter_begin(&two_iter, filter, FORWARD);
834 /* FIXME: This should probably be re-written with trees in mind */
836 for (;;) {
837 if (!one_iter.sm && !two_iter.sm)
838 break;
839 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
840 avl_insert(&results, one_iter.sm);
841 avl_iter_next(&one_iter);
842 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
843 if (one_iter.sm != two_iter.sm)
844 avl_insert(&results, one_iter.sm);
845 avl_iter_next(&one_iter);
846 avl_iter_next(&two_iter);
847 } else {
848 avl_iter_next(&two_iter);
852 free_stree(stree);
853 *stree = results;
858 * and_slist_stack() pops the top two slists, overwriting the one with
859 * the other and pushing it back on the stack.
861 void and_stree_stack(struct stree_stack **stack)
863 struct sm_state *tmp;
864 struct stree *right_stree = pop_stree(stack);
866 FOR_EACH_SM(right_stree, tmp) {
867 overwrite_sm_state_stree_stack(stack, tmp);
868 } END_FOR_EACH_SM(tmp);
869 free_stree(&right_stree);
873 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
874 * It pops the two slists from the top of the stack and merges them
875 * together in a way that preserves the things they have in common
876 * but creates a merged state for most of the rest.
877 * You could have code that had: if (foo || foo) { foo->baz;
878 * It's this function which ensures smatch does the right thing.
880 void or_stree_stack(struct stree_stack **pre_conds,
881 struct stree *cur_stree,
882 struct stree_stack **stack)
884 struct stree *new;
885 struct stree *old;
886 struct stree *pre_stree;
887 struct stree *res;
888 struct stree *tmp_stree;
890 new = pop_stree(stack);
891 old = pop_stree(stack);
893 pre_stree = pop_stree(pre_conds);
894 push_stree(pre_conds, clone_stree(pre_stree));
896 res = clone_stree(pre_stree);
897 overwrite_stree(old, &res);
899 tmp_stree = clone_stree(cur_stree);
900 overwrite_stree(new, &tmp_stree);
902 merge_stree(&res, tmp_stree);
903 filter_stree(&res, pre_stree);
905 push_stree(stack, res);
906 free_stree(&tmp_stree);
907 free_stree(&pre_stree);
908 free_stree(&new);
909 free_stree(&old);
913 * get_named_stree() is only used for gotos.
915 struct stree **get_named_stree(struct named_stree_stack *stack,
916 const char *name)
918 struct named_stree *tmp;
920 FOR_EACH_PTR(stack, tmp) {
921 if (!strcmp(tmp->name, name))
922 return &tmp->stree;
923 } END_FOR_EACH_PTR(tmp);
924 return NULL;
927 /* FIXME: These parameters are in a different order from expected */
928 void overwrite_stree(struct stree *from, struct stree **to)
930 struct sm_state *tmp;
932 FOR_EACH_SM(from, tmp) {
933 overwrite_sm_state_stree(to, tmp);
934 } END_FOR_EACH_SM(tmp);