assigned_expr: remove debug code
[smatch.git] / smatch_capped.c
blob59b8418e5960d3aec2e2a10885c35e4fd398e3b6
1 /*
2 * Copyright (C) 2011 Oracle. All rights reserved.
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 trying to make a list of the variables which
20 * have capped values. Sometimes we don't know what the
21 * cap is, for example if we are comparing variables but
22 * we don't know the values of the variables. In that
23 * case we only know that our variable is capped and we
24 * sort that information here.
27 #include "smatch.h"
28 #include "smatch_slist.h"
29 #include "smatch_extra.h"
31 static int my_id;
33 STATE(capped);
34 STATE(uncapped);
36 static void set_uncapped(struct sm_state *sm, struct expression *mod_expr)
38 set_state(my_id, sm->name, sm->sym, &uncapped);
41 static struct smatch_state *unmatched_state(struct sm_state *sm)
43 struct smatch_state *state;
45 state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
46 if (state && !estate_is_whole(state))
47 return &capped;
48 return &uncapped;
51 static int is_capped_macro(struct expression *expr)
53 char *name;
55 name = get_macro_name(expr->pos);
56 if (!name)
57 return 0;
59 if (strcmp(name, "min") == 0)
60 return 1;
61 if (strcmp(name, "MIN") == 0)
62 return 1;
63 if (strcmp(name, "min_t") == 0)
64 return 1;
66 return 0;
69 int is_capped(struct expression *expr)
71 sval_t dummy;
73 expr = strip_expr(expr);
74 while (expr && expr->type == EXPR_POSTOP) {
75 expr = strip_expr(expr->unop);
77 if (!expr)
78 return 0;
80 if (get_hard_max(expr, &dummy))
81 return 1;
83 if (is_capped_macro(expr))
84 return 1;
86 if (expr->type == EXPR_BINOP) {
87 struct range_list *left_rl, *right_rl;
89 if (expr->op == '&')
90 return 1;
91 if (expr->op == SPECIAL_RIGHTSHIFT)
92 return 1;
93 if (expr->op == '%')
94 return is_capped(expr->right);
95 if (!is_capped(expr->left))
96 return 0;
97 if (expr->op == '/')
98 return 1;
99 if (!is_capped(expr->right))
100 return 0;
101 if (expr->op == '*') {
102 get_absolute_rl(expr->left, &left_rl);
103 get_absolute_rl(expr->right, &right_rl);
104 if (sval_is_negative(rl_min(left_rl)) ||
105 sval_is_negative(rl_min(right_rl)))
106 return 0;
108 return 1;
110 if (get_state_expr(my_id, expr) == &capped)
111 return 1;
112 return 0;
115 int is_capped_var_sym(const char *name, struct symbol *sym)
117 if (get_state(my_id, name, sym) == &capped)
118 return 1;
119 return 0;
122 void set_param_capped_data(const char *name, struct symbol *sym, char *key, char *value)
124 char fullname[256];
126 if (strncmp(key, "$", 1))
127 return;
128 snprintf(fullname, 256, "%s%s", name, key + 1);
129 set_state(my_id, fullname, sym, &capped);
132 static void match_condition(struct expression *expr)
134 struct smatch_state *left_true = NULL;
135 struct smatch_state *left_false = NULL;
136 struct smatch_state *right_true = NULL;
137 struct smatch_state *right_false = NULL;
140 if (expr->type != EXPR_COMPARE)
141 return;
143 switch (expr->op) {
144 case '<':
145 case SPECIAL_LTE:
146 case SPECIAL_UNSIGNED_LT:
147 case SPECIAL_UNSIGNED_LTE:
148 left_true = &capped;
149 right_false = &capped;
150 break;
151 case '>':
152 case SPECIAL_GTE:
153 case SPECIAL_UNSIGNED_GT:
154 case SPECIAL_UNSIGNED_GTE:
155 left_false = &capped;
156 right_true = &capped;
157 break;
158 case SPECIAL_EQUAL:
159 left_true = &capped;
160 right_true = &capped;
161 break;
162 case SPECIAL_NOTEQUAL:
163 left_false = &capped;
164 right_false = &capped;
165 break;
167 default:
168 return;
171 set_true_false_states_expr(my_id, expr->left, left_true, left_false);
172 set_true_false_states_expr(my_id, expr->right, right_true, right_false);
175 static void match_assign(struct expression *expr)
177 if (is_capped(expr->right)) {
178 set_state_expr(my_id, expr->left, &capped);
179 } else {
180 if (get_state_expr(my_id, expr->left))
181 set_state_expr(my_id, expr->left, &uncapped);
185 static void match_caller_info(struct expression *expr)
187 struct expression *tmp;
188 sval_t sval;
189 int i;
191 i = -1;
192 FOR_EACH_PTR(expr->args, tmp) {
193 i++;
194 if (get_implied_value(tmp, &sval))
195 continue;
196 if (!is_capped(tmp))
197 continue;
198 sql_insert_caller_info(expr, CAPPED_DATA, i, "$", "1");
199 } END_FOR_EACH_PTR(tmp);
202 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
204 struct smatch_state *estate;
205 sval_t sval;
207 if (sm->state != &capped)
208 return;
209 estate = get_state(SMATCH_EXTRA, sm->name, sm->sym);
210 if (estate_get_single_value(estate, &sval))
211 return;
212 sql_insert_caller_info(call, CAPPED_DATA, param, printed_name, "1");
215 static void print_return_implies_capped(int return_id, char *return_ranges, struct expression *expr)
217 struct smatch_state *orig, *estate;
218 struct sm_state *sm;
219 const char *param_name;
220 int param;
221 sval_t sval;
223 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
224 if (sm->state != &capped)
225 continue;
227 param = get_param_num_from_sym(sm->sym);
228 if (param < 0)
229 continue;
231 estate = get_state(SMATCH_EXTRA, sm->name, sm->sym);
232 if (estate_get_single_value(estate, &sval))
233 continue;
235 orig = get_state_stree(get_start_states(), my_id, sm->name, sm->sym);
236 if (orig == &capped)
237 continue;
239 param_name = get_param_name(sm);
240 if (!param_name)
241 continue;
243 sql_insert_return_states(return_id, return_ranges, CAPPED_DATA,
244 param, param_name, "1");
245 } END_FOR_EACH_SM(sm);
248 static void db_return_states_capped(struct expression *expr, int param, char *key, char *value)
250 char *name;
251 struct symbol *sym;
253 name = return_state_to_var_sym(expr, param, key, &sym);
254 if (!name || !sym)
255 goto free;
257 set_state(my_id, name, sym, &capped);
258 free:
259 free_string(name);
262 void register_capped(int id)
264 my_id = id;
266 add_unmatched_state_hook(my_id, &unmatched_state);
267 select_caller_info_hook(set_param_capped_data, CAPPED_DATA);
268 add_hook(&match_condition, CONDITION_HOOK);
269 add_hook(&match_assign, ASSIGNMENT_HOOK);
270 add_modification_hook(my_id, &set_uncapped);
272 add_hook(&match_caller_info, FUNCTION_CALL_HOOK);
273 add_member_info_callback(my_id, struct_member_callback);
275 add_split_return_callback(print_return_implies_capped);
276 select_return_states_hook(CAPPED_DATA, &db_return_states_capped);