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
;
71 void __print_cur_stree(void)
73 __print_stree(cur_stree
);
76 bool __print_states(const char *owner
)
84 FOR_EACH_SM(__get_cur_stree(), sm
) {
85 if (!strstr(check_name(sm
->owner
), owner
))
87 sm_msg("%s", show_sm(sm
));
89 } END_FOR_EACH_SM(sm
);
92 sm_msg("no states found for '%s'", owner
);
104 void __set_cur_stree_readonly(void)
109 void __set_cur_stree_writable(void)
114 DECLARE_PTR_LIST(check_tracker_list
, check_tracker_hook
*);
115 static struct check_tracker_list
**tracker_hooks
;
117 void add_check_tracker(const char *check_name
, check_tracker_hook
*fn
)
119 check_tracker_hook
**p
;
122 owner
= id_from_name(check_name
);
124 printf("check not found. '%s'\n", check_name
);
128 p
= malloc(sizeof(check_tracker_hook
*));
130 add_ptr_list(&tracker_hooks
[owner
], p
);
133 static void call_tracker_hooks(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
135 struct check_tracker_list
*hooks
;
136 check_tracker_hook
**fn
;
138 if ((unsigned short)owner
== USHRT_MAX
)
141 hooks
= tracker_hooks
[owner
];
142 FOR_EACH_PTR(hooks
, fn
) {
143 (*fn
)(owner
, name
, sym
, state
);
144 } END_FOR_EACH_PTR(fn
);
147 void allocate_tracker_array(int num_checks
)
149 tracker_hooks
= malloc(num_checks
* sizeof(void *));
150 memset(tracker_hooks
, 0, num_checks
* sizeof(void *));
153 bool debug_on(const char *check_name
, const char *var
)
157 if (option_debug_check
&& strstr(check_name
, option_debug_check
))
159 if (option_debug_var
&& strcmp(var
, option_debug_var
) == 0)
164 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
166 struct sm_state
*ret
;
172 sm_perror("cur_stree is read only.");
174 if (debug_on(check_name(owner
), name
)) {
175 struct smatch_state
*s
;
177 s
= __get_state(owner
, name
, sym
);
179 sm_msg("%s new [%s] '%s' %s", __func__
,
180 check_name(owner
), name
, show_state(state
));
182 sm_msg("%s change [%s] '%s' %s => %s",
183 __func__
, check_name(owner
), name
, show_state(s
),
187 call_tracker_hooks(owner
, name
, sym
, state
);
189 if (owner
!= -1 && unreachable())
192 if (fake_cur_stree_stack
)
193 set_state_stree_stack(&fake_cur_stree_stack
, owner
, name
, sym
, state
);
195 ret
= set_state_stree(&cur_stree
, owner
, name
, sym
, state
);
200 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
, struct smatch_state
*state
)
204 struct sm_state
*ret
= NULL
;
206 expr
= strip_expr(expr
);
207 name
= expr_to_var_sym(expr
, &sym
);
210 ret
= set_state(owner
, name
, sym
, state
);
216 struct stree
*__swap_cur_stree(struct stree
*stree
)
218 struct stree
*orig
= cur_stree
;
224 void __push_fake_cur_stree(void)
226 push_stree(&fake_cur_stree_stack
, NULL
);
227 __save_pre_cond_states();
230 struct stree
*__pop_fake_cur_stree(void)
232 if (!fake_cur_stree_stack
)
233 sm_perror("popping too many fake cur strees.");
234 __use_pre_cond_states();
235 return pop_stree(&fake_cur_stree_stack
);
238 void __free_fake_cur_stree(void)
242 stree
= __pop_fake_cur_stree();
246 void __set_fake_cur_stree_fast(struct stree
*stree
)
249 sm_perror("cannot nest fast overlay");
252 fast_overlay
= stree
;
253 set_fast_math_only();
256 void __pop_fake_cur_stree_fast(void)
259 clear_fast_math_only();
262 void __merge_stree_into_cur(struct stree
*stree
)
265 struct sm_state
*orig
;
266 struct sm_state
*merged
;
268 FOR_EACH_SM(stree
, sm
) {
269 orig
= get_sm_state(sm
->owner
, sm
->name
, sm
->sym
);
271 merged
= merge_sm_states(orig
, sm
);
275 } END_FOR_EACH_SM(sm
);
278 void __set_sm(struct sm_state
*sm
)
281 sm_perror("cur_stree is read only.");
283 if (debug_on(check_name(sm
->owner
), sm
->name
)) {
284 struct smatch_state
*s
;
286 s
= __get_state(sm
->owner
, sm
->name
, sm
->sym
);
288 sm_msg("%s new %s", __func__
, show_sm(sm
));
290 sm_msg("%s change %s (was %s)", __func__
, show_sm(sm
),
297 if (fake_cur_stree_stack
)
298 overwrite_sm_state_stree_stack(&fake_cur_stree_stack
, sm
);
300 overwrite_sm_state_stree(&cur_stree
, sm
);
303 void __set_sm_cur_stree(struct sm_state
*sm
)
306 sm_perror("cur_stree is read only.");
308 if (debug_on(check_name(sm
->owner
), sm
->name
)) {
309 struct smatch_state
*s
;
311 s
= __get_state(sm
->owner
, sm
->name
, sm
->sym
);
313 sm_msg("%s new %s", __func__
, show_sm(sm
));
315 sm_msg("%s change %s (was %s)",
316 __func__
, show_sm(sm
), show_state(s
));
322 overwrite_sm_state_stree(&cur_stree
, sm
);
325 typedef void (get_state_hook
)(int owner
, const char *name
, struct symbol
*sym
);
326 DECLARE_PTR_LIST(fn_list
, get_state_hook
*);
327 static struct fn_list
*get_state_hooks
;
329 void add_get_state_hook(get_state_hook
*fn
)
331 get_state_hook
**p
= malloc(sizeof(get_state_hook
*));
333 add_ptr_list(&get_state_hooks
, p
);
336 static void call_get_state_hooks(int owner
, const char *name
, struct symbol
*sym
)
338 static int recursion
;
345 FOR_EACH_PTR(get_state_hooks
, fn
) {
346 (*fn
)(owner
, name
, sym
);
347 } END_FOR_EACH_PTR(fn
);
352 bool has_states(struct stree
*stree
, int owner
)
354 if (owner
< 0 || owner
> USHRT_MAX
)
358 if (owner
== USHRT_MAX
)
360 return stree
->has_states
[owner
];
363 struct smatch_state
*__get_state(int owner
, const char *name
, struct symbol
*sym
)
367 sm
= get_sm_state(owner
, name
, sym
);
373 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
)
375 call_get_state_hooks(owner
, name
, sym
);
377 return __get_state(owner
, name
, sym
);
380 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
)
382 struct expression
*fake_parent
;
385 struct smatch_state
*ret
= NULL
;
387 expr
= strip_expr(expr
);
388 name
= expr_to_var_sym(expr
, &sym
);
390 fake_parent
= expr_get_fake_parent_expr(expr
);
393 name
= expr_to_var_sym(fake_parent
->left
, &sym
);
397 ret
= get_state(owner
, name
, sym
);
403 bool has_possible_state(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
407 sm
= get_sm_state(owner
, name
, sym
);
411 return slist_has_state(sm
->possible
, state
);
414 bool expr_has_possible_state(int owner
, struct expression
*expr
, struct smatch_state
*state
)
418 sm
= get_sm_state_expr(owner
, expr
);
422 return slist_has_state(sm
->possible
, state
);
425 struct state_list
*get_possible_states(int owner
, const char *name
, struct symbol
*sym
)
427 struct sm_state
*sms
;
429 sms
= get_sm_state_stree(cur_stree
, owner
, name
, sym
);
431 return sms
->possible
;
435 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
)
439 struct state_list
*ret
= NULL
;
441 expr
= strip_expr(expr
);
442 name
= expr_to_var_sym(expr
, &sym
);
445 ret
= get_possible_states(owner
, name
, sym
);
451 struct sm_state
*get_sm_state(int owner
, const char *name
, struct symbol
*sym
)
453 struct sm_state
*ret
;
455 ret
= get_sm_state_stree(fast_overlay
, owner
, name
, sym
);
459 return get_sm_state_stree(cur_stree
, owner
, name
, sym
);
462 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
)
466 struct sm_state
*ret
= NULL
;
468 expr
= strip_expr(expr
);
469 name
= expr_to_var_sym(expr
, &sym
);
472 ret
= get_sm_state(owner
, name
, sym
);
478 void __delete_state(int owner
, const char *name
, struct symbol
*sym
)
480 delete_state_stree(&cur_stree
, owner
, name
, sym
);
481 if (cond_true_stack
) {
482 delete_state_stree_stack(&pre_cond_stack
, owner
, name
, sym
);
483 delete_state_stree_stack(&cond_true_stack
, owner
, name
, sym
);
484 delete_state_stree_stack(&cond_false_stack
, owner
, name
, sym
);
488 static void delete_all_states_stree_sym(struct stree
**stree
, struct symbol
*sym
)
490 struct state_list
*slist
= NULL
;
493 FOR_EACH_SM(*stree
, sm
) {
495 add_ptr_list(&slist
, sm
);
496 } END_FOR_EACH_SM(sm
);
498 FOR_EACH_PTR(slist
, sm
) {
499 delete_state_stree(stree
, sm
->owner
, sm
->name
, sm
->sym
);
500 } END_FOR_EACH_PTR(sm
);
505 static void delete_all_states_stree_stack_sym(struct stree_stack
**stack
, struct symbol
*sym
)
512 stree
= pop_stree(stack
);
513 delete_all_states_stree_sym(&stree
, sym
);
514 push_stree(stack
, stree
);
517 void __delete_all_states_sym(struct symbol
*sym
)
519 delete_all_states_stree_sym(&cur_stree
, sym
);
521 delete_all_states_stree_stack_sym(&true_stack
, sym
);
522 delete_all_states_stree_stack_sym(&true_stack
, sym
);
523 delete_all_states_stree_stack_sym(&false_stack
, sym
);
524 delete_all_states_stree_stack_sym(&pre_cond_stack
, sym
);
525 delete_all_states_stree_stack_sym(&cond_true_stack
, sym
);
526 delete_all_states_stree_stack_sym(&cond_false_stack
, sym
);
527 delete_all_states_stree_stack_sym(&fake_cur_stree_stack
, sym
);
528 delete_all_states_stree_stack_sym(&break_stack
, sym
);
529 delete_all_states_stree_stack_sym(&fake_break_stack
, sym
);
530 delete_all_states_stree_stack_sym(&switch_stack
, sym
);
531 delete_all_states_stree_stack_sym(&continue_stack
, sym
);
534 * deleting from the goto stack is problematic because we don't know
535 * if the label is in scope and also we need the value for --two-passes.
539 struct stree
*get_all_states_from_stree(int owner
, struct stree
*source
)
541 struct stree
*ret
= NULL
;
542 struct sm_state
*tmp
;
544 FOR_EACH_SM(source
, tmp
) {
545 if (tmp
->owner
== owner
)
546 avl_insert(&ret
, tmp
);
547 } END_FOR_EACH_SM(tmp
);
552 struct stree
*get_all_states_stree(int owner
)
554 return get_all_states_from_stree(owner
, cur_stree
);
557 struct stree
*__get_cur_stree(void)
562 int is_reachable(void)
569 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
570 struct smatch_state
*true_state
,
571 struct smatch_state
*false_state
)
574 sm_perror("cur_stree is read only.");
578 if (!true_state
&& !false_state
)
581 if (debug_on(check_name(owner
), name
)) {
582 struct smatch_state
*tmp
;
584 tmp
= __get_state(owner
, name
, sym
);
585 sm_msg("%s [%s] '%s'. Was %s. Now T:%s F:%s", __func__
,
586 check_name(owner
), name
, show_state(tmp
),
587 show_state(true_state
), show_state(false_state
));
593 if (!cond_false_stack
|| !cond_true_stack
) {
594 sm_perror("missing true/false stacks");
599 set_state_stree_stack(&cond_true_stack
, owner
, name
, sym
, true_state
);
601 set_state_stree_stack(&cond_false_stack
, owner
, name
, sym
, false_state
);
604 void set_true_false_states_expr(int owner
, struct expression
*expr
,
605 struct smatch_state
*true_state
,
606 struct smatch_state
*false_state
)
611 expr
= strip_expr(expr
);
612 name
= expr_to_var_sym(expr
, &sym
);
615 set_true_false_states(owner
, name
, sym
, true_state
, false_state
);
620 void __set_true_false_sm(struct sm_state
*true_sm
, struct sm_state
*false_sm
)
626 if (!true_sm
&& !false_sm
)
632 owner
= true_sm
? true_sm
->owner
: false_sm
->owner
;
633 name
= true_sm
? true_sm
->name
: false_sm
->name
;
634 sym
= true_sm
? true_sm
->sym
: false_sm
->sym
;
635 if (debug_on(check_name(owner
), name
)) {
636 struct smatch_state
*tmp
;
638 tmp
= __get_state(owner
, name
, sym
);
639 sm_msg("%s [%s] '%s'. Was %s. Now T:%s F:%s", __func__
,
640 check_name(owner
), name
, show_state(tmp
),
641 show_state(true_sm
? true_sm
->state
: NULL
),
642 show_state(false_sm
? false_sm
->state
: NULL
));
645 if (!cond_false_stack
|| !cond_true_stack
) {
646 sm_perror("missing true/false stacks");
651 overwrite_sm_state_stree_stack(&cond_true_stack
, true_sm
);
653 overwrite_sm_state_stree_stack(&cond_false_stack
, false_sm
);
656 void nullify_path(void)
658 if (fake_cur_stree_stack
) {
659 __free_fake_cur_stree();
660 __push_fake_cur_stree();
662 free_stree(&cur_stree
);
665 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
672 * At the start of every function we mark the path
673 * as unnull. That way there is always at least one state
674 * in the cur_stree until nullify_path is called. This
675 * is used in merge_slist() for the first null check.
677 void __unnullify_path(void)
680 set_state(-1, "unnull_path", NULL
, &true_state
);
683 int __path_is_null(void)
690 #define check_stree_stack_free(stack) do { \
692 sm_perror("stack not empty %s", #stack); \
693 free_stack_and_strees(stack); \
697 void save_all_states(void)
699 __add_ptr_list(&backup
, cur_stree
);
702 __add_ptr_list(&backup
, true_stack
);
704 __add_ptr_list(&backup
, false_stack
);
706 __add_ptr_list(&backup
, pre_cond_stack
);
707 pre_cond_stack
= NULL
;
709 __add_ptr_list(&backup
, cond_true_stack
);
710 cond_true_stack
= NULL
;
711 __add_ptr_list(&backup
, cond_false_stack
);
712 cond_false_stack
= NULL
;
714 __add_ptr_list(&backup
, fake_cur_stree_stack
);
715 fake_cur_stree_stack
= NULL
;
717 __add_ptr_list(&backup
, break_stack
);
719 __add_ptr_list(&backup
, fake_break_stack
);
720 fake_break_stack
= NULL
;
722 __add_ptr_list(&backup
, switch_stack
);
724 __add_ptr_list(&backup
, remaining_cases
);
725 remaining_cases
= NULL
;
726 __add_ptr_list(&backup
, default_stack
);
727 default_stack
= NULL
;
728 __add_ptr_list(&backup
, continue_stack
);
729 continue_stack
= NULL
;
731 __add_ptr_list(&backup
, goto_stack
);
735 static void *pop_backup(void)
739 ret
= last_ptr_list(backup
);
740 delete_ptr_list_last(&backup
);
744 void restore_all_states(void)
746 goto_stack
= pop_backup();
748 continue_stack
= pop_backup();
749 default_stack
= pop_backup();
750 remaining_cases
= pop_backup();
751 switch_stack
= pop_backup();
752 fake_break_stack
= pop_backup();
753 break_stack
= pop_backup();
755 fake_cur_stree_stack
= pop_backup();
757 cond_false_stack
= pop_backup();
758 cond_true_stack
= pop_backup();
760 pre_cond_stack
= pop_backup();
761 false_stack
= pop_backup();
762 true_stack
= pop_backup();
764 cur_stree
= pop_backup();
767 void free_goto_stack(void)
769 struct named_stree
*named_stree
;
771 FOR_EACH_PTR(goto_stack
, named_stree
) {
772 free_stree(&named_stree
->stree
);
773 } END_FOR_EACH_PTR(named_stree
);
774 __free_ptr_list((struct ptr_list
**)&goto_stack
);
777 void clear_all_states(void)
780 check_stree_stack_free(&true_stack
);
781 check_stree_stack_free(&false_stack
);
782 check_stree_stack_free(&pre_cond_stack
);
783 check_stree_stack_free(&cond_true_stack
);
784 check_stree_stack_free(&cond_false_stack
);
785 check_stree_stack_free(&break_stack
);
786 check_stree_stack_free(&fake_break_stack
);
787 check_stree_stack_free(&switch_stack
);
788 check_stree_stack_free(&continue_stack
);
789 check_stree_stack_free(&fake_cur_stree_stack
);
793 clear_array_values_cache();
794 free_every_single_sm_state();
795 free_tmp_expressions();
798 void __push_cond_stacks(void)
800 push_stree(&cond_true_stack
, NULL
);
801 push_stree(&cond_false_stack
, NULL
);
802 __push_fake_cur_stree();
805 void __fold_in_set_states(void)
807 struct stree
*new_states
;
810 new_states
= __pop_fake_cur_stree();
811 FOR_EACH_SM(new_states
, sm
) {
813 __set_true_false_sm(sm
, sm
);
814 } END_FOR_EACH_SM(sm
);
815 free_stree(&new_states
);
818 void __free_set_states(void)
820 struct stree
*new_states
;
822 new_states
= __pop_fake_cur_stree();
823 free_stree(&new_states
);
826 struct stree
*__copy_cond_true_states(void)
830 ret
= pop_stree(&cond_true_stack
);
831 push_stree(&cond_true_stack
, clone_stree(ret
));
835 struct stree
*__copy_cond_false_states(void)
839 ret
= pop_stree(&cond_false_stack
);
840 push_stree(&cond_false_stack
, clone_stree(ret
));
844 struct stree
*__pop_cond_true_stack(void)
846 return pop_stree(&cond_true_stack
);
849 struct stree
*__pop_cond_false_stack(void)
851 return pop_stree(&cond_false_stack
);
855 * This combines the pre cond states with either the true or false states.
857 * a = kmalloc() ; if (a !! foo(a)
858 * In the pre state a is possibly null. In the true state it is non null.
859 * In the false state it is null. Combine the pre and the false to get
860 * that when we call 'foo', 'a' is null.
862 static void __use_cond_stack(struct stree_stack
**stack
)
866 free_stree(&cur_stree
);
868 cur_stree
= pop_stree(&pre_cond_stack
);
869 push_stree(&pre_cond_stack
, clone_stree(cur_stree
));
871 stree
= pop_stree(stack
);
872 overwrite_stree(stree
, &cur_stree
);
873 push_stree(stack
, stree
);
876 void __use_pre_cond_states(void)
878 free_stree(&cur_stree
);
879 cur_stree
= pop_stree(&pre_cond_stack
);
882 void __use_cond_true_states(void)
884 __use_cond_stack(&cond_true_stack
);
887 void __use_cond_false_states(void)
889 __use_cond_stack(&cond_false_stack
);
892 void __negate_cond_stacks(void)
894 struct stree
*old_false
, *old_true
;
896 old_false
= pop_stree(&cond_false_stack
);
897 old_true
= pop_stree(&cond_true_stack
);
898 push_stree(&cond_false_stack
, old_true
);
899 push_stree(&cond_true_stack
, old_false
);
902 void __and_cond_states(void)
904 and_stree_stack(&cond_true_stack
);
905 or_stree_stack(&pre_cond_stack
, cur_stree
, &cond_false_stack
);
908 void __or_cond_states(void)
910 or_stree_stack(&pre_cond_stack
, cur_stree
, &cond_true_stack
);
911 and_stree_stack(&cond_false_stack
);
914 void __save_pre_cond_states(void)
916 push_stree(&pre_cond_stack
, clone_stree(cur_stree
));
919 void __discard_pre_cond_states(void)
923 tmp
= pop_stree(&pre_cond_stack
);
927 struct stree
*__get_true_states(void)
929 return clone_stree(top_stree(cond_true_stack
));
932 struct stree
*__get_false_states(void)
934 return clone_stree(top_stree(cond_false_stack
));
937 void __use_cond_states(void)
939 struct stree
*pre
, *pre_clone
, *true_states
, *false_states
;
941 pre
= pop_stree(&pre_cond_stack
);
942 pre_clone
= clone_stree(pre
);
944 true_states
= pop_stree(&cond_true_stack
);
945 overwrite_stree(true_states
, &pre
);
946 free_stree(&true_states
);
947 /* we use the true states right away */
948 free_stree(&cur_stree
);
951 false_states
= pop_stree(&cond_false_stack
);
952 overwrite_stree(false_states
, &pre_clone
);
953 free_stree(&false_states
);
954 push_stree(&false_stack
, pre_clone
);
957 void __push_true_states(void)
959 push_stree(&true_stack
, clone_stree(cur_stree
));
962 void __use_false_states(void)
964 free_stree(&cur_stree
);
965 cur_stree
= pop_stree(&false_stack
);
968 void __discard_false_states(void)
972 stree
= pop_stree(&false_stack
);
976 void __merge_false_states(void)
980 stree
= pop_stree(&false_stack
);
981 merge_stree(&cur_stree
, stree
);
986 * This function probably seemed common sensical when I wrote it but, oh wow,
987 * does it look subtle in retrospect. Say we set a state on one side of the if
988 * else path but not on the other, then what we should record in the fake stree
989 * is the merged state.
991 * This function relies on the fact that the we always set the cur_stree as well
992 * and we already have the infrastructure to merge things correctly into the
995 * So instead of merging fake strees together which is probably a lot of work,
996 * we just use it as a list of set states and look up the actual current values
1000 static void update_stree_with_merged(struct stree
**stree
)
1002 struct state_list
*slist
= NULL
;
1003 struct sm_state
*sm
, *new;
1005 FOR_EACH_SM(*stree
, sm
) {
1006 new = get_sm_state(sm
->owner
, sm
->name
, sm
->sym
);
1007 if (!new) /* This can happen if we go out of scope */
1009 add_ptr_list(&slist
, new);
1010 } END_FOR_EACH_SM(sm
);
1012 FOR_EACH_PTR(slist
, sm
) {
1013 overwrite_sm_state_stree(stree
, sm
);
1014 } END_FOR_EACH_PTR(sm
);
1019 static void update_fake_stree_with_merged(void)
1021 struct stree
*stree
;
1023 if (!fake_cur_stree_stack
)
1025 stree
= pop_stree(&fake_cur_stree_stack
);
1026 update_stree_with_merged(&stree
);
1027 push_stree(&fake_cur_stree_stack
, stree
);
1030 void __merge_true_states(void)
1032 struct stree
*stree
;
1034 stree
= pop_stree(&true_stack
);
1035 merge_stree(&cur_stree
, stree
);
1036 update_fake_stree_with_merged();
1040 void __push_continues(void)
1042 push_stree(&continue_stack
, NULL
);
1045 void __discard_continues(void)
1047 struct stree
*stree
;
1049 stree
= pop_stree(&continue_stack
);
1053 void __process_continues(void)
1055 struct stree
*stree
;
1057 stree
= pop_stree(&continue_stack
);
1059 stree
= clone_stree(cur_stree
);
1061 merge_stree(&stree
, cur_stree
);
1063 push_stree(&continue_stack
, stree
);
1066 void __merge_continues(void)
1068 struct stree
*stree
;
1070 stree
= pop_stree(&continue_stack
);
1071 merge_stree(&cur_stree
, stree
);
1075 void __push_breaks(void)
1077 push_stree(&break_stack
, NULL
);
1078 if (fake_cur_stree_stack
)
1079 push_stree(&fake_break_stack
, NULL
);
1082 void __process_breaks(void)
1084 struct stree
*stree
;
1086 stree
= pop_stree(&break_stack
);
1088 stree
= clone_stree(cur_stree
);
1090 merge_stree(&stree
, cur_stree
);
1091 push_stree(&break_stack
, stree
);
1093 if (!fake_cur_stree_stack
)
1096 stree
= pop_stree(&fake_break_stack
);
1098 stree
= clone_stree(top_stree(fake_cur_stree_stack
));
1100 merge_stree(&stree
, top_stree(fake_cur_stree_stack
));
1101 push_stree(&fake_break_stack
, stree
);
1104 int __has_breaks(void)
1106 struct stree
*stree
;
1109 stree
= pop_stree(&break_stack
);
1111 push_stree(&break_stack
, stree
);
1115 void __merge_breaks(void)
1117 struct stree
*stree
;
1118 struct sm_state
*sm
;
1120 stree
= pop_stree(&break_stack
);
1121 merge_stree(&cur_stree
, stree
);
1124 if (!fake_cur_stree_stack
)
1127 stree
= pop_stree(&fake_break_stack
);
1128 update_stree_with_merged(&stree
);
1129 FOR_EACH_SM(stree
, sm
) {
1130 overwrite_sm_state_stree_stack(&fake_cur_stree_stack
, sm
);
1131 } END_FOR_EACH_SM(sm
);
1135 void __use_breaks(void)
1137 struct stree
*stree
;
1138 struct sm_state
*sm
;
1140 free_stree(&cur_stree
);
1141 cur_stree
= pop_stree(&break_stack
);
1143 if (!fake_cur_stree_stack
)
1145 stree
= pop_stree(&fake_break_stack
);
1146 FOR_EACH_SM(stree
, sm
) {
1147 overwrite_sm_state_stree_stack(&fake_cur_stree_stack
, sm
);
1148 } END_FOR_EACH_SM(sm
);
1154 void __save_switch_states(struct expression
*switch_expr
)
1156 struct range_list
*rl
;
1158 get_absolute_rl(switch_expr
, &rl
);
1160 push_rl(&remaining_cases
, rl
);
1161 push_stree(&switch_stack
, clone_stree(cur_stree
));
1164 int have_remaining_cases(void)
1166 return !!top_rl(remaining_cases
);
1169 void __merge_switches(struct expression
*switch_expr
, struct range_list
*case_rl
)
1171 struct stree
*stree
;
1172 struct stree
*implied_stree
;
1174 stree
= pop_stree(&switch_stack
);
1177 * If the cur_stree was NULL before the start of the switch
1178 * statement then we don't want to unnullify it.
1181 push_stree(&switch_stack
, stree
);
1184 implied_stree
= __implied_case_stree(switch_expr
, case_rl
, &remaining_cases
, &stree
);
1185 merge_stree(&cur_stree
, implied_stree
);
1186 free_stree(&implied_stree
);
1187 push_stree(&switch_stack
, stree
);
1190 void __discard_switches(void)
1192 struct stree
*stree
;
1194 pop_rl(&remaining_cases
);
1195 stree
= pop_stree(&switch_stack
);
1199 void __push_default(void)
1201 push_stree(&default_stack
, NULL
);
1204 void __set_default(void)
1206 set_state_stree_stack(&default_stack
, 0, "has_default", NULL
, &true_state
);
1209 bool __has_default_case(void)
1211 struct stree
*stree
;
1213 stree
= last_ptr_list((struct ptr_list
*)default_stack
);
1217 int __pop_default(void)
1219 struct stree
*stree
;
1221 stree
= pop_stree(&default_stack
);
1229 static struct named_stree
*alloc_named_stree(const char *name
, struct symbol
*sym
, struct stree
*stree
)
1231 struct named_stree
*named_stree
= __alloc_named_stree(0);
1233 named_stree
->name
= (char *)name
;
1234 named_stree
->stree
= stree
;
1235 named_stree
->sym
= sym
;
1239 void __save_gotos(const char *name
, struct symbol
*sym
)
1241 struct stree
**stree
;
1242 struct stree
*clone
;
1244 stree
= get_named_stree(goto_stack
, name
, sym
);
1246 merge_stree(stree
, cur_stree
);
1249 struct named_stree
*named_stree
;
1251 clone
= clone_stree(cur_stree
);
1252 named_stree
= alloc_named_stree(name
, sym
, clone
);
1253 add_ptr_list(&goto_stack
, named_stree
);
1257 void __merge_gotos(const char *name
, struct symbol
*sym
)
1259 struct stree
**stree
;
1261 stree
= get_named_stree(goto_stack
, name
, sym
);
1263 merge_stree(&cur_stree
, *stree
);
1266 void __discard_fake_states(struct expression
*call
)
1268 struct stree
*new = NULL
;
1269 struct sm_state
*sm
;
1273 if (__fake_state_cnt
== 0)
1277 * This is just a best effort type of thing. There could be
1278 * fake states in the true/false trees already. They might
1279 * eventually get cleared out too because we call after probably
1280 * 50% of function calls. But the point is that I don't want to
1281 * waste resources tracking them.
1283 FOR_EACH_SM(cur_stree
, sm
) {
1285 len
= snprintf(buf
, sizeof(buf
), "__fake_param_%p_", call
);
1286 if (strncmp(sm
->name
, buf
, len
) != 0)
1287 avl_insert(&new, sm
);
1290 if (strncmp(sm
->name
, "__fake_param_", 13) != 0)
1291 avl_insert(&new, sm
);
1292 } END_FOR_EACH_SM(sm
);
1294 free_stree(&cur_stree
);
1296 __fake_state_cnt
= 0;