1*1f5207b7SJohn Levon /*
2*1f5207b7SJohn Levon * Copyright (C) 2012 Oracle.
3*1f5207b7SJohn Levon *
4*1f5207b7SJohn Levon * This program is free software; you can redistribute it and/or
5*1f5207b7SJohn Levon * modify it under the terms of the GNU General Public License
6*1f5207b7SJohn Levon * as published by the Free Software Foundation; either version 2
7*1f5207b7SJohn Levon * of the License, or (at your option) any later version.
8*1f5207b7SJohn Levon *
9*1f5207b7SJohn Levon * This program is distributed in the hope that it will be useful,
10*1f5207b7SJohn Levon * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*1f5207b7SJohn Levon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12*1f5207b7SJohn Levon * GNU General Public License for more details.
13*1f5207b7SJohn Levon *
14*1f5207b7SJohn Levon * You should have received a copy of the GNU General Public License
15*1f5207b7SJohn Levon * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16*1f5207b7SJohn Levon */
17*1f5207b7SJohn Levon
18*1f5207b7SJohn Levon /*
19*1f5207b7SJohn Levon * This test is used to warn about mixups between bit shifters and bit flags.
20*1f5207b7SJohn Levon *
21*1f5207b7SJohn Levon */
22*1f5207b7SJohn Levon
23*1f5207b7SJohn Levon #include "smatch.h"
24*1f5207b7SJohn Levon #include "smatch_function_hashtable.h"
25*1f5207b7SJohn Levon
26*1f5207b7SJohn Levon static int my_id;
27*1f5207b7SJohn Levon
28*1f5207b7SJohn Levon static DEFINE_HASHTABLE_INSERT(insert_struct, char, int);
29*1f5207b7SJohn Levon static DEFINE_HASHTABLE_SEARCH(search_struct, char, int);
30*1f5207b7SJohn Levon static struct hashtable *shifters;
31*1f5207b7SJohn Levon
get_shifter(struct expression * expr)32*1f5207b7SJohn Levon static const char *get_shifter(struct expression *expr)
33*1f5207b7SJohn Levon {
34*1f5207b7SJohn Levon const char *name;
35*1f5207b7SJohn Levon sval_t expr_value;
36*1f5207b7SJohn Levon const int *shifter_value;
37*1f5207b7SJohn Levon
38*1f5207b7SJohn Levon expr = strip_expr(expr);
39*1f5207b7SJohn Levon if (expr->type != EXPR_VALUE)
40*1f5207b7SJohn Levon return NULL;
41*1f5207b7SJohn Levon if (!get_value(expr, &expr_value))
42*1f5207b7SJohn Levon return NULL;
43*1f5207b7SJohn Levon name = pos_ident(expr->pos);
44*1f5207b7SJohn Levon if (!name)
45*1f5207b7SJohn Levon return NULL;
46*1f5207b7SJohn Levon shifter_value = search_struct(shifters, (char *)name);
47*1f5207b7SJohn Levon if (!shifter_value)
48*1f5207b7SJohn Levon return NULL;
49*1f5207b7SJohn Levon if (sval_cmp_val(expr_value, *shifter_value) != 0)
50*1f5207b7SJohn Levon return NULL;
51*1f5207b7SJohn Levon return name;
52*1f5207b7SJohn Levon }
53*1f5207b7SJohn Levon
match_assign(struct expression * expr)54*1f5207b7SJohn Levon static void match_assign(struct expression *expr)
55*1f5207b7SJohn Levon {
56*1f5207b7SJohn Levon const char *name;
57*1f5207b7SJohn Levon
58*1f5207b7SJohn Levon if (expr->op != SPECIAL_OR_ASSIGN)
59*1f5207b7SJohn Levon return;
60*1f5207b7SJohn Levon if (positions_eq(expr->pos, expr->right->pos))
61*1f5207b7SJohn Levon return;
62*1f5207b7SJohn Levon name = get_shifter(expr->right);
63*1f5207b7SJohn Levon if (!name)
64*1f5207b7SJohn Levon return;
65*1f5207b7SJohn Levon
66*1f5207b7SJohn Levon sm_warning("'%s' is a shifter (not for '%s').",
67*1f5207b7SJohn Levon name, show_special(expr->op));
68*1f5207b7SJohn Levon }
69*1f5207b7SJohn Levon
match_binop(struct expression * expr)70*1f5207b7SJohn Levon static void match_binop(struct expression *expr)
71*1f5207b7SJohn Levon {
72*1f5207b7SJohn Levon const char *name;
73*1f5207b7SJohn Levon
74*1f5207b7SJohn Levon if (positions_eq(expr->pos, expr->right->pos))
75*1f5207b7SJohn Levon return;
76*1f5207b7SJohn Levon if (expr->op != '&')
77*1f5207b7SJohn Levon return;
78*1f5207b7SJohn Levon name = get_shifter(expr->right);
79*1f5207b7SJohn Levon if (!name)
80*1f5207b7SJohn Levon return;
81*1f5207b7SJohn Levon
82*1f5207b7SJohn Levon sm_warning("bit shifter '%s' used for logical '%s'",
83*1f5207b7SJohn Levon name, show_special(expr->op));
84*1f5207b7SJohn Levon }
85*1f5207b7SJohn Levon
register_shifters(void)86*1f5207b7SJohn Levon static void register_shifters(void)
87*1f5207b7SJohn Levon {
88*1f5207b7SJohn Levon char filename[256];
89*1f5207b7SJohn Levon struct token *token;
90*1f5207b7SJohn Levon char *name;
91*1f5207b7SJohn Levon int *val;
92*1f5207b7SJohn Levon
93*1f5207b7SJohn Levon snprintf(filename, sizeof(filename), "%s.bit_shifters", option_project_str);
94*1f5207b7SJohn Levon token = get_tokens_file(filename);
95*1f5207b7SJohn Levon if (!token)
96*1f5207b7SJohn Levon return;
97*1f5207b7SJohn Levon if (token_type(token) != TOKEN_STREAMBEGIN)
98*1f5207b7SJohn Levon return;
99*1f5207b7SJohn Levon token = token->next;
100*1f5207b7SJohn Levon while (token_type(token) != TOKEN_STREAMEND) {
101*1f5207b7SJohn Levon if (token_type(token) != TOKEN_IDENT)
102*1f5207b7SJohn Levon return;
103*1f5207b7SJohn Levon name = alloc_string(show_ident(token->ident));
104*1f5207b7SJohn Levon token = token->next;
105*1f5207b7SJohn Levon if (token_type(token) != TOKEN_NUMBER)
106*1f5207b7SJohn Levon return;
107*1f5207b7SJohn Levon val = malloc(sizeof(int));
108*1f5207b7SJohn Levon *val = atoi(token->number);
109*1f5207b7SJohn Levon insert_struct(shifters, name, val);
110*1f5207b7SJohn Levon token = token->next;
111*1f5207b7SJohn Levon }
112*1f5207b7SJohn Levon clear_token_alloc();
113*1f5207b7SJohn Levon }
114*1f5207b7SJohn Levon
match_binop_info(struct expression * expr)115*1f5207b7SJohn Levon static void match_binop_info(struct expression *expr)
116*1f5207b7SJohn Levon {
117*1f5207b7SJohn Levon char *name;
118*1f5207b7SJohn Levon sval_t sval;
119*1f5207b7SJohn Levon
120*1f5207b7SJohn Levon if (positions_eq(expr->pos, expr->right->pos))
121*1f5207b7SJohn Levon return;
122*1f5207b7SJohn Levon if (expr->op != SPECIAL_LEFTSHIFT)
123*1f5207b7SJohn Levon return;
124*1f5207b7SJohn Levon if (expr->right->type != EXPR_VALUE)
125*1f5207b7SJohn Levon return;
126*1f5207b7SJohn Levon name = pos_ident(expr->right->pos);
127*1f5207b7SJohn Levon if (!name)
128*1f5207b7SJohn Levon return;
129*1f5207b7SJohn Levon if (!get_value(expr->right, &sval))
130*1f5207b7SJohn Levon return;
131*1f5207b7SJohn Levon sm_msg("info: bit shifter '%s' '%s'", name, sval_to_str(sval));
132*1f5207b7SJohn Levon }
133*1f5207b7SJohn Levon
match_call(const char * fn,struct expression * expr,void * _arg_no)134*1f5207b7SJohn Levon static void match_call(const char *fn, struct expression *expr, void *_arg_no)
135*1f5207b7SJohn Levon {
136*1f5207b7SJohn Levon struct expression *arg_expr;
137*1f5207b7SJohn Levon int arg_no = PTR_INT(_arg_no);
138*1f5207b7SJohn Levon sval_t sval;
139*1f5207b7SJohn Levon char *name;
140*1f5207b7SJohn Levon
141*1f5207b7SJohn Levon arg_expr = get_argument_from_call_expr(expr->args, arg_no);
142*1f5207b7SJohn Levon if (positions_eq(expr->pos, arg_expr->pos))
143*1f5207b7SJohn Levon return;
144*1f5207b7SJohn Levon name = pos_ident(arg_expr->pos);
145*1f5207b7SJohn Levon if (!name)
146*1f5207b7SJohn Levon return;
147*1f5207b7SJohn Levon if (!get_value(arg_expr, &sval))
148*1f5207b7SJohn Levon return;
149*1f5207b7SJohn Levon sm_msg("info: bit shifter '%s' '%s'", name, sval_to_str(sval));
150*1f5207b7SJohn Levon }
151*1f5207b7SJohn Levon
check_bit_shift(int id)152*1f5207b7SJohn Levon void check_bit_shift(int id)
153*1f5207b7SJohn Levon {
154*1f5207b7SJohn Levon my_id = id;
155*1f5207b7SJohn Levon
156*1f5207b7SJohn Levon shifters = create_function_hashtable(5000);
157*1f5207b7SJohn Levon register_shifters();
158*1f5207b7SJohn Levon
159*1f5207b7SJohn Levon add_hook(&match_assign, ASSIGNMENT_HOOK);
160*1f5207b7SJohn Levon add_hook(&match_binop, BINOP_HOOK);
161*1f5207b7SJohn Levon
162*1f5207b7SJohn Levon if (option_info) {
163*1f5207b7SJohn Levon add_hook(&match_binop_info, BINOP_HOOK);
164*1f5207b7SJohn Levon if (option_project == PROJ_KERNEL) {
165*1f5207b7SJohn Levon add_function_hook("set_bit", &match_call, INT_PTR(0));
166*1f5207b7SJohn Levon add_function_hook("test_bit", &match_call, INT_PTR(0));
167*1f5207b7SJohn Levon }
168*1f5207b7SJohn Levon }
169*1f5207b7SJohn Levon }
170