2 * Copyright (C) 2006 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
19 * You have a lists of states. kernel = locked, foo = NULL, ...
20 * When you hit an if {} else {} statement then you swap the list
21 * of states for a different list of states. The lists are stored
24 * At the beginning of this file there are list of the stacks that
25 * we use. Each function in this file does something to one of
28 * So the smatch_flow.c understands code but it doesn't understand states.
29 * smatch_flow calls functions in this file. This file calls functions
30 * in smatch_slist.c which just has boring generic plumbing for handling
31 * state lists. But really it's this file where all the magic happens.
37 #include "smatch_slist.h"
38 #include "smatch_extra.h"
40 struct smatch_state undefined
= { .name
= "undefined" };
41 struct smatch_state merged
= { .name
= "merged" };
42 struct smatch_state true_state
= { .name
= "true" };
43 struct smatch_state false_state
= { .name
= "false" };
45 static struct stree
*cur_stree
; /* current states */
46 static struct stree
*fast_overlay
;
48 static struct stree_stack
*true_stack
; /* states after a t/f branch */
49 static struct stree_stack
*false_stack
;
50 static struct stree_stack
*pre_cond_stack
; /* states before a t/f branch */
52 static struct stree_stack
*cond_true_stack
; /* states affected by a branch */
53 static struct stree_stack
*cond_false_stack
;
55 static struct stree_stack
*fake_cur_stree_stack
;
58 static struct stree_stack
*break_stack
;
59 static struct stree_stack
*fake_break_stack
;
60 static struct stree_stack
*switch_stack
;
61 static struct range_list_stack
*remaining_cases
;
62 static struct stree_stack
*default_stack
;
63 static struct stree_stack
*continue_stack
;
65 static struct named_stree_stack
*goto_stack
;
67 static struct ptr_list
*backup
;
68 static bool *keep_out_of_scope
;
72 void __print_cur_stree(void)
74 __print_stree(cur_stree
);
77 bool __print_states(const char *owner
)
85 FOR_EACH_SM(__get_cur_stree(), sm
) {
86 if (!strstr(check_name(sm
->owner
), owner
))
88 sm_msg("%s", show_sm(sm
));
90 } END_FOR_EACH_SM(sm
);
93 sm_msg("no states found for '%s'", owner
);
105 void __set_cur_stree_readonly(void)
110 void __set_cur_stree_writable(void)
115 DECLARE_PTR_LIST(check_tracker_list
, check_tracker_hook
*);
116 static struct check_tracker_list
**tracker_hooks
;
118 void add_check_tracker(const char *check_name
, check_tracker_hook
*fn
)
120 check_tracker_hook
**p
;
123 owner
= id_from_name(check_name
);
125 printf("check not found. '%s'\n", check_name
);
129 p
= malloc(sizeof(check_tracker_hook
*));
131 add_ptr_list(&tracker_hooks
[owner
], p
);
134 static void call_tracker_hooks(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
136 struct check_tracker_list
*hooks
;
137 check_tracker_hook
**fn
;
139 if ((unsigned short)owner
== USHRT_MAX
)
142 hooks
= tracker_hooks
[owner
];
143 FOR_EACH_PTR(hooks
, fn
) {
144 (*fn
)(owner
, name
, sym
, state
);
145 } END_FOR_EACH_PTR(fn
);
148 void allocate_tracker_array(int num_checks
)
150 tracker_hooks
= malloc(num_checks
* sizeof(void *));
151 memset(tracker_hooks
, 0, num_checks
* sizeof(void *));
153 keep_out_of_scope
= malloc(num_checks
* sizeof(*keep_out_of_scope
));
154 memset(keep_out_of_scope
, 0, num_checks
* sizeof(*keep_out_of_scope
));
157 bool debug_on(const char *check_name
, const char *var
)
161 if (option_debug_check
&& strstr(check_name
, option_debug_check
))
163 if (option_debug_var
&& strcmp(var
, option_debug_var
) == 0)
168 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
170 struct sm_state
*ret
;
176 sm_perror("cur_stree is read only.");
178 if (debug_on(check_name(owner
), name
)) {
179 struct smatch_state
*s
;
181 s
= __get_state(owner
, name
, sym
);
183 sm_msg("%s new [%s] '%s' %s", __func__
,
184 check_name(owner
), name
, show_state(state
));
186 sm_msg("%s change [%s] '%s' %s => %s",
187 __func__
, check_name(owner
), name
, show_state(s
),
191 call_tracker_hooks(owner
, name
, sym
, state
);
193 if (owner
!= -1 && unreachable())
196 if (fake_cur_stree_stack
)
197 set_state_stree_stack(&fake_cur_stree_stack
, owner
, name
, sym
, state
);
199 ret
= set_state_stree(&cur_stree
, owner
, name
, sym
, state
);
204 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
, struct smatch_state
*state
)
208 struct sm_state
*ret
= NULL
;
210 expr
= strip_expr(expr
);
211 name
= expr_to_var_sym(expr
, &sym
);
214 ret
= set_state(owner
, name
, sym
, state
);
220 struct stree
*__swap_cur_stree(struct stree
*stree
)
222 struct stree
*orig
= cur_stree
;
228 void __push_fake_cur_stree(void)
230 push_stree(&fake_cur_stree_stack
, NULL
);
231 __save_pre_cond_states();
234 struct stree
*__pop_fake_cur_stree(void)
236 if (!fake_cur_stree_stack
)
237 sm_perror("popping too many fake cur strees.");
238 __use_pre_cond_states();
239 return pop_stree(&fake_cur_stree_stack
);
242 void __free_fake_cur_stree(void)
246 stree
= __pop_fake_cur_stree();
250 void __set_fake_cur_stree_fast(struct stree
*stree
)
253 sm_perror("cannot nest fast overlay");
256 fast_overlay
= stree
;
257 set_fast_math_only();
260 void __pop_fake_cur_stree_fast(void)
263 clear_fast_math_only();
266 void __merge_stree_into_cur(struct stree
*stree
)
269 struct sm_state
*orig
;
270 struct sm_state
*merged
;
272 FOR_EACH_SM(stree
, sm
) {
273 orig
= get_sm_state(sm
->owner
, sm
->name
, sm
->sym
);
275 merged
= merge_sm_states(orig
, sm
);
279 } END_FOR_EACH_SM(sm
);
282 void __set_sm(struct sm_state
*sm
)
285 sm_perror("cur_stree is read only.");
287 if (debug_on(check_name(sm
->owner
), sm
->name
)) {
288 struct smatch_state
*s
;
290 s
= __get_state(sm
->owner
, sm
->name
, sm
->sym
);
292 sm_msg("%s new %s", __func__
, show_sm(sm
));
294 sm_msg("%s change %s (was %s)", __func__
, show_sm(sm
),
301 if (fake_cur_stree_stack
)
302 overwrite_sm_state_stree_stack(&fake_cur_stree_stack
, sm
);
304 overwrite_sm_state_stree(&cur_stree
, sm
);
307 void __set_sm_cur_stree(struct sm_state
*sm
)
310 sm_perror("cur_stree is read only.");
312 if (debug_on(check_name(sm
->owner
), sm
->name
)) {
313 struct smatch_state
*s
;
315 s
= __get_state(sm
->owner
, sm
->name
, sm
->sym
);
317 sm_msg("%s new %s", __func__
, show_sm(sm
));
319 sm_msg("%s change %s (was %s)",
320 __func__
, show_sm(sm
), show_state(s
));
326 overwrite_sm_state_stree(&cur_stree
, sm
);
329 typedef void (get_state_hook
)(int owner
, const char *name
, struct symbol
*sym
);
330 DECLARE_PTR_LIST(fn_list
, get_state_hook
*);
331 static struct fn_list
*get_state_hooks
;
333 void add_get_state_hook(get_state_hook
*fn
)
335 get_state_hook
**p
= malloc(sizeof(get_state_hook
*));
337 add_ptr_list(&get_state_hooks
, p
);
340 static void call_get_state_hooks(int owner
, const char *name
, struct symbol
*sym
)
342 static int recursion
;
349 FOR_EACH_PTR(get_state_hooks
, fn
) {
350 (*fn
)(owner
, name
, sym
);
351 } END_FOR_EACH_PTR(fn
);
356 bool has_states(struct stree
*stree
, int owner
)
358 if (owner
< 0 || owner
> USHRT_MAX
)
362 if (owner
== USHRT_MAX
)
364 return stree
->has_states
[owner
];
367 struct smatch_state
*__get_state(int owner
, const char *name
, struct symbol
*sym
)
371 sm
= get_sm_state(owner
, name
, sym
);
377 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
)
379 call_get_state_hooks(owner
, name
, sym
);
381 return __get_state(owner
, name
, sym
);
384 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
)
386 struct expression
*fake_parent
;
389 struct smatch_state
*ret
= NULL
;
391 expr
= strip_expr(expr
);
392 name
= expr_to_var_sym(expr
, &sym
);
394 fake_parent
= expr_get_fake_parent_expr(expr
);
397 name
= expr_to_var_sym(fake_parent
->left
, &sym
);
401 ret
= get_state(owner
, name
, sym
);
407 bool has_possible_state(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
411 sm
= get_sm_state(owner
, name
, sym
);
415 return slist_has_state(sm
->possible
, state
);
418 bool expr_has_possible_state(int owner
, struct expression
*expr
, struct smatch_state
*state
)
422 sm
= get_sm_state_expr(owner
, expr
);
426 return slist_has_state(sm
->possible
, state
);
429 struct state_list
*get_possible_states(int owner
, const char *name
, struct symbol
*sym
)
431 struct sm_state
*sms
;
433 sms
= get_sm_state_stree(cur_stree
, owner
, name
, sym
);
435 return sms
->possible
;
439 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
)
443 struct state_list
*ret
= NULL
;
445 expr
= strip_expr(expr
);
446 name
= expr_to_var_sym(expr
, &sym
);
449 ret
= get_possible_states(owner
, name
, sym
);
455 struct sm_state
*get_sm_state(int owner
, const char *name
, struct symbol
*sym
)
457 struct sm_state
*ret
;
459 ret
= get_sm_state_stree(fast_overlay
, owner
, name
, sym
);
463 return get_sm_state_stree(cur_stree
, owner
, name
, sym
);
466 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
)
470 struct sm_state
*ret
= NULL
;
472 expr
= strip_expr(expr
);
473 name
= expr_to_var_sym(expr
, &sym
);
476 ret
= get_sm_state(owner
, name
, sym
);
482 void __delete_state(int owner
, const char *name
, struct symbol
*sym
)
484 delete_state_stree(&cur_stree
, owner
, name
, sym
);
485 if (cond_true_stack
) {
486 delete_state_stree_stack(&pre_cond_stack
, owner
, name
, sym
);
487 delete_state_stree_stack(&cond_true_stack
, owner
, name
, sym
);
488 delete_state_stree_stack(&cond_false_stack
, owner
, name
, sym
);
492 void preserve_out_of_scope(int owner
)
494 keep_out_of_scope
[owner
] = true;
497 static void delete_all_states_stree_sym(struct stree
**stree
, struct symbol
*sym
)
499 struct state_list
*slist
= NULL
;
502 FOR_EACH_SM(*stree
, sm
) {
503 if (sm
->sym
== sym
&& !keep_out_of_scope
[sm
->owner
])
504 add_ptr_list(&slist
, sm
);
505 } END_FOR_EACH_SM(sm
);
507 FOR_EACH_PTR(slist
, sm
) {
508 delete_state_stree(stree
, sm
->owner
, sm
->name
, sm
->sym
);
509 } END_FOR_EACH_PTR(sm
);
514 static void delete_all_states_stree_stack_sym(struct stree_stack
**stack
, struct symbol
*sym
)
521 stree
= pop_stree(stack
);
522 delete_all_states_stree_sym(&stree
, sym
);
523 push_stree(stack
, stree
);
526 void __delete_all_states_sym(struct symbol
*sym
)
528 delete_all_states_stree_sym(&cur_stree
, sym
);
530 delete_all_states_stree_stack_sym(&true_stack
, sym
);
531 delete_all_states_stree_stack_sym(&true_stack
, sym
);
532 delete_all_states_stree_stack_sym(&false_stack
, sym
);
533 delete_all_states_stree_stack_sym(&pre_cond_stack
, sym
);
534 delete_all_states_stree_stack_sym(&cond_true_stack
, sym
);
535 delete_all_states_stree_stack_sym(&cond_false_stack
, sym
);
536 delete_all_states_stree_stack_sym(&fake_cur_stree_stack
, sym
);
537 delete_all_states_stree_stack_sym(&break_stack
, sym
);
538 delete_all_states_stree_stack_sym(&fake_break_stack
, sym
);
539 delete_all_states_stree_stack_sym(&switch_stack
, sym
);
540 delete_all_states_stree_stack_sym(&continue_stack
, sym
);
543 * deleting from the goto stack is problematic because we don't know
544 * if the label is in scope and also we need the value for --two-passes.
548 struct stree
*get_all_states_from_stree(int owner
, struct stree
*source
)
550 struct stree
*ret
= NULL
;
551 struct sm_state
*tmp
;
553 FOR_EACH_SM(source
, tmp
) {
554 if (tmp
->owner
== owner
)
555 avl_insert(&ret
, tmp
);
556 } END_FOR_EACH_SM(tmp
);
561 struct stree
*get_all_states_stree(int owner
)
563 return get_all_states_from_stree(owner
, cur_stree
);
566 struct stree
*__get_cur_stree(void)
571 int is_reachable(void)
578 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
579 struct smatch_state
*true_state
,
580 struct smatch_state
*false_state
)
583 sm_perror("cur_stree is read only.");
587 if (!true_state
&& !false_state
)
590 if (debug_on(check_name(owner
), name
)) {
591 struct smatch_state
*tmp
;
593 tmp
= __get_state(owner
, name
, sym
);
594 sm_msg("%s [%s] '%s'. Was %s. Now T:%s F:%s", __func__
,
595 check_name(owner
), name
, show_state(tmp
),
596 show_state(true_state
), show_state(false_state
));
602 if (!cond_false_stack
|| !cond_true_stack
) {
603 sm_perror("missing true/false stacks");
608 set_state_stree_stack(&cond_true_stack
, owner
, name
, sym
, true_state
);
610 set_state_stree_stack(&cond_false_stack
, owner
, name
, sym
, false_state
);
613 void set_true_false_states_expr(int owner
, struct expression
*expr
,
614 struct smatch_state
*true_state
,
615 struct smatch_state
*false_state
)
620 expr
= strip_expr(expr
);
621 name
= expr_to_var_sym(expr
, &sym
);
624 set_true_false_states(owner
, name
, sym
, true_state
, false_state
);
629 void __set_true_false_sm(struct sm_state
*true_sm
, struct sm_state
*false_sm
)
635 if (!true_sm
&& !false_sm
)
641 owner
= true_sm
? true_sm
->owner
: false_sm
->owner
;
642 name
= true_sm
? true_sm
->name
: false_sm
->name
;
643 sym
= true_sm
? true_sm
->sym
: false_sm
->sym
;
644 if (debug_on(check_name(owner
), name
)) {
645 struct smatch_state
*tmp
;
647 tmp
= __get_state(owner
, name
, sym
);
648 sm_msg("%s [%s] '%s'. Was %s. Now T:%s F:%s", __func__
,
649 check_name(owner
), name
, show_state(tmp
),
650 show_state(true_sm
? true_sm
->state
: NULL
),
651 show_state(false_sm
? false_sm
->state
: NULL
));
654 if (!cond_false_stack
|| !cond_true_stack
) {
655 sm_perror("missing true/false stacks");
660 overwrite_sm_state_stree_stack(&cond_true_stack
, true_sm
);
662 overwrite_sm_state_stree_stack(&cond_false_stack
, false_sm
);
665 void nullify_path(void)
667 if (fake_cur_stree_stack
) {
668 __free_fake_cur_stree();
669 __push_fake_cur_stree();
671 free_stree(&cur_stree
);
674 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
681 * At the start of every function we mark the path
682 * as unnull. That way there is always at least one state
683 * in the cur_stree until nullify_path is called. This
684 * is used in merge_slist() for the first null check.
686 void __unnullify_path(void)
689 set_state(-1, "unnull_path", NULL
, &true_state
);
692 int __path_is_null(void)
699 #define check_stree_stack_free(stack) do { \
701 sm_perror("stack not empty %s", #stack); \
702 free_stack_and_strees(stack); \
706 void save_all_states(void)
708 __add_ptr_list(&backup
, cur_stree
);
711 __add_ptr_list(&backup
, true_stack
);
713 __add_ptr_list(&backup
, false_stack
);
715 __add_ptr_list(&backup
, pre_cond_stack
);
716 pre_cond_stack
= NULL
;
718 __add_ptr_list(&backup
, cond_true_stack
);
719 cond_true_stack
= NULL
;
720 __add_ptr_list(&backup
, cond_false_stack
);
721 cond_false_stack
= NULL
;
723 __add_ptr_list(&backup
, fake_cur_stree_stack
);
724 fake_cur_stree_stack
= NULL
;
726 __add_ptr_list(&backup
, break_stack
);
728 __add_ptr_list(&backup
, fake_break_stack
);
729 fake_break_stack
= NULL
;
731 __add_ptr_list(&backup
, switch_stack
);
733 __add_ptr_list(&backup
, remaining_cases
);
734 remaining_cases
= NULL
;
735 __add_ptr_list(&backup
, default_stack
);
736 default_stack
= NULL
;
737 __add_ptr_list(&backup
, continue_stack
);
738 continue_stack
= NULL
;
740 __add_ptr_list(&backup
, goto_stack
);
744 static void *pop_backup(void)
748 ret
= last_ptr_list(backup
);
749 delete_ptr_list_last(&backup
);
753 void restore_all_states(void)
755 goto_stack
= pop_backup();
757 continue_stack
= pop_backup();
758 default_stack
= pop_backup();
759 remaining_cases
= pop_backup();
760 switch_stack
= pop_backup();
761 fake_break_stack
= pop_backup();
762 break_stack
= pop_backup();
764 fake_cur_stree_stack
= pop_backup();
766 cond_false_stack
= pop_backup();
767 cond_true_stack
= pop_backup();
769 pre_cond_stack
= pop_backup();
770 false_stack
= pop_backup();
771 true_stack
= pop_backup();
773 cur_stree
= pop_backup();
776 void free_goto_stack(void)
778 struct named_stree
*named_stree
;
780 FOR_EACH_PTR(goto_stack
, named_stree
) {
781 free_stree(&named_stree
->stree
);
782 } END_FOR_EACH_PTR(named_stree
);
783 __free_ptr_list((struct ptr_list
**)&goto_stack
);
786 void clear_all_states(void)
789 check_stree_stack_free(&true_stack
);
790 check_stree_stack_free(&false_stack
);
791 check_stree_stack_free(&pre_cond_stack
);
792 check_stree_stack_free(&cond_true_stack
);
793 check_stree_stack_free(&cond_false_stack
);
794 check_stree_stack_free(&break_stack
);
795 check_stree_stack_free(&fake_break_stack
);
796 check_stree_stack_free(&switch_stack
);
797 check_stree_stack_free(&continue_stack
);
798 check_stree_stack_free(&fake_cur_stree_stack
);
802 clear_array_values_cache();
803 free_every_single_sm_state();
804 free_tmp_expressions();
807 void __push_cond_stacks(void)
809 push_stree(&cond_true_stack
, NULL
);
810 push_stree(&cond_false_stack
, NULL
);
811 __push_fake_cur_stree();
814 void __fold_in_set_states(void)
816 struct stree
*new_states
;
819 new_states
= __pop_fake_cur_stree();
820 FOR_EACH_SM(new_states
, sm
) {
822 __set_true_false_sm(sm
, sm
);
823 } END_FOR_EACH_SM(sm
);
824 free_stree(&new_states
);
827 void __free_set_states(void)
829 struct stree
*new_states
;
831 new_states
= __pop_fake_cur_stree();
832 free_stree(&new_states
);
835 struct stree
*__copy_cond_true_states(void)
839 ret
= pop_stree(&cond_true_stack
);
840 push_stree(&cond_true_stack
, clone_stree(ret
));
844 struct stree
*__copy_cond_false_states(void)
848 ret
= pop_stree(&cond_false_stack
);
849 push_stree(&cond_false_stack
, clone_stree(ret
));
853 struct stree
*__pop_cond_true_stack(void)
855 return pop_stree(&cond_true_stack
);
858 struct stree
*__pop_cond_false_stack(void)
860 return pop_stree(&cond_false_stack
);
864 * This combines the pre cond states with either the true or false states.
868 * In the pre state a is possibly null. In the true state it is non null.
869 * In the false state it is null. Combine the pre and the false to get
870 * that when we call 'foo', 'a' is null.
872 static void __use_cond_stack(struct stree_stack
**stack
)
876 free_stree(&cur_stree
);
878 cur_stree
= pop_stree(&pre_cond_stack
);
879 push_stree(&pre_cond_stack
, clone_stree(cur_stree
));
881 stree
= pop_stree(stack
);
882 overwrite_stree(stree
, &cur_stree
);
883 push_stree(stack
, stree
);
886 void __use_pre_cond_states(void)
888 free_stree(&cur_stree
);
889 cur_stree
= pop_stree(&pre_cond_stack
);
892 void __use_cond_true_states(void)
894 __use_cond_stack(&cond_true_stack
);
897 void __use_cond_false_states(void)
899 __use_cond_stack(&cond_false_stack
);
902 void __negate_cond_stacks(void)
904 struct stree
*old_false
, *old_true
;
906 old_false
= pop_stree(&cond_false_stack
);
907 old_true
= pop_stree(&cond_true_stack
);
908 push_stree(&cond_false_stack
, old_true
);
909 push_stree(&cond_true_stack
, old_false
);
912 void __and_cond_states(void)
914 and_stree_stack(&cond_true_stack
);
915 or_stree_stack(&pre_cond_stack
, cur_stree
, &cond_false_stack
);
918 void __or_cond_states(void)
920 or_stree_stack(&pre_cond_stack
, cur_stree
, &cond_true_stack
);
921 and_stree_stack(&cond_false_stack
);
924 void __save_pre_cond_states(void)
926 push_stree(&pre_cond_stack
, clone_stree(cur_stree
));
929 void __discard_pre_cond_states(void)
933 tmp
= pop_stree(&pre_cond_stack
);
937 struct stree
*__get_true_states(void)
939 return clone_stree(top_stree(cond_true_stack
));
942 struct stree
*__get_false_states(void)
944 return clone_stree(top_stree(cond_false_stack
));
947 void __use_cond_states(void)
949 struct stree
*pre
, *pre_clone
, *true_states
, *false_states
;
951 pre
= pop_stree(&pre_cond_stack
);
952 pre_clone
= clone_stree(pre
);
954 true_states
= pop_stree(&cond_true_stack
);
955 overwrite_stree(true_states
, &pre
);
956 free_stree(&true_states
);
957 /* we use the true states right away */
958 free_stree(&cur_stree
);
961 false_states
= pop_stree(&cond_false_stack
);
962 overwrite_stree(false_states
, &pre_clone
);
963 free_stree(&false_states
);
964 push_stree(&false_stack
, pre_clone
);
967 void __push_true_states(void)
969 push_stree(&true_stack
, clone_stree(cur_stree
));
972 void __use_false_states(void)
974 free_stree(&cur_stree
);
975 cur_stree
= pop_stree(&false_stack
);
978 void __discard_false_states(void)
982 stree
= pop_stree(&false_stack
);
986 void __merge_false_states(void)
990 stree
= pop_stree(&false_stack
);
991 merge_stree(&cur_stree
, stree
);
996 * This function probably seemed common sensical when I wrote it but, oh wow,
997 * does it look subtle in retrospect. Say we set a state on one side of the if
998 * else path but not on the other, then what we should record in the fake stree
999 * is the merged state.
1001 * This function relies on the fact that the we always set the cur_stree as well
1002 * and we already have the infrastructure to merge things correctly into the
1005 * So instead of merging fake strees together which is probably a lot of work,
1006 * we just use it as a list of set states and look up the actual current values
1010 static void update_stree_with_merged(struct stree
**stree
)
1012 struct state_list
*slist
= NULL
;
1013 struct sm_state
*sm
, *new;
1015 FOR_EACH_SM(*stree
, sm
) {
1016 new = get_sm_state(sm
->owner
, sm
->name
, sm
->sym
);
1017 if (!new) /* This can happen if we go out of scope */
1019 add_ptr_list(&slist
, new);
1020 } END_FOR_EACH_SM(sm
);
1022 FOR_EACH_PTR(slist
, sm
) {
1023 overwrite_sm_state_stree(stree
, sm
);
1024 } END_FOR_EACH_PTR(sm
);
1029 static void update_fake_stree_with_merged(void)
1031 struct stree
*stree
;
1033 if (!fake_cur_stree_stack
)
1035 stree
= pop_stree(&fake_cur_stree_stack
);
1036 update_stree_with_merged(&stree
);
1037 push_stree(&fake_cur_stree_stack
, stree
);
1040 void __merge_true_states(void)
1042 struct stree
*stree
;
1044 stree
= pop_stree(&true_stack
);
1045 merge_stree(&cur_stree
, stree
);
1046 update_fake_stree_with_merged();
1050 void __push_continues(void)
1052 push_stree(&continue_stack
, NULL
);
1055 void __discard_continues(void)
1057 struct stree
*stree
;
1059 stree
= pop_stree(&continue_stack
);
1063 void __process_continues(void)
1065 struct stree
*stree
;
1067 stree
= pop_stree(&continue_stack
);
1069 stree
= clone_stree(cur_stree
);
1071 merge_stree(&stree
, cur_stree
);
1073 push_stree(&continue_stack
, stree
);
1076 void __merge_continues(void)
1078 struct stree
*stree
;
1080 stree
= pop_stree(&continue_stack
);
1081 merge_stree(&cur_stree
, stree
);
1085 void __push_breaks(void)
1087 push_stree(&break_stack
, NULL
);
1088 if (fake_cur_stree_stack
)
1089 push_stree(&fake_break_stack
, NULL
);
1092 void __process_breaks(void)
1094 struct stree
*stree
;
1096 stree
= pop_stree(&break_stack
);
1098 stree
= clone_stree(cur_stree
);
1100 merge_stree(&stree
, cur_stree
);
1101 push_stree(&break_stack
, stree
);
1103 if (!fake_cur_stree_stack
)
1106 stree
= pop_stree(&fake_break_stack
);
1108 stree
= clone_stree(top_stree(fake_cur_stree_stack
));
1110 merge_stree(&stree
, top_stree(fake_cur_stree_stack
));
1111 push_stree(&fake_break_stack
, stree
);
1114 int __has_breaks(void)
1116 struct stree
*stree
;
1119 stree
= pop_stree(&break_stack
);
1121 push_stree(&break_stack
, stree
);
1125 void __merge_breaks(void)
1127 struct stree
*stree
;
1128 struct sm_state
*sm
;
1130 stree
= pop_stree(&break_stack
);
1131 merge_stree(&cur_stree
, stree
);
1134 if (!fake_cur_stree_stack
)
1137 stree
= pop_stree(&fake_break_stack
);
1138 update_stree_with_merged(&stree
);
1139 FOR_EACH_SM(stree
, sm
) {
1140 overwrite_sm_state_stree_stack(&fake_cur_stree_stack
, sm
);
1141 } END_FOR_EACH_SM(sm
);
1145 void __use_breaks(void)
1147 struct stree
*stree
;
1148 struct sm_state
*sm
;
1150 free_stree(&cur_stree
);
1151 cur_stree
= pop_stree(&break_stack
);
1153 if (!fake_cur_stree_stack
)
1155 stree
= pop_stree(&fake_break_stack
);
1156 FOR_EACH_SM(stree
, sm
) {
1157 overwrite_sm_state_stree_stack(&fake_cur_stree_stack
, sm
);
1158 } END_FOR_EACH_SM(sm
);
1164 void __save_switch_states(struct expression
*switch_expr
)
1166 struct range_list
*rl
;
1168 get_absolute_rl(switch_expr
, &rl
);
1170 push_rl(&remaining_cases
, rl
);
1171 push_stree(&switch_stack
, clone_stree(cur_stree
));
1174 int have_remaining_cases(void)
1176 return !!top_rl(remaining_cases
);
1179 void __merge_switches(struct expression
*switch_expr
, struct range_list
*case_rl
)
1181 struct stree
*stree
;
1182 struct stree
*implied_stree
;
1184 stree
= pop_stree(&switch_stack
);
1187 * If the cur_stree was NULL before the start of the switch
1188 * statement then we don't want to unnullify it.
1191 push_stree(&switch_stack
, stree
);
1194 implied_stree
= __implied_case_stree(switch_expr
, case_rl
, &remaining_cases
, &stree
);
1195 merge_stree(&cur_stree
, implied_stree
);
1196 free_stree(&implied_stree
);
1197 push_stree(&switch_stack
, stree
);
1200 void __discard_switches(void)
1202 struct stree
*stree
;
1204 pop_rl(&remaining_cases
);
1205 stree
= pop_stree(&switch_stack
);
1209 void __push_default(void)
1211 push_stree(&default_stack
, NULL
);
1214 void __set_default(void)
1216 set_state_stree_stack(&default_stack
, 0, "has_default", NULL
, &true_state
);
1219 bool __has_default_case(void)
1221 struct stree
*stree
;
1223 stree
= last_ptr_list((struct ptr_list
*)default_stack
);
1227 int __pop_default(void)
1229 struct stree
*stree
;
1231 stree
= pop_stree(&default_stack
);
1239 static struct named_stree
*alloc_named_stree(const char *name
, struct symbol
*sym
, struct stree
*stree
)
1241 struct named_stree
*named_stree
= __alloc_named_stree(0);
1243 named_stree
->name
= (char *)name
;
1244 named_stree
->stree
= stree
;
1245 named_stree
->sym
= sym
;
1249 void __save_gotos(const char *name
, struct symbol
*sym
)
1251 struct stree
**stree
;
1252 struct stree
*clone
;
1254 stree
= get_named_stree(goto_stack
, name
, sym
);
1256 merge_stree(stree
, cur_stree
);
1259 struct named_stree
*named_stree
;
1261 clone
= clone_stree(cur_stree
);
1262 named_stree
= alloc_named_stree(name
, sym
, clone
);
1263 add_ptr_list(&goto_stack
, named_stree
);
1267 void __merge_gotos(const char *name
, struct symbol
*sym
)
1269 struct stree
**stree
;
1271 stree
= get_named_stree(goto_stack
, name
, sym
);
1273 merge_stree(&cur_stree
, *stree
);
1276 void __discard_fake_states(struct expression
*call
)
1278 struct stree
*new = NULL
;
1279 struct sm_state
*sm
;
1283 if (__fake_state_cnt
== 0)
1287 * This is just a best effort type of thing. There could be
1288 * fake states in the true/false trees already. They might
1289 * eventually get cleared out too because we call after probably
1290 * 50% of function calls. But the point is that I don't want to
1291 * waste resources tracking them.
1293 FOR_EACH_SM(cur_stree
, sm
) {
1295 len
= snprintf(buf
, sizeof(buf
), "__fake_param_%p_", call
);
1296 if (strncmp(sm
->name
, buf
, len
) != 0)
1297 avl_insert(&new, sm
);
1300 if (strncmp(sm
->name
, "__fake_param_", 13) != 0)
1301 avl_insert(&new, sm
);
1302 } END_FOR_EACH_SM(sm
);
1304 free_stree(&cur_stree
);
1306 __fake_state_cnt
= 0;