unreachable: don't complain about iterate_all_kinds()
[smatch.git] / smatch_states.c
blob58f77e6e638998c2cc6e58bd189264af42b73ea3
1 /*
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
22 * on stacks.
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
26 * of the stacks.
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.
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include "smatch.h"
37 #include "smatch_slist.h"
38 #include "smatch_extra.h"
40 struct smatch_state undefined = { .name = "undefined" };
41 struct smatch_state ghost = { .name = "ghost" };
42 struct smatch_state merged = { .name = "merged" };
43 struct smatch_state true_state = { .name = "true" };
44 struct smatch_state false_state = { .name = "false" };
46 static struct stree *cur_stree; /* current states */
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;
56 static int read_only;
58 static struct stree_stack *break_stack;
59 static struct stree_stack *switch_stack;
60 static struct range_list_stack *remaining_cases;
61 static struct stree_stack *default_stack;
62 static struct stree_stack *continue_stack;
64 static struct named_stree_stack *goto_stack;
66 static struct ptr_list *backup;
68 int option_debug;
70 void __print_cur_stree(void)
72 __print_stree(cur_stree);
75 int unreachable(void)
77 if (!cur_stree)
78 return 1;
79 return 0;
82 struct sm_state *set_state(int owner, const char *name, struct symbol *sym, struct smatch_state *state)
84 struct sm_state *ret;
86 if (!name)
87 return NULL;
89 if (read_only)
90 sm_msg("Smatch Internal Error: cur_stree is read only.");
92 if (option_debug || strcmp(check_name(owner), option_debug_check) == 0) {
93 struct smatch_state *s;
95 s = get_state(owner, name, sym);
96 if (!s)
97 sm_msg("new state. name='%s' [%s] %s",
98 name, check_name(owner), show_state(state));
99 else
100 sm_msg("state change name='%s' [%s] %s => %s",
101 name, check_name(owner), show_state(s),
102 show_state(state));
105 if (owner != -1 && unreachable())
106 return NULL;
108 if (fake_cur_stree_stack)
109 set_state_stree_stack(&fake_cur_stree_stack, owner, name, sym, state);
111 ret = set_state_stree(&cur_stree, owner, name, sym, state);
113 if (cond_true_stack) {
114 set_state_stree_stack(&cond_true_stack, owner, name, sym, state);
115 set_state_stree_stack(&cond_false_stack, owner, name, sym, state);
117 return ret;
120 struct sm_state *set_state_expr(int owner, struct expression *expr, struct smatch_state *state)
122 char *name;
123 struct symbol *sym;
124 struct sm_state *ret = NULL;
126 expr = strip_expr(expr);
127 name = expr_to_var_sym(expr, &sym);
128 if (!name || !sym)
129 goto free;
130 ret = set_state(owner, name, sym, state);
131 free:
132 free_string(name);
133 return ret;
136 void __swap_cur_stree(struct stree *stree)
138 free_stree(&cur_stree);
139 cur_stree = stree;
142 void __push_fake_cur_stree(void)
144 push_stree(&fake_cur_stree_stack, NULL);
145 __save_pre_cond_states();
148 struct stree *__pop_fake_cur_stree(void)
150 if (!fake_cur_stree_stack)
151 sm_msg("INTERNAL error: popping too many fake cur strees.");
152 __use_pre_cond_states();
153 return pop_stree(&fake_cur_stree_stack);
156 void __free_fake_cur_stree(void)
158 struct stree *stree;
160 stree = __pop_fake_cur_stree();
161 free_stree(&stree);
164 void __set_fake_cur_stree_fast(struct stree *stree)
166 push_stree(&pre_cond_stack, cur_stree);
167 cur_stree = stree;
168 read_only = 1;
171 void __pop_fake_cur_stree_fast(void)
173 cur_stree = pop_stree(&pre_cond_stack);
174 read_only = 0;
177 void __merge_stree_into_cur(struct stree *stree)
179 struct sm_state *sm;
180 struct sm_state *orig;
181 struct sm_state *merged;
183 FOR_EACH_SM(stree, sm) {
184 orig = get_sm_state(sm->owner, sm->name, sm->sym);
185 if (orig)
186 merged = merge_sm_states(orig, sm);
187 else
188 merged = sm;
189 __set_sm(merged);
190 } END_FOR_EACH_SM(sm);
193 void __set_sm(struct sm_state *sm)
195 if (read_only)
196 sm_msg("Smatch Internal Error: cur_stree is read only.");
198 if (option_debug ||
199 strcmp(check_name(sm->owner), option_debug_check) == 0) {
200 struct smatch_state *s;
202 s = get_state(sm->owner, sm->name, sm->sym);
203 if (!s)
204 sm_msg("new state. name='%s' [%s] %s",
205 sm->name, check_name(sm->owner),
206 show_state(sm->state));
207 else
208 sm_msg("state change name='%s' [%s] %s => %s",
209 sm->name, check_name(sm->owner), show_state(s),
210 show_state(sm->state));
213 if (unreachable())
214 return;
216 if (fake_cur_stree_stack)
217 overwrite_sm_state_stree_stack(&fake_cur_stree_stack, sm);
219 overwrite_sm_state_stree(&cur_stree, sm);
221 if (cond_true_stack) {
222 overwrite_sm_state_stree_stack(&cond_true_stack, sm);
223 overwrite_sm_state_stree_stack(&cond_false_stack, sm);
227 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym)
229 __get_state_hook(owner, name, sym);
231 return get_state_stree(cur_stree, owner, name, sym);
234 struct smatch_state *get_state_expr(int owner, struct expression *expr)
236 char *name;
237 struct symbol *sym;
238 struct smatch_state *ret = NULL;
240 expr = strip_expr(expr);
241 name = expr_to_var_sym(expr, &sym);
242 if (!name || !sym)
243 goto free;
244 ret = get_state(owner, name, sym);
245 free:
246 free_string(name);
247 return ret;
250 struct state_list *get_possible_states(int owner, const char *name, struct symbol *sym)
252 struct sm_state *sms;
254 sms = get_sm_state_stree(cur_stree, owner, name, sym);
255 if (sms)
256 return sms->possible;
257 return NULL;
260 struct state_list *get_possible_states_expr(int owner, struct expression *expr)
262 char *name;
263 struct symbol *sym;
264 struct state_list *ret = NULL;
266 expr = strip_expr(expr);
267 name = expr_to_var_sym(expr, &sym);
268 if (!name || !sym)
269 goto free;
270 ret = get_possible_states(owner, name, sym);
271 free:
272 free_string(name);
273 return ret;
276 struct sm_state *get_sm_state(int owner, const char *name, struct symbol *sym)
278 return get_sm_state_stree(cur_stree, owner, name, sym);
281 struct sm_state *get_sm_state_expr(int owner, struct expression *expr)
283 char *name;
284 struct symbol *sym;
285 struct sm_state *ret = NULL;
287 expr = strip_expr(expr);
288 name = expr_to_var_sym(expr, &sym);
289 if (!name || !sym)
290 goto free;
291 ret = get_sm_state(owner, name, sym);
292 free:
293 free_string(name);
294 return ret;
297 void delete_state(int owner, const char *name, struct symbol *sym)
299 delete_state_stree(&cur_stree, owner, name, sym);
300 if (cond_true_stack) {
301 delete_state_stree_stack(&pre_cond_stack, owner, name, sym);
302 delete_state_stree_stack(&cond_true_stack, owner, name, sym);
303 delete_state_stree_stack(&cond_false_stack, owner, name, sym);
307 void delete_state_expr(int owner, struct expression *expr)
309 char *name;
310 struct symbol *sym;
312 expr = strip_expr(expr);
313 name = expr_to_var_sym(expr, &sym);
314 if (!name || !sym)
315 goto free;
316 delete_state(owner, name, sym);
317 free:
318 free_string(name);
321 struct stree *get_all_states_from_stree(int owner, struct stree *source)
323 struct stree *ret = NULL;
324 struct sm_state *tmp;
326 FOR_EACH_SM(source, tmp) {
327 if (tmp->owner == owner)
328 avl_insert(&ret, tmp);
329 } END_FOR_EACH_SM(tmp);
331 return ret;
334 struct stree *get_all_states_stree(int owner)
336 return get_all_states_from_stree(owner, cur_stree);
339 struct stree *__get_cur_stree(void)
341 return cur_stree;
344 int is_reachable(void)
346 if (cur_stree)
347 return 1;
348 return 0;
351 void set_true_false_states(int owner, const char *name, struct symbol *sym,
352 struct smatch_state *true_state,
353 struct smatch_state *false_state)
355 if (read_only)
356 sm_msg("Smatch Internal Error: cur_stree is read only.");
358 if (option_debug || strcmp(check_name(owner), option_debug_check) == 0) {
359 struct smatch_state *tmp;
361 tmp = get_state(owner, name, sym);
362 sm_msg("set_true_false '%s'. Was %s. Now T:%s F:%s\n",
363 name, show_state(tmp),
364 show_state(true_state), show_state(false_state));
367 if (unreachable())
368 return;
370 if (!cond_false_stack || !cond_true_stack) {
371 printf("Error: missing true/false stacks\n");
372 return;
375 if (true_state) {
376 set_state_stree(&cur_stree, owner, name, sym, true_state);
377 set_state_stree_stack(&cond_true_stack, owner, name, sym, true_state);
379 if (false_state)
380 set_state_stree_stack(&cond_false_stack, owner, name, sym, false_state);
383 void set_true_false_states_expr(int owner, struct expression *expr,
384 struct smatch_state *true_state,
385 struct smatch_state *false_state)
387 char *name;
388 struct symbol *sym;
390 expr = strip_expr(expr);
391 name = expr_to_var_sym(expr, &sym);
392 if (!name || !sym)
393 goto free;
394 set_true_false_states(owner, name, sym, true_state, false_state);
395 free:
396 free_string(name);
399 void __set_true_false_sm(struct sm_state *true_sm, struct sm_state *false_sm)
401 if (unreachable())
402 return;
404 if (!cond_false_stack || !cond_true_stack) {
405 printf("Error: missing true/false stacks\n");
406 return;
409 if (true_sm) {
410 overwrite_sm_state_stree(&cur_stree, true_sm);
411 overwrite_sm_state_stree_stack(&cond_true_stack, true_sm);
413 if (false_sm)
414 overwrite_sm_state_stree_stack(&cond_false_stack, false_sm);
417 void nullify_path(void)
419 free_stree(&cur_stree);
422 void __match_nullify_path_hook(const char *fn, struct expression *expr,
423 void *unused)
425 nullify_path();
429 * At the start of every function we mark the path
430 * as unnull. That way there is always at least one state
431 * in the cur_stree until nullify_path is called. This
432 * is used in merge_slist() for the first null check.
434 void __unnullify_path(void)
436 if (!cur_stree)
437 set_state(-1, "unnull_path", NULL, &true_state);
440 int __path_is_null(void)
442 if (cur_stree)
443 return 0;
444 return 1;
447 static void check_stree_stack_free(struct stree_stack **stack)
449 if (*stack) {
450 sm_msg("smatch internal error: stack not empty");
451 free_stack_and_strees(stack);
455 void save_all_states(void)
457 __add_ptr_list(&backup, cur_stree, 0);
458 cur_stree = NULL;
460 __add_ptr_list(&backup, true_stack, 0);
461 true_stack = NULL;
462 __add_ptr_list(&backup, false_stack, 0);
463 false_stack = NULL;
464 __add_ptr_list(&backup, pre_cond_stack, 0);
465 pre_cond_stack = NULL;
467 __add_ptr_list(&backup, cond_true_stack, 0);
468 cond_true_stack = NULL;
469 __add_ptr_list(&backup, cond_false_stack, 0);
470 cond_false_stack = NULL;
472 __add_ptr_list(&backup, fake_cur_stree_stack, 0);
473 fake_cur_stree_stack = NULL;
475 __add_ptr_list(&backup, break_stack, 0);
476 break_stack = NULL;
477 __add_ptr_list(&backup, switch_stack, 0);
478 switch_stack = NULL;
479 __add_ptr_list(&backup, remaining_cases, 0);
480 remaining_cases = NULL;
481 __add_ptr_list(&backup, default_stack, 0);
482 default_stack = NULL;
483 __add_ptr_list(&backup, continue_stack, 0);
484 continue_stack = NULL;
486 __add_ptr_list(&backup, goto_stack, 0);
487 goto_stack = NULL;
490 static void *pop_backup(void)
492 void *ret;
494 ret = last_ptr_list(backup);
495 delete_ptr_list_last(&backup);
496 return ret;
499 void restore_all_states(void)
501 goto_stack = pop_backup();
503 continue_stack = pop_backup();
504 default_stack = pop_backup();
505 remaining_cases = pop_backup();
506 switch_stack = pop_backup();
507 break_stack = pop_backup();
509 fake_cur_stree_stack = pop_backup();
511 cond_false_stack = pop_backup();
512 cond_true_stack = pop_backup();
514 pre_cond_stack = pop_backup();
515 false_stack = pop_backup();
516 true_stack = pop_backup();
518 cur_stree = pop_backup();
521 void free_goto_stack(void)
523 struct named_stree *named_stree;
525 FOR_EACH_PTR(goto_stack, named_stree) {
526 free_stree(&named_stree->stree);
527 } END_FOR_EACH_PTR(named_stree);
528 __free_ptr_list((struct ptr_list **)&goto_stack);
531 void clear_all_states(void)
533 nullify_path();
534 check_stree_stack_free(&true_stack);
535 check_stree_stack_free(&false_stack);
536 check_stree_stack_free(&pre_cond_stack);
537 check_stree_stack_free(&cond_true_stack);
538 check_stree_stack_free(&cond_false_stack);
539 check_stree_stack_free(&break_stack);
540 check_stree_stack_free(&switch_stack);
541 check_stree_stack_free(&continue_stack);
543 free_goto_stack();
545 free_every_single_sm_state();
548 void __push_cond_stacks(void)
550 push_stree(&cond_true_stack, NULL);
551 push_stree(&cond_false_stack, NULL);
554 struct stree *__copy_cond_true_states(void)
556 struct stree *ret;
558 ret = pop_stree(&cond_true_stack);
559 push_stree(&cond_true_stack, clone_stree(ret));
560 return ret;
563 struct stree *__copy_cond_false_states(void)
565 struct stree *ret;
567 ret = pop_stree(&cond_false_stack);
568 push_stree(&cond_false_stack, clone_stree(ret));
569 return ret;
572 struct stree *__pop_cond_true_stack(void)
574 return pop_stree(&cond_true_stack);
577 struct stree *__pop_cond_false_stack(void)
579 return pop_stree(&cond_false_stack);
583 * This combines the pre cond states with either the true or false states.
584 * For example:
585 * a = kmalloc() ; if (a !! foo(a)
586 * In the pre state a is possibly null. In the true state it is non null.
587 * In the false state it is null. Combine the pre and the false to get
588 * that when we call 'foo', 'a' is null.
590 static void __use_cond_stack(struct stree_stack **stack)
592 struct stree *stree;
594 free_stree(&cur_stree);
596 cur_stree = pop_stree(&pre_cond_stack);
597 push_stree(&pre_cond_stack, clone_stree(cur_stree));
599 stree = pop_stree(stack);
600 overwrite_stree(stree, &cur_stree);
601 push_stree(stack, stree);
604 void __use_pre_cond_states(void)
606 free_stree(&cur_stree);
607 cur_stree = pop_stree(&pre_cond_stack);
610 void __use_cond_true_states(void)
612 __use_cond_stack(&cond_true_stack);
615 void __use_cond_false_states(void)
617 __use_cond_stack(&cond_false_stack);
620 void __negate_cond_stacks(void)
622 struct stree *old_false, *old_true;
624 __use_cond_stack(&cond_false_stack);
625 old_false = pop_stree(&cond_false_stack);
626 old_true = pop_stree(&cond_true_stack);
627 push_stree(&cond_false_stack, old_true);
628 push_stree(&cond_true_stack, old_false);
631 void __and_cond_states(void)
633 and_stree_stack(&cond_true_stack);
634 or_stree_stack(&pre_cond_stack, cur_stree, &cond_false_stack);
637 void __or_cond_states(void)
639 or_stree_stack(&pre_cond_stack, cur_stree, &cond_true_stack);
640 and_stree_stack(&cond_false_stack);
643 void __save_pre_cond_states(void)
645 push_stree(&pre_cond_stack, clone_stree(cur_stree));
648 void __discard_pre_cond_states(void)
650 struct stree *tmp;
652 tmp = pop_stree(&pre_cond_stack);
653 free_stree(&tmp);
656 void __use_cond_states(void)
658 struct stree *pre, *pre_clone, *true_states, *false_states;
660 pre = pop_stree(&pre_cond_stack);
661 pre_clone = clone_stree(pre);
663 true_states = pop_stree(&cond_true_stack);
664 overwrite_stree(true_states, &pre);
665 free_stree(&true_states);
666 /* we use the true states right away */
667 free_stree(&cur_stree);
668 cur_stree = pre;
670 false_states = pop_stree(&cond_false_stack);
671 overwrite_stree(false_states, &pre_clone);
672 free_stree(&false_states);
673 push_stree(&false_stack, pre_clone);
676 void __push_true_states(void)
678 push_stree(&true_stack, clone_stree(cur_stree));
681 void __use_false_states(void)
683 free_stree(&cur_stree);
684 cur_stree = pop_stree(&false_stack);
687 void __discard_false_states(void)
689 struct stree *stree;
691 stree = pop_stree(&false_stack);
692 free_stree(&stree);
695 void __merge_false_states(void)
697 struct stree *stree;
699 stree = pop_stree(&false_stack);
700 merge_stree(&cur_stree, stree);
701 free_stree(&stree);
704 void __merge_true_states(void)
706 struct stree *stree;
708 stree = pop_stree(&true_stack);
709 merge_stree(&cur_stree, stree);
710 free_stree(&stree);
713 void __push_continues(void)
715 push_stree(&continue_stack, NULL);
718 void __discard_continues(void)
720 struct stree *stree;
722 stree = pop_stree(&continue_stack);
723 free_stree(&stree);
726 void __process_continues(void)
728 struct stree *stree;
730 stree = pop_stree(&continue_stack);
731 if (!stree)
732 stree = clone_stree(cur_stree);
733 else
734 merge_stree(&stree, cur_stree);
736 push_stree(&continue_stack, stree);
739 static int top_stree_empty(struct stree_stack **stack)
741 struct stree *tmp;
742 int empty = 0;
744 tmp = pop_stree(stack);
745 if (!tmp)
746 empty = 1;
747 push_stree(stack, tmp);
748 return empty;
751 /* a silly loop does this: while(i--) { return; } */
752 void __warn_on_silly_pre_loops(void)
754 if (!__path_is_null())
755 return;
756 if (!top_stree_empty(&continue_stack))
757 return;
758 if (!top_stree_empty(&break_stack))
759 return;
760 /* if the path was nullified before the loop, then we already
761 printed an error earlier */
762 if (top_stree_empty(&false_stack))
763 return;
764 sm_msg("info: loop could be replaced with if statement.");
767 void __merge_continues(void)
769 struct stree *stree;
771 stree = pop_stree(&continue_stack);
772 merge_stree(&cur_stree, stree);
773 free_stree(&stree);
776 void __push_breaks(void)
778 push_stree(&break_stack, NULL);
781 void __process_breaks(void)
783 struct stree *stree;
785 stree = pop_stree(&break_stack);
786 if (!stree)
787 stree = clone_stree(cur_stree);
788 else
789 merge_stree(&stree, cur_stree);
791 push_stree(&break_stack, stree);
794 int __has_breaks(void)
796 struct stree *stree;
797 int ret;
799 stree = pop_stree(&break_stack);
800 ret = !!stree;
801 push_stree(&break_stack, stree);
802 return ret;
805 void __merge_breaks(void)
807 struct stree *stree;
809 stree = pop_stree(&break_stack);
810 merge_stree(&cur_stree, stree);
811 free_stree(&stree);
814 void __use_breaks(void)
816 free_stree(&cur_stree);
817 cur_stree = pop_stree(&break_stack);
820 void __save_switch_states(struct expression *switch_expr)
822 push_rl(&remaining_cases, __get_implied_values(switch_expr));
823 push_stree(&switch_stack, clone_stree(cur_stree));
826 void __merge_switches(struct expression *switch_expr, struct expression *case_expr)
828 struct stree *stree;
829 struct stree *implied_stree;
831 stree = pop_stree(&switch_stack);
832 implied_stree = __implied_case_stree(switch_expr, case_expr, &remaining_cases, &stree);
833 merge_stree(&cur_stree, implied_stree);
834 free_stree(&implied_stree);
835 push_stree(&switch_stack, stree);
838 void __discard_switches(void)
840 struct stree *stree;
842 pop_rl(&remaining_cases);
843 stree = pop_stree(&switch_stack);
844 free_stree(&stree);
847 void __push_default(void)
849 push_stree(&default_stack, NULL);
852 void __set_default(void)
854 set_state_stree_stack(&default_stack, 0, "has_default", NULL, &true_state);
857 int __pop_default(void)
859 struct stree *stree;
861 stree = pop_stree(&default_stack);
862 if (stree) {
863 free_stree(&stree);
864 return 1;
866 return 0;
869 static struct named_stree *alloc_named_stree(const char *name, struct stree *stree)
871 struct named_stree *named_stree = __alloc_named_stree(0);
873 named_stree->name = (char *)name;
874 named_stree->stree = stree;
875 return named_stree;
878 void __save_gotos(const char *name)
880 struct stree **stree;
881 struct stree *clone;
883 stree = get_named_stree(goto_stack, name);
884 if (stree) {
885 merge_stree(stree, cur_stree);
886 return;
887 } else {
888 struct named_stree *named_stree;
890 clone = clone_stree(cur_stree);
891 named_stree = alloc_named_stree(name, clone);
892 add_ptr_list(&goto_stack, named_stree);
896 void __merge_gotos(const char *name)
898 struct stree **stree;
900 stree = get_named_stree(goto_stack, name);
901 if (stree)
902 merge_stree(&cur_stree, *stree);