implied: use implications from function conditions
[smatch.git] / check_precedence.c
blobe0b8e37d40a1f0d67ede8e31646bd32c6af612bb
1 /*
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
18 #include "smatch.h"
20 static int my_id;
22 static int is_bool(struct expression *expr)
24 struct symbol *type;
26 type = get_type(expr);
27 if (!type)
28 return 0;
29 if (type_bits(type) == 1 && type->ctype.modifiers & MOD_UNSIGNED)
30 return 1;
31 return 0;
34 static int is_bool_from_context(struct expression *expr)
36 sval_t sval;
38 if (!get_implied_max(expr, &sval) || sval.uvalue > 1)
39 return 0;
40 if (!get_implied_min(expr, &sval) || sval.value < 0)
41 return 0;
42 return 1;
45 static int is_bool_op(struct expression *expr)
47 expr = strip_expr(expr);
49 if (expr->type == EXPR_PREOP && expr->op == '!')
50 return 1;
51 if (expr->type == EXPR_COMPARE)
52 return 1;
53 if (expr->type == EXPR_LOGICAL)
54 return 1;
55 return is_bool(expr);
58 static void match_condition(struct expression *expr)
60 int print = 0;
62 if (expr->type == EXPR_COMPARE) {
63 if (expr->left->type == EXPR_COMPARE || expr->right->type == EXPR_COMPARE)
64 print = 1;
65 if (expr->left->type == EXPR_PREOP && expr->left->op == '!') {
66 if (expr->left->unop->type == EXPR_PREOP && expr->left->unop->op == '!')
67 return;
68 if (expr->right->op == '!')
69 return;
70 if (is_bool(expr->right))
71 return;
72 if (is_bool(expr->left->unop))
73 return;
74 if (is_bool_from_context(expr->left->unop))
75 return;
76 print = 1;
80 if (expr->type == EXPR_BINOP) {
81 if (expr->left->type == EXPR_COMPARE || expr->right->type == EXPR_COMPARE)
82 print = 1;
85 if (print) {
86 sm_msg("warn: add some parenthesis here?");
87 return;
90 if (expr->type == EXPR_BINOP && expr->op == '&') {
91 int i = 0;
93 if (is_bool_op(expr->left))
94 i++;
95 if (is_bool_op(expr->right))
96 i++;
97 if (i == 1)
98 sm_msg("warn: maybe use && instead of &");
102 static void match_binop(struct expression *expr)
104 if (expr->op != '&')
105 return;
106 if (expr->left->op == '!')
107 sm_msg("warn: add some parenthesis here?");
110 static void match_mask(struct expression *expr)
112 if (expr->op != '&')
113 return;
114 if (expr->right->type != EXPR_BINOP)
115 return;
116 if (expr->right->op != SPECIAL_RIGHTSHIFT)
117 return;
119 sm_msg("warn: shift has higher precedence than mask");
122 static void match_subtract_shift(struct expression *expr)
124 if (expr->op != SPECIAL_LEFTSHIFT)
125 return;
126 if (expr->right->type != EXPR_BINOP)
127 return;
128 if (expr->right->op != '-')
129 return;
130 sm_msg("warn: subtract is higher precedence than shift");
133 void check_precedence(int id)
135 my_id = id;
137 add_hook(&match_condition, CONDITION_HOOK);
138 add_hook(&match_binop, BINOP_HOOK);
139 add_hook(&match_mask, BINOP_HOOK);
140 add_hook(&match_subtract_shift, BINOP_HOOK);