2 * Copyright (C) 2010 Dan Carpenter.
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 int is_bool(struct expression
*expr
)
26 type
= get_type(expr
);
29 if (type_bits(type
) == 1 && type
->ctype
.modifiers
& MOD_UNSIGNED
)
34 static int is_bool_from_context(struct expression
*expr
)
38 if (!get_implied_max(expr
, &sval
) || sval
.uvalue
> 1)
40 if (!get_implied_min(expr
, &sval
) || sval
.value
< 0)
45 static int is_bool_op(struct expression
*expr
)
47 expr
= strip_expr(expr
);
49 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '!')
51 if (expr
->type
== EXPR_COMPARE
)
53 if (expr
->type
== EXPR_LOGICAL
)
58 static void match_condition(struct expression
*expr
)
62 if (expr
->type
== EXPR_COMPARE
) {
63 if (expr
->left
->type
== EXPR_COMPARE
|| expr
->right
->type
== EXPR_COMPARE
)
65 if (expr
->left
->type
== EXPR_PREOP
&& expr
->left
->op
== '!') {
66 if (expr
->left
->unop
->type
== EXPR_PREOP
&& expr
->left
->unop
->op
== '!')
68 if (expr
->right
->op
== '!')
70 if (is_bool(expr
->right
))
72 if (is_bool(expr
->left
->unop
))
74 if (is_bool_from_context(expr
->left
->unop
))
80 if (expr
->type
== EXPR_BINOP
) {
81 if (expr
->left
->type
== EXPR_COMPARE
|| expr
->right
->type
== EXPR_COMPARE
)
86 sm_msg("warn: add some parenthesis here?");
90 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '&') {
93 if (is_bool_op(expr
->left
))
95 if (is_bool_op(expr
->right
))
98 sm_msg("warn: maybe use && instead of &");
102 static void match_binop(struct expression
*expr
)
106 if (expr
->left
->op
== '!')
107 sm_msg("warn: add some parenthesis here?");
110 static void match_mask(struct expression
*expr
)
114 if (expr
->right
->type
!= EXPR_BINOP
)
116 if (expr
->right
->op
!= SPECIAL_RIGHTSHIFT
)
119 sm_msg("warn: shift has higher precedence than mask");
122 static void match_subtract_shift(struct expression
*expr
)
124 if (expr
->op
!= SPECIAL_LEFTSHIFT
)
126 if (expr
->right
->type
!= EXPR_BINOP
)
128 if (expr
->right
->op
!= '-')
130 sm_msg("warn: subtract is higher precedence than shift");
133 void check_precedence(int id
)
137 add_hook(&match_condition
, CONDITION_HOOK
);
138 add_hook(&match_binop
, BINOP_HOOK
);
139 add_hook(&match_mask
, BINOP_HOOK
);
140 add_hook(&match_subtract_shift
, BINOP_HOOK
);