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
19 #include "smatch_function_hashtable.h"
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
)
28 return does_inc_dec(expr
->unop
);
33 static int expr_equiv_no_inc_dec(struct expression
*one
, struct expression
*two
)
35 if (does_inc_dec(one
) || does_inc_dec(two
))
37 return expr_equiv(one
, two
);
40 static int inconsistent_check(struct expression
*left
, struct expression
*right
)
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
);
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
);
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
)
71 if (right
->type
!= EXPR_COMPARE
|| right
->op
!= SPECIAL_NOTEQUAL
)
73 if (!inconsistent_check(left
, right
))
76 sm_warning("was && intended here instead of ||?");
79 static int is_kernel_min_macro(struct expression
*expr
)
83 if (option_project
!= PROJ_KERNEL
)
85 macro
= get_macro_name(expr
->pos
);
88 if (strcmp(macro
, "min") == 0 ||
89 strcmp(macro
, "min_t") == 0 ||
90 strcmp(macro
, "max") == 0 ||
91 strcmp(macro
, "max_t") == 0)
96 static void check_and(struct expression
*expr
)
98 struct expression
*left
, *right
;
100 if (is_kernel_min_macro(expr
))
103 left
= strip_expr(expr
->left
);
104 right
= strip_expr(expr
->right
);
106 if (left
->type
!= EXPR_COMPARE
|| left
->op
!= SPECIAL_EQUAL
)
108 if (right
->type
!= EXPR_COMPARE
|| right
->op
!= SPECIAL_EQUAL
)
110 if (!inconsistent_check(left
, right
))
113 sm_warning("was || intended here instead of &&?");
116 static void match_logic(struct expression
*expr
)
118 if (expr
->type
!= EXPR_LOGICAL
)
121 if (expr
->op
== SPECIAL_LOGICAL_OR
)
123 if (expr
->op
== SPECIAL_LOGICAL_AND
)
127 static void match_condition(struct expression
*expr
)
131 if (expr
->type
!= EXPR_BINOP
)
133 if (expr
->op
== '|') {
134 if (get_value(expr
->left
, &sval
) || get_value(expr
->right
, &sval
))
135 sm_warning("suspicious bitop condition");
142 if (get_macro_name(expr
->pos
))
144 if (is_unconstant_macro(expr
->left
) || is_unconstant_macro(expr
->right
))
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
;
158 if (!get_value(expr
, &sval
) || sval
.value
!= 0)
160 if (get_macro_name(expr
->pos
))
162 if (!get_value(expr
->left
, &left
) || !get_value(expr
->right
, &right
))
164 sm_warning("odd binop '0x%llx & 0x%llx'", left
.uvalue
, right
.uvalue
);
167 void check_or_vs_and(int id
)
171 add_hook(&match_logic
, LOGIC_HOOK
);
172 add_hook(&match_condition
, CONDITION_HOOK
);
174 add_hook(&match_binop
, BINOP_HOOK
);