try handle ++ and -- better.
[smatch.git] / smatch_states.c
blob87a68e51b94325fb1a07d4ad2b82056afaf47de4
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.
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include "smatch.h"
30 #include "smatch_slist.h"
31 #include "smatch_extra.h"
33 struct smatch_state undefined = { .name = "undefined" };
34 struct smatch_state merged = { .name = "merged" };
35 struct smatch_state true_state = { .name = "true" };
36 struct smatch_state false_state = { .name = "false" };
38 static struct state_list *cur_slist; /* current states */
40 static struct state_list_stack *true_stack; /* states after a t/f branch */
41 static struct state_list_stack *false_stack;
42 static struct state_list_stack *pre_cond_stack; /* states before a t/f branch */
44 static struct state_list_stack *cond_true_stack; /* states affected by a branch */
45 static struct state_list_stack *cond_false_stack;
47 int __fake_cur = 0;
48 struct state_list *__fake_cur_slist = NULL;
49 int __fake_conditions = 0;
50 struct state_list *__fake_cond_true = NULL;
51 struct state_list *__fake_cond_false = NULL;
53 static struct state_list_stack *break_stack;
54 static struct state_list_stack *switch_stack;
55 static struct range_list_stack *remaining_cases;
56 static struct state_list_stack *default_stack;
57 static struct state_list_stack *continue_stack;
58 static struct state_list_stack *false_only_stack;
60 static struct named_stack *goto_stack;
62 struct state_list_stack *implied_pools;
64 int debug_states;
66 void __print_cur_slist()
68 __print_slist(cur_slist);
71 static int unreachable()
73 static int reset_warnings = 1;
75 if (cur_slist) {
76 reset_warnings = 1;
77 return 0;
80 if (reset_warnings || debug_states)
81 sm_msg("info: ignoring unreachable code.");
82 reset_warnings = 0;
83 return 1;
86 void set_state(int owner, const char *name, struct symbol *sym,
87 struct smatch_state *state)
89 if (!name)
90 return;
92 if (debug_states) {
93 struct smatch_state *s;
95 s = get_state(owner, name, sym);
96 if (!s)
97 printf("%d new state. name='%s' owner=%d: %s\n",
98 get_lineno(), name, owner, show_state(state));
99 else
100 printf("%d state change name='%s' owner=%d: %s => %s\n",
101 get_lineno(), name, owner, show_state(s),
102 show_state(state));
105 if (owner != -1 && unreachable())
106 return;
108 if (__fake_cur) {
109 set_state_slist(&__fake_cur_slist, owner, name, sym, state);
110 return;
113 set_state_slist(&cur_slist, owner, name, sym, state);
115 if (cond_true_stack) {
116 set_state_stack(&cond_true_stack, owner, name, sym, state);
117 set_state_stack(&cond_false_stack, owner, name, sym, state);
121 void set_state_expr(int owner, struct expression *expr,
122 struct smatch_state *state)
124 char *name;
125 struct symbol *sym;
127 expr = strip_expr(expr);
128 name = get_variable_from_expr(expr, &sym);
129 if (!name || !sym)
130 goto free;
131 reset_on_container_modified(owner, expr);
132 set_state(owner, name, sym, state);
133 free:
134 free_string(name);
138 void __set_state(struct sm_state *sm)
140 if (debug_states) {
141 struct smatch_state *s;
143 s = get_state(sm->owner, sm->name, sm->sym);
144 if (!s)
145 printf("%d new state. name='%s' owner=%d: %s\n",
146 get_lineno(), sm->name, sm->owner, show_state(sm->state));
147 else
148 printf("%d state change name='%s' owner=%d: %s => %s\n",
149 get_lineno(), sm->name, sm->owner, show_state(s),
150 show_state(sm->state));
153 if (unreachable())
154 return;
156 overwrite_sm_state(&cur_slist, sm);
158 if (cond_true_stack) {
159 overwrite_sm_state_stack(&cond_true_stack, sm);
160 overwrite_sm_state_stack(&cond_false_stack, sm);
164 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym)
166 return get_state_slist(cur_slist, owner, name, sym);
169 struct smatch_state *get_state_expr(int owner, struct expression *expr)
171 char *name;
172 struct symbol *sym;
173 struct smatch_state *ret = NULL;
175 expr = strip_expr(expr);
176 name = get_variable_from_expr(expr, &sym);
177 if (!name || !sym)
178 goto free;
179 ret = get_state(owner, name, sym);
180 free:
181 free_string(name);
182 return ret;
185 struct state_list *get_possible_states(int owner, const char *name,
186 struct symbol *sym)
188 struct sm_state *sms;
190 sms = get_sm_state_slist(cur_slist, owner, name, sym);
191 if (sms)
192 return sms->possible;
193 return NULL;
196 struct state_list *get_possible_states_expr(int owner, struct expression *expr)
198 char *name;
199 struct symbol *sym;
200 struct state_list *ret = NULL;
202 expr = strip_expr(expr);
203 name = get_variable_from_expr(expr, &sym);
204 if (!name || !sym)
205 goto free;
206 ret = get_possible_states(owner, name, sym);
207 free:
208 free_string(name);
209 return ret;
212 struct sm_state *get_sm_state(int owner, const char *name, struct symbol *sym)
214 return get_sm_state_slist(cur_slist, owner, name, sym);
217 struct sm_state *get_sm_state_expr(int owner, struct expression *expr)
219 char *name;
220 struct symbol *sym;
221 struct sm_state *ret = NULL;
223 expr = strip_expr(expr);
224 name = get_variable_from_expr(expr, &sym);
225 if (!name || !sym)
226 goto free;
227 ret = get_sm_state(owner, name, sym);
228 free:
229 free_string(name);
230 return ret;
233 void delete_state(int owner, const char *name, struct symbol *sym)
235 delete_state_slist(&cur_slist, owner, name, sym);
238 void delete_state_expr(int owner, struct expression *expr)
240 char *name;
241 struct symbol *sym;
243 expr = strip_expr(expr);
244 name = get_variable_from_expr(expr, &sym);
245 if (!name || !sym)
246 goto free;
247 delete_state(owner, name, sym);
248 free:
249 free_string(name);
252 struct state_list *get_all_states(int owner)
254 struct state_list *slist = NULL;
255 struct sm_state *tmp;
257 FOR_EACH_PTR(cur_slist, tmp) {
258 if (tmp->owner == owner) {
259 add_ptr_list(&slist, tmp);
261 } END_FOR_EACH_PTR(tmp);
263 return slist;
266 int is_reachable()
268 if (cur_slist)
269 return 1;
270 return 0;
273 struct state_list *__get_cur_slist()
275 return cur_slist;
278 void set_true_false_states(int owner, const char *name, struct symbol *sym,
279 struct smatch_state *true_state,
280 struct smatch_state *false_state)
282 if (debug_states) {
283 struct smatch_state *tmp;
285 tmp = get_state(owner, name, sym);
286 sm_debug("%d set_true_false '%s'. Was %s. Now T:%s F:%s\n",
287 get_lineno(), name, show_state(tmp),
288 show_state(true_state), show_state(false_state));
291 if (unreachable())
292 return;
294 if (__fake_conditions) {
295 if (true_state)
296 set_state_slist(&__fake_cond_true, owner, name, sym, true_state);
297 if (false_state)
298 set_state_slist(&__fake_cond_false, owner, name, sym, false_state);
299 return;
302 if (!cond_false_stack || !cond_true_stack) {
303 printf("Error: missing true/false stacks\n");
304 return;
307 if (true_state) {
308 set_state_slist(&cur_slist, owner, name, sym, true_state);
309 set_state_stack(&cond_true_stack, owner, name, sym, true_state);
311 if (false_state)
312 set_state_stack(&cond_false_stack, owner, name, sym, false_state);
315 void set_true_false_states_expr(int owner, struct expression *expr,
316 struct smatch_state *true_state,
317 struct smatch_state *false_state)
319 char *name;
320 struct symbol *sym;
322 expr = strip_expr(expr);
323 name = get_variable_from_expr(expr, &sym);
324 if (!name || !sym)
325 goto free;
326 reset_on_container_modified(owner, expr);
327 set_true_false_states(owner, name, sym, true_state, false_state);
328 free:
329 free_string(name);
332 void __set_true_false_sm(struct sm_state *true_state,
333 struct sm_state *false_state)
335 if (unreachable())
336 return;
338 if (!cond_false_stack || !cond_true_stack) {
339 printf("Error: missing true/false stacks\n");
340 return;
343 if (true_state) {
344 overwrite_sm_state(&cur_slist, true_state);
345 overwrite_sm_state_stack(&cond_true_stack, true_state);
347 if (false_state)
348 overwrite_sm_state_stack(&cond_false_stack, false_state);
351 void nullify_path()
353 free_slist(&cur_slist);
356 void __match_nullify_path_hook(const char *fn, struct expression *expr,
357 void *unused)
359 nullify_path();
362 * At the start of every function we mark the path
363 * as unnull. That there is always at least one state
364 * in the cur_slist until nullify_path is called. This
365 * is used in merge_slist() for the first null check.
368 void __unnullify_path()
370 set_state(-1, "unnull_path", NULL, &true_state);
373 int __path_is_null()
375 if (cur_slist)
376 return 0;
377 return 1;
380 void clear_all_states()
382 struct named_slist *named_slist;
384 nullify_path();
385 free_stack_and_slists(&true_stack);
386 free_stack_and_slists(&false_stack);
387 free_stack_and_slists(&false_only_stack);
388 free_stack_and_slists(&pre_cond_stack);
389 free_stack_and_slists(&cond_true_stack);
390 free_stack_and_slists(&cond_false_stack);
391 free_stack_and_slists(&break_stack);
392 free_stack_and_slists(&switch_stack);
393 free_stack_and_slists(&continue_stack);
394 free_stack_and_slists(&implied_pools);
396 FOR_EACH_PTR(goto_stack, named_slist) {
397 free_slist(&named_slist->slist);
398 } END_FOR_EACH_PTR(named_slist);
399 __free_ptr_list((struct ptr_list **)&goto_stack);
400 free_every_single_sm_state();
404 void __push_cond_stacks()
406 push_slist(&cond_true_stack, NULL);
407 push_slist(&cond_false_stack, NULL);
411 * This combines the pre cond states with either the true or false states.
412 * For example:
413 * a = kmalloc() ; if (a !! foo(a)
414 * In the pre state a is possibly null. In the true state it is non null.
415 * In the false state it is null. Combine the pre and the false to get
416 * that when we call 'foo', 'a' is null.
419 static void __use_cond_stack(struct state_list_stack **stack)
421 struct state_list *slist;
423 free_slist(&cur_slist);
425 cur_slist = pop_slist(&pre_cond_stack);
426 push_slist(&pre_cond_stack, clone_slist(cur_slist));
428 slist = pop_slist(stack);
429 overwrite_slist(slist, &cur_slist);
430 push_slist(stack, slist);
433 void __save_false_states_for_later()
435 struct state_list *pre_conditions;
436 struct state_list *false_states;
437 struct state_list *tmp;
439 pre_conditions = pop_slist(&pre_cond_stack);
440 false_states = pop_slist(&cond_false_stack);
441 tmp = clone_slist(pre_conditions);
443 overwrite_slist(false_states, &tmp);
445 push_slist(&pre_cond_stack, tmp);
446 push_slist(&pre_cond_stack, pre_conditions);
447 push_slist(&cond_false_stack, false_states);
450 void __use_previously_stored_false_states()
452 free_slist(&cur_slist);
453 cur_slist = pop_slist(&pre_cond_stack);
456 void __use_cond_true_states()
458 __use_cond_stack(&cond_true_stack);
461 void __use_cond_false_states()
463 __use_cond_stack(&cond_false_stack);
466 void __negate_cond_stacks()
468 struct state_list *old_false, *old_true;
470 old_false = pop_slist(&cond_false_stack);
471 old_true = pop_slist(&cond_true_stack);
473 overwrite_slist(old_false, &cur_slist);
475 push_slist(&cond_false_stack, old_true);
476 push_slist(&cond_true_stack, old_false);
479 void __and_cond_states()
481 and_slist_stack(&cond_true_stack);
482 or_slist_stack(&pre_cond_stack, cur_slist, &cond_false_stack);
485 void __or_cond_states()
487 or_slist_stack(&pre_cond_stack, cur_slist, &cond_true_stack);
488 and_slist_stack(&cond_false_stack);
491 void __save_pre_cond_states()
493 push_slist(&pre_cond_stack, clone_slist(cur_slist));
496 void __pop_pre_cond_states()
498 struct state_list *tmp;
500 tmp = pop_slist(&pre_cond_stack);
501 free_slist(&tmp);
504 void __use_false_only_stack()
506 struct state_list *slist;
508 slist = pop_slist(&false_only_stack);
509 overwrite_slist(slist, &cur_slist);
510 free_slist(&slist);
513 void __pop_false_only_stack()
515 struct state_list *slist;
517 slist = pop_slist(&false_only_stack);
518 free_slist(&slist);
521 void __use_cond_states()
523 struct state_list *pre, *pre_clone, *true_states, *false_states;
525 pre = pop_slist(&pre_cond_stack);
526 pre_clone = clone_slist(pre);
528 true_states = pop_slist(&cond_true_stack);
529 overwrite_slist(true_states, &pre);
530 /* we use the true states right away */
531 free_slist(&cur_slist);
532 cur_slist = pre;
535 false_states = pop_slist(&cond_false_stack);
536 push_slist(&false_only_stack, clone_slist(false_states));
537 overwrite_slist(false_states, &pre_clone);
538 push_slist(&false_stack, pre_clone);
541 void __push_true_states()
543 push_slist(&true_stack, clone_slist(cur_slist));
546 void __use_false_states()
548 free_slist(&cur_slist);
549 cur_slist = pop_slist(&false_stack);
552 void __pop_false_states()
554 struct state_list *slist;
556 slist = pop_slist(&false_stack);
557 free_slist(&slist);
560 void __merge_false_states()
562 struct state_list *slist;
564 slist = pop_slist(&false_stack);
565 merge_slist(&cur_slist, slist);
566 free_slist(&slist);
569 void __merge_true_states()
571 struct state_list *slist;
573 slist = pop_slist(&true_stack);
574 merge_slist(&cur_slist, slist);
575 free_slist(&slist);
578 void __push_continues()
580 push_slist(&continue_stack, NULL);
583 void __pop_continues()
585 struct state_list *slist;
587 slist = pop_slist(&continue_stack);
588 free_slist(&slist);
591 void __process_continues()
593 struct state_list *slist;
595 slist = pop_slist(&continue_stack);
596 if (!slist) {
597 slist = clone_slist(cur_slist);
598 } else {
599 merge_slist(&slist, cur_slist);
601 push_slist(&continue_stack, slist);
604 static int top_slist_empty(struct state_list_stack **stack)
606 struct state_list *tmp;
607 int empty = 0;
609 tmp = pop_slist(stack);
610 if (!tmp)
611 empty = 1;
612 push_slist(stack, tmp);
613 return empty;
616 /* a silly loop does this: while(i--) { return; } */
617 void __warn_on_silly_pre_loops()
619 if (!__path_is_null())
620 return;
621 if (!top_slist_empty(&continue_stack))
622 return;
623 if (!top_slist_empty(&break_stack))
624 return;
625 /* if the path was nullified before the loop, then we already
626 printed an error earlier */
627 if (top_slist_empty(&false_stack))
628 return;
629 sm_msg("info: loop could be replaced with if statement.");
632 void __merge_continues()
634 struct state_list *slist;
636 slist = pop_slist(&continue_stack);
637 merge_slist(&cur_slist, slist);
638 free_slist(&slist);
641 void __push_breaks()
643 push_slist(&break_stack, NULL);
646 void __process_breaks()
648 struct state_list *slist;
650 slist = pop_slist(&break_stack);
651 if (!slist) {
652 slist = clone_slist(cur_slist);
653 } else {
654 merge_slist(&slist, cur_slist);
656 push_slist(&break_stack, slist);
659 void __merge_breaks()
661 struct state_list *slist;
663 slist = pop_slist(&break_stack);
664 merge_slist(&cur_slist, slist);
665 free_slist(&slist);
668 void __use_breaks()
670 free_slist(&cur_slist);
671 cur_slist = pop_slist(&break_stack);
674 void __save_switch_states(struct expression *switch_expr)
676 push_range_list(&remaining_cases, __get_implied_values(switch_expr));
677 push_slist(&switch_stack, clone_slist(cur_slist));
680 void __merge_switches(struct expression *switch_expr, struct expression *case_expr)
682 struct state_list *slist;
683 struct state_list *implied_slist;
685 slist = pop_slist(&switch_stack);
686 implied_slist = __implied_case_slist(switch_expr, case_expr, &remaining_cases, &slist);
687 merge_slist(&cur_slist, implied_slist);
688 free_slist(&implied_slist);
689 push_slist(&switch_stack, slist);
692 void __pop_switches()
694 struct state_list *slist;
696 pop_range_list(&remaining_cases);
697 slist = pop_slist(&switch_stack);
698 free_slist(&slist);
701 void __push_default()
703 push_slist(&default_stack, NULL);
706 void __set_default()
708 set_state_stack(&default_stack, 0, "has_default", NULL, &true_state);
711 int __pop_default()
713 struct state_list *slist;
715 slist = pop_slist(&default_stack);
716 if (slist) {
717 free_slist(&slist);
718 return 1;
720 return 0;
723 static struct named_slist *alloc_named_slist(const char *name,
724 struct state_list *slist)
726 struct named_slist *named_slist = __alloc_named_slist(0);
728 named_slist->name = (char *)name;
729 named_slist->slist = slist;
730 return named_slist;
733 void __save_gotos(const char *name)
735 struct state_list **slist;
736 struct state_list *clone;
738 slist = get_slist_from_named_stack(goto_stack, name);
739 if (slist) {
740 clone = clone_slist(cur_slist);
741 merge_slist(slist, clone);
742 free_slist(&clone);
743 return;
744 } else {
745 struct named_slist *named_slist;
747 clone = clone_slist(cur_slist);
748 named_slist = alloc_named_slist(name, clone);
749 add_ptr_list(&goto_stack, named_slist);
753 void __merge_gotos(const char *name)
755 struct state_list **slist;
757 slist = get_slist_from_named_stack(goto_stack, name);
758 if (slist)
759 merge_slist(&cur_slist, *slist);