unreachable code: don't warn about empty statements
[smatch.git] / check_logical_instead_of_bitwise.c
blob57a61a03caf3054e8cac62cfa9f4c92bd03ddddb
1 /*
2 * sparse/check_logical_instead_of_bitwise.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "smatch.h"
12 static int my_id;
14 static int is_bitshift(struct expression *expr)
16 expr = strip_expr(expr);
18 if (expr->type != EXPR_BINOP)
19 return 0;
20 if (expr->op == SPECIAL_LEFTSHIFT || expr->op == SPECIAL_RIGHTSHIFT)
21 return 1;
22 return 0;
25 static void match_logic(struct expression *expr)
27 sval_t sval;
29 if (expr->type != EXPR_LOGICAL)
30 return;
32 if (get_macro_name(expr->pos))
33 return;
35 if (!get_value(expr->right, &sval)) {
36 if (!get_value(expr->left, &sval))
37 return;
40 if (sval.value == 0 || sval.value == 1)
41 return;
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)
52 return;
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)
59 my_id = id;
61 add_hook(&match_logic, LOGIC_HOOK);
62 add_hook(&match_assign, ASSIGNMENT_HOOK);