implied: fix reversed debug output
[smatch.git] / smatch_returns.c
blobfc07e818fca82b66e2f6663b4a7f56aa079609f3
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)
66 * FIXME: either this isn't needed or we need to copy a stree into the
67 * return_stree_stack as well.
69 merge_stree(&all_return_states, __get_cur_stree());
70 call_hooks();
73 struct stree *get_all_return_states(void)
75 return all_return_states;
78 struct stree_stack *get_all_return_strees(void)
80 return return_stree_stack;
83 static void free_resources(struct symbol *sym)
85 free_stree(&all_return_states);
86 free_stack_and_strees(&return_stree_stack);
89 void register_returns_early(int id)
91 RETURN_ID = id;
93 set_dynamic_states(RETURN_ID);
94 add_split_return_callback(match_return);
97 void register_returns(int id)
99 add_hook(&match_end_func, END_FUNC_HOOK);
100 add_function_data((unsigned long *)&all_return_states);
101 add_function_data((unsigned long *)&return_stree_stack);
102 add_hook(&free_resources, AFTER_FUNC_HOOK);