math: handle logical expressions
[smatch.git] / smatch_states.c
blob36827ff6144f6dcfc281090023111ed51725c868
1 /*
2 * sparse/smatch_states.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * You have a lists of states. kernel = locked, foo = NULL, ...
12 * When you hit an if {} else {} statement then you swap the list
13 * of states for a different list of states. The lists are stored
14 * on stacks.
16 * At the beginning of this file there are list of the stacks that
17 * we use. Each function in this file does something to one of
18 * of the stacks.
20 * So the smatch_flow.c understands code but it doesn't understand states.
21 * smatch_flow calls functions in this file. This file calls functions
22 * in smatch_slist.c which just has boring generic plumbing for handling
23 * state lists. But really it's this file where all the magic happens.
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include "smatch.h"
29 #include "smatch_slist.h"
30 #include "smatch_extra.h"
32 struct smatch_state undefined = { .name = "undefined" };
33 struct smatch_state merged = { .name = "merged" };
34 struct smatch_state true_state = { .name = "true" };
35 struct smatch_state false_state = { .name = "false" };
37 static struct state_list *cur_slist; /* current states */
39 static struct state_list_stack *true_stack; /* states after a t/f branch */
40 static struct state_list_stack *false_stack;
41 static struct state_list_stack *pre_cond_stack; /* states before a t/f branch */
43 static struct state_list_stack *cond_true_stack; /* states affected by a branch */
44 static struct state_list_stack *cond_false_stack;
46 static struct state_list_stack *fake_cur_slist_stack;
48 static struct state_list_stack *break_stack;
49 static struct state_list_stack *switch_stack;
50 static struct range_list_stack *remaining_cases;
51 static struct state_list_stack *default_stack;
52 static struct state_list_stack *continue_stack;
54 static struct named_stack *goto_stack;
56 struct state_list_stack *implied_pools;
58 int option_debug;
60 void __print_cur_slist(void)
62 __print_slist(cur_slist);
65 static int in_declarations_bit(void)
67 struct statement *tmp;
69 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
70 if (tmp->type == STMT_DECLARATION)
71 return 1;
72 return 0;
73 } END_FOR_EACH_PTR_REVERSE(tmp);
74 return 0;
77 int unreachable(void)
79 static int reset_warnings = 1;
81 if (cur_slist) {
82 reset_warnings = 1;
83 return 0;
86 if (in_declarations_bit())
87 return 1;
89 /* option spammy turns on a noisier version of this */
90 if (reset_warnings && !option_spammy)
91 sm_msg("info: ignoring unreachable code.");
92 reset_warnings = 0;
93 return 1;
96 struct sm_state *set_state(int owner, const char *name, struct symbol *sym, struct smatch_state *state)
98 struct sm_state *ret;
100 if (!name)
101 return NULL;
103 if (option_debug) {
104 struct smatch_state *s;
106 s = get_state(owner, name, sym);
107 if (!s)
108 printf("%d new state. name='%s' [%s] %s\n",
109 get_lineno(), name, check_name(owner), show_state(state));
110 else
111 printf("%d state change name='%s' [%s] %s => %s\n",
112 get_lineno(), name, check_name(owner), show_state(s),
113 show_state(state));
116 if (owner != -1 && unreachable())
117 return NULL;
119 __use_default_modification_hook(owner, name);
121 if (fake_cur_slist_stack)
122 set_state_stack(&fake_cur_slist_stack, owner, name, sym, state);
124 ret = set_state_slist(&cur_slist, owner, name, sym, state);
126 if (cond_true_stack) {
127 set_state_stack(&cond_true_stack, owner, name, sym, state);
128 set_state_stack(&cond_false_stack, owner, name, sym, state);
130 return ret;
133 struct sm_state *set_state_expr(int owner, struct expression *expr, struct smatch_state *state)
135 char *name;
136 struct symbol *sym;
137 struct sm_state *ret = NULL;
139 expr = strip_expr(expr);
140 name = get_variable_from_expr(expr, &sym);
141 if (!name || !sym)
142 goto free;
143 ret = set_state(owner, name, sym, state);
144 free:
145 free_string(name);
146 return ret;
149 void __push_fake_cur_slist()
151 push_slist(&fake_cur_slist_stack, NULL);
152 __save_pre_cond_states();
155 struct state_list *__pop_fake_cur_slist()
157 __use_pre_cond_states();
158 return pop_slist(&fake_cur_slist_stack);
161 void __set_sm(struct sm_state *sm)
163 if (option_debug) {
164 struct smatch_state *s;
166 s = get_state(sm->owner, sm->name, sm->sym);
167 if (!s)
168 printf("%d new state. name='%s' [%s] %s\n",
169 get_lineno(), sm->name, check_name(sm->owner),
170 show_state(sm->state));
171 else
172 printf("%d state change name='%s' [%s] %s => %s\n",
173 get_lineno(), sm->name, check_name(sm->owner), show_state(s),
174 show_state(sm->state));
177 if (unreachable())
178 return;
180 overwrite_sm_state(&cur_slist, sm);
182 if (cond_true_stack) {
183 overwrite_sm_state_stack(&cond_true_stack, sm);
184 overwrite_sm_state_stack(&cond_false_stack, sm);
188 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym)
190 return get_state_slist(cur_slist, owner, name, sym);
193 struct smatch_state *get_state_expr(int owner, struct expression *expr)
195 char *name;
196 struct symbol *sym;
197 struct smatch_state *ret = NULL;
199 expr = strip_expr(expr);
200 name = get_variable_from_expr(expr, &sym);
201 if (!name || !sym)
202 goto free;
203 ret = get_state(owner, name, sym);
204 free:
205 free_string(name);
206 return ret;
209 struct state_list *get_possible_states(int owner, const char *name, struct symbol *sym)
211 struct sm_state *sms;
213 sms = get_sm_state_slist(cur_slist, owner, name, sym);
214 if (sms)
215 return sms->possible;
216 return NULL;
219 struct state_list *get_possible_states_expr(int owner, struct expression *expr)
221 char *name;
222 struct symbol *sym;
223 struct state_list *ret = NULL;
225 expr = strip_expr(expr);
226 name = get_variable_from_expr(expr, &sym);
227 if (!name || !sym)
228 goto free;
229 ret = get_possible_states(owner, name, sym);
230 free:
231 free_string(name);
232 return ret;
235 struct sm_state *get_sm_state(int owner, const char *name, struct symbol *sym)
237 return get_sm_state_slist(cur_slist, owner, name, sym);
240 struct sm_state *get_sm_state_expr(int owner, struct expression *expr)
242 char *name;
243 struct symbol *sym;
244 struct sm_state *ret = NULL;
246 expr = strip_expr(expr);
247 name = get_variable_from_expr(expr, &sym);
248 if (!name || !sym)
249 goto free;
250 ret = get_sm_state(owner, name, sym);
251 free:
252 free_string(name);
253 return ret;
256 void delete_state(int owner, const char *name, struct symbol *sym)
258 delete_state_slist(&cur_slist, owner, name, sym);
259 if (cond_true_stack) {
260 delete_state_stack(&pre_cond_stack, owner, name, sym);
261 delete_state_stack(&cond_true_stack, owner, name, sym);
262 delete_state_stack(&cond_false_stack, owner, name, sym);
266 void delete_state_expr(int owner, struct expression *expr)
268 char *name;
269 struct symbol *sym;
271 expr = strip_expr(expr);
272 name = get_variable_from_expr(expr, &sym);
273 if (!name || !sym)
274 goto free;
275 delete_state(owner, name, sym);
276 free:
277 free_string(name);
280 struct state_list *get_all_states(int owner)
282 struct state_list *slist = NULL;
283 struct sm_state *tmp;
285 FOR_EACH_PTR(cur_slist, tmp) {
286 if (tmp->owner == owner)
287 add_ptr_list(&slist, tmp);
288 } END_FOR_EACH_PTR(tmp);
290 return slist;
293 int is_reachable(void)
295 if (cur_slist)
296 return 1;
297 return 0;
300 struct state_list *__get_cur_slist(void)
302 return cur_slist;
305 void set_true_false_states(int owner, const char *name, struct symbol *sym,
306 struct smatch_state *true_state,
307 struct smatch_state *false_state)
309 if (option_debug) {
310 struct smatch_state *tmp;
312 tmp = get_state(owner, name, sym);
313 sm_debug("%d set_true_false '%s'. Was %s. Now T:%s F:%s\n",
314 get_lineno(), name, show_state(tmp),
315 show_state(true_state), show_state(false_state));
318 if (unreachable())
319 return;
321 if (!cond_false_stack || !cond_true_stack) {
322 printf("Error: missing true/false stacks\n");
323 return;
326 __use_default_modification_hook(owner, name);
328 if (true_state) {
329 set_state_slist(&cur_slist, owner, name, sym, true_state);
330 set_state_stack(&cond_true_stack, owner, name, sym, true_state);
332 if (false_state)
333 set_state_stack(&cond_false_stack, owner, name, sym, false_state);
336 void set_true_false_states_expr(int owner, struct expression *expr,
337 struct smatch_state *true_state,
338 struct smatch_state *false_state)
340 char *name;
341 struct symbol *sym;
343 expr = strip_expr(expr);
344 name = get_variable_from_expr(expr, &sym);
345 if (!name || !sym)
346 goto free;
347 set_true_false_states(owner, name, sym, true_state, false_state);
348 free:
349 free_string(name);
352 void __set_true_false_sm(struct sm_state *true_sm, struct sm_state *false_sm)
354 if (unreachable())
355 return;
357 if (!cond_false_stack || !cond_true_stack) {
358 printf("Error: missing true/false stacks\n");
359 return;
362 if (true_sm) {
363 overwrite_sm_state(&cur_slist, true_sm);
364 overwrite_sm_state_stack(&cond_true_stack, true_sm);
366 if (false_sm)
367 overwrite_sm_state_stack(&cond_false_stack, false_sm);
370 void nullify_path(void)
372 free_slist(&cur_slist);
375 void __match_nullify_path_hook(const char *fn, struct expression *expr,
376 void *unused)
378 nullify_path();
382 * At the start of every function we mark the path
383 * as unnull. That way there is always at least one state
384 * in the cur_slist until nullify_path is called. This
385 * is used in merge_slist() for the first null check.
387 void __unnullify_path(void)
389 set_state(-1, "unnull_path", NULL, &true_state);
392 int __path_is_null(void)
394 if (cur_slist)
395 return 0;
396 return 1;
399 static void check_stack_free(struct state_list_stack **stack)
401 if (*stack) {
402 sm_msg("smatch internal error: stack not empty");
403 free_stack_and_slists(stack);
407 void clear_all_states(void)
409 struct named_slist *named_slist;
411 nullify_path();
412 check_stack_free(&true_stack);
413 check_stack_free(&false_stack);
414 check_stack_free(&pre_cond_stack);
415 check_stack_free(&cond_true_stack);
416 check_stack_free(&cond_false_stack);
417 check_stack_free(&break_stack);
418 check_stack_free(&switch_stack);
419 check_stack_free(&continue_stack);
420 free_stack_and_slists(&implied_pools);
422 FOR_EACH_PTR(goto_stack, named_slist) {
423 free_slist(&named_slist->slist);
424 } END_FOR_EACH_PTR(named_slist);
425 __free_ptr_list((struct ptr_list **)&goto_stack);
426 free_every_single_sm_state();
429 void __push_cond_stacks(void)
431 push_slist(&cond_true_stack, NULL);
432 push_slist(&cond_false_stack, NULL);
435 struct state_list *__copy_cond_true_states(void)
437 struct state_list *ret;
439 ret = pop_slist(&cond_true_stack);
440 push_slist(&cond_true_stack, clone_slist(ret));
441 return ret;
444 struct state_list *__copy_cond_false_states(void)
446 struct state_list *ret;
448 ret = pop_slist(&cond_false_stack);
449 push_slist(&cond_false_stack, clone_slist(ret));
450 return ret;
453 struct state_list *__pop_cond_true_stack(void)
455 return pop_slist(&cond_true_stack);
458 struct state_list *__pop_cond_false_stack(void)
460 return pop_slist(&cond_false_stack);
464 * This combines the pre cond states with either the true or false states.
465 * For example:
466 * a = kmalloc() ; if (a !! foo(a)
467 * In the pre state a is possibly null. In the true state it is non null.
468 * In the false state it is null. Combine the pre and the false to get
469 * that when we call 'foo', 'a' is null.
471 static void __use_cond_stack(struct state_list_stack **stack)
473 struct state_list *slist;
475 free_slist(&cur_slist);
477 cur_slist = pop_slist(&pre_cond_stack);
478 push_slist(&pre_cond_stack, clone_slist(cur_slist));
480 slist = pop_slist(stack);
481 overwrite_slist(slist, &cur_slist);
482 push_slist(stack, slist);
485 void __use_pre_cond_states(void)
487 free_slist(&cur_slist);
488 cur_slist = pop_slist(&pre_cond_stack);
491 void __use_cond_true_states(void)
493 __use_cond_stack(&cond_true_stack);
496 void __use_cond_false_states(void)
498 __use_cond_stack(&cond_false_stack);
501 void __negate_cond_stacks(void)
503 struct state_list *old_false, *old_true;
505 __use_cond_stack(&cond_false_stack);
506 old_false = pop_slist(&cond_false_stack);
507 old_true = pop_slist(&cond_true_stack);
508 push_slist(&cond_false_stack, old_true);
509 push_slist(&cond_true_stack, old_false);
512 void __and_cond_states(void)
514 and_slist_stack(&cond_true_stack);
515 or_slist_stack(&pre_cond_stack, cur_slist, &cond_false_stack);
518 void __or_cond_states(void)
520 or_slist_stack(&pre_cond_stack, cur_slist, &cond_true_stack);
521 and_slist_stack(&cond_false_stack);
524 void __save_pre_cond_states(void)
526 push_slist(&pre_cond_stack, clone_slist(cur_slist));
529 void __discard_pre_cond_states(void)
531 struct state_list *tmp;
533 tmp = pop_slist(&pre_cond_stack);
534 free_slist(&tmp);
537 void __use_cond_states(void)
539 struct state_list *pre, *pre_clone, *true_states, *false_states;
541 pre = pop_slist(&pre_cond_stack);
542 pre_clone = clone_slist(pre);
544 true_states = pop_slist(&cond_true_stack);
545 overwrite_slist(true_states, &pre);
546 /* we use the true states right away */
547 free_slist(&cur_slist);
548 cur_slist = pre;
550 false_states = pop_slist(&cond_false_stack);
551 overwrite_slist(false_states, &pre_clone);
552 push_slist(&false_stack, pre_clone);
555 void __push_true_states(void)
557 push_slist(&true_stack, clone_slist(cur_slist));
560 void __use_false_states(void)
562 free_slist(&cur_slist);
563 cur_slist = pop_slist(&false_stack);
566 void __discard_false_states(void)
568 struct state_list *slist;
570 slist = pop_slist(&false_stack);
571 free_slist(&slist);
574 void __merge_false_states(void)
576 struct state_list *slist;
578 slist = pop_slist(&false_stack);
579 merge_slist(&cur_slist, slist);
580 free_slist(&slist);
583 void __merge_true_states(void)
585 struct state_list *slist;
587 slist = pop_slist(&true_stack);
588 merge_slist(&cur_slist, slist);
589 free_slist(&slist);
592 void __push_continues(void)
594 push_slist(&continue_stack, NULL);
597 void __discard_continues(void)
599 struct state_list *slist;
601 slist = pop_slist(&continue_stack);
602 free_slist(&slist);
605 void __process_continues(void)
607 struct state_list *slist;
609 slist = pop_slist(&continue_stack);
610 if (!slist)
611 slist = clone_slist(cur_slist);
612 else
613 merge_slist(&slist, cur_slist);
615 push_slist(&continue_stack, slist);
618 static int top_slist_empty(struct state_list_stack **stack)
620 struct state_list *tmp;
621 int empty = 0;
623 tmp = pop_slist(stack);
624 if (!tmp)
625 empty = 1;
626 push_slist(stack, tmp);
627 return empty;
630 /* a silly loop does this: while(i--) { return; } */
631 void __warn_on_silly_pre_loops(void)
633 if (!__path_is_null())
634 return;
635 if (!top_slist_empty(&continue_stack))
636 return;
637 if (!top_slist_empty(&break_stack))
638 return;
639 /* if the path was nullified before the loop, then we already
640 printed an error earlier */
641 if (top_slist_empty(&false_stack))
642 return;
643 sm_msg("info: loop could be replaced with if statement.");
646 void __merge_continues(void)
648 struct state_list *slist;
650 slist = pop_slist(&continue_stack);
651 merge_slist(&cur_slist, slist);
652 free_slist(&slist);
655 void __push_breaks(void)
657 push_slist(&break_stack, NULL);
660 void __process_breaks(void)
662 struct state_list *slist;
664 slist = pop_slist(&break_stack);
665 if (!slist)
666 slist = clone_slist(cur_slist);
667 else
668 merge_slist(&slist, cur_slist);
670 push_slist(&break_stack, slist);
673 void __merge_breaks(void)
675 struct state_list *slist;
677 slist = pop_slist(&break_stack);
678 merge_slist(&cur_slist, slist);
679 free_slist(&slist);
682 void __use_breaks(void)
684 free_slist(&cur_slist);
685 cur_slist = pop_slist(&break_stack);
688 void __save_switch_states(struct expression *switch_expr)
690 push_range_list(&remaining_cases, __get_implied_values(switch_expr));
691 push_slist(&switch_stack, clone_slist(cur_slist));
694 void __merge_switches(struct expression *switch_expr, struct expression *case_expr)
696 struct state_list *slist;
697 struct state_list *implied_slist;
699 slist = pop_slist(&switch_stack);
700 implied_slist = __implied_case_slist(switch_expr, case_expr, &remaining_cases, &slist);
701 merge_slist(&cur_slist, implied_slist);
702 free_slist(&implied_slist);
703 push_slist(&switch_stack, slist);
706 void __discard_switches(void)
708 struct state_list *slist;
710 pop_range_list(&remaining_cases);
711 slist = pop_slist(&switch_stack);
712 free_slist(&slist);
715 void __push_default(void)
717 push_slist(&default_stack, NULL);
720 void __set_default(void)
722 set_state_stack(&default_stack, 0, "has_default", NULL, &true_state);
725 int __pop_default(void)
727 struct state_list *slist;
729 slist = pop_slist(&default_stack);
730 if (slist) {
731 free_slist(&slist);
732 return 1;
734 return 0;
737 static struct named_slist *alloc_named_slist(const char *name, struct state_list *slist)
739 struct named_slist *named_slist = __alloc_named_slist(0);
741 named_slist->name = (char *)name;
742 named_slist->slist = slist;
743 return named_slist;
746 void __save_gotos(const char *name)
748 struct state_list **slist;
749 struct state_list *clone;
751 slist = get_slist_from_named_stack(goto_stack, name);
752 if (slist) {
753 clone = clone_slist(cur_slist);
754 merge_slist(slist, clone);
755 free_slist(&clone);
756 return;
757 } else {
758 struct named_slist *named_slist;
760 clone = clone_slist(cur_slist);
761 named_slist = alloc_named_slist(name, clone);
762 add_ptr_list(&goto_stack, named_slist);
766 void __merge_gotos(const char *name)
768 struct state_list **slist;
770 slist = get_slist_from_named_stack(goto_stack, name);
771 if (slist)
772 merge_slist(&cur_slist, *slist);