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