double_checking: avoid more false positives
[smatch.git] / smatch_returns.c
blob84069d882af891c681a734b118c74171f2bb72d0
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"
21 static int my_id;
23 struct return_states_callback {
24 void (*callback)(struct stree *stree);
26 ALLOCATOR(return_states_callback, "return states callbacks");
27 DECLARE_PTR_LIST(callback_list, struct return_states_callback);
28 static struct callback_list *callback_list;
30 static struct stree *all_return_states;
31 static struct stree_stack *saved_stack;
33 void all_return_states_hook(void (*callback)(struct stree *stree))
35 struct return_states_callback *rs_cb = __alloc_return_states_callback(0);
37 rs_cb->callback = callback;
38 add_ptr_list(&callback_list, rs_cb);
41 static void call_hooks(void)
43 struct return_states_callback *rs_cb;
45 FOR_EACH_PTR(callback_list, rs_cb) {
46 rs_cb->callback(all_return_states);
47 } END_FOR_EACH_PTR(rs_cb);
50 static void match_return(struct expression *ret_value)
52 merge_stree_no_pools(&all_return_states, __get_cur_stree());
55 static void match_end_func(struct symbol *sym)
57 merge_stree(&all_return_states, __get_cur_stree());
58 call_hooks();
59 free_stree(&all_return_states);
62 static void match_save_states(struct expression *expr)
64 push_stree(&saved_stack, all_return_states);
65 all_return_states = NULL;
68 static void match_restore_states(struct expression *expr)
70 free_stree(&all_return_states);
71 all_return_states = pop_stree(&saved_stack);
74 void register_returns(int id)
76 my_id = id;
78 add_hook(&match_return, RETURN_HOOK);
79 add_hook(&match_end_func, END_FUNC_HOOK);
80 add_hook(&match_save_states, INLINE_FN_START);
81 add_hook(&match_restore_states, INLINE_FN_END);