ranges: add a missing cast in add_range_t()
[smatch.git] / smatch_slist.c
blob55becc967cff5c8438417612d6c7d888b648b218
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)
103 return ret;
105 if (!b->sym && a->sym)
106 return -1;
107 if (!a->sym && b->sym)
108 return 1;
109 if (a->sym > b->sym)
110 return -1;
111 if (a->sym < b->sym)
112 return 1;
114 return 0;
117 static int cmp_sm_states(const struct sm_state *a, const struct sm_state *b, int preserve)
119 int ret;
121 ret = cmp_tracker(a, b);
122 if (ret)
123 return ret;
125 /* todo: add hook for smatch_extra.c */
126 if (a->state > b->state)
127 return -1;
128 if (a->state < b->state)
129 return 1;
130 /* This is obviously a massive disgusting hack but we need to preserve
131 * the unmerged states for smatch extra because we use them in
132 * smatch_db.c. Meanwhile if we preserve all the other unmerged states
133 * then it uses a lot of memory and we don't use it. Hence this hack.
135 * Also sometimes even just preserving every possible SMATCH_EXTRA state
136 * takes too much resources so we have to cap that. Capping is probably
137 * not often a problem in real life.
139 if (a->owner == SMATCH_EXTRA && preserve) {
140 if (a == b)
141 return 0;
142 if (a->merged == 1 && b->merged == 0)
143 return -1;
144 if (a->merged == 0)
145 return 1;
148 return 0;
151 struct sm_state *alloc_sm_state(int owner, const char *name,
152 struct symbol *sym, struct smatch_state *state)
154 struct sm_state *sm_state = __alloc_sm_state(0);
156 sm_state_counter++;
158 sm_state->name = alloc_sname(name);
159 sm_state->owner = owner;
160 sm_state->sym = sym;
161 sm_state->state = state;
162 sm_state->line = get_lineno();
163 sm_state->merged = 0;
164 sm_state->implied = 0;
165 sm_state->pool = NULL;
166 sm_state->left = NULL;
167 sm_state->right = NULL;
168 sm_state->nr_children = 1;
169 sm_state->possible = NULL;
170 add_ptr_list(&sm_state->possible, sm_state);
171 return sm_state;
174 static struct sm_state *alloc_state_no_name(int owner, const char *name,
175 struct symbol *sym,
176 struct smatch_state *state)
178 struct sm_state *tmp;
180 tmp = alloc_sm_state(owner, NULL, sym, state);
181 tmp->name = name;
182 return tmp;
185 int too_many_possible(struct sm_state *sm)
187 if (ptr_list_size((struct ptr_list *)sm->possible) >= 100)
188 return 1;
189 return 0;
192 void add_possible_sm(struct sm_state *to, struct sm_state *new)
194 struct sm_state *tmp;
195 int preserve = 1;
197 if (too_many_possible(to))
198 preserve = 0;
200 FOR_EACH_PTR(to->possible, tmp) {
201 if (cmp_sm_states(tmp, new, preserve) < 0)
202 continue;
203 else if (cmp_sm_states(tmp, new, preserve) == 0) {
204 return;
205 } else {
206 INSERT_CURRENT(new, tmp);
207 return;
209 } END_FOR_EACH_PTR(tmp);
210 add_ptr_list(&to->possible, new);
213 static void copy_possibles(struct sm_state *to, struct sm_state *from)
215 struct sm_state *tmp;
217 FOR_EACH_PTR(from->possible, tmp) {
218 add_possible_sm(to, tmp);
219 } END_FOR_EACH_PTR(tmp);
222 char *alloc_sname(const char *str)
224 char *tmp;
226 if (!str)
227 return NULL;
228 tmp = __alloc_sname(strlen(str) + 1);
229 strcpy(tmp, str);
230 return tmp;
233 int out_of_memory(void)
236 * I decided to use 50M here based on trial and error.
237 * It works out OK for the kernel and so it should work
238 * for most other projects as well.
240 if (sm_state_counter * sizeof(struct sm_state) >= 50000000)
241 return 1;
242 return 0;
245 int low_on_memory(void)
247 if (sm_state_counter * sizeof(struct sm_state) >= 25000000)
248 return 1;
249 return 0;
252 static void free_sm_state(struct sm_state *sm)
254 free_slist(&sm->possible);
256 * fixme. Free the actual state.
257 * Right now we leave it until the end of the function
258 * because we don't want to double free it.
259 * Use the freelist to not double free things
263 static void free_all_sm_states(struct allocation_blob *blob)
265 unsigned int size = sizeof(struct sm_state);
266 unsigned int offset = 0;
268 while (offset < blob->offset) {
269 free_sm_state((struct sm_state *)(blob->data + offset));
270 offset += size;
274 /* At the end of every function we free all the sm_states */
275 void free_every_single_sm_state(void)
277 struct allocator_struct *desc = &sm_state_allocator;
278 struct allocation_blob *blob = desc->blobs;
280 desc->blobs = NULL;
281 desc->allocations = 0;
282 desc->total_bytes = 0;
283 desc->useful_bytes = 0;
284 desc->freelist = NULL;
285 while (blob) {
286 struct allocation_blob *next = blob->next;
287 free_all_sm_states(blob);
288 blob_free(blob, desc->chunking);
289 blob = next;
291 clear_sname_alloc();
292 clear_smatch_state_alloc();
294 free_stack_and_strees(&all_pools);
295 sm_state_counter = 0;
298 struct sm_state *clone_sm(struct sm_state *s)
300 struct sm_state *ret;
302 ret = alloc_state_no_name(s->owner, s->name, s->sym, s->state);
303 ret->merged = s->merged;
304 ret->implied = s->implied;
305 ret->line = s->line;
306 /* clone_sm() doesn't copy the pools. Each state needs to have
307 only one pool. */
308 ret->possible = clone_slist(s->possible);
309 ret->left = s->left;
310 ret->right = s->right;
311 ret->nr_children = s->nr_children;
312 return ret;
315 int is_merged(struct sm_state *sm)
317 return sm->merged;
320 int is_implied(struct sm_state *sm)
322 return sm->implied;
325 int slist_has_state(struct state_list *slist, struct smatch_state *state)
327 struct sm_state *tmp;
329 FOR_EACH_PTR(slist, tmp) {
330 if (tmp->state == state)
331 return 1;
332 } END_FOR_EACH_PTR(tmp);
333 return 0;
336 struct state_list *clone_slist(struct state_list *from_slist)
338 struct sm_state *sm;
339 struct state_list *to_slist = NULL;
341 FOR_EACH_PTR(from_slist, sm) {
342 add_ptr_list(&to_slist, sm);
343 } END_FOR_EACH_PTR(sm);
344 return to_slist;
347 struct smatch_state *merge_states(int owner, const char *name,
348 struct symbol *sym,
349 struct smatch_state *state1,
350 struct smatch_state *state2)
352 struct smatch_state *ret;
354 if (state1 == state2)
355 ret = state1;
356 else if (__has_merge_function(owner))
357 ret = __client_merge_function(owner, state1, state2);
358 else if (state1 == &ghost)
359 ret = state2;
360 else if (state2 == &ghost)
361 ret = state1;
362 else if (!state1 || !state2)
363 ret = &undefined;
364 else
365 ret = &merged;
366 return ret;
369 struct sm_state *merge_sm_states(struct sm_state *one, struct sm_state *two)
371 struct smatch_state *s;
372 struct sm_state *result;
374 if (one == two)
375 return one;
376 s = merge_states(one->owner, one->name, one->sym, one->state, two->state);
377 result = alloc_state_no_name(one->owner, one->name, one->sym, s);
378 result->merged = 1;
379 result->left = one;
380 result->right = two;
381 result->nr_children = one->nr_children + two->nr_children;
382 copy_possibles(result, one);
383 copy_possibles(result, two);
385 if (option_debug ||
386 strcmp(check_name(one->owner), option_debug_check) == 0) {
387 struct sm_state *tmp;
388 int i = 0;
390 printf("%s:%d %s() merge [%s] '%s' %s(L %d) + %s(L %d) => %s (",
391 get_filename(), get_lineno(), get_function(),
392 check_name(one->owner), one->name,
393 show_state(one->state), one->line,
394 show_state(two->state), two->line,
395 show_state(s));
397 FOR_EACH_PTR(result->possible, tmp) {
398 if (i++)
399 printf(", ");
400 printf("%s", show_state(tmp->state));
401 } END_FOR_EACH_PTR(tmp);
402 printf(")\n");
405 return result;
408 struct sm_state *get_sm_state_stree(struct stree *stree, int owner, const char *name,
409 struct symbol *sym)
411 struct tracker tracker = {
412 .owner = owner,
413 .name = (char *)name,
414 .sym = sym,
417 if (!name)
418 return NULL;
421 return avl_lookup(stree, (struct sm_state *)&tracker);
424 struct smatch_state *get_state_stree(struct stree *stree,
425 int owner, const char *name,
426 struct symbol *sym)
428 struct sm_state *sm;
430 sm = get_sm_state_stree(stree, owner, name, sym);
431 if (sm)
432 return sm->state;
433 return NULL;
436 /* FIXME: this is almost exactly the same as set_sm_state_slist() */
437 void overwrite_sm_state_stree(struct stree **stree, struct sm_state *new)
439 avl_insert(stree, new);
442 void overwrite_sm_state_stree_stack(struct stree_stack **stack,
443 struct sm_state *sm)
445 struct stree *stree;
447 stree = pop_stree(stack);
448 overwrite_sm_state_stree(&stree, sm);
449 push_stree(stack, stree);
452 struct sm_state *set_state_stree(struct stree **stree, int owner, const char *name,
453 struct symbol *sym, struct smatch_state *state)
455 struct sm_state *new = alloc_sm_state(owner, name, sym, state);
457 avl_insert(stree, new);
458 return new;
461 void set_state_stree_perm(struct stree **stree, int owner, const char *name,
462 struct symbol *sym, struct smatch_state *state)
464 struct sm_state *sm;
466 sm = malloc(sizeof(*sm) + strlen(name) + 1);
467 memset(sm, 0, sizeof(*sm));
468 sm->owner = owner;
469 sm->name = (char *)(sm + 1);
470 strcpy((char *)sm->name, name);
471 sm->sym = sym;
472 sm->state = state;
474 overwrite_sm_state_stree(stree, sm);
477 void delete_state_stree(struct stree **stree, int owner, const char *name,
478 struct symbol *sym)
480 struct tracker tracker = {
481 .owner = owner,
482 .name = (char *)name,
483 .sym = sym,
486 avl_remove(stree, (struct sm_state *)&tracker);
489 void delete_state_stree_stack(struct stree_stack **stack, int owner, const char *name,
490 struct symbol *sym)
492 struct stree *stree;
494 stree = pop_stree(stack);
495 delete_state_stree(&stree, owner, name, sym);
496 push_stree(stack, stree);
499 void push_stree(struct stree_stack **stack, struct stree *stree)
501 add_ptr_list(stack, stree);
504 struct stree *pop_stree(struct stree_stack **stack)
506 struct stree *stree;
508 stree = last_ptr_list((struct ptr_list *)*stack);
509 delete_ptr_list_last((struct ptr_list **)stack);
510 return stree;
513 void free_slist(struct state_list **slist)
515 __free_ptr_list((struct ptr_list **)slist);
518 void free_stree_stack(struct stree_stack **stack)
520 __free_ptr_list((struct ptr_list **)stack);
523 void free_stack_and_strees(struct stree_stack **stree_stack)
525 struct stree *stree;
527 FOR_EACH_PTR(*stree_stack, stree) {
528 free_stree(&stree);
529 } END_FOR_EACH_PTR(stree);
530 free_stree_stack(stree_stack);
533 struct sm_state *set_state_stree_stack(struct stree_stack **stack, int owner, const char *name,
534 struct symbol *sym, struct smatch_state *state)
536 struct stree *stree;
537 struct sm_state *sm;
539 stree = pop_stree(stack);
540 sm = set_state_stree(&stree, owner, name, sym, state);
541 push_stree(stack, stree);
543 return sm;
547 * get_sm_state_stack() gets the state for the top slist on the stack.
549 struct sm_state *get_sm_state_stree_stack(struct stree_stack *stack,
550 int owner, const char *name,
551 struct symbol *sym)
553 struct stree *stree;
554 struct sm_state *ret;
556 stree = pop_stree(&stack);
557 ret = get_sm_state_stree(stree, owner, name, sym);
558 push_stree(&stack, stree);
559 return ret;
562 struct smatch_state *get_state_stree_stack(struct stree_stack *stack,
563 int owner, const char *name,
564 struct symbol *sym)
566 struct sm_state *sm;
568 sm = get_sm_state_stree_stack(stack, owner, name, sym);
569 if (sm)
570 return sm->state;
571 return NULL;
574 static void match_states_stree(struct stree **one, struct stree **two)
576 struct smatch_state *tmp_state;
577 struct sm_state *sm;
578 struct state_list *add_to_one = NULL;
579 struct state_list *add_to_two = NULL;
580 AvlIter one_iter;
581 AvlIter two_iter;
583 avl_iter_begin(&one_iter, *one, FORWARD);
584 avl_iter_begin(&two_iter, *two, FORWARD);
586 for (;;) {
587 if (!one_iter.sm && !two_iter.sm)
588 break;
589 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
590 __set_fake_cur_stree_fast(*two);
591 tmp_state = __client_unmatched_state_function(one_iter.sm);
592 __pop_fake_cur_stree_fast();
593 sm = alloc_state_no_name(one_iter.sm->owner, one_iter.sm->name,
594 one_iter.sm->sym, tmp_state);
595 add_ptr_list(&add_to_two, sm);
596 avl_iter_next(&one_iter);
597 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
598 avl_iter_next(&one_iter);
599 avl_iter_next(&two_iter);
600 } else {
601 __set_fake_cur_stree_fast(*one);
602 tmp_state = __client_unmatched_state_function(two_iter.sm);
603 __pop_fake_cur_stree_fast();
604 sm = alloc_state_no_name(two_iter.sm->owner, two_iter.sm->name,
605 two_iter.sm->sym, tmp_state);
606 add_ptr_list(&add_to_one, sm);
607 avl_iter_next(&two_iter);
611 FOR_EACH_PTR(add_to_one, sm) {
612 avl_insert(one, sm);
613 } END_FOR_EACH_PTR(sm);
615 FOR_EACH_PTR(add_to_two, sm) {
616 avl_insert(two, sm);
617 } END_FOR_EACH_PTR(sm);
619 free_slist(&add_to_one);
620 free_slist(&add_to_two);
623 static void call_pre_merge_hooks(struct stree **one, struct stree **two)
625 struct sm_state *sm;
627 save_all_states();
629 __swap_cur_stree(*one);
630 FOR_EACH_SM(*two, sm) {
631 call_pre_merge_hook(sm);
632 } END_FOR_EACH_SM(sm);
633 *one = clone_stree(__get_cur_stree());
635 __swap_cur_stree(*two);
636 FOR_EACH_SM(*one, sm) {
637 call_pre_merge_hook(sm);
638 } END_FOR_EACH_SM(sm);
639 *two = clone_stree(__get_cur_stree());
641 restore_all_states();
644 static void clone_pool_havers_stree(struct stree **stree)
646 struct sm_state *sm, *tmp;
647 struct state_list *slist = NULL;
649 FOR_EACH_SM(*stree, sm) {
650 if (sm->pool) {
651 tmp = clone_sm(sm);
652 add_ptr_list(&slist, tmp);
654 } END_FOR_EACH_SM(sm);
656 FOR_EACH_PTR(slist, sm) {
657 avl_insert(stree, sm);
658 } END_FOR_EACH_PTR(sm);
660 free_slist(&slist);
663 int __stree_id;
666 * merge_slist() is called whenever paths merge, such as after
667 * an if statement. It takes the two slists and creates one.
669 static void __merge_stree(struct stree **to, struct stree *stree, int add_pool)
671 struct stree *results = NULL;
672 struct stree *implied_one = NULL;
673 struct stree *implied_two = NULL;
674 AvlIter one_iter;
675 AvlIter two_iter;
676 struct sm_state *tmp_sm;
678 if (out_of_memory())
679 return;
681 /* merging a null and nonnull path gives you only the nonnull path */
682 if (!stree)
683 return;
684 if (*to == stree)
685 return;
687 if (!*to) {
688 *to = clone_stree(stree);
689 return;
692 implied_one = clone_stree(*to);
693 implied_two = clone_stree(stree);
695 match_states_stree(&implied_one, &implied_two);
696 call_pre_merge_hooks(&implied_one, &implied_two);
698 if (add_pool) {
699 clone_pool_havers_stree(&implied_one);
700 clone_pool_havers_stree(&implied_two);
702 set_stree_id(&implied_one, ++__stree_id);
703 set_stree_id(&implied_two, ++__stree_id);
706 push_stree(&all_pools, implied_one);
707 push_stree(&all_pools, implied_two);
709 avl_iter_begin(&one_iter, implied_one, FORWARD);
710 avl_iter_begin(&two_iter, implied_two, FORWARD);
712 for (;;) {
713 if (!one_iter.sm && !two_iter.sm)
714 break;
715 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
716 sm_msg("error: Internal smatch error.");
717 avl_iter_next(&one_iter);
718 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
719 if (add_pool && one_iter.sm != two_iter.sm) {
720 one_iter.sm->pool = implied_one;
721 two_iter.sm->pool = implied_two;
723 tmp_sm = merge_sm_states(one_iter.sm, two_iter.sm);
724 avl_insert(&results, tmp_sm);
725 avl_iter_next(&one_iter);
726 avl_iter_next(&two_iter);
727 } else {
728 sm_msg("error: Internal smatch error.");
729 avl_iter_next(&two_iter);
733 free_stree(to);
734 *to = results;
737 void merge_stree(struct stree **to, struct stree *stree)
739 __merge_stree(to, stree, 1);
742 void merge_stree_no_pools(struct stree **to, struct stree *stree)
744 __merge_stree(to, stree, 0);
748 * This is unfortunately a bit subtle... The problem is that if a
749 * state is set on one fake stree but not the other then we should
750 * look up the the original state and use that as the unset state.
751 * Fortunately, after you pop your fake stree then the cur_slist should
752 * reflect the original state.
754 void merge_fake_stree(struct stree **to, struct stree *stree)
756 struct stree *one = *to;
757 struct stree *two = stree;
758 struct sm_state *sm;
759 struct state_list *add_to_one = NULL;
760 struct state_list *add_to_two = NULL;
761 AvlIter one_iter;
762 AvlIter two_iter;
764 if (!stree)
765 return;
766 if (*to == stree)
767 return;
768 if (!*to) {
769 *to = clone_stree(stree);
770 return;
773 avl_iter_begin(&one_iter, one, FORWARD);
774 avl_iter_begin(&two_iter, two, FORWARD);
776 for (;;) {
777 if (!one_iter.sm && !two_iter.sm)
778 break;
779 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
780 sm = get_sm_state(one_iter.sm->owner, one_iter.sm->name,
781 one_iter.sm->sym);
782 if (sm)
783 add_ptr_list(&add_to_two, sm);
784 avl_iter_next(&one_iter);
785 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
786 avl_iter_next(&one_iter);
787 avl_iter_next(&two_iter);
788 } else {
789 sm = get_sm_state(two_iter.sm->owner, two_iter.sm->name,
790 two_iter.sm->sym);
791 if (sm)
792 add_ptr_list(&add_to_one, sm);
793 avl_iter_next(&two_iter);
797 FOR_EACH_PTR(add_to_one, sm) {
798 avl_insert(&one, sm);
799 } END_FOR_EACH_PTR(sm);
801 FOR_EACH_PTR(add_to_two, sm) {
802 avl_insert(&two, sm);
803 } END_FOR_EACH_PTR(sm);
805 free_slist(&add_to_one);
806 free_slist(&add_to_two);
808 __merge_stree(&one, two, 1);
810 *to = one;
814 * filter_slist() removes any sm states "slist" holds in common with "filter"
816 void filter_stree(struct stree **stree, struct stree *filter)
818 struct stree *results = NULL;
819 AvlIter one_iter;
820 AvlIter two_iter;
822 avl_iter_begin(&one_iter, *stree, FORWARD);
823 avl_iter_begin(&two_iter, filter, FORWARD);
825 /* FIXME: This should probably be re-written with trees in mind */
827 for (;;) {
828 if (!one_iter.sm && !two_iter.sm)
829 break;
830 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
831 avl_insert(&results, one_iter.sm);
832 avl_iter_next(&one_iter);
833 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
834 if (one_iter.sm != two_iter.sm)
835 avl_insert(&results, one_iter.sm);
836 avl_iter_next(&one_iter);
837 avl_iter_next(&two_iter);
838 } else {
839 avl_iter_next(&two_iter);
843 free_stree(stree);
844 *stree = results;
849 * and_slist_stack() pops the top two slists, overwriting the one with
850 * the other and pushing it back on the stack.
852 void and_stree_stack(struct stree_stack **stack)
854 struct sm_state *tmp;
855 struct stree *right_stree = pop_stree(stack);
857 FOR_EACH_SM(right_stree, tmp) {
858 overwrite_sm_state_stree_stack(stack, tmp);
859 } END_FOR_EACH_SM(tmp);
860 free_stree(&right_stree);
864 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
865 * It pops the two slists from the top of the stack and merges them
866 * together in a way that preserves the things they have in common
867 * but creates a merged state for most of the rest.
868 * You could have code that had: if (foo || foo) { foo->baz;
869 * It's this function which ensures smatch does the right thing.
871 void or_stree_stack(struct stree_stack **pre_conds,
872 struct stree *cur_stree,
873 struct stree_stack **stack)
875 struct stree *new;
876 struct stree *old;
877 struct stree *pre_stree;
878 struct stree *res;
879 struct stree *tmp_stree;
881 new = pop_stree(stack);
882 old = pop_stree(stack);
884 pre_stree = pop_stree(pre_conds);
885 push_stree(pre_conds, clone_stree(pre_stree));
887 res = clone_stree(pre_stree);
888 overwrite_stree(old, &res);
890 tmp_stree = clone_stree(cur_stree);
891 overwrite_stree(new, &tmp_stree);
893 merge_stree(&res, tmp_stree);
894 filter_stree(&res, pre_stree);
896 push_stree(stack, res);
897 free_stree(&tmp_stree);
898 free_stree(&pre_stree);
899 free_stree(&new);
900 free_stree(&old);
904 * get_named_stree() is only used for gotos.
906 struct stree **get_named_stree(struct named_stree_stack *stack,
907 const char *name)
909 struct named_stree *tmp;
911 FOR_EACH_PTR(stack, tmp) {
912 if (!strcmp(tmp->name, name))
913 return &tmp->stree;
914 } END_FOR_EACH_PTR(tmp);
915 return NULL;
918 /* FIXME: These parameters are in a different order from expected */
919 void overwrite_stree(struct stree *from, struct stree **to)
921 struct sm_state *tmp;
923 FOR_EACH_SM(from, tmp) {
924 overwrite_sm_state_stree(to, tmp);
925 } END_FOR_EACH_SM(tmp);