param_key: fix container of when no struct member is referenced
[smatch.git] / smatch_power_of_two.c
bloba9b6d8acb7ca73e1f27c6edd9057106c8b1c4ac1
1 /*
2 * Copyright (C) 2021
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_extra.h"
20 #include "smatch_slist.h"
22 static int my_id;
24 STATE(power_of_two);
26 static struct smatch_state *unmatched_state(struct sm_state *sm)
28 struct smatch_state *state;
29 sval_t sval;
31 state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
32 if (!state)
33 return &undefined;
34 if (!estate_get_single_value(state, &sval))
35 return &undefined;
37 if (!(sval.uvalue & (sval.uvalue - 1)))
38 return &power_of_two;
39 return &undefined;
42 static bool implied_power_of_two(struct expression *expr)
44 sval_t sval;
46 if (!get_implied_value(expr, &sval))
47 return false;
48 if (!(sval.uvalue & (sval.uvalue - 1)))
49 return true;
50 return false;
53 bool is_power_of_two(struct expression *expr)
55 expr = strip_expr(expr);
57 if (expr->type == EXPR_BINOP &&
58 expr->op == SPECIAL_LEFTSHIFT &&
59 is_power_of_two(expr->left))
60 return true;
62 if (implied_power_of_two(expr))
63 return true;
65 if (get_state_expr(my_id, expr) == &power_of_two)
66 return true;
68 return false;
71 static void set_power_of_two(struct expression *expr)
73 struct symbol *type;
75 type = get_type(expr);
76 if (type_bits(type) == 1)
77 return;
79 set_state_expr(my_id, expr->left, &power_of_two);
82 static bool is_sign_expansion(struct expression *expr)
84 struct range_list *rl;
85 struct symbol *type_left;
86 struct symbol *type_right;
88 type_left = get_type(expr->left);
89 type_right = get_type(expr->right);
90 if (!type_left || !type_right)
91 return true;
92 if (type_bits(type_left) <= type_bits(type_right))
93 return false;
95 get_absolute_rl(expr->right, &rl);
96 if (sval_is_negative(rl_min(rl)))
97 return true;
99 return false;
102 static void match_assign(struct expression *expr)
104 if (expr->op != '=')
105 return;
107 if (implied_power_of_two(expr->right)) {
108 if (get_state_expr(my_id, expr->left))
109 set_state_expr(my_id, expr->left, &power_of_two);
110 return;
113 if (is_sign_expansion(expr))
114 return;
116 if (is_power_of_two(expr->right))
117 set_power_of_two(expr->left);
120 static bool is_minus_mask(struct expression *left, struct expression *right)
122 if (right->type != EXPR_BINOP ||
123 right->op != '-')
124 return false;
126 if (right->right->value != 1)
127 return false;
129 if (expr_equiv(left, right->left))
130 return true;
132 return false;
135 static void match_condition(struct expression *expr)
137 expr = strip_expr(expr);
138 if (expr->type != EXPR_BINOP ||
139 expr->op != '&')
140 return;
142 if (is_minus_mask(strip_expr(expr->left), strip_expr(expr->right))) {
143 set_true_false_states_expr(my_id, expr->left, NULL, &power_of_two);
144 return;
147 if (is_minus_mask(strip_expr(expr->right), strip_expr(expr->left))) {
148 set_true_false_states_expr(my_id, expr->right, NULL, &power_of_two);
149 return;
153 static void caller_info_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
155 if (sm->state != &power_of_two)
156 return;
158 sql_insert_caller_info(call, POWER_OF_TWO, param, printed_name, "");
161 static void set_power_of_two_name_sym(const char *name, struct symbol *sym, char *value)
163 set_state(my_id, name, sym, &power_of_two);
166 static void return_info_callback(int return_id, char *return_ranges,
167 struct expression *returned_expr,
168 int param,
169 const char *printed_name,
170 struct sm_state *sm)
172 struct smatch_state *estate;
173 sval_t sval;
175 if (sm->state != &power_of_two)
176 return;
178 if (param != -1 && !param_was_set_var_sym(sm->name, sm->sym))
179 return;
181 estate = get_state(SMATCH_EXTRA, sm->name, sm->sym);
182 if (estate_get_single_value(estate, &sval))
183 return;
185 sql_insert_return_states(return_id, return_ranges, POWER_OF_TWO_SET, param, printed_name, "");
188 static void returns_power_of_two_set(struct expression *expr, int param, char *key, char *value)
190 char *name;
191 struct symbol *sym;
193 name = get_name_sym_from_param_key(expr, param, key, &sym);
194 if (!name)
195 return;
196 set_state(my_id, name, sym, &power_of_two);
199 void register_power_of_two(int id)
201 my_id = id;
203 add_hook(&match_assign, ASSIGNMENT_HOOK);
204 add_hook(&match_condition, CONDITION_HOOK);
205 add_unmatched_state_hook(my_id, &unmatched_state);
206 add_caller_info_callback(my_id, caller_info_callback);
207 select_caller_name_sym(set_power_of_two_name_sym, POWER_OF_TWO);
208 add_return_info_callback(my_id, return_info_callback);
209 select_return_states_hook(POWER_OF_TWO_SET, &returns_power_of_two_set);