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 #include "smatch_extra.h"
20 #include "smatch_slist.h"
26 bool is_power_of_two(struct expression
*expr
)
30 expr
= strip_expr(expr
);
32 if (expr
->type
== EXPR_BINOP
&&
33 expr
->op
== SPECIAL_LEFTSHIFT
&&
34 is_power_of_two(expr
->left
))
37 if (get_implied_value(expr
, &sval
)) {
38 if (!(sval
.uvalue
& (sval
.uvalue
- 1)))
43 if (get_state_expr(my_id
, expr
) == &power_of_two
)
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
)
59 if (type_bits(type_left
) <= type_bits(type_right
))
62 get_absolute_rl(expr
->right
, &rl
);
63 if (sval_is_negative(rl_min(rl
)))
69 static void match_assign(struct expression
*expr
)
74 if (is_sign_expansion(expr
))
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
||
87 if (right
->right
->value
!= 1)
90 if (expr_equiv(left
, right
->left
))
96 static void match_condition(struct expression
*expr
)
98 expr
= strip_expr(expr
);
99 if (expr
->type
!= EXPR_BINOP
||
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
);
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
);
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
)
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
,
130 const char *printed_name
,
133 struct smatch_state
*estate
;
136 if (param
!= -1 && !param_was_set_var_sym(sm
->name
, sm
->sym
))
139 estate
= get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
140 if (estate_get_single_value(estate
, &sval
))
143 sql_insert_return_states(return_id
, return_ranges
, POWER_OF_TWO_SET
, param
, printed_name
, "");
146 static void returns_power_of_two_set(struct expression
*expr
, int param
, char *key
, char *value
)
151 name
= get_name_sym_from_key(expr
, param
, key
, &sym
);
154 set_state(my_id
, name
, sym
, &power_of_two
);
157 void register_power_of_two(int id
)
161 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
162 add_hook(&match_condition
, CONDITION_HOOK
);
163 add_caller_info_callback(my_id
, caller_info_callback
);
164 select_caller_name_sym(set_power_of_two
, POWER_OF_TWO
);
165 add_return_info_callback(my_id
, return_info_callback
);
166 select_return_states_hook(POWER_OF_TWO_SET
, &returns_power_of_two_set
);