extra: don't reset impossible states after a condition
[smatch.git] / smatch_returns.c
blob5c7157cf1f06411ac896aae8d85820d0b5fe4986
1 /*
2 * Copyright (C) 2011 Oracle.
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
18 #include "smatch.h"
19 #include "smatch_slist.h"
20 #include "smatch_extra.h"
22 int RETURN_ID;
24 struct return_states_callback {
25 void (*callback)(void);
27 ALLOCATOR(return_states_callback, "return states callbacks");
28 DECLARE_PTR_LIST(callback_list, struct return_states_callback);
29 static struct callback_list *callback_list;
31 DECLARE_PTR_LIST(stree_stack_stack, struct stree_stack);
32 static void push_stree_stack(struct stree_stack_stack **stack_stack, struct stree_stack *stack)
34 add_ptr_list(stack_stack, stack);
37 static struct stree_stack *pop_stree_stack(struct stree_stack_stack **stack_stack)
39 struct stree_stack *stack;
41 stack = last_ptr_list((struct ptr_list *)*stack_stack);
42 delete_ptr_list_last((struct ptr_list **)stack_stack);
43 return stack;
46 static struct stree_stack *return_stree_stack;
47 static struct stree_stack_stack *saved_stack_stack;
48 static struct stree *all_return_states;
49 static struct stree_stack *saved_stack;
51 void all_return_states_hook(void (*callback)(void))
53 struct return_states_callback *rs_cb = __alloc_return_states_callback(0);
55 rs_cb->callback = callback;
56 add_ptr_list(&callback_list, rs_cb);
59 static void call_hooks(void)
61 struct return_states_callback *rs_cb;
63 __set_fake_cur_stree_fast(all_return_states);
64 FOR_EACH_PTR(callback_list, rs_cb) {
65 rs_cb->callback();
66 } END_FOR_EACH_PTR(rs_cb);
67 __pop_fake_cur_stree_fast();
70 static void match_return(int return_id, char *return_ranges, struct expression *expr)
72 struct stree *stree;
74 stree = clone_stree(__get_cur_stree());
75 merge_stree_no_pools(&all_return_states, stree);
76 push_stree(&return_stree_stack, stree);
79 static void match_end_func(struct symbol *sym)
82 * FIXME: either this isn't needed or we need to copy a stree into the
83 * return_stree_stack as well.
85 merge_stree(&all_return_states, __get_cur_stree());
86 call_hooks();
89 static void match_save_states(struct expression *expr)
91 push_stree(&saved_stack, all_return_states);
92 all_return_states = NULL;
94 push_stree_stack(&saved_stack_stack, return_stree_stack);
95 return_stree_stack = NULL;
98 static void match_restore_states(struct expression *expr)
100 /* This free_stree() isn't needed is it?? */
101 free_stree(&all_return_states);
103 all_return_states = pop_stree(&saved_stack);
104 return_stree_stack = pop_stree_stack(&saved_stack_stack);
107 struct stree *get_all_return_states(void)
109 return all_return_states;
112 struct stree_stack *get_all_return_strees(void)
114 return return_stree_stack;
117 static void free_resources(struct symbol *sym)
119 struct stree *tmp;
121 free_stree(&all_return_states);
123 FOR_EACH_PTR(return_stree_stack, tmp) {
124 free_stree(&tmp);
125 } END_FOR_EACH_PTR(tmp);
126 free_stree_stack(&return_stree_stack);
129 void register_returns_early(int id)
131 RETURN_ID = id;
133 add_split_return_callback(match_return);
136 void register_returns(int id)
138 add_hook(&match_end_func, END_FUNC_HOOK);
139 add_hook(&match_save_states, INLINE_FN_START);
140 add_hook(&match_restore_states, INLINE_FN_END);
141 add_hook(&free_resources, AFTER_FUNC_HOOK);