-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcheck_precedence.c
153 lines (131 loc) · 3.44 KB
/
check_precedence.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
/*
* Copyright (C) 2010 Dan Carpenter.
*
* 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
*/
#include "smatch.h"
#include "smatch_extra.h"
static int my_id;
static int is_bool(struct expression *expr)
{
struct symbol *type;
type = get_type(expr);
if (!type)
return 0;
if (type_bits(type) == 1 && type->ctype.modifiers & MOD_UNSIGNED)
return 1;
return 0;
}
static int is_bool_from_context(struct expression *expr)
{
sval_t sval;
if (!get_implied_max(expr, &sval) || sval.uvalue > 1)
return 0;
if (!get_implied_min(expr, &sval) || sval.value < 0)
return 0;
return 1;
}
static int is_bool_op(struct expression *expr)
{
expr = strip_expr(expr);
if (expr->type == EXPR_PREOP && expr->op == '!')
return 1;
if (expr->type == EXPR_COMPARE)
return 1;
if (expr->type == EXPR_LOGICAL)
return 1;
return is_bool(expr);
}
static void match_condition(struct expression *expr)
{
int print = 0;
if (expr->type == EXPR_COMPARE) {
if (expr->left->type == EXPR_COMPARE || expr->right->type == EXPR_COMPARE)
print = 1;
if (expr->left->type == EXPR_PREOP && expr->left->op == '!') {
if (expr->left->unop->type == EXPR_PREOP && expr->left->unop->op == '!')
return;
if (expr->right->op == '!')
return;
if (is_bool(expr->right))
return;
if (is_bool(expr->left->unop))
return;
if (is_bool_from_context(expr->left->unop))
return;
print = 1;
}
}
if (expr->type == EXPR_BINOP) {
if (expr->left->type == EXPR_COMPARE || expr->right->type == EXPR_COMPARE)
print = 1;
}
if (print) {
sm_warning("add some parenthesis here?");
return;
}
if (expr->type == EXPR_BINOP && expr->op == '&') {
int i = 0;
if (is_bool_op(expr->left))
i++;
if (is_bool_op(expr->right))
i++;
if (i == 1)
sm_warning("maybe use && instead of &");
}
}
static void match_binop(struct expression *expr)
{
if (expr->op != '&')
return;
if (expr->left->op == '!')
sm_warning("add some parenthesis here?");
}
static void match_mask(struct expression *expr)
{
if (expr->op != '&')
return;
if (expr->right->type != EXPR_BINOP)
return;
if (expr->right->op != SPECIAL_RIGHTSHIFT)
return;
sm_warning("shift has higher precedence than mask");
}
static void match_mask_compare(struct expression *expr)
{
if (expr->op != '&')
return;
if (expr->right->type != EXPR_COMPARE)
return;
sm_warning("compare has higher precedence than mask");
}
static void match_subtract_shift(struct expression *expr)
{
if (expr->op != SPECIAL_LEFTSHIFT)
return;
if (expr->right->type != EXPR_BINOP)
return;
if (expr->right->op != '-')
return;
sm_warning("subtract is higher precedence than shift");
}
void check_precedence(int id)
{
my_id = id;
add_hook(&match_condition, CONDITION_HOOK);
add_hook(&match_binop, BINOP_HOOK);
add_hook(&match_mask, BINOP_HOOK);
add_hook(&match_mask_compare, BINOP_HOOK);
add_hook(&match_subtract_shift, BINOP_HOOK);
}