2 * Copyright (C) 2014 Oracle.
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
22 static void match_binop(struct expression
*expr
)
27 if (expr
->op
!= SPECIAL_RIGHTSHIFT
)
30 if (!get_implied_value(expr
->right
, &bits
))
33 type
= get_type(expr
->left
);
36 if (type_bits(type
) == -1 || type_bits(type
) > bits
.value
)
38 if (is_ignored_expr(my_id
, expr
))
40 sm_warning("right shifting more than type allows %d vs %lld", type_bits(type
), bits
.value
);
43 static void match_binop2(struct expression
*expr
)
45 struct expression
*left
;
46 struct expression
*tmp
;
49 if (expr
->op
!= SPECIAL_RIGHTSHIFT
)
52 left
= strip_expr(expr
->left
);
53 tmp
= get_assigned_expr(left
);
56 if (left
->type
!= EXPR_BINOP
|| left
->op
!= '&')
59 if (!get_implied_value(expr
->right
, &shift
))
61 if (!get_value(left
->right
, &mask
))
64 if (mask
.uvalue
>> shift
.uvalue
)
67 sm_warning("mask and shift to zero");
70 static void match_assign(struct expression
*expr
)
75 if (expr
->op
!= SPECIAL_SHR_ASSIGN
)
78 if (!get_implied_value(expr
->right
, &bits
))
80 type
= get_type(expr
->left
);
83 if (type_bits(type
) > bits
.value
)
85 sm_warning("right shift assign to zero");
88 void check_shift_to_zero(int id
)
92 add_hook(&match_binop
, BINOP_HOOK
);
93 add_hook(&match_binop2
, BINOP_HOOK
);
95 add_hook(&match_assign
, ASSIGNMENT_HOOK
);