conditions: don't parse select statements twice
[smatch.git] / smatch_slist.c
blob789269d840a349add34430c565483f40f6161bde
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'%s",
45 check_name(sm->owner), sm->name, show_state(sm->state),
46 sm->merged ? " [merged]" : "");
47 if (pos > sizeof(buf))
48 goto truncate;
50 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
51 return buf;
53 pos += snprintf(buf + pos, sizeof(buf) - pos, " (");
54 if (pos > sizeof(buf))
55 goto truncate;
56 i = 0;
57 FOR_EACH_PTR(sm->possible, tmp) {
58 if (i++)
59 pos += snprintf(buf + pos, sizeof(buf) - pos, ", ");
60 if (pos > sizeof(buf))
61 goto truncate;
62 pos += snprintf(buf + pos, sizeof(buf) - pos, "%s",
63 show_state(tmp->state));
64 if (pos > sizeof(buf))
65 goto truncate;
66 } END_FOR_EACH_PTR(tmp);
67 snprintf(buf + pos, sizeof(buf) - pos, ")");
69 return buf;
71 truncate:
72 for (i = 0; i < 3; i++)
73 buf[sizeof(buf) - 2 - i] = '.';
74 return buf;
77 void __print_stree(struct stree *stree)
79 struct sm_state *sm;
81 option_debug++;
82 sm_msg("dumping stree [%ld states]", stree_count(stree));
83 FOR_EACH_SM(stree, sm) {
84 sm_printf("%s\n", show_sm(sm));
85 } END_FOR_EACH_SM(sm);
86 sm_printf("---\n");
87 option_debug--;
90 /* NULL states go at the end to simplify merge_slist */
91 int cmp_tracker(const struct sm_state *a, const struct sm_state *b)
93 int ret;
95 if (a == b)
96 return 0;
97 if (!b)
98 return -1;
99 if (!a)
100 return 1;
102 if (a->owner < b->owner)
103 return -1;
104 if (a->owner > b->owner)
105 return 1;
107 ret = strcmp(a->name, b->name);
108 if (ret < 0)
109 return -1;
110 if (ret > 0)
111 return 1;
113 if (!b->sym && a->sym)
114 return -1;
115 if (!a->sym && b->sym)
116 return 1;
117 if (a->sym < b->sym)
118 return -1;
119 if (a->sym > b->sym)
120 return 1;
122 return 0;
125 int *dynamic_states;
126 void allocate_dynamic_states_array(int num_checks)
128 dynamic_states = calloc(num_checks + 1, sizeof(int));
131 void set_dynamic_states(unsigned short owner)
133 dynamic_states[owner] = true;
136 bool has_dynamic_states(unsigned short owner)
138 if (owner >= num_checks)
139 return false;
140 return dynamic_states[owner];
143 static int cmp_possible_sm(const struct sm_state *a, const struct sm_state *b, int preserve)
145 int ret;
147 if (a == b)
148 return 0;
150 if (!has_dynamic_states(a->owner)) {
151 if (a->state > b->state)
152 return -1;
153 if (a->state < b->state)
154 return 1;
155 return 0;
158 if (a->owner == SMATCH_EXTRA) {
160 * In Smatch extra you can have borrowed implications.
162 * FIXME: review how borrowed implications work and if they
163 * are the best way. See also smatch_implied.c.
166 ret = cmp_tracker(a, b);
167 if (ret)
168 return ret;
171 * We want to preserve leaf states. They're use to split
172 * returns in smatch_db.c.
175 if (preserve) {
176 if (a->merged && !b->merged)
177 return -1;
178 if (!a->merged)
179 return 1;
182 if (!a->state->name || !b->state->name)
183 return 0;
185 return strcmp(a->state->name, b->state->name);
188 struct sm_state *alloc_sm_state(int owner, const char *name,
189 struct symbol *sym, struct smatch_state *state)
191 struct sm_state *sm_state = __alloc_sm_state(0);
193 sm_state_counter++;
195 sm_state->name = alloc_sname(name);
196 sm_state->owner = owner;
197 sm_state->sym = sym;
198 sm_state->state = state;
199 sm_state->line = get_lineno();
200 sm_state->merged = 0;
201 sm_state->pool = NULL;
202 sm_state->left = NULL;
203 sm_state->right = NULL;
204 sm_state->possible = NULL;
205 add_ptr_list(&sm_state->possible, sm_state);
206 return sm_state;
209 static struct sm_state *alloc_state_no_name(int owner, const char *name,
210 struct symbol *sym,
211 struct smatch_state *state)
213 struct sm_state *tmp;
215 tmp = alloc_sm_state(owner, NULL, sym, state);
216 tmp->name = name;
217 return tmp;
220 int too_many_possible(struct sm_state *sm)
222 if (ptr_list_size((struct ptr_list *)sm->possible) >= 100)
223 return 1;
224 return 0;
227 void add_possible_sm(struct sm_state *to, struct sm_state *new)
229 struct sm_state *tmp;
230 int preserve = 1;
231 int cmp;
233 if (too_many_possible(to))
234 preserve = 0;
236 FOR_EACH_PTR(to->possible, tmp) {
237 cmp = cmp_possible_sm(tmp, new, preserve);
238 if (cmp < 0)
239 continue;
240 else if (cmp == 0) {
241 return;
242 } else {
243 INSERT_CURRENT(new, tmp);
244 return;
246 } END_FOR_EACH_PTR(tmp);
247 add_ptr_list(&to->possible, new);
250 static void copy_possibles(struct sm_state *to, struct sm_state *one, struct sm_state *two)
252 struct sm_state *large = one;
253 struct sm_state *small = two;
254 struct sm_state *tmp;
257 * We spend a lot of time copying the possible lists. I've tried to
258 * optimize the process a bit.
262 if (ptr_list_size((struct ptr_list *)two->possible) >
263 ptr_list_size((struct ptr_list *)one->possible)) {
264 large = two;
265 small = one;
268 to->possible = clone_slist(large->possible);
269 add_possible_sm(to, to);
270 FOR_EACH_PTR(small->possible, tmp) {
271 add_possible_sm(to, tmp);
272 } END_FOR_EACH_PTR(tmp);
275 char *alloc_sname(const char *str)
277 char *tmp;
279 if (!str)
280 return NULL;
281 tmp = __alloc_sname(strlen(str) + 1);
282 strcpy(tmp, str);
283 return tmp;
286 static struct symbol *oom_func;
287 static int oom_limit = 3000000; /* Start with a 3GB limit */
288 int out_of_memory(void)
290 if (oom_func)
291 return 1;
294 * I decided to use 50M here based on trial and error.
295 * It works out OK for the kernel and so it should work
296 * for most other projects as well.
298 if (sm_state_counter * sizeof(struct sm_state) >= 100000000)
299 return 1;
302 * We're reading from statm to figure out how much memory we
303 * are using. The problem is that at the end of the function
304 * we release the memory, so that it can be re-used but it
305 * stays in cache, it's not released to the OS. So then if
306 * we allocate memory for different purposes we can easily
307 * hit the 3GB limit on the next function, so that's why I give
308 * the next function an extra 100MB to work with.
311 if (get_mem_kb() > oom_limit) {
312 oom_func = cur_func_sym;
313 final_pass++;
314 sm_perror("OOM: %luKb sm_state_count = %d", get_mem_kb(), sm_state_counter);
315 final_pass--;
316 return 1;
319 return 0;
322 int low_on_memory(void)
324 if (sm_state_counter * sizeof(struct sm_state) >= 25000000)
325 return 1;
326 return 0;
329 static void free_sm_state(struct sm_state *sm)
331 free_slist(&sm->possible);
333 * fixme. Free the actual state.
334 * Right now we leave it until the end of the function
335 * because we don't want to double free it.
336 * Use the freelist to not double free things
340 static void free_all_sm_states(struct allocation_blob *blob)
342 unsigned int size = sizeof(struct sm_state);
343 unsigned int offset = 0;
345 while (offset < blob->offset) {
346 free_sm_state((struct sm_state *)(blob->data + offset));
347 offset += size;
351 /* At the end of every function we free all the sm_states */
352 void free_every_single_sm_state(void)
354 struct allocator_struct *desc = &sm_state_allocator;
355 struct allocation_blob *blob = desc->blobs;
357 desc->blobs = NULL;
358 desc->allocations = 0;
359 desc->total_bytes = 0;
360 desc->useful_bytes = 0;
361 desc->freelist = NULL;
362 while (blob) {
363 struct allocation_blob *next = blob->next;
364 free_all_sm_states(blob);
365 blob_free(blob, desc->chunking);
366 blob = next;
368 clear_sname_alloc();
369 clear_smatch_state_alloc();
371 free_stack_and_strees(&all_pools);
372 sm_state_counter = 0;
373 if (oom_func) {
374 oom_limit += 100000;
375 oom_func = NULL;
379 unsigned long get_pool_count(void)
381 return ptr_list_size((struct ptr_list *)all_pools);
384 struct sm_state *clone_sm(struct sm_state *s)
386 struct sm_state *ret;
388 ret = alloc_state_no_name(s->owner, s->name, s->sym, s->state);
389 ret->merged = s->merged;
390 ret->line = s->line;
391 /* clone_sm() doesn't copy the pools. Each state needs to have
392 only one pool. */
393 ret->possible = clone_slist(s->possible);
394 ret->left = s->left;
395 ret->right = s->right;
396 return ret;
399 int is_merged(struct sm_state *sm)
401 return sm->merged;
404 int is_leaf(struct sm_state *sm)
406 return !sm->merged;
409 int slist_has_state(struct state_list *slist, struct smatch_state *state)
411 struct sm_state *tmp;
413 FOR_EACH_PTR(slist, tmp) {
414 if (tmp->state == state)
415 return 1;
416 } END_FOR_EACH_PTR(tmp);
417 return 0;
420 struct state_list *clone_slist(struct state_list *from_slist)
422 struct sm_state *sm;
423 struct state_list *to_slist = NULL;
425 FOR_EACH_PTR(from_slist, sm) {
426 add_ptr_list(&to_slist, sm);
427 } END_FOR_EACH_PTR(sm);
428 return to_slist;
431 static struct smatch_state *merge_states(int owner, const char *name,
432 struct symbol *sym,
433 struct smatch_state *state1,
434 struct smatch_state *state2)
436 struct smatch_state *ret;
438 if (state1 == state2)
439 ret = state1;
440 else if (__has_merge_function(owner))
441 ret = __client_merge_function(owner, state1, state2);
442 else if (state1 == &ghost)
443 ret = state2;
444 else if (state2 == &ghost)
445 ret = state1;
446 else if (!state1 || !state2)
447 ret = &undefined;
448 else
449 ret = &merged;
450 return ret;
453 struct sm_state *merge_sm_states(struct sm_state *one, struct sm_state *two)
455 struct smatch_state *s;
456 struct sm_state *result;
457 static int warned;
459 if (one->state->data && !has_dynamic_states(one->owner))
460 sm_msg("dynamic state: %s", show_sm(one));
462 if (one == two)
463 return one;
464 if (out_of_memory()) {
465 if (!warned)
466 sm_warning("Function too hairy. No more merges.");
467 warned = 1;
468 return one;
470 warned = 0;
471 s = merge_states(one->owner, one->name, one->sym, one->state, two->state);
472 result = alloc_state_no_name(one->owner, one->name, one->sym, s);
473 result->merged = 1;
474 result->left = one;
475 result->right = two;
477 copy_possibles(result, one, two);
480 * The ->line information is used by deref_check where we complain about
481 * checking pointers that have already been dereferenced. Let's say we
482 * dereference a pointer on both the true and false paths and then merge
483 * the states here. The result state is &derefed, but the ->line number
484 * is on the line where the pointer is merged not where it was
485 * dereferenced..
487 * So in that case, let's just pick one dereference and set the ->line
488 * to point at it.
492 if (result->state == one->state)
493 result->line = one->line;
494 if (result->state == two->state)
495 result->line = two->line;
497 if (debug_on(check_name(one->owner), one->name)) {
498 struct sm_state *tmp;
499 int i = 0;
501 printf("%s:%d %s() merge [%s] '%s' %s(L %d) + %s(L %d) => %s (",
502 get_filename(), get_lineno(), get_function(),
503 check_name(one->owner), one->name,
504 show_state(one->state), one->line,
505 show_state(two->state), two->line,
506 show_state(s));
508 FOR_EACH_PTR(result->possible, tmp) {
509 if (i++)
510 printf(", ");
511 printf("%s", show_state(tmp->state));
512 } END_FOR_EACH_PTR(tmp);
513 printf(")\n");
516 return result;
519 struct sm_state *get_sm_state_stree(struct stree *stree, int owner, const char *name,
520 struct symbol *sym)
522 struct tracker tracker = {
523 .owner = owner,
524 .name = (char *)name,
525 .sym = sym,
528 if (!name)
529 return NULL;
532 return avl_lookup(stree, (struct sm_state *)&tracker);
535 struct smatch_state *get_state_stree(struct stree *stree,
536 int owner, const char *name,
537 struct symbol *sym)
539 struct sm_state *sm;
541 sm = get_sm_state_stree(stree, owner, name, sym);
542 if (sm)
543 return sm->state;
544 return NULL;
547 /* FIXME: this is almost exactly the same as set_sm_state_slist() */
548 void overwrite_sm_state_stree(struct stree **stree, struct sm_state *new)
550 avl_insert(stree, new);
553 void overwrite_sm_state_stree_stack(struct stree_stack **stack,
554 struct sm_state *sm)
556 struct stree *stree;
558 stree = pop_stree(stack);
559 overwrite_sm_state_stree(&stree, sm);
560 push_stree(stack, stree);
563 struct sm_state *set_state_stree(struct stree **stree, int owner, const char *name,
564 struct symbol *sym, struct smatch_state *state)
566 struct sm_state *new = alloc_sm_state(owner, name, sym, state);
568 avl_insert(stree, new);
569 return new;
572 void set_state_stree_perm(struct stree **stree, int owner, const char *name,
573 struct symbol *sym, struct smatch_state *state)
575 struct sm_state *sm;
577 sm = malloc(sizeof(*sm) + strlen(name) + 1);
578 memset(sm, 0, sizeof(*sm));
579 sm->owner = owner;
580 sm->name = (char *)(sm + 1);
581 strcpy((char *)sm->name, name);
582 sm->sym = sym;
583 sm->state = state;
585 overwrite_sm_state_stree(stree, sm);
588 void delete_state_stree(struct stree **stree, int owner, const char *name,
589 struct symbol *sym)
591 struct tracker tracker = {
592 .owner = owner,
593 .name = (char *)name,
594 .sym = sym,
597 avl_remove(stree, (struct sm_state *)&tracker);
600 void delete_state_stree_stack(struct stree_stack **stack, int owner, const char *name,
601 struct symbol *sym)
603 struct stree *stree;
605 stree = pop_stree(stack);
606 delete_state_stree(&stree, owner, name, sym);
607 push_stree(stack, stree);
610 void push_stree(struct stree_stack **stack, struct stree *stree)
612 add_ptr_list(stack, stree);
615 struct stree *pop_stree(struct stree_stack **stack)
617 struct stree *stree;
619 stree = last_ptr_list((struct ptr_list *)*stack);
620 delete_ptr_list_last((struct ptr_list **)stack);
621 return stree;
624 struct stree *top_stree(struct stree_stack *stack)
626 return last_ptr_list((struct ptr_list *)stack);
629 void free_slist(struct state_list **slist)
631 __free_ptr_list((struct ptr_list **)slist);
634 void free_stree_stack(struct stree_stack **stack)
636 __free_ptr_list((struct ptr_list **)stack);
639 void free_stack_and_strees(struct stree_stack **stree_stack)
641 struct stree *stree;
643 FOR_EACH_PTR(*stree_stack, stree) {
644 free_stree(&stree);
645 } END_FOR_EACH_PTR(stree);
646 free_stree_stack(stree_stack);
649 struct sm_state *set_state_stree_stack(struct stree_stack **stack, int owner, const char *name,
650 struct symbol *sym, struct smatch_state *state)
652 struct stree *stree;
653 struct sm_state *sm;
655 stree = pop_stree(stack);
656 sm = set_state_stree(&stree, owner, name, sym, state);
657 push_stree(stack, stree);
659 return sm;
663 * get_sm_state_stack() gets the state for the top slist on the stack.
665 struct sm_state *get_sm_state_stree_stack(struct stree_stack *stack,
666 int owner, const char *name,
667 struct symbol *sym)
669 struct stree *stree;
670 struct sm_state *ret;
672 stree = pop_stree(&stack);
673 ret = get_sm_state_stree(stree, owner, name, sym);
674 push_stree(&stack, stree);
675 return ret;
678 struct smatch_state *get_state_stree_stack(struct stree_stack *stack,
679 int owner, const char *name,
680 struct symbol *sym)
682 struct sm_state *sm;
684 sm = get_sm_state_stree_stack(stack, owner, name, sym);
685 if (sm)
686 return sm->state;
687 return NULL;
690 static void match_states_stree(struct stree **one, struct stree **two)
692 struct smatch_state *tmp_state;
693 struct sm_state *sm;
694 struct state_list *add_to_one = NULL;
695 struct state_list *add_to_two = NULL;
696 AvlIter one_iter;
697 AvlIter two_iter;
699 __set_cur_stree_readonly();
701 avl_iter_begin(&one_iter, *one, FORWARD);
702 avl_iter_begin(&two_iter, *two, FORWARD);
704 for (;;) {
705 if (!one_iter.sm && !two_iter.sm)
706 break;
707 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
708 __set_fake_cur_stree_fast(*two);
709 __in_unmatched_hook++;
710 tmp_state = __client_unmatched_state_function(one_iter.sm);
711 __in_unmatched_hook--;
712 __pop_fake_cur_stree_fast();
713 sm = alloc_state_no_name(one_iter.sm->owner, one_iter.sm->name,
714 one_iter.sm->sym, tmp_state);
715 add_ptr_list(&add_to_two, sm);
716 avl_iter_next(&one_iter);
717 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
718 avl_iter_next(&one_iter);
719 avl_iter_next(&two_iter);
720 } else {
721 __set_fake_cur_stree_fast(*one);
722 __in_unmatched_hook++;
723 tmp_state = __client_unmatched_state_function(two_iter.sm);
724 __in_unmatched_hook--;
725 __pop_fake_cur_stree_fast();
726 sm = alloc_state_no_name(two_iter.sm->owner, two_iter.sm->name,
727 two_iter.sm->sym, tmp_state);
728 add_ptr_list(&add_to_one, sm);
729 avl_iter_next(&two_iter);
733 __set_cur_stree_writable();
735 FOR_EACH_PTR(add_to_one, sm) {
736 avl_insert(one, sm);
737 } END_FOR_EACH_PTR(sm);
739 FOR_EACH_PTR(add_to_two, sm) {
740 avl_insert(two, sm);
741 } END_FOR_EACH_PTR(sm);
743 free_slist(&add_to_one);
744 free_slist(&add_to_two);
747 static void call_pre_merge_hooks(struct stree **one, struct stree **two)
749 struct sm_state *sm, *cur;
750 struct stree *new;
752 __in_unmatched_hook++;
754 __set_fake_cur_stree_fast(*one);
755 __push_fake_cur_stree();
756 FOR_EACH_SM(*two, sm) {
757 cur = get_sm_state(sm->owner, sm->name, sm->sym);
758 if (cur == sm)
759 continue;
760 call_pre_merge_hook(cur, sm);
761 } END_FOR_EACH_SM(sm);
762 new = __pop_fake_cur_stree();
763 overwrite_stree(new, one);
764 free_stree(&new);
765 __pop_fake_cur_stree_fast();
767 __set_fake_cur_stree_fast(*two);
768 __push_fake_cur_stree();
769 FOR_EACH_SM(*one, sm) {
770 cur = get_sm_state(sm->owner, sm->name, sm->sym);
771 if (cur == sm)
772 continue;
773 call_pre_merge_hook(cur, sm);
774 } END_FOR_EACH_SM(sm);
775 new = __pop_fake_cur_stree();
776 overwrite_stree(new, two);
777 free_stree(&new);
778 __pop_fake_cur_stree_fast();
780 __in_unmatched_hook--;
783 static void clone_pool_havers_stree(struct stree **stree)
785 struct sm_state *sm, *tmp;
786 struct state_list *slist = NULL;
788 FOR_EACH_SM(*stree, sm) {
789 if (sm->pool) {
790 tmp = clone_sm(sm);
791 add_ptr_list(&slist, tmp);
793 } END_FOR_EACH_SM(sm);
795 FOR_EACH_PTR(slist, sm) {
796 avl_insert(stree, sm);
797 } END_FOR_EACH_PTR(sm);
799 free_slist(&slist);
802 int __stree_id;
805 * merge_slist() is called whenever paths merge, such as after
806 * an if statement. It takes the two slists and creates one.
808 static void __merge_stree(struct stree **to, struct stree *stree, int add_pool)
810 struct stree *results = NULL;
811 struct stree *implied_one = NULL;
812 struct stree *implied_two = NULL;
813 AvlIter one_iter;
814 AvlIter two_iter;
815 struct sm_state *one, *two, *res;
817 if (out_of_memory())
818 return;
820 /* merging a null and nonnull path gives you only the nonnull path */
821 if (!stree)
822 return;
823 if (*to == stree)
824 return;
826 if (!*to) {
827 *to = clone_stree(stree);
828 return;
831 implied_one = clone_stree(*to);
832 implied_two = clone_stree(stree);
834 match_states_stree(&implied_one, &implied_two);
835 call_pre_merge_hooks(&implied_one, &implied_two);
837 if (add_pool) {
838 clone_pool_havers_stree(&implied_one);
839 clone_pool_havers_stree(&implied_two);
841 set_stree_id(&implied_one, ++__stree_id);
842 set_stree_id(&implied_two, ++__stree_id);
843 if (implied_one->base_stree)
844 set_stree_id(&implied_one->base_stree, ++__stree_id);
845 if (implied_two->base_stree)
846 set_stree_id(&implied_two->base_stree, ++__stree_id);
849 push_stree(&all_pools, implied_one);
850 push_stree(&all_pools, implied_two);
852 avl_iter_begin(&one_iter, implied_one, FORWARD);
853 avl_iter_begin(&two_iter, implied_two, FORWARD);
855 for (;;) {
856 if (!one_iter.sm || !two_iter.sm)
857 break;
859 one = one_iter.sm;
860 two = two_iter.sm;
862 if (one == two) {
863 avl_insert(&results, one);
864 goto next;
867 if (add_pool) {
868 one->pool = implied_one;
869 if (implied_one->base_stree)
870 one->pool = implied_one->base_stree;
871 two->pool = implied_two;
872 if (implied_two->base_stree)
873 two->pool = implied_two->base_stree;
875 res = merge_sm_states(one, two);
876 add_possible_sm(res, one);
877 add_possible_sm(res, two);
878 avl_insert(&results, res);
879 next:
880 avl_iter_next(&one_iter);
881 avl_iter_next(&two_iter);
884 free_stree(to);
885 *to = results;
888 void merge_stree(struct stree **to, struct stree *stree)
890 __merge_stree(to, stree, 1);
893 void merge_stree_no_pools(struct stree **to, struct stree *stree)
895 __merge_stree(to, stree, 0);
899 * This is unfortunately a bit subtle... The problem is that if a
900 * state is set on one fake stree but not the other then we should
901 * look up the the original state and use that as the unset state.
902 * Fortunately, after you pop your fake stree then the cur_slist should
903 * reflect the original state.
905 void merge_fake_stree(struct stree **to, struct stree *stree)
907 struct stree *one = *to;
908 struct stree *two = stree;
909 struct sm_state *sm;
910 struct state_list *add_to_one = NULL;
911 struct state_list *add_to_two = NULL;
912 AvlIter one_iter;
913 AvlIter two_iter;
915 if (!stree)
916 return;
917 if (*to == stree)
918 return;
919 if (!*to) {
920 *to = clone_stree(stree);
921 return;
924 avl_iter_begin(&one_iter, one, FORWARD);
925 avl_iter_begin(&two_iter, two, FORWARD);
927 for (;;) {
928 if (!one_iter.sm && !two_iter.sm)
929 break;
930 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
931 sm = get_sm_state(one_iter.sm->owner, one_iter.sm->name,
932 one_iter.sm->sym);
933 if (sm)
934 add_ptr_list(&add_to_two, sm);
935 avl_iter_next(&one_iter);
936 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
937 avl_iter_next(&one_iter);
938 avl_iter_next(&two_iter);
939 } else {
940 sm = get_sm_state(two_iter.sm->owner, two_iter.sm->name,
941 two_iter.sm->sym);
942 if (sm)
943 add_ptr_list(&add_to_one, sm);
944 avl_iter_next(&two_iter);
948 FOR_EACH_PTR(add_to_one, sm) {
949 avl_insert(&one, sm);
950 } END_FOR_EACH_PTR(sm);
952 FOR_EACH_PTR(add_to_two, sm) {
953 avl_insert(&two, sm);
954 } END_FOR_EACH_PTR(sm);
956 one->base_stree = clone_stree(__get_cur_stree());
957 FOR_EACH_SM(one, sm) {
958 avl_insert(&one->base_stree, sm);
959 } END_FOR_EACH_SM(sm);
961 two->base_stree = clone_stree(__get_cur_stree());
962 FOR_EACH_SM(two, sm) {
963 avl_insert(&two->base_stree, sm);
964 } END_FOR_EACH_SM(sm);
966 free_slist(&add_to_one);
967 free_slist(&add_to_two);
969 __merge_stree(&one, two, 1);
971 *to = one;
975 * filter_slist() removes any sm states "slist" holds in common with "filter"
977 void filter_stree(struct stree **stree, struct stree *filter)
979 struct stree *results = NULL;
980 AvlIter one_iter;
981 AvlIter two_iter;
983 avl_iter_begin(&one_iter, *stree, FORWARD);
984 avl_iter_begin(&two_iter, filter, FORWARD);
986 /* FIXME: This should probably be re-written with trees in mind */
988 for (;;) {
989 if (!one_iter.sm && !two_iter.sm)
990 break;
991 if (cmp_tracker(one_iter.sm, two_iter.sm) < 0) {
992 avl_insert(&results, one_iter.sm);
993 avl_iter_next(&one_iter);
994 } else if (cmp_tracker(one_iter.sm, two_iter.sm) == 0) {
995 if (one_iter.sm != two_iter.sm)
996 avl_insert(&results, one_iter.sm);
997 avl_iter_next(&one_iter);
998 avl_iter_next(&two_iter);
999 } else {
1000 avl_iter_next(&two_iter);
1004 free_stree(stree);
1005 *stree = results;
1010 * and_slist_stack() pops the top two slists, overwriting the one with
1011 * the other and pushing it back on the stack.
1013 void and_stree_stack(struct stree_stack **stack)
1015 struct sm_state *tmp;
1016 struct stree *right_stree = pop_stree(stack);
1018 FOR_EACH_SM(right_stree, tmp) {
1019 overwrite_sm_state_stree_stack(stack, tmp);
1020 } END_FOR_EACH_SM(tmp);
1021 free_stree(&right_stree);
1025 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
1026 * It pops the two slists from the top of the stack and merges them
1027 * together in a way that preserves the things they have in common
1028 * but creates a merged state for most of the rest.
1029 * You could have code that had: if (foo || foo) { foo->baz;
1030 * It's this function which ensures smatch does the right thing.
1032 void or_stree_stack(struct stree_stack **pre_conds,
1033 struct stree *cur_stree,
1034 struct stree_stack **stack)
1036 struct stree *new;
1037 struct stree *old;
1038 struct stree *pre_stree;
1039 struct stree *res;
1040 struct stree *tmp_stree;
1042 new = pop_stree(stack);
1043 old = pop_stree(stack);
1045 pre_stree = pop_stree(pre_conds);
1046 push_stree(pre_conds, clone_stree(pre_stree));
1048 res = clone_stree(pre_stree);
1049 overwrite_stree(old, &res);
1051 tmp_stree = clone_stree(cur_stree);
1052 overwrite_stree(new, &tmp_stree);
1054 merge_stree(&res, tmp_stree);
1055 filter_stree(&res, pre_stree);
1057 push_stree(stack, res);
1058 free_stree(&tmp_stree);
1059 free_stree(&pre_stree);
1060 free_stree(&new);
1061 free_stree(&old);
1065 * get_named_stree() is only used for gotos.
1067 struct stree **get_named_stree(struct named_stree_stack *stack,
1068 const char *name,
1069 struct symbol *sym)
1071 struct named_stree *tmp;
1073 FOR_EACH_PTR(stack, tmp) {
1074 if (tmp->sym == sym &&
1075 strcmp(tmp->name, name) == 0)
1076 return &tmp->stree;
1077 } END_FOR_EACH_PTR(tmp);
1078 return NULL;
1081 /* FIXME: These parameters are in a different order from expected */
1082 void overwrite_stree(struct stree *from, struct stree **to)
1084 struct sm_state *tmp;
1086 FOR_EACH_SM(from, tmp) {
1087 overwrite_sm_state_stree(to, tmp);
1088 } END_FOR_EACH_SM(tmp);