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.
28 #include "smatch_slist.h"
35 static int is_capped_macro(struct expression
*expr
)
39 name
= get_macro_name(expr
->pos
);
43 if (strcmp(name
, "min") == 0)
45 if (strcmp(name
, "MIN") == 0)
47 if (strcmp(name
, "min_t") == 0)
53 int is_capped(struct expression
*expr
)
57 expr
= strip_expr(expr
);
58 while (expr
&& expr
->type
== EXPR_POSTOP
) {
59 expr
= strip_expr(expr
->unop
);
64 if (get_hard_max(expr
, &dummy
))
67 if (is_capped_macro(expr
))
70 if (expr
->type
== EXPR_BINOP
) {
73 if (expr
->op
== SPECIAL_RIGHTSHIFT
)
76 return is_capped(expr
->right
);
77 if (!is_capped(expr
->left
))
81 if (!is_capped(expr
->right
))
85 if (get_state_expr(my_id
, expr
) == &capped
)
90 int is_capped_var_sym(const char *name
, struct symbol
*sym
)
92 if (get_state(my_id
, name
, sym
) == &capped
)
97 void set_param_capped_data(const char *name
, struct symbol
*sym
, char *key
, char *value
)
101 if (strncmp(key
, "$", 1))
103 snprintf(fullname
, 256, "%s%s", name
, key
+ 1);
104 set_state(my_id
, fullname
, sym
, &capped
);
107 static void match_condition(struct expression
*expr
)
109 struct smatch_state
*left_true
= NULL
;
110 struct smatch_state
*left_false
= NULL
;
111 struct smatch_state
*right_true
= NULL
;
112 struct smatch_state
*right_false
= NULL
;
115 if (expr
->type
!= EXPR_COMPARE
)
121 case SPECIAL_UNSIGNED_LT
:
122 case SPECIAL_UNSIGNED_LTE
:
124 right_false
= &capped
;
128 case SPECIAL_UNSIGNED_GT
:
129 case SPECIAL_UNSIGNED_GTE
:
130 left_false
= &capped
;
131 right_true
= &capped
;
135 right_true
= &capped
;
137 case SPECIAL_NOTEQUAL
:
138 left_false
= &capped
;
139 right_false
= &capped
;
146 set_true_false_states_expr(my_id
, expr
->right
, right_true
, right_false
);
147 set_true_false_states_expr(my_id
, expr
->left
, left_true
, left_false
);
150 static void match_assign(struct expression
*expr
)
152 if (is_capped(expr
->right
)) {
153 set_state_expr(my_id
, expr
->left
, &capped
);
155 if (get_state_expr(my_id
, expr
->left
))
156 set_state_expr(my_id
, expr
->left
, &uncapped
);
160 static void match_caller_info(struct expression
*expr
)
162 struct expression
*tmp
;
166 FOR_EACH_PTR(expr
->args
, tmp
) {
168 sql_insert_caller_info(expr
, CAPPED_DATA
, i
, "$", "1");
170 } END_FOR_EACH_PTR(tmp
);
173 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
)
175 if (sm
->state
!= &capped
)
177 sql_insert_caller_info(call
, CAPPED_DATA
, param
, printed_name
, "1");
180 static int is_unmodified(const char *name
)
184 snprintf(orig
, sizeof(orig
), "%s orig", name
);
186 if (get_comparison_strings(name
, orig
) == SPECIAL_EQUAL
)
191 static void print_return_implies_capped(int return_id
, char *return_ranges
, struct expression
*expr
)
193 struct smatch_state
*orig
;
195 const char *param_name
;
198 FOR_EACH_SM(__get_cur_stree(), sm
) {
199 if (sm
->owner
!= my_id
)
201 if (sm
->state
!= &capped
)
204 param
= get_param_num_from_sym(sm
->sym
);
208 orig
= get_state_stree(get_start_states(), my_id
, sm
->name
, sm
->sym
);
212 if (!is_unmodified(sm
->name
))
215 param_name
= get_param_name(sm
);
219 sql_insert_return_states(return_id
, return_ranges
, CAPPED_DATA
,
220 param
, param_name
, "1");
221 } END_FOR_EACH_SM(sm
);
224 static void db_return_states_capped(struct expression
*expr
, int param
, char *key
, char *value
)
229 name
= return_state_to_var_sym(expr
, param
, key
, &sym
);
233 set_state(my_id
, name
, sym
, &capped
);
238 void register_capped(int id
)
242 select_caller_info_hook(set_param_capped_data
, CAPPED_DATA
);
243 add_hook(&match_condition
, CONDITION_HOOK
);
244 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
246 add_hook(&match_caller_info
, FUNCTION_CALL_HOOK
);
247 add_member_info_callback(my_id
, struct_member_callback
);
249 add_split_return_callback(print_return_implies_capped
);
250 select_return_states_hook(CAPPED_DATA
, &db_return_states_capped
);