implied: get rid of the ->implied member
[smatch.git] / check_or_vs_and.c
blobe1dd0c53883b161e1dcb0c32c409d0d55b8af716
1 /*
2 * Copyright (C) 2012 Oracle.
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"
19 #include "smatch_function_hashtable.h"
21 static int my_id;
23 DEFINE_STRING_HASHTABLE_STATIC(unconstant_macros);
25 static int does_inc_dec(struct expression *expr)
27 if (expr->type == EXPR_PREOP || expr->type == EXPR_POSTOP) {
28 if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
29 return 1;
30 return does_inc_dec(expr->unop);
32 return 0;
35 static int expr_equiv_no_inc_dec(struct expression *one, struct expression *two)
37 if (does_inc_dec(one) || does_inc_dec(two))
38 return 0;
39 return expr_equiv(one, two);
42 static int inconsistent_check(struct expression *left, struct expression *right)
44 sval_t sval;
46 if (get_value(left->left, &sval)) {
47 if (get_value(right->left, &sval))
48 return expr_equiv_no_inc_dec(left->right, right->right);
49 if (get_value(right->right, &sval))
50 return expr_equiv_no_inc_dec(left->right, right->left);
51 return 0;
53 if (get_value(left->right, &sval)) {
54 if (get_value(right->left, &sval))
55 return expr_equiv_no_inc_dec(left->left, right->right);
56 if (get_value(right->right, &sval))
57 return expr_equiv_no_inc_dec(left->left, right->left);
58 return 0;
61 return 0;
64 static void check_or(struct expression *expr)
66 struct expression *left, *right;
68 left = strip_expr(expr->left);
69 right = strip_expr(expr->right);
71 if (left->type != EXPR_COMPARE ||
72 left->op != SPECIAL_NOTEQUAL)
73 return;
74 if (right->type != EXPR_COMPARE ||
75 right->op != SPECIAL_NOTEQUAL)
76 return;
77 if (!inconsistent_check(left, right))
78 return;
80 sm_msg("warn: was && intended here instead of ||?");
83 static void check_and(struct expression *expr)
85 struct expression *left, *right;
87 left = strip_expr(expr->left);
88 right = strip_expr(expr->right);
90 if (left->type != EXPR_COMPARE ||
91 left->op != SPECIAL_EQUAL)
92 return;
93 if (right->type != EXPR_COMPARE ||
94 right->op != SPECIAL_EQUAL)
95 return;
96 if (!inconsistent_check(left, right))
97 return;
99 sm_msg("warn: was || intended here instead of &&?");
102 static void match_logic(struct expression *expr)
104 if (expr->type != EXPR_LOGICAL)
105 return;
107 if (expr->op == SPECIAL_LOGICAL_OR)
108 check_or(expr);
109 if (expr->op == SPECIAL_LOGICAL_AND)
110 check_and(expr);
113 static int is_unconstant_macro(struct expression *expr)
115 char *macro;
117 macro = get_macro_name(expr->pos);
118 if (!macro)
119 return 0;
120 if (search_unconstant_macros(unconstant_macros, macro))
121 return 1;
122 return 0;
125 static void match_condition(struct expression *expr)
127 sval_t sval;
129 if (expr->type != EXPR_BINOP)
130 return;
131 if (expr->op == '|') {
132 if (get_value(expr->left, &sval) || get_value(expr->right, &sval))
133 sm_msg("warn: suspicious bitop condition");
134 return;
137 if (expr->op != '&')
138 return;
140 if (get_macro_name(expr->pos))
141 return;
142 if (is_unconstant_macro(expr->left) || is_unconstant_macro(expr->right))
143 return;
145 if ((get_value(expr->left, &sval) && sval.value == 0) ||
146 (get_value(expr->right, &sval) && sval.value == 0))
147 sm_msg("warn: bitwise AND condition is false here");
150 static void match_binop(struct expression *expr)
152 sval_t left, right, sval;
154 if (expr->op != '&')
155 return;
156 if (!get_value(expr, &sval) || sval.value != 0)
157 return;
158 if (get_macro_name(expr->pos))
159 return;
160 if (!get_value(expr->left, &left) || !get_value(expr->right, &right))
161 return;
162 sm_msg("warn: odd binop '0x%llx & 0x%llx'", left.uvalue, right.uvalue);
165 void check_or_vs_and(int id)
167 my_id = id;
169 unconstant_macros = create_function_hashtable(100);
170 load_strings("unconstant_macros", unconstant_macros);
172 add_hook(&match_logic, LOGIC_HOOK);
173 add_hook(&match_condition, CONDITION_HOOK);
174 if (option_spammy)
175 add_hook(&match_binop, BINOP_HOOK);