comparison: select the caller_info
[smatch.git] / smatch_power_of_two.c
blob2acfa57174141312f3a9b5f640a27c5fc31a8959
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 bool is_power_of_two(struct expression *expr)
28 sval_t sval;
30 expr = strip_expr(expr);
32 if (expr->type == EXPR_BINOP &&
33 expr->op == SPECIAL_LEFTSHIFT &&
34 is_power_of_two(expr->left))
35 return true;
37 if (get_implied_value(expr, &sval)) {
38 if (!(sval.uvalue & (sval.uvalue - 1)))
39 return true;
40 return false;
43 if (get_state_expr(my_id, expr) == &power_of_two)
44 return true;
46 return false;
49 static bool is_sign_expansion(struct expression *expr)
51 struct range_list *rl;
52 struct symbol *type_left;
53 struct symbol *type_right;
55 type_left = get_type(expr->left);
56 type_right = get_type(expr->right);
57 if (!type_left || !type_right)
58 return true;
59 if (type_bits(type_left) <= type_bits(type_right))
60 return false;
62 get_absolute_rl(expr->right, &rl);
63 if (sval_is_negative(rl_min(rl)))
64 return true;
66 return false;
69 static void match_assign(struct expression *expr)
71 if (expr->op != '=')
72 return;
74 if (is_sign_expansion(expr))
75 return;
77 if (is_power_of_two(expr->right))
78 set_state_expr(my_id, expr->left, &power_of_two);
81 static bool is_minus_mask(struct expression *left, struct expression *right)
83 if (right->type != EXPR_BINOP ||
84 right->op != '-')
85 return false;
87 if (right->right->value != 1)
88 return false;
90 if (expr_equiv(left, right->left))
91 return true;
93 return false;
96 static void match_condition(struct expression *expr)
98 expr = strip_expr(expr);
99 if (expr->type != EXPR_BINOP ||
100 expr->op != '&')
101 return;
103 if (is_minus_mask(strip_expr(expr->left), strip_expr(expr->right))) {
104 set_true_false_states_expr(my_id, expr->left, NULL, &power_of_two);
105 return;
108 if (is_minus_mask(strip_expr(expr->right), strip_expr(expr->left))) {
109 set_true_false_states_expr(my_id, expr->right, NULL, &power_of_two);
110 return;
114 static void caller_info_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
116 if (sm->state != &power_of_two)
117 return;
119 sql_insert_caller_info(call, POWER_OF_TWO, param, printed_name, "");
122 static void set_power_of_two(const char *name, struct symbol *sym, char *value)
124 set_state(my_id, name, sym, &power_of_two);
127 static void return_info_callback(int return_id, char *return_ranges,
128 struct expression *returned_expr,
129 int param,
130 const char *printed_name,
131 struct sm_state *sm)
133 struct smatch_state *estate;
134 sval_t sval;
136 if (sm->state != &power_of_two)
137 return;
139 if (param != -1 && !param_was_set_var_sym(sm->name, sm->sym))
140 return;
142 estate = get_state(SMATCH_EXTRA, sm->name, sm->sym);
143 if (estate_get_single_value(estate, &sval))
144 return;
146 sql_insert_return_states(return_id, return_ranges, POWER_OF_TWO_SET, param, printed_name, "");
149 static void returns_power_of_two_set(struct expression *expr, int param, char *key, char *value)
151 char *name;
152 struct symbol *sym;
154 name = get_name_sym_from_param_key(expr, param, key, &sym);
155 if (!name)
156 return;
157 set_state(my_id, name, sym, &power_of_two);
160 void register_power_of_two(int id)
162 my_id = id;
164 add_hook(&match_assign, ASSIGNMENT_HOOK);
165 add_hook(&match_condition, CONDITION_HOOK);
166 add_caller_info_callback(my_id, caller_info_callback);
167 select_caller_name_sym(set_power_of_two, POWER_OF_TWO);
168 add_return_info_callback(my_id, return_info_callback);
169 select_return_states_hook(POWER_OF_TWO_SET, &returns_power_of_two_set);