db: fix nested call handling
[smatch.git] / smatch_param_filter.c
blob53d0abec16329d962ecbbe38f968f57a3d3ee0b9
1 /*
2 * Copyright (C) 2012 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
19 * This is for functions like:
21 * void foo(int *x)
22 * {
23 * if (*x == 42)
24 * *x = 0;
25 * }
27 * The final value of *x depends on the input to the function but with *x == 42
28 * filtered out.
32 #include "smatch.h"
33 #include "smatch_extra.h"
34 #include "smatch_slist.h"
36 static int my_id;
38 STATE(modified);
39 STATE(original);
41 static struct stree *start_states;
42 static struct stree_stack *saved_stack;
43 static void save_start_states(struct statement *stmt)
45 start_states = get_all_states_stree(SMATCH_EXTRA);
48 static void match_end_func(void)
50 free_stree(&start_states);
53 static void match_save_states(struct expression *expr)
55 push_stree(&saved_stack, start_states);
56 start_states = NULL;
59 static void match_restore_states(struct expression *expr)
61 start_states = pop_stree(&saved_stack);
64 static struct smatch_state *unmatched_state(struct sm_state *sm)
66 return &original;
69 static void extra_mod_hook(const char *name, struct symbol *sym, struct smatch_state *state)
71 int param;
73 param = get_param_num_from_sym(sym);
74 if (param < 0)
75 return;
77 set_state(my_id, name, sym, &modified);
80 static struct range_list *get_orig_rl(struct sm_state *sm)
82 struct range_list *ret = NULL;
83 struct sm_state *tmp;
84 struct smatch_state *extra;
86 FOR_EACH_PTR(sm->possible, tmp) {
87 if (tmp->state != &original)
88 continue;
89 extra = get_state_stree(tmp->pool, SMATCH_EXTRA, tmp->name, tmp->sym);
90 if (!extra) {
91 // sm_msg("debug: no value found in pool %p", tmp->pool);
92 return NULL;
94 ret = rl_union(ret, estate_rl(extra));
95 } END_FOR_EACH_PTR(tmp);
96 return ret;
99 static void print_one_mod_param(int return_id, char *return_ranges,
100 int param, struct sm_state *sm)
102 const char *param_name;
103 struct range_list *rl;
105 param_name = get_param_name(sm);
106 if (!param_name)
107 return;
108 rl = get_orig_rl(sm);
109 if (is_whole_rl(rl))
110 return;
112 sql_insert_return_states(return_id, return_ranges, FILTER_VALUE, param,
113 param_name, show_rl(rl));
116 static void print_one_extra_param(int return_id, char *return_ranges,
117 int param, struct sm_state *sm)
119 struct smatch_state *old;
120 const char *param_name;
122 if (estate_is_whole(sm->state))
123 return;
124 old = get_state_stree(start_states, SMATCH_EXTRA, sm->name, sm->sym);
125 if (old && estates_equiv(old, sm->state))
126 return;
128 param_name = get_param_name(sm);
129 if (!param_name)
130 return;
132 sql_insert_return_states(return_id, return_ranges, FILTER_VALUE, param,
133 param_name, sm->state->name);
136 static void print_return_value_param(int return_id, char *return_ranges, struct expression *expr)
138 struct stree *extra_stree;
139 struct sm_state *tmp;
140 struct sm_state *sm;
141 int param;
143 extra_stree = get_all_states_stree(SMATCH_EXTRA);
145 FOR_EACH_SM(extra_stree, tmp) {
146 param = get_param_num_from_sym(tmp->sym);
147 if (param < 0)
148 continue;
150 * skip the parameter itself because that's handled by
151 * smatch_param_limit.c.
153 if (tmp->sym->ident && strcmp(tmp->sym->ident->name, tmp->name) == 0)
154 continue;
156 sm = get_sm_state(my_id, tmp->name, tmp->sym);
157 if (sm)
158 print_one_mod_param(return_id, return_ranges, param, sm);
159 else
160 print_one_extra_param(return_id, return_ranges, param, tmp);
161 } END_FOR_EACH_SM(tmp);
163 free_stree(&extra_stree);
166 void register_param_filter(int id)
168 my_id = id;
170 add_hook(&save_start_states, AFTER_DEF_HOOK);
171 add_extra_mod_hook(&extra_mod_hook);
172 add_unmatched_state_hook(my_id, &unmatched_state);
173 add_split_return_callback(&print_return_value_param);
174 add_hook(&match_end_func, END_FUNC_HOOK);
175 add_hook(&match_save_states, INLINE_FN_START);
176 add_hook(&match_restore_states, INLINE_FN_END);