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
19 #include "smatch_extra.h"
23 static int is_bool(struct expression
*expr
)
27 type
= get_type(expr
);
30 if (type_bits(type
) == 1 && type
->ctype
.modifiers
& MOD_UNSIGNED
)
35 static int is_bool_from_context(struct expression
*expr
)
39 if (!get_implied_max(expr
, &sval
) || sval
.uvalue
> 1)
41 if (!get_implied_min(expr
, &sval
) || sval
.value
< 0)
46 static int is_bool_op(struct expression
*expr
)
48 expr
= strip_expr(expr
);
50 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '!')
52 if (expr
->type
== EXPR_COMPARE
)
54 if (expr
->type
== EXPR_LOGICAL
)
59 static void match_condition(struct expression
*expr
)
63 if (expr
->type
== EXPR_COMPARE
) {
64 if (expr
->left
->type
== EXPR_COMPARE
|| expr
->right
->type
== EXPR_COMPARE
)
66 if (expr
->left
->type
== EXPR_PREOP
&& expr
->left
->op
== '!') {
67 if (expr
->left
->unop
->type
== EXPR_PREOP
&& expr
->left
->unop
->op
== '!')
69 if (expr
->right
->op
== '!')
71 if (is_bool(expr
->right
))
73 if (is_bool(expr
->left
->unop
))
75 if (is_bool_from_context(expr
->left
->unop
))
81 if (expr
->type
== EXPR_BINOP
) {
82 if (expr
->left
->type
== EXPR_COMPARE
|| expr
->right
->type
== EXPR_COMPARE
)
87 sm_warning("add some parenthesis here?");
91 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '&') {
94 if (is_bool_op(expr
->left
))
96 if (is_bool_op(expr
->right
))
99 sm_warning("maybe use && instead of &");
103 static void match_binop(struct expression
*expr
)
107 if (expr
->left
->op
== '!')
108 sm_warning("add some parenthesis here?");
111 static void match_mask(struct expression
*expr
)
115 if (expr
->right
->type
!= EXPR_BINOP
)
117 if (expr
->right
->op
!= SPECIAL_RIGHTSHIFT
)
120 sm_warning("shift has higher precedence than mask");
123 static void match_mask_compare(struct expression
*expr
)
127 if (expr
->right
->type
!= EXPR_COMPARE
)
130 sm_warning("compare has higher precedence than mask");
133 static void match_subtract_shift(struct expression
*expr
)
135 if (expr
->op
!= SPECIAL_LEFTSHIFT
)
137 if (expr
->right
->type
!= EXPR_BINOP
)
139 if (expr
->right
->op
!= '-')
141 sm_warning("subtract is higher precedence than shift");
144 void check_precedence(int id
)
148 add_hook(&match_condition
, CONDITION_HOOK
);
149 add_hook(&match_binop
, BINOP_HOOK
);
150 add_hook(&match_mask
, BINOP_HOOK
);
151 add_hook(&match_mask_compare
, BINOP_HOOK
);
152 add_hook(&match_subtract_shift
, BINOP_HOOK
);