type: fix "strange non-value function or array" warnings
[smatch.git] / smatch_states.c
blob086585f20ab928b414c515780f8438a7a32a7d5b
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 void __set_sm_cur_stree(struct sm_state *sm)
229 if (read_only)
230 sm_msg("Smatch Internal Error: cur_stree is read only.");
232 if (option_debug ||
233 strcmp(check_name(sm->owner), option_debug_check) == 0) {
234 struct smatch_state *s;
236 s = get_state(sm->owner, sm->name, sm->sym);
237 if (!s)
238 sm_msg("new state. name='%s' [%s] %s",
239 sm->name, check_name(sm->owner),
240 show_state(sm->state));
241 else
242 sm_msg("state change name='%s' [%s] %s => %s",
243 sm->name, check_name(sm->owner), show_state(s),
244 show_state(sm->state));
247 if (unreachable())
248 return;
250 overwrite_sm_state_stree(&cur_stree, sm);
253 void __set_sm_fake_stree(struct sm_state *sm)
255 if (read_only)
256 sm_msg("Smatch Internal Error: cur_stree is read only.");
258 if (option_debug ||
259 strcmp(check_name(sm->owner), option_debug_check) == 0) {
260 struct smatch_state *s;
262 s = get_state(sm->owner, sm->name, sm->sym);
263 if (!s)
264 sm_msg("new state. name='%s' [%s] %s",
265 sm->name, check_name(sm->owner),
266 show_state(sm->state));
267 else
268 sm_msg("state change name='%s' [%s] %s => %s",
269 sm->name, check_name(sm->owner), show_state(s),
270 show_state(sm->state));
273 if (unreachable())
274 return;
276 overwrite_sm_state_stree_stack(&fake_cur_stree_stack, sm);
279 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym)
281 __get_state_hook(owner, name, sym);
283 return get_state_stree(cur_stree, owner, name, sym);
286 struct smatch_state *get_state_expr(int owner, struct expression *expr)
288 char *name;
289 struct symbol *sym;
290 struct smatch_state *ret = NULL;
292 expr = strip_expr(expr);
293 name = expr_to_var_sym(expr, &sym);
294 if (!name || !sym)
295 goto free;
296 ret = get_state(owner, name, sym);
297 free:
298 free_string(name);
299 return ret;
302 struct state_list *get_possible_states(int owner, const char *name, struct symbol *sym)
304 struct sm_state *sms;
306 sms = get_sm_state_stree(cur_stree, owner, name, sym);
307 if (sms)
308 return sms->possible;
309 return NULL;
312 struct state_list *get_possible_states_expr(int owner, struct expression *expr)
314 char *name;
315 struct symbol *sym;
316 struct state_list *ret = NULL;
318 expr = strip_expr(expr);
319 name = expr_to_var_sym(expr, &sym);
320 if (!name || !sym)
321 goto free;
322 ret = get_possible_states(owner, name, sym);
323 free:
324 free_string(name);
325 return ret;
328 struct sm_state *get_sm_state(int owner, const char *name, struct symbol *sym)
330 return get_sm_state_stree(cur_stree, owner, name, sym);
333 struct sm_state *get_sm_state_expr(int owner, struct expression *expr)
335 char *name;
336 struct symbol *sym;
337 struct sm_state *ret = NULL;
339 expr = strip_expr(expr);
340 name = expr_to_var_sym(expr, &sym);
341 if (!name || !sym)
342 goto free;
343 ret = get_sm_state(owner, name, sym);
344 free:
345 free_string(name);
346 return ret;
349 void delete_state(int owner, const char *name, struct symbol *sym)
351 delete_state_stree(&cur_stree, owner, name, sym);
352 if (cond_true_stack) {
353 delete_state_stree_stack(&pre_cond_stack, owner, name, sym);
354 delete_state_stree_stack(&cond_true_stack, owner, name, sym);
355 delete_state_stree_stack(&cond_false_stack, owner, name, sym);
359 void delete_state_expr(int owner, struct expression *expr)
361 char *name;
362 struct symbol *sym;
364 expr = strip_expr(expr);
365 name = expr_to_var_sym(expr, &sym);
366 if (!name || !sym)
367 goto free;
368 delete_state(owner, name, sym);
369 free:
370 free_string(name);
373 struct stree *get_all_states_from_stree(int owner, struct stree *source)
375 struct stree *ret = NULL;
376 struct sm_state *tmp;
378 FOR_EACH_SM(source, tmp) {
379 if (tmp->owner == owner)
380 avl_insert(&ret, tmp);
381 } END_FOR_EACH_SM(tmp);
383 return ret;
386 struct stree *get_all_states_stree(int owner)
388 return get_all_states_from_stree(owner, cur_stree);
391 struct stree *__get_cur_stree(void)
393 return cur_stree;
396 int is_reachable(void)
398 if (cur_stree)
399 return 1;
400 return 0;
403 void set_true_false_states(int owner, const char *name, struct symbol *sym,
404 struct smatch_state *true_state,
405 struct smatch_state *false_state)
407 if (read_only)
408 sm_msg("Smatch Internal Error: cur_stree is read only.");
410 if (option_debug || strcmp(check_name(owner), option_debug_check) == 0) {
411 struct smatch_state *tmp;
413 tmp = get_state(owner, name, sym);
414 sm_msg("set_true_false '%s'. Was %s. Now T:%s F:%s\n",
415 name, show_state(tmp),
416 show_state(true_state), show_state(false_state));
419 if (unreachable())
420 return;
422 if (!cond_false_stack || !cond_true_stack) {
423 printf("Error: missing true/false stacks\n");
424 return;
427 if (true_state)
428 set_state_stree_stack(&cond_true_stack, owner, name, sym, true_state);
429 if (false_state)
430 set_state_stree_stack(&cond_false_stack, owner, name, sym, false_state);
433 void set_true_false_states_expr(int owner, struct expression *expr,
434 struct smatch_state *true_state,
435 struct smatch_state *false_state)
437 char *name;
438 struct symbol *sym;
440 expr = strip_expr(expr);
441 name = expr_to_var_sym(expr, &sym);
442 if (!name || !sym)
443 goto free;
444 set_true_false_states(owner, name, sym, true_state, false_state);
445 free:
446 free_string(name);
449 void __set_true_false_sm(struct sm_state *true_sm, struct sm_state *false_sm)
451 if (unreachable())
452 return;
454 if (!cond_false_stack || !cond_true_stack) {
455 printf("Error: missing true/false stacks\n");
456 return;
459 if (true_sm)
460 overwrite_sm_state_stree_stack(&cond_true_stack, true_sm);
461 if (false_sm)
462 overwrite_sm_state_stree_stack(&cond_false_stack, false_sm);
465 void __set_true_false_sm_if_not_already_set(struct sm_state *true_sm, struct sm_state *false_sm)
467 if (unreachable())
468 return;
470 if (!cond_false_stack || !cond_true_stack) {
471 printf("Error: missing true/false stacks\n");
472 return;
475 if (true_sm)
476 set_sm_state_stree_stack_if_not_already_set(&cond_true_stack, true_sm);
477 if (false_sm)
478 set_sm_state_stree_stack_if_not_already_set(&cond_false_stack, false_sm);
481 void nullify_path(void)
483 free_stree(&cur_stree);
486 void __match_nullify_path_hook(const char *fn, struct expression *expr,
487 void *unused)
489 nullify_path();
493 * At the start of every function we mark the path
494 * as unnull. That way there is always at least one state
495 * in the cur_stree until nullify_path is called. This
496 * is used in merge_slist() for the first null check.
498 void __unnullify_path(void)
500 if (!cur_stree)
501 set_state(-1, "unnull_path", NULL, &true_state);
504 int __path_is_null(void)
506 if (cur_stree)
507 return 0;
508 return 1;
511 static void check_stree_stack_free(struct stree_stack **stack)
513 if (*stack) {
514 sm_msg("smatch internal error: stack not empty");
515 free_stack_and_strees(stack);
519 void save_all_states(void)
521 __add_ptr_list(&backup, cur_stree, 0);
522 cur_stree = NULL;
524 __add_ptr_list(&backup, true_stack, 0);
525 true_stack = NULL;
526 __add_ptr_list(&backup, false_stack, 0);
527 false_stack = NULL;
528 __add_ptr_list(&backup, pre_cond_stack, 0);
529 pre_cond_stack = NULL;
531 __add_ptr_list(&backup, cond_true_stack, 0);
532 cond_true_stack = NULL;
533 __add_ptr_list(&backup, cond_false_stack, 0);
534 cond_false_stack = NULL;
536 __add_ptr_list(&backup, fake_cur_stree_stack, 0);
537 fake_cur_stree_stack = NULL;
539 __add_ptr_list(&backup, break_stack, 0);
540 break_stack = NULL;
541 __add_ptr_list(&backup, switch_stack, 0);
542 switch_stack = NULL;
543 __add_ptr_list(&backup, remaining_cases, 0);
544 remaining_cases = NULL;
545 __add_ptr_list(&backup, default_stack, 0);
546 default_stack = NULL;
547 __add_ptr_list(&backup, continue_stack, 0);
548 continue_stack = NULL;
550 __add_ptr_list(&backup, goto_stack, 0);
551 goto_stack = NULL;
554 static void *pop_backup(void)
556 void *ret;
558 ret = last_ptr_list(backup);
559 delete_ptr_list_last(&backup);
560 return ret;
563 void restore_all_states(void)
565 goto_stack = pop_backup();
567 continue_stack = pop_backup();
568 default_stack = pop_backup();
569 remaining_cases = pop_backup();
570 switch_stack = pop_backup();
571 break_stack = pop_backup();
573 fake_cur_stree_stack = pop_backup();
575 cond_false_stack = pop_backup();
576 cond_true_stack = pop_backup();
578 pre_cond_stack = pop_backup();
579 false_stack = pop_backup();
580 true_stack = pop_backup();
582 cur_stree = pop_backup();
585 void free_goto_stack(void)
587 struct named_stree *named_stree;
589 FOR_EACH_PTR(goto_stack, named_stree) {
590 free_stree(&named_stree->stree);
591 } END_FOR_EACH_PTR(named_stree);
592 __free_ptr_list((struct ptr_list **)&goto_stack);
595 void clear_all_states(void)
597 nullify_path();
598 check_stree_stack_free(&true_stack);
599 check_stree_stack_free(&false_stack);
600 check_stree_stack_free(&pre_cond_stack);
601 check_stree_stack_free(&cond_true_stack);
602 check_stree_stack_free(&cond_false_stack);
603 check_stree_stack_free(&break_stack);
604 check_stree_stack_free(&switch_stack);
605 check_stree_stack_free(&continue_stack);
607 free_goto_stack();
609 free_every_single_sm_state();
612 void __push_cond_stacks(void)
614 push_stree(&cond_true_stack, NULL);
615 push_stree(&cond_false_stack, NULL);
618 struct stree *__copy_cond_true_states(void)
620 struct stree *ret;
622 ret = pop_stree(&cond_true_stack);
623 push_stree(&cond_true_stack, clone_stree(ret));
624 return ret;
627 struct stree *__copy_cond_false_states(void)
629 struct stree *ret;
631 ret = pop_stree(&cond_false_stack);
632 push_stree(&cond_false_stack, clone_stree(ret));
633 return ret;
636 struct stree *__pop_cond_true_stack(void)
638 return pop_stree(&cond_true_stack);
641 struct stree *__pop_cond_false_stack(void)
643 return pop_stree(&cond_false_stack);
647 * This combines the pre cond states with either the true or false states.
648 * For example:
649 * a = kmalloc() ; if (a !! foo(a)
650 * In the pre state a is possibly null. In the true state it is non null.
651 * In the false state it is null. Combine the pre and the false to get
652 * that when we call 'foo', 'a' is null.
654 static void __use_cond_stack(struct stree_stack **stack)
656 struct stree *stree;
658 free_stree(&cur_stree);
660 cur_stree = pop_stree(&pre_cond_stack);
661 push_stree(&pre_cond_stack, clone_stree(cur_stree));
663 stree = pop_stree(stack);
664 overwrite_stree(stree, &cur_stree);
665 push_stree(stack, stree);
668 void __use_pre_cond_states(void)
670 free_stree(&cur_stree);
671 cur_stree = pop_stree(&pre_cond_stack);
674 void __use_cond_true_states(void)
676 __use_cond_stack(&cond_true_stack);
679 void __use_cond_false_states(void)
681 __use_cond_stack(&cond_false_stack);
684 void __negate_cond_stacks(void)
686 struct stree *old_false, *old_true;
688 __use_cond_stack(&cond_false_stack);
689 old_false = pop_stree(&cond_false_stack);
690 old_true = pop_stree(&cond_true_stack);
691 push_stree(&cond_false_stack, old_true);
692 push_stree(&cond_true_stack, old_false);
695 void __and_cond_states(void)
697 and_stree_stack(&cond_true_stack);
698 or_stree_stack(&pre_cond_stack, cur_stree, &cond_false_stack);
701 void __or_cond_states(void)
703 or_stree_stack(&pre_cond_stack, cur_stree, &cond_true_stack);
704 and_stree_stack(&cond_false_stack);
707 void __save_pre_cond_states(void)
709 push_stree(&pre_cond_stack, clone_stree(cur_stree));
712 void __discard_pre_cond_states(void)
714 struct stree *tmp;
716 tmp = pop_stree(&pre_cond_stack);
717 free_stree(&tmp);
720 void __use_cond_states(void)
722 struct stree *pre, *pre_clone, *true_states, *false_states;
724 pre = pop_stree(&pre_cond_stack);
725 pre_clone = clone_stree(pre);
727 true_states = pop_stree(&cond_true_stack);
728 overwrite_stree(true_states, &pre);
729 free_stree(&true_states);
730 /* we use the true states right away */
731 free_stree(&cur_stree);
732 cur_stree = pre;
734 false_states = pop_stree(&cond_false_stack);
735 overwrite_stree(false_states, &pre_clone);
736 free_stree(&false_states);
737 push_stree(&false_stack, pre_clone);
740 void __push_true_states(void)
742 push_stree(&true_stack, clone_stree(cur_stree));
745 void __use_false_states(void)
747 free_stree(&cur_stree);
748 cur_stree = pop_stree(&false_stack);
751 void __discard_false_states(void)
753 struct stree *stree;
755 stree = pop_stree(&false_stack);
756 free_stree(&stree);
759 void __merge_false_states(void)
761 struct stree *stree;
763 stree = pop_stree(&false_stack);
764 merge_stree(&cur_stree, stree);
765 free_stree(&stree);
768 void __merge_true_states(void)
770 struct stree *stree;
772 stree = pop_stree(&true_stack);
773 merge_stree(&cur_stree, stree);
774 free_stree(&stree);
777 void __push_continues(void)
779 push_stree(&continue_stack, NULL);
782 void __discard_continues(void)
784 struct stree *stree;
786 stree = pop_stree(&continue_stack);
787 free_stree(&stree);
790 void __process_continues(void)
792 struct stree *stree;
794 stree = pop_stree(&continue_stack);
795 if (!stree)
796 stree = clone_stree(cur_stree);
797 else
798 merge_stree(&stree, cur_stree);
800 push_stree(&continue_stack, stree);
803 void __merge_continues(void)
805 struct stree *stree;
807 stree = pop_stree(&continue_stack);
808 merge_stree(&cur_stree, stree);
809 free_stree(&stree);
812 void __push_breaks(void)
814 push_stree(&break_stack, NULL);
817 void __process_breaks(void)
819 struct stree *stree;
821 stree = pop_stree(&break_stack);
822 if (!stree)
823 stree = clone_stree(cur_stree);
824 else
825 merge_stree(&stree, cur_stree);
827 push_stree(&break_stack, stree);
830 int __has_breaks(void)
832 struct stree *stree;
833 int ret;
835 stree = pop_stree(&break_stack);
836 ret = !!stree;
837 push_stree(&break_stack, stree);
838 return ret;
841 void __merge_breaks(void)
843 struct stree *stree;
845 stree = pop_stree(&break_stack);
846 merge_stree(&cur_stree, stree);
847 free_stree(&stree);
850 void __use_breaks(void)
852 free_stree(&cur_stree);
853 cur_stree = pop_stree(&break_stack);
856 void __save_switch_states(struct expression *switch_expr)
858 push_rl(&remaining_cases, __get_implied_values(switch_expr));
859 push_stree(&switch_stack, clone_stree(cur_stree));
862 void __merge_switches(struct expression *switch_expr, struct expression *case_expr, struct expression *case_to)
864 struct stree *stree;
865 struct stree *implied_stree;
867 stree = pop_stree(&switch_stack);
868 implied_stree = __implied_case_stree(switch_expr, case_expr, case_to, &remaining_cases, &stree);
869 merge_stree(&cur_stree, implied_stree);
870 free_stree(&implied_stree);
871 push_stree(&switch_stack, stree);
874 void __discard_switches(void)
876 struct stree *stree;
878 pop_rl(&remaining_cases);
879 stree = pop_stree(&switch_stack);
880 free_stree(&stree);
883 void __push_default(void)
885 push_stree(&default_stack, NULL);
888 void __set_default(void)
890 set_state_stree_stack(&default_stack, 0, "has_default", NULL, &true_state);
893 int __pop_default(void)
895 struct stree *stree;
897 stree = pop_stree(&default_stack);
898 if (stree) {
899 free_stree(&stree);
900 return 1;
902 return 0;
905 static struct named_stree *alloc_named_stree(const char *name, struct stree *stree)
907 struct named_stree *named_stree = __alloc_named_stree(0);
909 named_stree->name = (char *)name;
910 named_stree->stree = stree;
911 return named_stree;
914 void __save_gotos(const char *name)
916 struct stree **stree;
917 struct stree *clone;
919 stree = get_named_stree(goto_stack, name);
920 if (stree) {
921 merge_stree(stree, cur_stree);
922 return;
923 } else {
924 struct named_stree *named_stree;
926 clone = clone_stree(cur_stree);
927 named_stree = alloc_named_stree(name, clone);
928 add_ptr_list(&goto_stack, named_stree);
932 void __merge_gotos(const char *name)
934 struct stree **stree;
936 stree = get_named_stree(goto_stack, name);
937 if (stree)
938 merge_stree(&cur_stree, *stree);