db/fixup_kernel.sh: fix clear_user() handling
[smatch.git] / smatch_returns.c
blobfd32065304249330448b0b953af413d6de8ef7b0
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 static struct stree_stack *return_stree_stack;
32 static struct stree *all_return_states;
34 void all_return_states_hook(void (*callback)(void))
36 struct return_states_callback *rs_cb = __alloc_return_states_callback(0);
38 rs_cb->callback = callback;
39 add_ptr_list(&callback_list, rs_cb);
42 static void call_hooks(void)
44 struct return_states_callback *rs_cb;
45 struct stree *orig;
47 orig = __swap_cur_stree(all_return_states);
48 FOR_EACH_PTR(callback_list, rs_cb) {
49 rs_cb->callback();
50 } END_FOR_EACH_PTR(rs_cb);
51 __swap_cur_stree(orig);
54 static void match_return(int return_id, char *return_ranges, struct expression *expr)
56 struct stree *stree;
58 stree = clone_stree(__get_cur_stree());
59 merge_stree_no_pools(&all_return_states, stree);
60 push_stree(&return_stree_stack, stree);
63 static void match_end_func(struct symbol *sym)
65 call_hooks();
68 struct stree *get_all_return_states(void)
70 return all_return_states;
73 struct stree_stack *get_all_return_strees(void)
75 return return_stree_stack;
78 static void free_resources(struct symbol *sym)
80 free_stree(&all_return_states);
81 free_stack_and_strees(&return_stree_stack);
84 void register_returns_early(int id)
86 RETURN_ID = id;
88 set_dynamic_states(RETURN_ID);
89 add_split_return_callback(match_return);
92 void register_returns(int id)
94 add_hook(&match_end_func, END_FUNC_HOOK);
95 add_function_data((unsigned long *)&all_return_states);
96 add_function_data((unsigned long *)&return_stree_stack);
97 add_hook(&free_resources, AFTER_FUNC_HOOK);