kernel_user_data: delete some dead code
[smatch.git] / check_shift_to_zero.c
blob42827c45c6a8de5b80738618a864ede50c26d46a
1 /*
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
18 #include "smatch.h"
20 static int my_id;
22 static void match_binop(struct expression *expr)
24 struct symbol *type;
25 sval_t bits;
27 if (expr->op != SPECIAL_RIGHTSHIFT)
28 return;
30 if (!get_implied_value(expr->right, &bits))
31 return;
33 type = get_type(expr->left);
34 if (!type)
35 return;
36 if (type_bits(type) == -1 || type_bits(type) > bits.value)
37 return;
38 if (is_ignored_expr(my_id, expr))
39 return;
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;
47 sval_t mask, shift;
48 char *macro, *inner;
50 if (expr->op != SPECIAL_RIGHTSHIFT)
51 return;
53 left = strip_expr(expr->left);
54 tmp = get_assigned_expr(left);
55 if (tmp)
56 left = tmp;
57 if (left->type != EXPR_BINOP || left->op != '&')
58 return;
60 if (!get_value(expr->right, &shift))
61 return;
62 if (!get_value(left->right, &mask))
63 return;
65 if (mask.uvalue >> shift.uvalue)
66 return;
68 macro = get_macro_name(expr->pos);
69 inner = get_inner_macro(expr->pos);
70 if (macro && inner) {
71 if (strcmp(macro, "unlikely") == 0)
72 goto warn;
73 if (strcmp(inner, "unlikely") == 0 ||
74 strcmp(inner, "__const_hweight8") == 0 ||
75 strcmp(inner, "BITS_PER_LONG") == 0 ||
76 strcmp(inner, "CORDIC_PRECISION_SHIFT") == 0)
77 return;
80 warn:
81 sm_warning("mask and shift to zero: expr='%s'", expr_to_str(expr));
84 static void match_assign(struct expression *expr)
86 struct symbol *type;
87 sval_t bits;
89 if (expr->op != SPECIAL_SHR_ASSIGN)
90 return;
92 if (!get_implied_value(expr->right, &bits))
93 return;
94 type = get_type(expr->left);
95 if (!type)
96 return;
97 if (type_bits(type) > bits.value)
98 return;
99 sm_warning("right shift assign to zero");
102 void check_shift_to_zero(int id)
104 my_id = id;
106 add_hook(&match_binop, BINOP_HOOK);
107 add_hook(&match_binop2, BINOP_HOOK);
109 add_hook(&match_assign, ASSIGNMENT_HOOK);