2 * sparse/check_precedence.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
14 static int is_bool(struct expression
*expr
)
18 type
= get_type(expr
);
21 if (type
->bit_size
== 1 && type
->ctype
.modifiers
& MOD_UNSIGNED
)
26 static int is_bool_op(struct expression
*expr
)
28 expr
= strip_expr(expr
);
30 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '!')
32 if (expr
->type
== EXPR_COMPARE
)
34 if (expr
->type
== EXPR_LOGICAL
)
39 static void match_condition(struct expression
*expr
)
43 if (expr
->type
== EXPR_COMPARE
) {
44 if (expr
->left
->type
== EXPR_COMPARE
|| expr
->right
->type
== EXPR_COMPARE
)
46 if (expr
->left
->op
== '!') {
47 if (expr
->left
->type
== EXPR_PREOP
&& expr
->left
->unop
->op
== '!')
49 if (expr
->right
->op
== '!')
51 if (is_bool(expr
->right
))
53 if (is_bool(expr
->left
->unop
))
59 if (expr
->type
== EXPR_BINOP
) {
60 if (expr
->left
->type
== EXPR_COMPARE
|| expr
->right
->type
== EXPR_COMPARE
)
65 sm_msg("warn: add some parenthesis here?");
69 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '&') {
72 if (is_bool_op(expr
->left
))
74 if (is_bool_op(expr
->right
))
77 sm_msg("warn: maybe use && instead of &");
81 static void match_binop(struct expression
*expr
)
85 if (expr
->left
->op
== '!')
86 sm_msg("warn: add some parenthesis here?");
89 void check_precedence(int id
)
93 add_hook(&match_condition
, CONDITION_HOOK
);
94 add_hook(&match_binop
, BINOP_HOOK
);