2 * sparse/check_logical_instead_of_bitwise.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
14 static int is_bitshift(struct expression
*expr
)
16 expr
= strip_expr(expr
);
18 if (expr
->type
!= EXPR_BINOP
)
20 if (expr
->op
== SPECIAL_LEFTSHIFT
|| expr
->op
== SPECIAL_RIGHTSHIFT
)
25 static void match_logic(struct expression
*expr
)
29 if (expr
->type
!= EXPR_LOGICAL
)
32 if (get_macro_name(expr
->pos
))
35 if (!get_value(expr
->right
, &sval
)) {
36 if (!get_value(expr
->left
, &sval
))
40 if (sval
.value
== 0 || sval
.value
== 1)
43 sm_msg("warn: should this be a bitwise op?");
46 static void match_assign(struct expression
*expr
)
48 struct expression
*right
;
50 right
= strip_expr(expr
->right
);
51 if (right
->type
!= EXPR_LOGICAL
)
53 if (is_bitshift(right
->left
) || is_bitshift(right
->right
))
54 sm_msg("warn: should this be a bitwise op?");
57 void check_logical_instead_of_bitwise(int id
)
61 add_hook(&match_logic
, LOGIC_HOOK
);
62 add_hook(&match_assign
, ASSIGNMENT_HOOK
);