expressions: make gen_expr_from_param_key() work for returned params
[smatch.git] / check_or_vs_and.c
blob95936d1eb504c553fe76cd862c8c2ae41e977314
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 static int does_inc_dec(struct expression *expr)
25 if (expr->type == EXPR_PREOP || expr->type == EXPR_POSTOP) {
26 if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
27 return 1;
28 return does_inc_dec(expr->unop);
30 return 0;
33 static int expr_equiv_no_inc_dec(struct expression *one, struct expression *two)
35 if (does_inc_dec(one) || does_inc_dec(two))
36 return 0;
37 return expr_equiv(one, two);
40 static int inconsistent_check(struct expression *left, struct expression *right)
42 sval_t sval;
44 if (get_value(left->left, &sval)) {
45 if (get_value(right->left, &sval))
46 return expr_equiv_no_inc_dec(left->right, right->right);
47 if (get_value(right->right, &sval))
48 return expr_equiv_no_inc_dec(left->right, right->left);
49 return 0;
51 if (get_value(left->right, &sval)) {
52 if (get_value(right->left, &sval))
53 return expr_equiv_no_inc_dec(left->left, right->right);
54 if (get_value(right->right, &sval))
55 return expr_equiv_no_inc_dec(left->left, right->left);
56 return 0;
59 return 0;
62 static void check_or(struct expression *expr)
64 struct expression *left, *right;
66 left = strip_expr(expr->left);
67 right = strip_expr(expr->right);
69 if (left->type != EXPR_COMPARE || left->op != SPECIAL_NOTEQUAL)
70 return;
71 if (right->type != EXPR_COMPARE || right->op != SPECIAL_NOTEQUAL)
72 return;
73 if (!inconsistent_check(left, right))
74 return;
76 sm_warning("was && intended here instead of ||?");
79 static int is_kernel_min_macro(struct expression *expr)
81 char *macro;
83 if (option_project != PROJ_KERNEL)
84 return 0;
85 macro = get_macro_name(expr->pos);
86 if (!macro)
87 return 0;
88 if (strcmp(macro, "min") == 0 ||
89 strcmp(macro, "min_t") == 0 ||
90 strcmp(macro, "max") == 0 ||
91 strcmp(macro, "max_t") == 0)
92 return 1;
93 return 0;
96 static void check_and(struct expression *expr)
98 struct expression *left, *right;
100 if (is_kernel_min_macro(expr))
101 return;
103 left = strip_expr(expr->left);
104 right = strip_expr(expr->right);
106 if (left->type != EXPR_COMPARE || left->op != SPECIAL_EQUAL)
107 return;
108 if (right->type != EXPR_COMPARE || right->op != SPECIAL_EQUAL)
109 return;
110 if (!inconsistent_check(left, right))
111 return;
113 sm_warning("was || intended here instead of &&?");
116 static void match_logic(struct expression *expr)
118 if (expr->type != EXPR_LOGICAL)
119 return;
121 if (expr->op == SPECIAL_LOGICAL_OR)
122 check_or(expr);
123 if (expr->op == SPECIAL_LOGICAL_AND)
124 check_and(expr);
127 static void match_condition(struct expression *expr)
129 sval_t sval;
131 if (expr->type != EXPR_BINOP)
132 return;
133 if (expr->op == '|') {
134 if (get_value(expr->left, &sval) || get_value(expr->right, &sval))
135 sm_warning("suspicious bitop condition");
136 return;
139 if (expr->op != '&')
140 return;
142 if (get_macro_name(expr->pos))
143 return;
144 if (is_unconstant_macro(expr->left) || is_unconstant_macro(expr->right))
145 return;
147 if ((get_value(expr->left, &sval) && sval.value == 0) ||
148 (get_value(expr->right, &sval) && sval.value == 0))
149 sm_warning("bitwise AND condition is false here");
152 static void match_binop(struct expression *expr)
154 sval_t left, right, sval;
156 if (expr->op != '&')
157 return;
158 if (!get_value(expr, &sval) || sval.value != 0)
159 return;
160 if (get_macro_name(expr->pos))
161 return;
162 if (!get_value(expr->left, &left) || !get_value(expr->right, &right))
163 return;
164 sm_warning("odd binop '0x%llx & 0x%llx'", left.uvalue, right.uvalue);
167 void check_or_vs_and(int id)
169 my_id = id;
171 add_hook(&match_logic, LOGIC_HOOK);
172 add_hook(&match_condition, CONDITION_HOOK);
173 if (option_spammy)
174 add_hook(&match_binop, BINOP_HOOK);