math: improve how known logical operations are handled
[smatch.git] / check_logical_instead_of_bitwise.c
blob2a4e99610e9dc49ef9e8b979797556781db0ec13
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 long long val;
29 if (expr->type != EXPR_LOGICAL)
30 return;
32 if (get_macro_name(expr->pos))
33 return;
35 if (!get_value(expr->right, &val)) {
36 if (!get_value(expr->left, &val))
37 return;
40 if (val == 0 || val == 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);