missing_break: use fewer states
[smatch.git] / smatch_states.c
blob53d5c8c99eb843221dc57114c596f1a0ebf84171
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 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 */
47 static struct stree_stack *true_stack; /* states after a t/f branch */
48 static struct stree_stack *false_stack;
49 static struct stree_stack *pre_cond_stack; /* states before a t/f branch */
51 static struct stree_stack *cond_true_stack; /* states affected by a branch */
52 static struct stree_stack *cond_false_stack;
54 static struct stree_stack *fake_cur_stree_stack;
55 static int read_only;
57 static struct stree_stack *break_stack;
58 static struct stree_stack *switch_stack;
59 static struct range_list_stack *remaining_cases;
60 static struct stree_stack *default_stack;
61 static struct stree_stack *continue_stack;
63 static struct named_stree_stack *goto_stack;
65 static struct ptr_list *backup;
67 int option_debug;
69 void __print_cur_stree(void)
71 __print_stree(cur_stree);
74 static int in_declarations_bit(void)
76 struct statement *tmp;
78 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
79 if (tmp->type == STMT_DECLARATION)
80 return 1;
81 return 0;
82 } END_FOR_EACH_PTR_REVERSE(tmp);
83 return 0;
86 int unreachable(void)
88 static int reset_warnings = 1;
90 if (cur_stree) {
91 if (!__inline_fn)
92 reset_warnings = 1;
93 return 0;
96 if (in_declarations_bit())
97 return 1;
99 /* option spammy turns on a noisier version of this */
100 if (reset_warnings && !option_spammy)
101 sm_msg("info: ignoring unreachable code.");
102 if (!__inline_fn)
103 reset_warnings = 0;
104 return 1;
107 struct sm_state *set_state(int owner, const char *name, struct symbol *sym, struct smatch_state *state)
109 struct sm_state *ret;
111 if (!name)
112 return NULL;
114 if (read_only)
115 sm_msg("Smatch Internal Error: cur_stree is read only.");
117 if (option_debug || strcmp(check_name(owner), option_debug_check) == 0) {
118 struct smatch_state *s;
120 s = get_state(owner, name, sym);
121 if (!s)
122 printf("%d new state. name='%s' [%s] %s\n",
123 get_lineno(), name, check_name(owner), show_state(state));
124 else
125 printf("%d state change name='%s' [%s] %s => %s\n",
126 get_lineno(), name, check_name(owner), show_state(s),
127 show_state(state));
130 if (owner != -1 && unreachable())
131 return NULL;
133 if (fake_cur_stree_stack)
134 set_state_stree_stack(&fake_cur_stree_stack, owner, name, sym, state);
136 ret = set_state_stree(&cur_stree, owner, name, sym, state);
138 if (cond_true_stack) {
139 set_state_stree_stack(&cond_true_stack, owner, name, sym, state);
140 set_state_stree_stack(&cond_false_stack, owner, name, sym, state);
142 return ret;
145 struct sm_state *set_state_expr(int owner, struct expression *expr, struct smatch_state *state)
147 char *name;
148 struct symbol *sym;
149 struct sm_state *ret = NULL;
151 expr = strip_expr(expr);
152 name = expr_to_var_sym(expr, &sym);
153 if (!name || !sym)
154 goto free;
155 ret = set_state(owner, name, sym, state);
156 free:
157 free_string(name);
158 return ret;
161 void __push_fake_cur_stree()
163 push_stree(&fake_cur_stree_stack, NULL);
164 __save_pre_cond_states();
167 struct stree *__pop_fake_cur_stree()
169 __use_pre_cond_states();
170 return pop_stree(&fake_cur_stree_stack);
173 void __free_fake_cur_stree()
175 struct stree *stree;
177 __use_pre_cond_states();
178 stree = pop_stree(&fake_cur_stree_stack);
179 free_stree(&stree);
182 void __set_fake_cur_stree_fast(struct stree *stree)
184 push_stree(&pre_cond_stack, cur_stree);
185 cur_stree = stree;
186 read_only = 1;
189 void __pop_fake_cur_stree_fast()
191 cur_stree = pop_stree(&pre_cond_stack);
192 read_only = 0;
195 void __merge_stree_into_cur(struct stree *stree)
197 struct sm_state *sm;
198 struct sm_state *orig;
199 struct sm_state *merged;
201 FOR_EACH_SM(stree, sm) {
202 orig = get_sm_state(sm->owner, sm->name, sm->sym);
203 if (orig)
204 merged = merge_sm_states(orig, sm);
205 else
206 merged = sm;
207 __set_sm(merged);
208 } END_FOR_EACH_SM(sm);
211 void __set_sm(struct sm_state *sm)
213 if (read_only)
214 sm_msg("Smatch Internal Error: cur_stree is read only.");
216 if (option_debug ||
217 strcmp(check_name(sm->owner), option_debug_check) == 0) {
218 struct smatch_state *s;
220 s = get_state(sm->owner, sm->name, sm->sym);
221 if (!s)
222 printf("%d new state. name='%s' [%s] %s\n",
223 get_lineno(), sm->name, check_name(sm->owner),
224 show_state(sm->state));
225 else
226 printf("%d state change name='%s' [%s] %s => %s\n",
227 get_lineno(), sm->name, check_name(sm->owner), show_state(s),
228 show_state(sm->state));
231 if (unreachable())
232 return;
234 if (fake_cur_stree_stack)
235 overwrite_sm_state_stree_stack(&fake_cur_stree_stack, sm);
237 overwrite_sm_state_stree(&cur_stree, sm);
239 if (cond_true_stack) {
240 overwrite_sm_state_stree_stack(&cond_true_stack, sm);
241 overwrite_sm_state_stree_stack(&cond_false_stack, sm);
245 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym)
247 return get_state_stree(cur_stree, owner, name, sym);
250 struct smatch_state *get_state_expr(int owner, struct expression *expr)
252 char *name;
253 struct symbol *sym;
254 struct smatch_state *ret = NULL;
256 expr = strip_expr(expr);
257 name = expr_to_var_sym(expr, &sym);
258 if (!name || !sym)
259 goto free;
260 ret = get_state(owner, name, sym);
261 free:
262 free_string(name);
263 return ret;
266 struct state_list *get_possible_states(int owner, const char *name, struct symbol *sym)
268 struct sm_state *sms;
270 sms = get_sm_state_stree(cur_stree, owner, name, sym);
271 if (sms)
272 return sms->possible;
273 return NULL;
276 struct state_list *get_possible_states_expr(int owner, struct expression *expr)
278 char *name;
279 struct symbol *sym;
280 struct state_list *ret = NULL;
282 expr = strip_expr(expr);
283 name = expr_to_var_sym(expr, &sym);
284 if (!name || !sym)
285 goto free;
286 ret = get_possible_states(owner, name, sym);
287 free:
288 free_string(name);
289 return ret;
292 struct sm_state *get_sm_state(int owner, const char *name, struct symbol *sym)
294 return get_sm_state_stree(cur_stree, owner, name, sym);
297 struct sm_state *get_sm_state_expr(int owner, struct expression *expr)
299 char *name;
300 struct symbol *sym;
301 struct sm_state *ret = NULL;
303 expr = strip_expr(expr);
304 name = expr_to_var_sym(expr, &sym);
305 if (!name || !sym)
306 goto free;
307 ret = get_sm_state(owner, name, sym);
308 free:
309 free_string(name);
310 return ret;
313 void delete_state(int owner, const char *name, struct symbol *sym)
315 delete_state_stree(&cur_stree, owner, name, sym);
316 if (cond_true_stack) {
317 delete_state_stree_stack(&pre_cond_stack, owner, name, sym);
318 delete_state_stree_stack(&cond_true_stack, owner, name, sym);
319 delete_state_stree_stack(&cond_false_stack, owner, name, sym);
323 void delete_state_expr(int owner, struct expression *expr)
325 char *name;
326 struct symbol *sym;
328 expr = strip_expr(expr);
329 name = expr_to_var_sym(expr, &sym);
330 if (!name || !sym)
331 goto free;
332 delete_state(owner, name, sym);
333 free:
334 free_string(name);
337 struct stree *get_all_states_from_stree(int owner, struct stree *source)
339 struct stree *ret = NULL;
340 struct sm_state *tmp;
342 FOR_EACH_SM(source, tmp) {
343 if (tmp->owner == owner)
344 avl_insert(&ret, tmp);
345 } END_FOR_EACH_SM(tmp);
347 return ret;
350 struct stree *get_all_states_stree(int owner)
352 return get_all_states_from_stree(owner, cur_stree);
355 struct stree *__get_cur_stree(void)
357 return cur_stree;
360 int is_reachable(void)
362 if (cur_stree)
363 return 1;
364 return 0;
367 void set_true_false_states(int owner, const char *name, struct symbol *sym,
368 struct smatch_state *true_state,
369 struct smatch_state *false_state)
371 if (option_debug || strcmp(check_name(owner), option_debug_check) == 0) {
372 struct smatch_state *tmp;
374 tmp = get_state(owner, name, sym);
375 printf("%d set_true_false '%s'. Was %s. Now T:%s F:%s\n",
376 get_lineno(), name, show_state(tmp),
377 show_state(true_state), show_state(false_state));
380 if (unreachable())
381 return;
383 if (!cond_false_stack || !cond_true_stack) {
384 printf("Error: missing true/false stacks\n");
385 return;
388 if (true_state) {
389 set_state_stree(&cur_stree, owner, name, sym, true_state);
390 set_state_stree_stack(&cond_true_stack, owner, name, sym, true_state);
392 if (false_state)
393 set_state_stree_stack(&cond_false_stack, owner, name, sym, false_state);
396 void set_true_false_states_expr(int owner, struct expression *expr,
397 struct smatch_state *true_state,
398 struct smatch_state *false_state)
400 char *name;
401 struct symbol *sym;
403 expr = strip_expr(expr);
404 name = expr_to_var_sym(expr, &sym);
405 if (!name || !sym)
406 goto free;
407 set_true_false_states(owner, name, sym, true_state, false_state);
408 free:
409 free_string(name);
412 void __set_true_false_sm(struct sm_state *true_sm, struct sm_state *false_sm)
414 if (unreachable())
415 return;
417 if (!cond_false_stack || !cond_true_stack) {
418 printf("Error: missing true/false stacks\n");
419 return;
422 if (true_sm) {
423 overwrite_sm_state_stree(&cur_stree, true_sm);
424 overwrite_sm_state_stree_stack(&cond_true_stack, true_sm);
426 if (false_sm)
427 overwrite_sm_state_stree_stack(&cond_false_stack, false_sm);
430 void nullify_path(void)
432 free_stree(&cur_stree);
435 void __match_nullify_path_hook(const char *fn, struct expression *expr,
436 void *unused)
438 nullify_path();
442 * At the start of every function we mark the path
443 * as unnull. That way there is always at least one state
444 * in the cur_stree until nullify_path is called. This
445 * is used in merge_slist() for the first null check.
447 void __unnullify_path(void)
449 if (!cur_stree)
450 set_state(-1, "unnull_path", NULL, &true_state);
453 int __path_is_null(void)
455 if (cur_stree)
456 return 0;
457 return 1;
460 static void check_stree_stack_free(struct stree_stack **stack)
462 if (*stack) {
463 sm_msg("smatch internal error: stack not empty");
464 free_stack_and_strees(stack);
468 void save_all_states(void)
470 __add_ptr_list(&backup, cur_stree, 0);
471 cur_stree = NULL;
473 __add_ptr_list(&backup, true_stack, 0);
474 true_stack = NULL;
475 __add_ptr_list(&backup, false_stack, 0);
476 false_stack = NULL;
477 __add_ptr_list(&backup, pre_cond_stack, 0);
478 pre_cond_stack = NULL;
480 __add_ptr_list(&backup, cond_true_stack, 0);
481 cond_true_stack = NULL;
482 __add_ptr_list(&backup, cond_false_stack, 0);
483 cond_false_stack = NULL;
485 __add_ptr_list(&backup, fake_cur_stree_stack, 0);
486 fake_cur_stree_stack = NULL;
488 __add_ptr_list(&backup, break_stack, 0);
489 break_stack = NULL;
490 __add_ptr_list(&backup, switch_stack, 0);
491 switch_stack = NULL;
492 __add_ptr_list(&backup, remaining_cases, 0);
493 remaining_cases = NULL;
494 __add_ptr_list(&backup, default_stack, 0);
495 default_stack = NULL;
496 __add_ptr_list(&backup, continue_stack, 0);
497 continue_stack = NULL;
499 __add_ptr_list(&backup, goto_stack, 0);
500 goto_stack = NULL;
503 static void *pop_backup(void)
505 void *ret;
507 ret = last_ptr_list(backup);
508 delete_ptr_list_last(&backup);
509 return ret;
512 void restore_all_states(void)
514 goto_stack = pop_backup();
516 continue_stack = pop_backup();
517 default_stack = pop_backup();
518 remaining_cases = pop_backup();
519 switch_stack = pop_backup();
520 break_stack = pop_backup();
522 fake_cur_stree_stack = pop_backup();
524 cond_false_stack = pop_backup();
525 cond_true_stack = pop_backup();
527 pre_cond_stack = pop_backup();
528 false_stack = pop_backup();
529 true_stack = pop_backup();
531 cur_stree = pop_backup();
534 void free_goto_stack(void)
536 struct named_stree *named_stree;
538 FOR_EACH_PTR(goto_stack, named_stree) {
539 free_stree(&named_stree->stree);
540 } END_FOR_EACH_PTR(named_stree);
541 __free_ptr_list((struct ptr_list **)&goto_stack);
544 void clear_all_states(void)
546 nullify_path();
547 check_stree_stack_free(&true_stack);
548 check_stree_stack_free(&false_stack);
549 check_stree_stack_free(&pre_cond_stack);
550 check_stree_stack_free(&cond_true_stack);
551 check_stree_stack_free(&cond_false_stack);
552 check_stree_stack_free(&break_stack);
553 check_stree_stack_free(&switch_stack);
554 check_stree_stack_free(&continue_stack);
556 free_goto_stack();
558 free_every_single_sm_state();
561 void __push_cond_stacks(void)
563 push_stree(&cond_true_stack, NULL);
564 push_stree(&cond_false_stack, NULL);
567 struct stree *__copy_cond_true_states(void)
569 struct stree *ret;
571 ret = pop_stree(&cond_true_stack);
572 push_stree(&cond_true_stack, clone_stree(ret));
573 return ret;
576 struct stree *__copy_cond_false_states(void)
578 struct stree *ret;
580 ret = pop_stree(&cond_false_stack);
581 push_stree(&cond_false_stack, clone_stree(ret));
582 return ret;
585 struct stree *__pop_cond_true_stack(void)
587 return pop_stree(&cond_true_stack);
590 struct stree *__pop_cond_false_stack(void)
592 return pop_stree(&cond_false_stack);
596 * This combines the pre cond states with either the true or false states.
597 * For example:
598 * a = kmalloc() ; if (a !! foo(a)
599 * In the pre state a is possibly null. In the true state it is non null.
600 * In the false state it is null. Combine the pre and the false to get
601 * that when we call 'foo', 'a' is null.
603 static void __use_cond_stack(struct stree_stack **stack)
605 struct stree *stree;
607 free_stree(&cur_stree);
609 cur_stree = pop_stree(&pre_cond_stack);
610 push_stree(&pre_cond_stack, clone_stree(cur_stree));
612 stree = pop_stree(stack);
613 overwrite_stree(stree, &cur_stree);
614 push_stree(stack, stree);
617 void __use_pre_cond_states(void)
619 free_stree(&cur_stree);
620 cur_stree = pop_stree(&pre_cond_stack);
623 void __use_cond_true_states(void)
625 __use_cond_stack(&cond_true_stack);
628 void __use_cond_false_states(void)
630 __use_cond_stack(&cond_false_stack);
633 void __negate_cond_stacks(void)
635 struct stree *old_false, *old_true;
637 __use_cond_stack(&cond_false_stack);
638 old_false = pop_stree(&cond_false_stack);
639 old_true = pop_stree(&cond_true_stack);
640 push_stree(&cond_false_stack, old_true);
641 push_stree(&cond_true_stack, old_false);
644 void __and_cond_states(void)
646 and_stree_stack(&cond_true_stack);
647 or_stree_stack(&pre_cond_stack, cur_stree, &cond_false_stack);
650 void __or_cond_states(void)
652 or_stree_stack(&pre_cond_stack, cur_stree, &cond_true_stack);
653 and_stree_stack(&cond_false_stack);
656 void __save_pre_cond_states(void)
658 push_stree(&pre_cond_stack, clone_stree(cur_stree));
661 void __discard_pre_cond_states(void)
663 struct stree *tmp;
665 tmp = pop_stree(&pre_cond_stack);
666 free_stree(&tmp);
669 void __use_cond_states(void)
671 struct stree *pre, *pre_clone, *true_states, *false_states;
673 pre = pop_stree(&pre_cond_stack);
674 pre_clone = clone_stree(pre);
676 true_states = pop_stree(&cond_true_stack);
677 overwrite_stree(true_states, &pre);
678 free_stree(&true_states);
679 /* we use the true states right away */
680 free_stree(&cur_stree);
681 cur_stree = pre;
683 false_states = pop_stree(&cond_false_stack);
684 overwrite_stree(false_states, &pre_clone);
685 free_stree(&false_states);
686 push_stree(&false_stack, pre_clone);
689 void __push_true_states(void)
691 push_stree(&true_stack, clone_stree(cur_stree));
694 void __use_false_states(void)
696 free_stree(&cur_stree);
697 cur_stree = pop_stree(&false_stack);
700 void __discard_false_states(void)
702 struct stree *stree;
704 stree = pop_stree(&false_stack);
705 free_stree(&stree);
708 void __merge_false_states(void)
710 struct stree *stree;
712 stree = pop_stree(&false_stack);
713 merge_stree(&cur_stree, stree);
714 free_stree(&stree);
717 void __merge_true_states(void)
719 struct stree *stree;
721 stree = pop_stree(&true_stack);
722 merge_stree(&cur_stree, stree);
723 free_stree(&stree);
726 void __push_continues(void)
728 push_stree(&continue_stack, NULL);
731 void __discard_continues(void)
733 struct stree *stree;
735 stree = pop_stree(&continue_stack);
736 free_stree(&stree);
739 void __process_continues(void)
741 struct stree *stree;
743 stree = pop_stree(&continue_stack);
744 if (!stree)
745 stree = clone_stree(cur_stree);
746 else
747 merge_stree(&stree, cur_stree);
749 push_stree(&continue_stack, stree);
752 static int top_stree_empty(struct stree_stack **stack)
754 struct stree *tmp;
755 int empty = 0;
757 tmp = pop_stree(stack);
758 if (!tmp)
759 empty = 1;
760 push_stree(stack, tmp);
761 return empty;
764 /* a silly loop does this: while(i--) { return; } */
765 void __warn_on_silly_pre_loops(void)
767 if (!__path_is_null())
768 return;
769 if (!top_stree_empty(&continue_stack))
770 return;
771 if (!top_stree_empty(&break_stack))
772 return;
773 /* if the path was nullified before the loop, then we already
774 printed an error earlier */
775 if (top_stree_empty(&false_stack))
776 return;
777 sm_msg("info: loop could be replaced with if statement.");
780 void __merge_continues(void)
782 struct stree *stree;
784 stree = pop_stree(&continue_stack);
785 merge_stree(&cur_stree, stree);
786 free_stree(&stree);
789 void __push_breaks(void)
791 push_stree(&break_stack, NULL);
794 void __process_breaks(void)
796 struct stree *stree;
798 stree = pop_stree(&break_stack);
799 if (!stree)
800 stree = clone_stree(cur_stree);
801 else
802 merge_stree(&stree, cur_stree);
804 push_stree(&break_stack, stree);
807 int __has_breaks(void)
809 struct stree *stree;
810 int ret;
812 stree = pop_stree(&break_stack);
813 ret = !!stree;
814 push_stree(&break_stack, stree);
815 return ret;
818 void __merge_breaks(void)
820 struct stree *stree;
822 stree = pop_stree(&break_stack);
823 merge_stree(&cur_stree, stree);
824 free_stree(&stree);
827 void __use_breaks(void)
829 free_stree(&cur_stree);
830 cur_stree = pop_stree(&break_stack);
833 void __save_switch_states(struct expression *switch_expr)
835 push_rl(&remaining_cases, __get_implied_values(switch_expr));
836 push_stree(&switch_stack, clone_stree(cur_stree));
839 void __merge_switches(struct expression *switch_expr, struct expression *case_expr)
841 struct stree *stree;
842 struct stree *implied_stree;
844 stree = pop_stree(&switch_stack);
845 implied_stree = __implied_case_stree(switch_expr, case_expr, &remaining_cases, &stree);
846 merge_stree(&cur_stree, implied_stree);
847 free_stree(&implied_stree);
848 push_stree(&switch_stack, stree);
851 void __discard_switches(void)
853 struct stree *stree;
855 pop_rl(&remaining_cases);
856 stree = pop_stree(&switch_stack);
857 free_stree(&stree);
860 void __push_default(void)
862 push_stree(&default_stack, NULL);
865 void __set_default(void)
867 set_state_stree_stack(&default_stack, 0, "has_default", NULL, &true_state);
870 int __pop_default(void)
872 struct stree *stree;
874 stree = pop_stree(&default_stack);
875 if (stree) {
876 free_stree(&stree);
877 return 1;
879 return 0;
882 static struct named_stree *alloc_named_stree(const char *name, struct stree *stree)
884 struct named_stree *named_stree = __alloc_named_stree(0);
886 named_stree->name = (char *)name;
887 named_stree->stree = stree;
888 return named_stree;
891 void __save_gotos(const char *name)
893 struct stree **stree;
894 struct stree *clone;
896 stree = get_named_stree(goto_stack, name);
897 if (stree) {
898 merge_stree(stree, cur_stree);
899 return;
900 } else {
901 struct named_stree *named_stree;
903 clone = clone_stree(cur_stree);
904 named_stree = alloc_named_stree(name, clone);
905 add_ptr_list(&goto_stack, named_stree);
909 void __merge_gotos(const char *name)
911 struct stree **stree;
913 stree = get_named_stree(goto_stack, name);
914 if (stree)
915 merge_stree(&cur_stree, *stree);